( ~modes = [ [ "saw", \pos_saw ], [ "reverse", \pos_reverse ], [ "sine", \pos_sine ] ]; ~outputDir = Platform.recordingsDir +/+ "GrainBuffers"; ~grainsb = Bus.audio(s, 2); ~granulators = Array.new(4); ~grainmodes = [ 0, 0, 0, 0 ]; // keep track of mode so don't swap if not needed ~posb = Array.new(4); ~rectriggerb = Array.new(4); ~patterns = Array.new(4); ~players = Array.new(4); ~loopsynths = Array.new(4); // create the control busses (0..3).do({ ~posb.add(Bus.control(s, 1)); ~rectriggerb.add(Bus.control(s, 1)); }); // start the granulators (0..3).do({ |i| var pb = ~posb[i], rtb = ~rectriggerb[i]; ~granulators.add(Granulator.new(~buflen, ~infxb, ~grainsb, pb, rtb)); }); // set up the Patterns which drive the position synths (0..3).do({ |i| ~patterns.add(~makePattern.value(i, 0)); }); // play the four patterns // to-do - will these need separate tempoclocks? (0..3).do({|i| ~players.add(~patterns[i].play(~tc, quant: ~beatsperbar)) }); ~setmode = { arg track, mode; if( ~grainmodes[track] != mode, { ~grainmodes[track] = mode; ~players[track].stop; ~patterns[track].free; ~patterns[track] = ~makePattern.value(track, mode); ~players[track] = ~patterns[track].play(~tc, quant: ~beatsperbar); }); }; ~makePattern = { arg track, mode; var ptrig, ppos, synth = ~modes[mode][1]; [ "makePattern", ~track, ~buflen ].postln; ptrig = Pbind( \instrument, \trigger, \dur, ~beatsperbar, \out, ~rectriggerb[track] ); ppos = Pbind( \instrument, synth, \dur, ~beatsperbar, \length, ~buflen, \out, ~posb[track] ); Ppar([ptrig, ppos]); }; ~dumpbuffers = { |prefix| (0..3).do({|i| var filename = ~outputDir +/+ prefix ++ 'buffer' ++ i.asString ++ '.aiff'; ~granulators[i].buffer.write(filename); }); } )