{
   "labels" : [
      "nature",
      "lifeforms",
      "insects"
   ],
   "id" : "1-4QB",
   "is_private" : null,
   "code" : "// Fig 50.7: Field Cricket\r\n\r\n(\r\na = {\r\n\tvar modulator, mod1, mod2, mod3;\r\n\t\r\n\t// repeat time is 0.7s: equates to 1.43 Hz.\r\n\tmodulator = LFSaw.ar(1.43, 1, 0.5, 0.5);\r\n\tmod2 = (modulator * 40.6 * 2pi).cos.squared;\r\n\tmod3 = modulator * 3147;\r\n\tmod3 = (mod3 * 2pi).cos + ((mod3 * 2 * 2pi).cos * 0.3);\r\n\tmod1 = ((Wrap.ar(modulator.min(0.1714) * 5.84) - 0.5).squared * (-4) + 1) * (mod2 * mod3);\r\n\tmod1 = (mod1 * 0.1)!2;\r\n}.play;\r\n)\r\n\r\n// To stop: \r\na.free;\r\n\r\n\r\n\r\n// Fig 50.8: Field Cricket 2\r\n\r\n(\r\nb = {\r\n\tvar trig, seq, demand, cricket;\r\n\t\r\n\t// instead of [metro], Impulse.kr is used here. Delta t = 17 ms equates to 58.82 Hz.\r\n\ttrig = Impulse.kr(58.82);\r\n\t\r\n\t// the main idea of the following line was to use an approach\r\n\t// that uses the same coefficients as described in the pd patch\r\n\tseq = Dseq(Array.fill(41, {|i| if(i<7, {(i+2)/9},{0}) }),inf);\r\n\tdemand = Demand.kr(trig,0,seq);\r\n\t\r\n\t// Implementation of the pd code for pulses including amplitude grow:\r\n\t// cricket = EnvGen.ar(Env.new([0, 1, 1, 0], [0.0001, 0.0001, 0]), trig) * demand;\r\n\t\r\n\t// 2nd implementation: pure data seemed to slightly disobey its own specifications, \r\n\t// so I analysed the waveform and came up with this:\r\n\tcricket = EnvGen.ar(Env.new([0, 1, 0], [4/44100, 0]), trig) * demand;\r\n\t\r\n\t\r\n\tcricket = OnePole.ar(cricket, exp(-2pi * (1000 * SampleDur.ir)));\r\n\tcricket = (\r\n\t\t\t// changed the Q factor of the first 3 BPFs to approximate farnells sound \r\n\t\t\tBPF.ar(cricket, 4500 + ((0..2)*50), 300.reciprocal, 100)).sum \r\n\t\t\t+ BPF.ar(cricket, 9000, 500.reciprocal, 42\r\n\t);\t\t   \r\n\tcricket = ((cricket - OnePole.ar(cricket, exp(-2pi * (4000 * SampleDur.ir)))) * 0.5)!2;\r\n}.play;\r\n)\r\n\r\n// To stop: \r\nb.free;\r\n\r\n\r\n\r\n// Fig: 50.10: Cicada with 3 call types\r\n\r\n(\r\nc = {\r\n\tvar sig, trig, seq, freq, mul, vals;\r\n\t\r\n\ttrig = Impulse.kr(0.2);\r\n\tvals = [\r\n\t\t[0.5, 128],\r\n\t\t[8,6],\r\n\t\t[30,3]\r\n\t];\r\n\tfreq = TChoose.kr(trig, vals);\r\n\t\r\n\t\t\t\r\n\tsig = WhiteNoise.ar;\r\n\t// The one pole filters in pure data and SC differ, so I changed the coefficents \r\n\t// a little. Also the  multiplication by 5 is not in the book, but helps to \r\n\t// approach the audible result of Farnells patch.\r\n\tsig = (sig - OnePole.ar(sig, exp(-2pi * (8000 * SampleDur.ir))));\r\n\tsig = (sig - OnePole.ar(sig, exp(-2pi * (8000 * SampleDur.ir))));\r\n\tsig = OnePole.ar(sig, exp(-2pi * (10000 * SampleDur.ir)));\r\n\tsig = OnePole.ar(sig, exp(-2pi * (10000 * SampleDur.ir)));\r\n\tsig = sig * 5;\r\n\r\n\t\t\r\n\tsig = BPF.ar(sig, [7500, 5500], 40.reciprocal).sum * SinOsc.ar(500);\r\n\tsig = sig * (1 / (SinOsc.ar( freq[0], 0, freq[1] ).squared + 1));\r\n\tsig = (sig - OnePole.ar(sig, exp(-2pi * (4000 * SampleDur.ir)))) * 4.dup;\r\n}.play;\r\n)\r\n\r\n// To stop: \r\nc.free;\r\n\r\n\r\n\r\n// Fig: 50.13 Direct signal implementation of housefly wing\r\n\r\n// The adjustable parameters in the pd patch can be controlled by the mouse movement: \r\n// MouseX is controlling the wing-frequency, MouseY is controlling the wing-resonance.\r\n\r\n(\r\nSynthDef(\\houseflyWing, { |out=0|\r\n\tvar sig, downstroke, upstroke, wingFreq, wingRes;\r\n\t\r\n\t// this is already a preparation for fig 50.14 and is not described \r\n\t// in the pure data patch on fig 50.13\r\n\twingFreq = In.ar(10,2);\r\n\twingRes = In.ar(20,2);\r\n\r\n\t// Also, it is prepared for some other input from a different source, \r\n\t// to not only control the patch with the mouse movement. \r\n\t// See also the following URL for more information about the next lines: \r\n\t// http://supercollider.sourceforge.net/wiki/index.php/Boolean_logic_in_the_server \r\n\twingFreq = Select.ar(wingFreq > 0, [K2A.ar(MouseX.kr(0, 300)), wingFreq]);\r\n\twingRes = Select.ar(wingRes > 0, [K2A.ar(MouseY.kr(3,5)), wingRes]);\r\n\t\t\r\n\tsig = LFSaw.ar(wingFreq, 1, 0.5, 0.5);\r\n\tsig = ((sig * 0.2).min(sig * (-1) + 1)).min(sig.min(sig * (-1) + 1));\r\n\tsig = (sig * 6 - 0.5) * 2; \t\r\n\t\r\n\tdownstroke = (wingRes) * sig.min(0);\r\n\tdownstroke = (Wrap.ar(downstroke) * 2pi).cos * sig.min(0) * 0.5 + sig.min(0);\r\n\tupstroke = sig.max(0).cubed * 2;\t\r\n\t\t\r\n\tsig = downstroke + upstroke;\t\r\n\tsig = (sig - OnePole.ar(sig, exp(-2pi * (700 * SampleDur.ir)))).dup * 0.05;\r\n\tOut.ar(out, sig);\r\n}).add;\r\nx = Synth(\\houseflyWing);\r\n)\r\n\r\n\r\n\r\n// Fig: 50.14 Buzzing housefly\r\n\r\n(\r\nSynthDef(\\buzzingHousefly, { \r\n\tvar beatingFreq, resonanceMod;\r\n\t\r\n\tbeatingFreq = OnePole.ar(WhiteNoise.ar, exp(-2pi * (4 * SampleDur.ir)));\r\n\tbeatingFreq = OnePole.ar(beatingFreq, exp(-2pi * (4 * SampleDur.ir)));\r\n\tbeatingFreq = beatingFreq * 700 + 220;\r\n\t\t\r\n\tresonanceMod = OnePole.ar(WhiteNoise.ar, exp(-2pi * (5 * SampleDur.ir)));\r\n\tresonanceMod = OnePole.ar(resonanceMod, exp(-2pi * (5 * SampleDur.ir)));\r\n\t\r\n\tOut.ar(10, [beatingFreq, (resonanceMod * 3) + beatingFreq]);\r\n\tOut.ar(20, (resonanceMod * 40 + 5)!2 );\t\r\n}).add;\r\ny = Synth(\\buzzingHousefly);\r\n)\r\n\r\n// Now again the housefly wings are controlled by the mouse movement:\r\ny.free;\r\n\r\n// To stop: \r\nx.free;\r\n\r\n// If the Synth(\\buzzingHousefly) was executed before the Synth (\\houseflyWing) you'll \r\n// have to execute the following line to hear the effects of the Synth(\\buzzingHousefly). \r\n// This is because In.ar and Out.ar are used in this example to communicate between both \r\n// patches, and when working with In.ar it is always necessary to have the Order of \r\n// execution of synths on the server in mind. (see also Helpfile: Order of execution).\r\nSynth(\\buzzingHousefly, addAction: \\addToHead);\r\n\r\n\r\n// code also available here:\r\n// http://en.wikibooks.org/wiki/Designing_Sound_in_SuperCollider/Insects",
   "name" : "Insects",
   "author" : "DSastre",
   "ancestor_list" : [],
   "description" : "Different insect sounds, including field crickets, a cicada and a housefly. Based on pure data code from the book \"Designing Sound\" by Andy Farnell."
}
