Skip to content

Commit 835fb83

Browse files
committed
Refactor BandPass and fix bandwidth setting (see #46)
1 parent 622df3d commit 835fb83

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/processing/sound/BandPass.java

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
* @webBrief This is a band pass filter.
1111
* @param parent PApplet: typically use "this"
1212
**/
13-
public class BandPass extends Effect<FilterBandPass> {
13+
public class BandPass extends Filter<FilterBandPass> {
14+
15+
// when set to a positive value (desired bandpass bandwidth in Hertz), any
16+
// change to freq() will be followed by a re-calculation of Q that will
17+
// produce the given bandwidth
18+
private float bandwidth = -1;
1419

1520
public BandPass(PApplet parent) {
1621
super(parent);
@@ -22,39 +27,56 @@ protected FilterBandPass newInstance() {
2227
}
2328

2429
/**
25-
* Set the bandwidth for the filter.
30+
* Sets the bandwidth of this BandPass filter.
2631
* @webref Effects:BandPass
27-
* @webBrief Set the bandwidth for the filter.
28-
* @param freq Bandwidth in Hz
32+
* @webBrief Sets the bandwidth for the filter.
33+
* @param bw the filter bandwidth in Hertz
34+
* @see BandPass#res()
2935
**/
3036
public void bw(float bw) {
31-
// TODO check filter quality
32-
this.left.Q.set(this.left.frequency.get() / bw);
33-
this.right.Q.set(this.right.frequency.get() / bw);
37+
this.bandwidth = bw;
38+
this.updateQ();
39+
}
40+
41+
private void updateQ() {
42+
if (this.bandwidth > 0) {
43+
// TODO check if the value is still in the [0.1, 10] range?
44+
this.res((float) this.left.frequency.get() / this.bandwidth);
45+
}
46+
}
47+
48+
/**
49+
* Sets a fixed Q factor for this filter. If you want to specify a fixed
50+
* bandwidth for this bandpass filter (in Hertz) that is maintained even as
51+
* the center frequency of the filter changes, use <code>bw(float)</code>
52+
* instead.
53+
* @webref Effects:BandPass
54+
* @webBrief Sets the resonance (or 'Q factor') of this filter.
55+
* @param q the desired Q factor, a value between 0.1 and 10
56+
* @see BandPass#bw()
57+
*/
58+
public void res(float q) {
59+
super.res(q);
60+
this.bandwidth = -1;
3461
}
3562

3663
/**
37-
* Set the cutoff frequency for the filter.
64+
* Sets the center frequency of the filter.
3865
* @webref Effects:BandPass
39-
* @webBrief Set the cutoff frequency for the filter.
40-
* @param freq Cutoff frequency between 0 and 20000
66+
* @webBrief Sets the center frequency of the filter.
67+
* @param freq the center frequency in Hertz
4168
**/
4269
public void freq(float freq) {
43-
this.left.frequency.set(freq);
44-
this.right.frequency.set(freq);
45-
}
46-
47-
public void process(SoundObject input, float freq) {
48-
this.freq(freq);
49-
this.process(input);
70+
super.freq(freq);
71+
this.updateQ();
5072
}
5173

5274
/**
5375
* Start applying this bandpass filter to an input signal.
5476
* @webref Effects:BandPass
55-
* @param input the sound source to apply the filter to
56-
* @param freq Cutoff frequency between 0 and 20000
57-
* @param bw Set the bandwidth
77+
* @param input the sound source to filter
78+
* @param freq the center frequency in Hertz
79+
* @param bw the filter bandwidth in Hertz
5880
**/
5981
public void process(SoundObject input, float freq, float bw) {
6082
this.freq(freq);
@@ -63,11 +85,11 @@ public void process(SoundObject input, float freq, float bw) {
6385
}
6486

6587
/**
66-
* Sets frequency and bandwidth of the filter with one method.
88+
* Sets frequency and bandwidth of the filter with one method.
6789
* @webref Effects:BandPass
6890
* @webBrief Sets frequency and bandwidth of the filter with one method.
69-
* @param freq Set the frequency
70-
* @param bw Set the bandwidth
91+
* @param freq the center frequency in Hertz
92+
* @param bw the filter bandwidth in Hertz
7193
**/
7294
public void set(float freq, float bw) {
7395
this.freq(freq);

0 commit comments

Comments
 (0)