« Return to Thread: Cropping buffer - Server.sync + best way

Re: Cropping buffer - Server.sync + best way

by Fredrik Olofsson :: Rate this Message:

Reply to Author | View in Thread

well, your method looks a bit strange as a whole.  can't explain why  
nor do i know what's wrong.  but try this instead.  not thoroughly  
tested but seem to work.

/*
                s.boot
                b = Buffer.read(s, "sounds/a11wlk01.wav");
                b.plot
                b.numFrames
                b.play
                b
                c= b.crop(100000, 66666, {|buf| buf.numFrames.postln;})
                c.plot
                c.numFrames
                c.play
                c
                d= c.crop(10000, 6666, {|buf| buf.numFrames.postln;})
                d.plot
                d.numFrames
                d.play
                d
*/

+ Buffer {
        crop {arg startFrame, numFrames, action;
                ^Buffer.alloc(server, numFrames, numChannels, {|buf|
                        buf.updateInfo({
                                this.copyData(buf, 0, startFrame, numFrames);
                                buf.updateInfo(this.free(action));
                        });
                });
        }
}


btw, anyone know if copyData synchronous or not?
_f



21 jul 2008 kl. 19.52 skrev thor:

>
> Great comments Fredrik.
>
> It turns out that the method is not working so well: I can only  
> crop it once,
> the second time I try to crop the same buffer, the frames disappear  
> for some
> reason and it seems to happen when the this.updateInfo is called.
>
> Check out the method here with the postlns. But I've implemented  
> all your
> suggestions in this class.
>
> Test code:
>
> b = Buffer.read(s, "sounds/a11wlk01.wav");
> b.numFrames
> b.play
>
> b.crop(100000, 66666, false, {|buf| buf.numFrames.postln;})
> b.numFrames
> b.play
>
> b.crop(10000, 6666, false, {|buf| buf.numFrames.postln;})
> b.numFrames
> b.play
>
>
> + Buffer {
> crop {arg startFrame, numFrames, overwrite=false, action;
> var cond, tempbuf, tempbufnum, newbuf;
> cond = Condition.new;
>
> Routine.run {
>
> tempbuf = Buffer.alloc(server, numFrames, numChannels);
> server.sync(cond);
> this.copyData(tempbuf, 0, startFrame, numFrames);
> tempbufnum = bufnum;
> server.sync(cond);
> this.free;
> [\tempbuf, tempbuf].postln;
>
> server.sync(cond);
>
> newbuf = Buffer.alloc(server, tempbuf.numFrames,  
> tempbuf.numChannels, bufnum:tempbufnum);
> server.sync(cond);
> tempbuf.copyData(newbuf);
> server.sync(cond);
>
> [\newbuf, newbuf].postln;
>
> tempbuf.free;
> tempbuf = nil;
>
> if(overwrite, { newbuf.write(this.path, "AIFF", "int16") });
>
> server.sync(cond);
> [\newbuf2, newbuf].postln;
> this.updateInfo(action); // update so the buffer object has the  
> new buffer info
> };
> }
> }
>
>
>
>
> On 21 Jul 2008, at 02:01, Fredrik Olofsson wrote:
>
>> some minor suggestions...
>> i would avoid Server.default inside a method.  specially here  
>> since the Buffer object already knows which server it belongs to.
>> and this.numChannels and this.bufnum could be just numChannels and  
>> bufnum.
>> your argument numFrames you might want to rename just to make it  
>> different from Buffer.numFrames.
>> the first .sync should not be needed.
>> and perhaps add an action function?
>> _f
>>
>>
>> /*
>> b = Buffer.read(s, "sounds/a11wlk01.wav");
>> b.play
>> b.crop(100000, 66666, false)
>> b.play
>> b
>> */
>>
>> + Buffer {
>> crop {arg startFrame, frames, overwrite=false, action;
>> var cond, tempbuf, newbuf;
>> cond = Condition.new;
>>
>> Routine.run {
>> tempbuf = Buffer.alloc(server, frames, numChannels);
>> server.sync(cond);
>> this.copyData(tempbuf, 0, startFrame, frames);
>> server.sync(cond);
>> this.free;
>> server.sync(cond);
>>
>> newbuf = Buffer.alloc(server, tempbuf.numFrames,  
>> tempbuf.numChannels, bufnum:bufnum);
>> server.sync(cond);
>> tempbuf.copyData(newbuf);
>> server.sync(cond);
>> tempbuf.free;
>> tempbuf = nil;
>>
>> if(overwrite, { newbuf.write(this.path, "aiff", "int16") });
>>
>> server.sync(cond);
>> this.updateInfo(action); // update so the buffer object has the  
>> new buffer info
>> };
>> }
>> }
>>
>>
>>
>> 21 jul 2008 kl. 01.04 skrev thor:
>>
>>>
>>> Thanks for all the replies. Right now I don't have time to  
>>> investigate the bind issue
>>> but I've implemented the crop method like this:
>>>
>>> (still interested in improvements : )
>>>
>>>
>>> b = Buffer.read(s, "sounds/a11wlk01.wav");
>>> b.play
>>> b.crop(100000, 66666, false)
>>> b.play
>>> b
>>>
>>>
>>> + Buffer {
>>> crop {arg startFrame, numFrames, overwrite=false;
>>> var s, cond, tempbuf, tempbufnum, newbuf, chNum;
>>> cond = Condition.new;
>>> s = Server.default;
>>>
>>> Routine.run {
>>> chNum = this.numChannels;
>>> s.sync(cond);
>>> tempbuf = Buffer.alloc(s, numFrames, chNum);
>>> s.sync(cond);
>>> this.copyData(tempbuf, 0, startFrame, numFrames);
>>> tempbufnum = this.bufnum;
>>> s.sync(cond);
>>> this.free;
>>> s.sync(cond);
>>>
>>> newbuf = Buffer.alloc(s, tempbuf.numFrames,  
>>> tempbuf.numChannels, bufnum:tempbufnum);
>>> s.sync(cond);
>>> tempbuf.copyData(newbuf);
>>> s.sync(cond);
>>> tempbuf.free;
>>> tempbuf = nil;
>>>
>>> if(overwrite, { newbuf.write(this.path, "aiff", "int16") });
>>>
>>> s.sync(cond);
>>> this.updateInfo; // update so the buffer object has the new  
>>> buffer info
>>> };
>>> }
>>> }


   #|
      fredrikolofsson.com     klippav.org     musicalfieldsforever.com
   |#


_______________________________________________
sc-users mailing list

info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

 « Return to Thread: Cropping buffer - Server.sync + best way