[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [sc-users] Read single channel of external file into buffer
Hi Axel,
These are two approaches; The first one uses several predefined
synthdefs - as many as the number of channels you want to deal with.
The second approach generates the synthdef accordingly to the number
of channels needed.
I have added a couple of ideas of how to load soundfiles into a
buffers array as well.
//-----------------------------------------------------------------------
// using static defs
(
SynthDef(\BufRd1, { arg out=0, bufnum, amp=0.1;
Out.ar(out,
BufRd.ar(1, bufnum,
Phasor.ar(0, BufRateScale.kr(bufnum), 0, BufFrames.kr(bufnum))) * amp ! 2);
}).send(s);
SynthDef(\BufRd2, { arg out=0, bufnum, amp=0.1;
Out.ar(out,
BufRd.ar(2, bufnum,
Phasor.ar(0, BufRateScale.kr(bufnum), 0, BufFrames.kr(bufnum))) * amp);
}).send(s);
)
// load some samples
(
~buffers = nil;
[ // location of samples
"/absolute/path/to/sample1",
"~/relative/path/to/sample2".standardizePath,
"~/relative/path/to/sample3".standardizePath // up to sample n....
].do({ arg file, i;
var sf;
sf = SoundFile.new;
if ( sf.openRead(file) == true, {
~buffers = ~buffers.add(Buffer.read(s, file));
}, {
("Error loading" + file).postln;
});
sf.close;
});
)
// play a random buffer
(
var buffer;
buffer = ~buffers.choose; // pick a sample from the list
("buffer path:" + buffer.path).postln;
("numChannels:" + buffer.numChannels).postln;
z = Synth("BufRd" ++ buffer.numChannels, [\bufnum, buffer.bufnum]);
)
z.free
~buffers.do(_.free) // free the buffers
//-----------------------------------------------------------------------
// using dynamic defs
// load all samples in a directory
(
var path;
~buffers = nil;
path = "~/relative/path/to/samples/folder".standardizePath;
PathName(path).files.do({ arg path, i;
var sf;
sf = SoundFile.new;
if ( sf.openRead(path.fullPath) == true, {
~buffers = ~buffers.add(Buffer.read(s, path.fullPath));
}, {
("Error loading" + path.fullPath).postln;
});
sf.close;
});
)
// play a random sample
(
var c, buffer;
c = Condition.new;
Routine.run({
buffer = ~buffers.choose; // pick a sample from the list
SynthDef(\test, { arg out=0, bufnum, amp=0.1;
Out.ar(out,
BufRd.ar(buffer.numChannels, bufnum,
Phasor.ar(0, BufRateScale.kr(bufnum), 0,
BufFrames.kr(bufnum))) * amp);
}).send(s);
s.sync(c);
("buffer path:" + buffer.path).postln;
("numChannels:" + buffer.numChannels).postln;
z = Synth(\test, [\bufnum, buffer.bufnum]);
});
)
z.free
~buffers.do(_.free) // free the buffers when done
hope this helps.
cheers,
x
On 6/5/06, Axel BALLEY <a.balley@xxxxxxxxxxxxxx> wrote:
Hi list !
I need to read a buffer using the BufRd UGen, which unfortunately has
a channel num fixed at synth build-time. I use the read method of the
Buffer class to create buffers from external sound files, and am
looking for some code that would create one channel buffers from both
mono and stereo files, i.e. it would only read the left channel of a
stereo file.
Can anyone help ?
Thanks in advance :)
--
Axel Balley
Jazzmutant
www.jazzmutant.com
_______________________________________________
sc-users mailing list
sc-users@xxxxxxxxxxxxxxx
http://www.create.ucsb.edu/mailman/listinfo/sc-users