PlayBuf.ar loads an audio buffer and plays it back with variable speed, direction, and looping
To play a sound file in SC, first load it into a Buffer: Buffer.read(s, "/path/to/file.wav"). Then use PlayBuf.ar(numChannels, bufnum, rate, loop: 1). Rate 1 = normal, 2 = double speed (octave up), -1 = reverse, 0.5 = half speed. loop: 1 loops continuously. doneAction: 2 frees the synth when non-looping playback ends. A quick way to get the file path in SC: drag the file onto a blank SC document and it auto-inserts the quoted path. PlayBuf is the foundation for sampler-style instruments.
Examples
~buf = Buffer.read(s, "/home/Music/sample.wav");
{PlayBuf.ar(1, ~buf, rate:1, loop:1)}.play;
{PlayBuf.ar(1, ~buf, MouseY.kr(0.5,2))}.play; // mouse speed
{PlayBuf.ar(1, ~buf, -1, loop:1)}.play; // reverse
Assessment
Write a PlayBuf sampler that plays a mono file and responds to MouseX to scrub speed 0.25x–4x with looping. What does doneAction: 2 do in a PlayBuf context?