Buffers inside routine

Hello everybody,
I am kind of new to supercollider and in this forum so maybe my question is a bit basic.

Ι am trying to use a routine which every one second change the content of a buffer used for wavetable synthesis and this routine runs in system clock.The problem is it sound to me kind of not coherent in terms of tempo.

Could someone point what am I doing wrong or propose to me a better practice to achieve the same result?

This is the chunk of code.


(
r=Routine{
loop{
		s.bind{
~env=Env([0]++ Array.rand(10,-1.0,1.0).normalize(-1,1)++[0],Array.rand(11,0.01,1),Array.rand(11,-4.0,4.0));


	
(
s.waitForBoot{
Buffer.freeAll;
s.sync;
~buf=Buffer.alloc(s,2.pow(13));
s.sync;
~wt=~env.discretize(2.pow(12)).asWavetable;

~buf.sendCollection(~wt);
s.sync;
});
		};
			1.wait;}
}
)
r.play(SystemClock);
r.stop;




(
{Osc.ar(~buf,freq:[200,201] ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*0.5 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*0.25 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*0.125 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*2 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*3/2 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*4/3 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*5/4 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*6/5 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*7/6 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*8/7 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*9/8 ,mul: 0.1)}.play;
{Osc.ar(~buf,freq:[200,201]*11/10 ,mul: 0.1)}.play;
)

Hi foudou7,

Because you have to create the buffer, load it, then sync the server in every loop, there is going to be jitter.

My solution here makes 100 buffers ahead of time, then just assigns a new buffer to the synth on every loop. I think this sounds really dope!

//this will take a second
//make 100 different buffers
(
    {
        ~bufs = List.newClear;
        100.do{|i|
            var env=Env([0]++ Array.rand(10,-1.0,1.0).normalize(-1,1)++[0],Array.rand(11,0.01,1),Array.rand(11,-4.0,4.0));
            var wt=env.discretize(2.pow(12)).asWavetable;
            i.postln;
            ~bufs.add(Buffer.loadCollection(s, wt));
            s.sync;
        };
        "I'm done".postln;
    }.fork
)

//make the synth
(
a = {|buf|
    var left = Mix(Osc.ar(buf, 200*[0.125, 0.25, 0.5, 2, 3/2, 4/3, 5/4, 6/5, 7/6, 8/7, 9/8, 11/10], 0, 0.04));
    var right = Mix(Osc.ar(buf, 201*[0.125, 0.25, 0.5, 2, 3/2, 4/3, 5/4, 6/5, 7/6, 8/7, 9/8, 11/10], 0, 0.04));
    Out.ar(0, [left, right])
}.play(s, args:[\buf, ~bufs[0]]);
)

//change the buffer every second
(
Routine({
	inf.do{
		a.set(\buf, ~bufs.choose);
		1.wait;
	}
}).play

)

Sam

Alright, this makes sense.

Thank you Sam for the response!