// Sound file playback example using CtkBuffer // Create CtkSynthDef (similar to the "playmono" SynthDef example) ~play = CtkSynthDef(\play, {arg dur, loc, buffer, start, amp = 0, rate=1; var src, env, halfpi, panl, panr; halfpi = 0.5pi; amp = amp.dbamp; env = EnvGen.kr(Env([0, amp, amp, 0], [0.01, dur - 0.02, 0.01]), doneAction: 2); src = PlayBuf.ar(1, buffer, rate * BufRateScale.kr(buffer), 1.0, start * BufSampleRate.kr(buffer), 1.0); src = src * env; panl = src * (loc * halfpi).cos; panr = src * (loc * halfpi).sin; Out.ar(0, [panl, panr]) }); // ~playfun: function to play notes // starttime: time in the score to start playing // gestdur: total duration of the gesture // ampenv: amplitude env. for the events (in dB, x vals normalized to 1) // rateenv: playback rate env. for the events (x values normalized to 1) // panenv: panning env. for the events (0=L, 1=R, x values normalized to 1) // buffer: instance of CtkBuffer to play from // score: instance of CtkScore to write to ~playfun = {arg starttime, gestdur, ampenv, rateenv, panenv, buffer, score; var note, now, ratio, rate, pan, amp; now = 0; while({ ratio = now / gestdur; amp = ampenv[ratio]; rate = rateenv[ratio]; pan = panenv[ratio]; ~play.new(now + starttime, 0.5) // the arg will parse the CtkBuffer and grab its bufnum .buffer_(buffer) .amp_(amp) .rate_(coin(0.5).if(1, -1) * rate) .loc_(pan) .dur_(0.5) .start_((buffer.duration - (0.5 * rate)).rand) .addTo(score); now = now + rrand(0.1, 0.2); now < gestdur; }); }; // create new CtkScore ~score = CtkScore.new; // create buffer and add it to the score ~buf = CtkBuffer("sounds/a11wlk01-44_1.aiff").addTo(~score); // fill score up ~playfun.value(0, 20, Env([-12, -3, -30], [0.2, 0.8], \sin), Env([0.5, 2.0, 1.0], [0.2, 0.8], [3, -5]), Env([0.5, 0.0, 1.0], [0.2, 0.8], [3, -5]), ~buf, ~score); // play it! ~score.play(s);