diff --git a/release-packaging/Classes/FluidBufToKr.sc b/release-packaging/Classes/FluidBufToKr.sc index 0e81386..84a0cb6 100644 --- a/release-packaging/Classes/FluidBufToKr.sc +++ b/release-packaging/Classes/FluidBufToKr.sc @@ -1,31 +1,64 @@ FluidKrToBuf { *kr { - arg krStream, buffer; + arg krStream, buffer, krStartChan = 0, krNumChans = -1, destStartFrame = 0; + var endChan; + + // fix -1 default + if(krNumChans == -1,{krNumChans = krStream.numChannels - krStartChan}); + + // what is the last channel that will be used + endChan = (krStartChan + krNumChans) - 1; if(buffer.isKindOf(Buffer).or(buffer.isKindOf(LocalBuf)),{ - 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; + + // sanity check + if(buffer.numFrames == 0){"% Buffer has 0 frames".format(this.class).warn}; + + // oopsie check + if(buffer.numFrames > 1000){ + Error("% Buffer is % frames. This is probably not the buffer you intended.".format(this.class,buffer.numFrames)).throw; }; + + // out of bounds check + if((destStartFrame + krNumChans) > buffer.numFrames,{ + Error("% (destStartFrame + krNumChans) > buffer.numFrames".format(this.class)).throw; + }); + }); - ^krStream.numChannels.do{ - arg i; - BufWr.kr(krStream[i], buffer, i); + ^(krStartChan..endChan).do{ + arg kr_i, i; + BufWr.kr(krStream[kr_i], buffer, destStartFrame + i); } } } FluidBufToKr { *kr { - arg buffer numFrames, startFrame=0; + arg buffer, startFrame = 0, numFrames = -1; - if(buffer.isKindOf(Buffer) or: {buffer.isKindOf(LocalBuf)}, { - numFrames = numFrames ?? {buffer.numFrames - startFrame}; - }, { - numFrames = numFrames ? 1; + // out of bounds check + if(startFrame < 0,{Error("% startFrame must be >= 0".format(this.class)).throw;}); + + if(buffer.isKindOf(Buffer) or: {buffer.isKindOf(LocalBuf)},{ + + // fix default -1 + if(numFrames == -1,{numFrames = buffer.numFrames - startFrame}); + + // dummy check + if(numFrames < 1,{Error("% numFrames must be >= 1".format(this.class)).throw}); + + // out of bounds check + if((startFrame+numFrames) > buffer.numFrames,{Error("% (startFrame + numFrames) > buffer.numFrames".format(this.class)).throw;}); + + },{ + // make sure the numFrames give is a positive integer + if((numFrames < 1) || (numFrames.isInteger.not),{ + Error("% if no buffer is specified, numFrames must be a value >= 1.".format(this.class)).throw; + }); }); + // oopsie check if(numFrames > 1000) { Error("%: numframes is % frames. This is probably not what you intended.".format(this.class, numFrames)).throw; }; diff --git a/test/FluidBufToKr test.scd b/test/FluidBufToKr test.scd index b3c070c..3d2954c 100644 --- a/test/FluidBufToKr test.scd +++ b/test/FluidBufToKr test.scd @@ -93,7 +93,7 @@ s.waitForBoot{ ) ( -// FluidBufToKr +// FluidBufToKr should throw error s.waitForBoot{ Routine{ @@ -154,7 +154,7 @@ s.waitForBoot{ Routine{ ~synth = { arg buf; - FluidBufToKr.kr(buf,5).poll; + FluidBufToKr.kr(buf,numFrames:5).poll; }.play; 2.wait; @@ -197,7 +197,7 @@ s.waitForBoot{ 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 + FluidBufToKr.kr(buf,numFrames:5).poll; ////////// this will work becaues it knows how many frames the buffer will be }.play; 2.wait; @@ -208,3 +208,155 @@ s.waitForBoot{ }.play; }; ) + +// test start frame: + + +( +// should skip the 0 +s.waitForBoot{ + Routine{ + var buf = Buffer.loadCollection(s,[0,1,2,3,4,7]); + + s.sync; + + { + var sig = FluidBufToKr.kr(buf,1); + sig.poll; + }.play; + }.play; +} +) + +( +// should be 2,3,4 +s.waitForBoot{ + Routine{ + var buf = Buffer.loadCollection(s,[0,1,2,3,4,7]); + + s.sync; + + { + var sig = FluidBufToKr.kr(buf,2,3); + sig.poll; + }.play; + }.play; +} +) + +( +// last four slots should be 0 +s.waitForBoot{ + Routine{ + var buf = Buffer.loadCollection(s,0.dup(10)); + + s.sync; + + { + FluidKrToBuf.kr(LFDNoise3.kr(1.dup(6)),buf); + }.play; + + 1.wait; + + // defer{buf.plot}; + buf.loadToFloatArray(action:{ + arg vals; + vals.postln; + }); + }.play; +} +) + +( +// middle slots should be not zero +s.waitForBoot{ + Routine{ + var buf = Buffer.loadCollection(s,0.dup(4)); + + s.sync; + + { + FluidKrToBuf.kr(LFDNoise3.kr(1.dup(2)),buf,destStartFrame:1); + }.play; + + 1.wait; + + // defer{buf.plot}; + buf.loadToFloatArray(action:{ + arg vals; + vals.postln; + }); + }.play; +} +) + +( +// should throw error +s.waitForBoot{ + Routine{ + var buf = Buffer.loadCollection(s,0.dup(4)); + + s.sync; + + { + FluidKrToBuf.kr(LFDNoise3.kr(1.dup(2)),buf,destStartFrame:3); + }.play; + + 1.wait; + + // defer{buf.plot}; + buf.loadToFloatArray(action:{ + arg vals; + vals.postln; + }); + }.play; +} +) + +( +// should be 0,0,200,3000,0,0 +s.waitForBoot{ + Routine{ + var buf = Buffer.alloc(s,7); + + s.sync; + + { + var sig = 3.collect{arg i; DC.kr((i+1)*100)}; + // sig.poll; + FluidKrToBuf.kr(sig,buf,1,2,2); + }.play; + + 1.wait; + + buf.loadToFloatArray(action:{ + arg vals; + vals.postln; + }); + }.play; +} +) + +( +// should be 100,200,300,400 +s.waitForBoot{ + Routine{ + var buf = Buffer.alloc(s,4); + + s.sync; + + { + var sig = 4.collect{arg i; DC.kr((i+1)*100)}; + // sig.poll; + FluidKrToBuf.kr(sig,buf); + }.play; + + 1.wait; + + buf.loadToFloatArray(action:{ + arg vals; + vals.postln; + }); + }.play; +} +)