Merge pull request #42 from flucoma/BufToKr-enhancements

Buf to kr enhancements
nix
Ted Moore 4 years ago committed by GitHub
commit a3f5ceac0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,9 +1,15 @@
FluidKrToBuf { FluidKrToBuf {
*kr { *kr {
arg krStream, buffer; arg krStream, buffer;
if(buffer.numFrames == 0) {"FluidKrToBuf: UGen will have 0 outputs!".warn};
if(buffer.numFrames > 1000) {"FluidKrToBuf: Buffer is % frames. This is probably not the buffer you intended.".format(buffer.numFrames).error}; if(buffer.isKindOf(Buffer).or(buffer.isKindOf(LocalBuf)),{
^buffer.numFrames.do{ if(buffer.numFrames == 0) {"FluidKrToBuf:kr Buffer has 0 frames".warn};
if(buffer.numFrames > 1000) {
Error("FluidKrToBuf:kr Buffer is % frames. This is probably not the buffer you intended.".format(buffer.numFrames)).throw;
};
});
^krStream.numChannels.do{
arg i; arg i;
BufWr.kr(krStream[i], buffer, i); BufWr.kr(krStream[i], buffer, i);
} }
@ -12,12 +18,23 @@ FluidKrToBuf {
FluidBufToKr { FluidBufToKr {
*kr { *kr {
arg buffer; arg buffer, numFrames = -1;
if(buffer.numFrames == 0) {"FluidKrToBuf: Buffer has 0 frames!".warn};
if(buffer.numFrames > 1000) {"FluidKrToBuf: Buffer is % frames. This is probably not the buffer you intended.".format(buffer.numFrames).error}; if((buffer.isKindOf(Buffer).or(buffer.isKindOf(LocalBuf))).not.and(numFrames.isNil),{
Error("FluidBufToKr:kr needs to be passed either an existing buffer or an OutputProxy and a number of frames for the buffer that will be supplied").throw;
});
if(numFrames == -1,{
numFrames = buffer.numFrames;
});
if(numFrames == 0) {"FluidKrToBuf:kr indicated numFrames is zero.".warn};
if(numFrames > 1000) {
Error("FluidKrToBuf: Buffer is indicated to have % frames. This is probably not the buffer you intended.".format(numFrames)).throw;
};
if(buffer.numFrames > 1,{ if(numFrames > 1,{
^buffer.numFrames.collect{ ^numFrames.collect{
arg i; arg i;
BufRd.kr(1,buffer,i,0,0); BufRd.kr(1,buffer,i,0,0);
} }

@ -14,6 +14,9 @@ Initialize an instance of this pseudo UGen
ARGUMENT:: buffer ARGUMENT:: buffer
The link::Classes/Buffer:: that this pseudo UGen will read out of. Must be a one-channel buffer. The link::Classes/Buffer:: that this pseudo UGen will read out of. Must be a one-channel buffer.
ARGUMENT:: numFrames
How many frames the buffer is that will evenutally passed. If providing a buffer directly (instead of as an argument to a SynthDef), the default of -1 will get the number of frames from the buffer passed.
returns:: a Kr stream that has the same number of channels as frames in the link::Classes/Buffer::. returns:: a Kr stream that has the same number of channels as frames in the link::Classes/Buffer::.
INSTANCEMETHODS:: INSTANCEMETHODS::
@ -21,19 +24,37 @@ INSTANCEMETHODS::
EXAMPLES:: EXAMPLES::
code:: code::
// make a buffer with some data in it
~buf = Buffer.loadCollection(s,[0,1,2,3,4,7]);
// play it on the server and read out of this buffer!
(
{
var sig = FluidBufToKr.kr(~buf);
sig.poll;
}.play;
)
// =============== passing a buffer as an argument ======================
// create a synth that both writes into a buffer (with FluidKrToBuf) and reads
// out of the same buffer (with FluidBufToKr)
( (
// FluidBufToKr ~synth = {
s.waitForBoot{ arg buf = 999;
Routine{ FluidKrToBuf.kr(SinOsc.kr(Array.fill(5,{rrand(0.0,1.0)})),buf);
var buf = Buffer.loadCollection(s,[0,1,2,3,4,7]);
// you need to specify the 5 so the synth here will know how many channels to make
s.sync; // the output proxy
FluidBufToKr.kr(buf,5).poll;
{ }.play;
var sig = FluidBufToKr.kr(buf); // you should see all zeros! (unless your buffer #999 has something in it already!)
sig.poll;
}.play;
}.play;
}
) )
// ...then after it is running, instantiate the buffer
~buffer = Buffer.alloc(s,5);
// ...then send it to the buffer
~synth.set(\buf,~buffer);
// you should be able to see the sine oscillators now!
:: ::

@ -64,7 +64,7 @@ s.waitForBoot{
FluidKrToBuf.kr(sig,buf); FluidKrToBuf.kr(sig,buf);
}.play; }.play;
3.wait; 1.wait;
defer{buf.plot}; defer{buf.plot};
}.play; }.play;
@ -85,7 +85,7 @@ s.waitForBoot{
FluidKrToBuf.kr(sig,buf); FluidKrToBuf.kr(sig,buf);
}.play; }.play;
3.wait; 1.wait;
defer{buf.plot}; defer{buf.plot};
}.play; }.play;
@ -94,8 +94,7 @@ s.waitForBoot{
( (
// FluidBufToKr // FluidBufToKr
// 100 is fine
/// 1000 is fine
s.waitForBoot{ s.waitForBoot{
Routine{ Routine{
var buf = Buffer.loadCollection(s,Array.fill(1001,{arg i; i})); var buf = Buffer.loadCollection(s,Array.fill(1001,{arg i; i}));
@ -112,8 +111,7 @@ s.waitForBoot{
( (
// FluidBufToKr // FluidBufToKr
// 100 is fine // This should throw an error because this sound file buffer is longer than 1000 samples
/// 1000 is fine
s.waitForBoot{ s.waitForBoot{
Routine{ Routine{
var buf = Buffer.read(s,"/Users/macprocomputer/Desktop/_flucoma/code/flucoma-core-src/AudioFiles/Harker-DS-TenOboeMultiphonics-M.wav"); var buf = Buffer.read(s,"/Users/macprocomputer/Desktop/_flucoma/code/flucoma-core-src/AudioFiles/Harker-DS-TenOboeMultiphonics-M.wav");
@ -130,8 +128,7 @@ s.waitForBoot{
( (
// FluidKrToBuf test with super long buffer // FluidKrToBuf test with super long buffer
// 100 is fine // This should throw an error because this sound file buffer is longer than 1000 samples
// 1000 is fine
s.waitForBoot{ s.waitForBoot{
Routine{ Routine{
// var buf = Buffer.alloc(s,1000); // var buf = Buffer.alloc(s,1000);
@ -140,7 +137,7 @@ s.waitForBoot{
s.sync; s.sync;
{ {
var sig = SinOsc.kr(rrand(1.0.dup(buf.numFrames),4.0)); var sig = SinOsc.kr(rrand(1.0.dup(10),4.0));
FluidKrToBuf.kr(sig,buf); FluidKrToBuf.kr(sig,buf);
}.play; }.play;
@ -151,3 +148,63 @@ s.waitForBoot{
} }
) )
// ===================== pass a buffer to a running synth =======================
(
s.waitForBoot{
Routine{
~synth = {
arg buf;
FluidBufToKr.kr(buf,5).poll;
}.play;
2.wait;
"make buffer".postln;
~buffer = Buffer.alloc(s,5);
s.sync;
~buffer.setn(0,Array.fill(5,{rrand(0,100)}));
s.sync;
~synth.set(\buf,~buffer);
}.play;
};
)
(
// throws error because number of frames not specified
s.waitForBoot{
Routine{
~synth = {
arg buf;
FluidKrToBuf.kr(SinOsc.kr(Array.fill(5,{rrand(0.0,1.0)})),buf);
FluidBufToKr.kr(buf).poll; ///////// this will now throw an error asking for a numFrames
}.play;
2.wait;
~buffer = Buffer.alloc(s,5);
s.sync;
~synth.set(\buf,~buffer);
}.play;
};
)
(
// works
s.waitForBoot{
Routine{
~synth = {
arg buf = 999;
FluidKrToBuf.kr(SinOsc.kr(Array.fill(5,{rrand(0.0,1.0)})),buf);
FluidBufToKr.kr(buf,5).poll; ////////// this will work becaues it knows how many frames the buffer will be
}.play;
2.wait;
~buffer = Buffer.alloc(s,5);
s.sync;
~synth.set(\buf,~buffer);
}.play;
};
)

Loading…
Cancel
Save