{
   "labels" : [],
   "code" : "//f0 sound detector - automatically record sound into buffers - with playback - originally written for av-programming udk class ws2011/12\r\n(\r\nz= 16;\t//number of buffers in b\r\nl= 2;\t//length of buffers in seconds\r\nx= false;\t//mute recording\r\ns.latency= 0.05;\r\ns.waitForBoot{\r\n\tvar detectorSynth, recorderSynths, responder;\r\n\tSynthDef(\\f0SoundDetector, {|thresh= 0.075, time= 0.2|\r\n\t\tvar input= SoundIn.ar;\r\n\t\tvar off= DetectSilence.ar(input, thresh, time);\r\n\t\tvar on= 1-off;\t\t\t\t//invert\r\n\t\tvar cnt= PulseCount.ar(on)%z+1;\t//buffer index counter\r\n\t\tvar changed= HPZ1.ar(on).abs>0;\t//trigger\r\n\t\tSendTrig.ar(changed, 0, on*cnt);\t//0= rec off, >0 rec start index\r\n\t}).add;\r\n\tSynthDef(\\f0SoundRecorder, {|buf, t_trig= 0|\r\n\t\tvar src= DelayN.ar(SoundIn.ar, 0.1, 0.1);\t//compensate for detector latency\r\n\t\tRecordBuf.ar(src, buf, loop: 0, trigger: t_trig);\r\n\t}).add;\r\n\tSynthDef(\\f0SoundPlayer, {|out= 0, buf, pan= 0, amp= 0.1, atk= 0.01, rel= 0.1, gate= 1, speed= 1|\r\n\t\tvar env= EnvGen.ar(Env.asr(atk, amp, rel), gate, doneAction:2);\r\n\t\tvar src= PlayBuf.ar(1, buf, speed, loop:1);\r\n\t\tOut.ar(out, Pan2.ar(src*env, pan));\r\n\t}).add;\r\n\ts.sync;\r\n\tb= {Buffer.alloc(s, s.sampleRate*l)}.dup(z);\t//mono buffer\r\n\ts.sync;\r\n\tresponder= OSCFunc({|m|\r\n\t\tvar index= m[3].asInteger;\r\n\t\tif(x.not, {\t\t\t\t\t\t//if not muted\r\n\t\t\tif(index>=1, {\r\n\t\t\t\t(\"recording\"+index).postln;\r\n\t\t\t\trecorderSynths[index-1].set(\\t_trig, 1);\t//start sound recording\r\n\t\t\t}, {\r\n\t\t\t\tif(index==0, {\r\n\t\t\t\t\t\"recording stopped\".postln;\r\n\t\t\t\t});\r\n\t\t\t});\r\n\t\t});\r\n\t}, \\tr, s.addr);\r\n\tdetectorSynth= Synth(\\f0SoundDetector);\r\n\trecorderSynths= b.collect{|x| Synth(\\f0SoundRecorder, [\\buf, x])};\r\n\tCmdPeriod.doOnce({b.do{|x| x.free}; responder.free});\r\n\ts.sync;\r\n\tPdef(\\pattern1).clear;\r\n\tPdef(\\pattern1).play(quant:1);\r\n\tPdef(\\pattern1, Pbind(\r\n\t\t\\instrument, \\f0SoundPlayer,\r\n\t\t\\index, Pn(Pshuf((0..(z-1)), 4)),\t//index selector, scramble every 4th time\r\n\t\t\\buf, Pfunc({|ev| b[ev.index]}),\r\n\t\t\\amp, 0.8,\r\n\t\t\\dur, Pseq([0.25, 0.25, 0.5, 1], inf),\r\n\t\t\\pan, Pwhite(-0.8, 0.8, inf),\r\n\t\t\\speed, Prand([1, 1.2], inf)\r\n\t));\r\n};\r\n)\r\n\r\n/*\r\nx= true;\t//mute recording (stop collecting more)\r\nx= false;\t//unmute\r\n*/\r\n\r\n/*\r\n(\t//rewite the playback as it is running\r\nPdef(\\pattern1, Pbind(\r\n\t\\instrument, \\f0SoundPlayer,\r\n\t\\index, Pseq((0..(z-1)), inf),\t//index selector, played in order\r\n\t\\buf, Pfunc({|ev| b[ev.index]}).trace,\r\n\t\\amp, 0.8,\r\n\t\\dur, Prand([0.25, 0.5, 1, 2, Pn(0.125, 4)], inf),\r\n\t\\pan, Pwhite(-0.8, 0.8, inf),\r\n\t\\speed, Prand([1, 1.25, 1.5, 1.75, 0.75, 0.5], inf)*Prand([1, -1], inf)\t//back and forth\r\n));\r\n)\r\nPdef(\\pattern1).stop\r\n*/",
   "is_private" : null,
   "id" : "1-19k",
   "name" : "f0 sound detector",
   "author" : "redFrik",
   "description" : "automatically record sound into buffers - with playback",
   "ancestor_list" : []
}
