Skip to content

Commit

Permalink
Add cutoff frequency and resonance modulation to Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinstadler committed Oct 12, 2023
1 parent c7cff61 commit 9565505
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/processing/sound/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.jsyn.devices.AudioDeviceOutputStream;
import com.jsyn.devices.javasound.JavaSoundAudioDevice;
import com.jsyn.devices.jportaudio.JPortAudioDevice;
import com.jsyn.ports.UnitInputPort;
import com.jsyn.unitgen.ChannelOut;
import com.jsyn.unitgen.Multiply;
import com.jsyn.unitgen.UnitGenerator;
Expand Down Expand Up @@ -583,6 +584,15 @@ private void registerWithParent(PApplet theParent) {
theParent.registerMethod("resume", this.registeredCallback);
}

protected static void setModulation(UnitInputPort port, Modulator modulator) {
if (modulator == null) {
port.disconnectAll();
} else {
port.setValueAdded(true);
port.connect(modulator.getModulator());
}
}

// static helper methods that do stuff like checking argument values or
// printing library messages

Expand Down
30 changes: 29 additions & 1 deletion src/processing/sound/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import processing.core.PApplet;

/**
* Common superclass for JSyn filters that have a 'Q' unitport
* Common superclass for JSyn filters that have a 'frequency' and a 'Q' unitport
* @webref Effects:Filter
*/
public abstract class Filter<E extends FilterBiquadCommon> extends Effect<E> {

public Filter(PApplet parent) {
super(parent);
this.left.frequency.setValueAdded(true);
this.right.frequency.setValueAdded(true);
this.left.Q.setValueAdded(true);
this.right.Q.setValueAdded(true);
}

/**
Expand All @@ -28,6 +32,18 @@ public void res(float q) {
this.right.Q.set(q);
}

/**
* Modulates the resonance of this filter using another generator, typically a
* (low frequency) oscillator. The effective resonance of the filter will be
* the sum of the static value passed to <code>.res(float)</code>, and the
* dynamic value produced by the modulator (which fluctuates around 0).
* @param modulator an oscillator or noise object
*/
public void res(Modulator modulator) {
Engine.setModulation(this.left.Q, modulator);
Engine.setModulation(this.right.Q, modulator);
}

/**
* Sets the cutoff frequency for the filter.
* @webref Effects:Filter
Expand All @@ -39,6 +55,18 @@ public void freq(float freq) {
this.right.frequency.set(freq);
}

/**
* Modulates the frequency of this filter using another generator, typically a
* (low frequency) oscillator. The effective cutoff frequency of the filter
* will be the sum of the static value passed to <code>.freq(float)</code>, and
* the dynamic value produced by the modulator (which fluctuates around 0).
* @param modulator an oscillator or noise object
*/
public void freq(Modulator modulator) {
Engine.setModulation(this.left.frequency, modulator);
Engine.setModulation(this.right.frequency, modulator);
}

public void process(SoundObject input, float freq) {
this.freq(freq);
this.process(input);
Expand Down
14 changes: 7 additions & 7 deletions src/processing/sound/Oscillator.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ public void freq(float freq) {
this.oscillator.frequency.set(freq);
}

/*
/**
* Modulates the frequency of this oscillator using another generator,
* typically a (low frequency) oscillator.
* typically a (low frequency) oscillator. The effective frequency of the
* oscillator will be the sum of the static <code>float</code> value passed to
* <code>freq()</code>, and the dynamic value produced by the modulator (which
* fluctuates around 0).
* @param modulator an oscillator or noise object
* @webBrief Modulates the frequency of this oscillator.
*/
public void freq(Modulator modulator) {
this.oscillator.frequency.disconnectAll();
this.oscillator.frequency.connect(modulator.getModulator());
Engine.setModulation(this.oscillator.frequency, modulator);
}

public void amp(Modulator modulator) {
this.oscillator.amplitude.disconnectAll();
this.oscillator.amplitude.connect(modulator.getModulator());
Engine.setModulation(this.oscillator.amplitude, modulator);
}

public void play() {
Expand Down
9 changes: 9 additions & 0 deletions src/processing/sound/SoundObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,13 @@ protected void removeEffect(Effect<? extends UnitFilter> effect) {
this.circuit.removeEffect();
}
}

/**
* Gets the <code>JSynCircuit</code> object which encapsulates all the JSyn
* units (basic sound generator, pan and amplitude) which control the sound
* synthesis of this SoundObject.
*/
public JSynCircuit getUnitGenerator() {
return this.circuit;
}
}

0 comments on commit 9565505

Please sign in to comment.