Skip to content

Commit

Permalink
Add common filter superclass with resonance option (implements #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Sep 23, 2023
1 parent 49a5f84 commit 622df3d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 46 deletions.
69 changes: 69 additions & 0 deletions src/processing/sound/Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package processing.sound;

import com.jsyn.unitgen.FilterBandPass;
import com.jsyn.unitgen.FilterBiquadCommon;

import processing.core.PApplet;

// common superclass for JSyn filters that have a 'Q' unitport
abstract class Filter<E extends FilterBiquadCommon> extends Effect<E> {

public Filter(PApplet parent) {
super(parent);
}

/**
* Sets the resonance (or 'Q factor') of this filter. Increasing Q increases
* the Resonance of the filter at its cutoff frequency. Defaults to 1.
* @webref Effects:Filter
* @webBrief Sets the resonance (or 'Q factor') of this filter.
* @param q the desired Q factor, a value between 0.1 and 10
*/
public void res(float q) {
// TODO check for [0.1, 10] range
this.left.Q.set(q);
this.right.Q.set(q);
}

/**
* Sets the cutoff frequency for the filter.
* @webref Effects:Filter
* @webBrief Sets the cutoff frequency for the filter.
* @param freq the cutoff frequency in Hertz
**/
public void freq(float freq) {
this.left.frequency.set(freq);
this.right.frequency.set(freq);
}

public void process(SoundObject input, float freq) {
this.freq(freq);
this.process(input);
}

/**
* Starts applying this filter to an input signal.
* @webref Effects:Filter
* @webBrief Starts applying this filter to an input signal.
* @param input the sound source to filter
* @param freq the cutoff frequency in Hertz
* @param q the resonance (or 'Q factor'), a value between 0.1 and 10
**/
public void process(SoundObject input, float freq, float q) {
this.freq(freq);
this.res(q);
this.process(input);
}

/**
* Sets frequency and bandwidth of the filter with one method.
* @webref Effects:Filter
* @webBrief Sets frequency and bandwidth of the filter with one method.
* @param freq the cutoff frequency in Hertz
* @param q the resonance (or 'Q factor'), a value between 0.1 and 10
**/
public void set(float freq, float q) {
this.freq(freq);
this.res(q);
}
}
24 changes: 1 addition & 23 deletions src/processing/sound/HighPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @webBrief This is a high pass filter.
* @param parent PApplet: typically use "this"
**/
public class HighPass extends Effect<FilterHighPass> {
public class HighPass extends Filter<FilterHighPass> {

public HighPass(PApplet parent) {
super(parent);
Expand All @@ -20,26 +20,4 @@ public HighPass(PApplet parent) {
protected FilterHighPass newInstance() {
return new FilterHighPass();
}

/**
* Set the cut off frequency for the filter.
* @webref Effects:HighPass
* @webBrief Set the cut off frequency for the filter.
* @param freq the cutoff frequency in Hertz
*/
public void freq(float freq) {
this.left.frequency.set(freq);
this.right.frequency.set(freq);
}

/**
* Start applying this highpass filter to an input signal.
* @webref Effects:HighPass
* @param input the sound source to apply the filter to
* @param freq cutoff frequency
**/
public void process(SoundObject input, float freq) {
this.freq(freq);
this.process(input);
}
}
24 changes: 1 addition & 23 deletions src/processing/sound/LowPass.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @webref Effects:LowPass
* @param parent PApplet: typically use "this"
**/
public class LowPass extends Effect<FilterLowPass> {
public class LowPass extends Filter<FilterLowPass> {

public LowPass(PApplet parent) {
super(parent);
Expand All @@ -19,26 +19,4 @@ public LowPass(PApplet parent) {
protected FilterLowPass newInstance() {
return new FilterLowPass();
}

/**
* Set the cut off frequency for the filter.
* @webref Effects:LowPass
* @webBrief Set the cut off frequency for the filter.
* @param freq the cutoff frequency in Hertz
*/
public void freq(float freq) {
this.left.frequency.set(freq);
this.right.frequency.set(freq);
}

/**
* Start applying this highpass filter to an input signal.
* @webref Effects:LowPass
* @param input the sound source to apply the filter to
* @param freq cutoff frequency
**/
public void process(SoundObject input, float freq) {
this.freq(freq);
this.process(input);
}
}

0 comments on commit 622df3d

Please sign in to comment.