// Adapted or directly from:
// https://composerprogrammer.com/teaching/supercollider/sctutorial/2.1%20Subtractive%20and%20Additive%20Synthesis.html

// Subtractive synthesis means starting will lots of frequencies and subtracting away from the harmonically rich sound to get where we want to be. We subtract by using filters!

// You can make very loud and very high pitch sounds that may hurt you!
// I don't recommend wearing headphones unless you know your code works!

// Boot the server - do this first.. the server then dynamically runs the code you activate later.
(
Server.default=s=Server.internal;
s.boot;
)

// this line will make a pure white noise source, equal energy at all spectral frequencies;
// it can be unpleasant to listen to- the 0.1 makes sure its not too loud, but be careful playing this
// The Pan2.ar function just sends the mono signal to both the left and right channel
{Pan2.ar(WhiteNoise.ar(0.1))}.scope
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!

// The LPF is a Low Pass Filter which tails off energy above its cutoff frequency, which is 1000Hz in this example. Low pass means pass the low frequencies below the cutoff.
{Pan2.ar(LPF.ar(WhiteNoise.ar(0.1),1000))}.scope
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!

// Let's use our mouse to change the cutoff location:
{Pan2.ar(LPF.ar(WhiteNoise.ar(0.1),MouseX.kr(100,5000,1,0.1)))}.scope
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!

// Let's now use a Resonant Low Pass Filter. This means there is resonance at the cutoff frequency!
// Our mouse is used to change the cutoff location again,along with how much resonance we have:
// Don't make resonance really really small (or zero)
{Pan2.ar(RLPF.ar(WhiteNoise.ar(0.1),MouseX.kr(100,5000,1,0.1),MouseY.kr(0.01,1,1,0.1)))}.scope
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!

// We don't have to use white noise, we can use a harmoically rich tone, like a square wave!
// The "Pulse.ar" is how we define a square wave in SC.
// Two band limited square waves thru a resonant low pass filter
{Pan2.ar(RLPF.ar(Pulse.ar([100, 250], 0.5, 0.1), XLine.kr(8000, 400, 5), 0.05))}.play;
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!

// Let's use a square wave in our previous example, but modulate pitch with Y mouse position!
{Pan2.ar(RLPF.ar(Pulse.ar(MouseY.kr(110,440,1,0.1),0.5,0.1),MouseX.kr(100,5000,1,0.1),0.1))}.scope
// Command+. (MAC) or Control+. (Linux+Windows) will turn it off!
