/* Reading in soundfiles */ ( SynthDef(\playmono, {arg dur, loc, buffer, start, amp = 0; 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); // PlayBuf.ar(arg numChannels, bufnum=0, rate=1.0, trigger=1.0, startPos=0.0, loop = 0.0); // the number of channels is FIXED and MUST match the number of channels in the soundfile // This SynthDef will only be able to play mono files // since loop is set to 0.0, if the end of the file is reached, nothing else will play. // Placing BufRateScale into the rate arg will let SC play files with different samplerates // easily src = PlayBuf.ar(1, buffer, -1 * BufRateScale.kr(buffer), 1.0, start * BufSampleRate.kr(buffer), 1.0); // multiply the sound by the env now. It is more efficient to multiply an env with a mono // sound then later with a stereo sound src = src * env; panl = src * (loc * halfpi).cos; panr = src * (loc * halfpi).sin; // The output can be an array of values. Here, two values are in the array, so they will // play in outputs 0 and 1 (left and right) Out.ar(0, [panl, panr]) }).load(s); ) // To use a soundfile, one first needs to be read into s = Server.internal.boot; s.scope(2); // allocRead will read an entire soundfile into a buffer. This one is located in the SC // sounds folder. s.sendBundle(0.1, [\b_allocRead, 1, "sounds/a11wlk01.wav"]); s.sendBundle(0.1, [\s_new, \playmono, -1, 0, 1, \dur, 5, \loc, 0.5, \buffer, 1, \start, 0.0]); ( SynthDef(\playstereo, {arg dur, buffer, start, rate, amp = 0; var env, halfdur, left, right; amp = amp.dbamp; halfdur = dur * 0.5; env = EnvGen.kr(Env([0, amp, 0], [halfdur, halfdur], \sin), doneAction: 2); // because this is a stereo file, PlayBuf outputs an array with two channels // you can assign a variable to each channel with the #. Here, left will // be assigned to the first channel, and right to the second #left, right = PlayBuf.ar(2, buffer, rate * BufRateScale.kr(buffer), 1.0, start * BufSampleRate.kr(buffer), 0.0); OffsetOut.ar(0, [left, right] * env) }).load(s); ) // I have a folder in my home directory called Snd where I store all my samples. If you do the // same and store the samples from our website there, these examples will work as is. s.sendBundle(0.1, [\b_allocRead, 1, "~/Snd/fueye.aiff".standardizePath]); s.sendBundle(0.1, [\s_new, \playstereo, -1, 0, 1, \dur, 4, \buffer, 1, \start, 0, \rate, 1]); // playback lower and backwards s.sendBundle(0.1, [\s_new, \playstereo, -1, 0, 1, \dur, 4, \buffer, 1, \start, 3, \rate, -0.7]); /* In the next SynthDef, BufRd is used: BufRd.ar(arg numChannels, bufnum=0, phase=0.0, loop=1.0, interpolation=2); The phase arg needs to be an audio rate signal that tells BufRd a position in the soundfile. BufRd can interpolate in 3 different ways. 1 = no interp. 2 = linear interp. 4 = cubic interp. There is no rate control passed into BufRd, but it can be controlled by the UGen that is controlling the phase argument. Notice that more of the BufInfo Ugens are used. Here, Phasor is controlling the phase arg: Phasor.ar(arg trig = 0.0, rate = 1.0, start = 0.0, end = 1.0, resetPos = 0.0) This SynthDef again uses a mono file */ ( SynthDef(\bufrd, {arg dur, buffer, start, rate, loc, amp = 0; var env, src, phase, halfdur, halfpi, panl, panr; halfdur = dur * 0.5; amp = amp.dbamp; env = EnvGen.kr(Env([0, amp, 0], [halfdur, halfdur], \sin), doneAction: 2); // Phasor will output numbers from 0 to the number of frames in the buffer // over the scaled playrate of the buffer (based on the buffer SampleRate) * the rate // that is passed in as an arg to the SynthDef. phase = Phasor.ar(0, BufRateScale.kr(buffer) * rate, 0, BufFrames.kr(buffer)); src = BufRd.ar(1, buffer, phase, 1, 4); src = src * env; halfpi = 0.5pi; panl = src * (loc * halfpi).cos; panr = src * (loc * halfpi).sin; Out.ar(0, [panl, panr]); }).load(s); ) s.sendBundle(0.1, [\b_allocRead, 1, "sounds/a11wlk01-44_1.aiff"]); s.sendBundle(0.1, [\s_new, \bufrd, -1, 0, 1, \dur, 4, \loc, 0.5, \buffer, 1, \start, 0, \rate, 0.5]); /* Since BufRd can take ANY audio rate UGen, you can also use some randomness! */ ( SynthDef(\bufrdrand, {arg dur, buffer, loc, amp = 0; var env, src, phase, halfdur, halfpi, panl, panr; halfdur = dur * 0.5; amp = amp.dbamp; env = EnvGen.kr(Env([0, amp, 0], [halfdur, halfdur], \sin), doneAction: 2); // using half the number of frames for the mul and add args to LFNoise1, we'll get // interpolated randomness for a pointer. We set the rannge of LFNoise1 to 0 to the number // of frames in the soundfile using the BufFrames info UGen phase = LFNoise1.ar(1).range(0, BufFrames.kr(buffer)); src = BufRd.ar(1, buffer, phase, 1, 4); src = src * env; halfpi = 0.5pi; panl = src * (loc * halfpi).cos; panr = src * (loc * halfpi).sin; Out.ar(0, [panl, panr]); }).load(s); ) s = Server.internal.boot; // allocRead will read an entire soundfile into a buffer. This one is located in the SC // sounds folder. s.sendBundle(0.1, [\b_allocRead, 1, "sounds/a11wlk01-44_1.aiff"]); s.sendBundle(0.1, [\s_new, \bufrdrand, -1, 0, 1, \dur, 10, \loc, 0.5, \buffer, 1]); ( SynthDef(\playmonobpf, {arg dur, loc, buffer, start, amp = 0, freq, ampscale=1; var src, env, halfpi, panl, panr; halfpi = 0.5pi; amp = amp.dbamp; // this env uses levelScale to boost the signal env = EnvGen.kr(Env([0, 1, 1, 0], [0.01, dur - 0.02, 0.01]), levelScale: amp, doneAction: 2); // PlayBuf.ar(arg numChannels, bufnum=0, rate=1.0, trigger=1.0, startPos=0.0, loop = 0.0); // the number of channels is FIXED and MUST match the number of channels in the soundfile // This SynthDef will only be able to play mono files // since loop is set to 0.0, if the end of the file is reached, nothing else will play. // Placing BufRateScale into the rate arg will let SC play files with different samplerates // easily src = PlayBuf.ar(1, buffer, 1.0 * BufRateScale.kr(buffer), 1.0, start * BufSampleRate.kr(buffer), 1.0); // let this one loop src = BPF.ar(src, freq * [1, 2, 3, 4], 0.001, [1, 0.3, 0.7, 0.1]).sum; // multiply the sound by the env now. It is more efficient to multiply an env with a mono // sound then later with a stereo sound src = src * env; panl = src * (loc * halfpi).cos; panr = src * (loc * halfpi).sin; // The output can be an array of values. Here, two values are in the array, so they will // play in outputs 0 and 1 (left and right) Out.ar(0, [panl, panr]*ampscale) }).load(s); ) s.sendBundle(0.1, [\b_allocRead, 0, "sounds/a11wlk01-44_1.aiff"]); s.sendBundle(0.1, [\s_new, \playmonobpf, -1, 0, 1, \dur, 5, \loc, 0, \buffer, 1, \start, 0.0, \amp, 12, \freq, 880]);