To get started, boot the server.
s.boot;
This notebook uses a guitar loop to demonstrate properties of filters. Store the sound in the buffer. If using the SC IDE use the first cell to get the path of the lecture folder. If using the notebook, use the second cell.
~lectureFolderPath = thisProcess.nowExecutingPath.dirname; // if using the SC IDE
~lectureFolderPath = "/Users/andrewdavis/Wellesley/CS203/website/lecture_materials/filters/code" // paste in path if using notebook
~guitarBuf = Buffer.read(s, ~lectureFolderPath +/+ "/guitarLoop.wav")
SynthDef(\play, {
arg out = 0, bufnum;
Out.ar(out, 0.2 * PlayBuf.ar(1, bufnum, 1, loop: 1));
}).add;
Test below to make sure everything works. Note the guitar sound is mono.
~guitar = Synth(\play, [\bufnum, ~guitarBuf]);
~guitar.free;
SynthDef(\lpf, {
arg out = 0, in, cutoff = 1000;
var sig;
sig = In.ar(in, 1); // Mono signal
sig = LPF.ar(sig, cutoff);
Out.ar(out, sig ! 2); // Duplicate for stereo
}).add;
We can test the effect of a low pass filter on white noise. Why white noise? Because white noise has equal energy across the frequency spectrum.
SynthDef(\noise, {
arg out = 0;
var sig = WhiteNoise.ar(0.05);
Out.ar(out, sig ! 2);
}).add;
FreqScope.new
~noise = Synth(\noise); // Note the equal energy of all frequencies
~noise.free;
Now compare the white noise after it has been filtered through the lowpass signal.
~lpfBus = Bus.audio(s, 1);
~lpf = Synth(\lpf, [\in, ~lpfBus]);
~noise = Synth(\noise, [\out, ~lpfBus], ~lpf, \addBefore);
~lpf.set(\cutoff, 2000) // changing the cutoff affects the timbre
Stop the sound.
~noise.free;
~lpf.free;
~lpfBus.free;
Let's also hear how a lowpass filter sounds with the guitar loop from above.
~guitar = Synth(\play, [\bufnum, ~guitarBuf]); // the original sound
~guitar.free;
Now play the guitar through the lowpass filter.
~lpfBus = Bus.audio(s, 1);
~lpf = Synth(\lpf, [\in, ~lpfBus]);
~guitar = Synth(\play, [\bufnum, ~guitarBuf, \out, ~lpfBus]);
~lpf.set(\cutoff, 2000) // changing the cutoff affects the timbre
Stop the sound.
~guitar.free;
~lpf.free;
~lpfBus.free;
SynthDef(\rlpf, {
arg out = 0, in, cutoff = 1000, q = 1;
var sig;
sig = In.ar(in, 1); // Mono signal
sig = RLPF.ar(sig, cutoff, 1/q); // the argument for RLPF is the reciprocal of q
Out.ar(out, sig ! 2); // Duplicate for stereo
}).add;
Test the resonant lowpass filter on noise.
~noise = Synth(\noise)
~noise.free
~rlpfBus = Bus.audio(s, 1);
~rlpf = Synth(\rlpf, [\in, ~rlpfBus]);
~noise = Synth(\noise, [\out, ~rlpfBus], ~rlpf, \addBefore);
Try changing some of the filter parameters.
~rlpf.set(\cutoff, 1000)
~rlpf.set(\q, 50) // setting the Q high brings out the pitch at the cutoff
~noise.free;
~rlpf.free;
~rlpfBus.free;
SynthDef(\hpf, {
arg out = 0, in, cutoff = 1000;
var sig;
sig = In.ar(in, 1); // Mono signal
sig = HPF.ar(sig, cutoff);
Out.ar(out, sig ! 2); // Duplicate for stereo
}).add;
Listen to the high pass filter on noise.
~hpfBus = Bus.audio(s, 1);
~hpf = Synth(\hpf, [\in, ~hpfBus]);
~noise = Synth(\noise, [\out, ~hpfBus], ~hpf, \addBefore);
~hpf.set(\cutoff, 5000)
~hpfBus.free;
~hpf.free;
~noise.free;
Listen to the lowpass filter on a guitar.
~hpfBus = Bus.audio(s, 1);
~hpf = Synth(\hpf, [\in, ~hpfBus]);
~guitar = Synth(\play, [\bufnum, ~guitarBuf, \out, ~hpfBus]);
~hpf.set(\cutoff, 2000)
~hpfBus.free;
~hpf.free;
~guitar.free;
SynthDef(\bpf, {
arg out = 0, in, center_freq = 1000, q = 1;
var sig;
sig = In.ar(in, 1); // Mono signal
sig = BPF.ar(sig, center_freq, 1/q);
Out.ar(out, sig ! 2); // Duplicate for stereo
}).add;
Test with noise.
~bpfBus = Bus.audio(s, 1);
~bpf = Synth(\bpf, [\in, ~bpfBus]);
~noise = Synth(\noise, [\out, ~bpfBus], ~bpf, \addBefore);
~bpf.set(\center_freq, 2000)
~bpf.set(\q, 50) // a higher q narrows the passband
~bpfBus.free;
~bpf.free;
~noise.free;
Test with guitar.
~bpfBus = Bus.audio(s, 1);
~bpf = Synth(\bpf, [\in, ~bpfBus]);
~guitar = Synth(\play, [\bufnum, ~guitarBuf, \out, ~bpfBus]);
~bpf.set(\center_freq, 4000)
~bpf.set(\q, 10)
~bpfBus.free;
~bpf.free;
~guitar.free;
The End!