Single code execution | OSC

I am trying to get the follow code to execute in one block:

(

SynthDef(\smplr_m3, {arg startPos = 0, buf, rate = 1, loop = 1,
	atk = 0.005, rel = 0.005, curve = 0, dur = 0.1,
	amp = 0.5, out = 0, pan = 0;

	var env, sig;
	env= Env([0,1,1,0], [atk, dur-atk-rel, rel], curve).ar(doneAction:2);
	sig = PlayBuf.ar(numChannels: 1, bufnum: buf, rate: BufRateScale.kr(buf) * rate, startPos: startPos, loop: loop);
	sig = sig * env * amp;
	OffsetOut.ar(out, Pan2.ar(sig, pan));
}).add;


// Logic for providing slices to the queue
~sliceProducer = {arg count, slices, beats, queue;

	var pos = count % beats;
	var delta = (slices/beats).reciprocal;
	var slice = (count/delta % slices);
	var event = (delta:delta, slice:slice);
	var rem = (beats - pos);

	if (1.2.coin && (rem <= 1) ) {

		var div = delta/2;
		var event = (delta:div, slice:slice);
		2.do({
			queue.add(event);
		});
	} {
		if (pos == 0) {
			event[\slice] = 0;
			queue.add(event);
		} {
			event[\slice] = (0..slices-1).choose;
			queue.add(event);
		}
	};
};


TempoClock.default.tempo_(2);
~buf = Buffer.read(s, "E:/ArtyTorrent Pack 34-Drum n Bass Loops-160-169bpm-WAV samples/Desimal Breaks 160-168/Desimal Breaks 160-168/Desimal dnbsamples_break15 162.wav");


// main interface
~slcr = {arg buf, beats = 8, beatDiv = 2, sliceProducer, amp = 0.1, clock = TempoClock.default;

	// buffer info
	var numFrames = buf.numFrames;
	var sampleRate = buf.sampleRate;

	// length in seconds of sample
	var len = numFrames/sampleRate;

	// beats per second
	var bps = beats/len;

	// number of slices
	var slices = beats * beatDiv;

	// frames per slice
	var fps = numFrames/slices;

	// yields slice data to the pattern
	var rtn = Routine({

		var queue = LinkedList.new;
		var count = 0;

		inf.do({arg i;

			var event;

			if (queue.isEmpty) {
				sliceProducer.value(count, slices, beats, queue);
			};

			event = queue.popFirst;
			count = count + event[\delta];
			[event[\delta], event[\slice] * fps].yield;
		});
	});

	// return the pattern
	Pbind(\instrument, \smplr_m3,
		[\delta, \startPos], rtn,
		\buf, buf,
		\rate, Pfunc({ clock.tempo }) / bps,
		\amp, amp,
		\dur, Pfunc({ clock.beatDur }) * Pkey(\delta)
	);
};

)

(
Ndef(\out).clear;
Ndef(\out).play;
Ndef(\out)[0] = ~slcr.(buf: ~buf, beats: 4, beatDiv: 2, sliceProducer: ~sliceProducer, amp: 0.5);
Ndef(\out)[10] = \filter -> {arg in; FreeVerb.ar(in, 0.1, 0.5); };
)

How do I include the last 4 Ndefs within the first set of brackets “()” ?

I also like to add OSC, like:

x.set(\beats,4);
x.set(\beatDiv,8);

So that I can change the beat-slicing on the fly … how do I go about doing so?

I think the answer is s.waitForBoot{…} . The only thing I did here was to place all your code in a s.waitForBoot function and add a s.sync after the SynthDef and a couple between the Ndefs (this is provided your default server is ‘s’). There were a couple of times where I had to shut down SC altogether when testing this: before adding s.sync in the right places, SC kept looking for a synth it could not find. Basically s.sync is a message sent from the server to the language saying ‘I am done with the task you asked me to do, you can proceed’. Even after quitting or killing the server and rebooting, sometimes this does not stop the endless printing of error messages. There might be an alternative to shutting down SC, I just have not been able to find it. Best way to test the code below (and really any code that needs to be initiated in one go) is to

  • quit SC
  • open SC,
  • run the code in one go.

Don’t manually boot the server first, let waitForBoot do it.

Let me know if this works:

(
s.waitForBoot{

	SynthDef(\smplr_m3, {arg startPos = 0, buf, rate = 1, loop = 1,
		atk = 0.005, rel = 0.005, curve = 0, dur = 0.1,
		amp = 0.5, out = 0, pan = 0;

		var env, sig;
		env= Env([0,1,1,0], [atk, dur-atk-rel, rel], curve).ar(doneAction:2);
		sig = PlayBuf.ar(numChannels: 1, bufnum: buf, rate: BufRateScale.kr(buf) * rate, startPos: startPos, loop: loop);
		sig = sig * env * amp;
		OffsetOut.ar(out, Pan2.ar(sig, pan));
	}).add;

	s.sync;


	// Logic for providing slices to the queue
	~sliceProducer = {arg count, slices, beats, queue;

		var pos = count % beats;
		var delta = (slices/beats).reciprocal;
		var slice = (count/delta % slices);
		var event = (delta:delta, slice:slice);
		var rem = (beats - pos);

		if (1.2.coin && (rem <= 1) ) {

			var div = delta/2;
			var event = (delta:div, slice:slice);
			2.do({
				queue.add(event);
			});
		} {
			if (pos == 0) {
				event[\slice] = 0;
				queue.add(event);
			} {
				event[\slice] = (0..slices-1).choose;
				queue.add(event);
			}
		};
	};


	TempoClock.default.tempo_(2);
	~buf = Buffer.read(s, "E:/ArtyTorrent Pack 34-Drum n Bass Loops-160-169bpm-WAV samples/Desimal Breaks 160-168/Desimal Breaks 160-168/Desimal dnbsamples_break15 162.wav");

	// main interface
	~slcr = {arg buf, beats = 8, beatDiv = 2, sliceProducer, amp = 0.1, clock = TempoClock.default;

		// buffer info
		var numFrames = buf.numFrames;
		var sampleRate = buf.sampleRate;

		// length in seconds of sample
		var len = numFrames/sampleRate;

		// beats per second
		var bps = beats/len;

		// number of slices
		var slices = beats * beatDiv;

		// frames per slice
		var fps = numFrames/slices;

		// yields slice data to the pattern
		var rtn = Routine({

			var queue = LinkedList.new;
			var count = 0;

			inf.do({arg i;

				var event;

				if (queue.isEmpty) {
					sliceProducer.value(count, slices, beats, queue);
				};

				event = queue.popFirst;
				count = count + event[\delta];
				[event[\delta], event[\slice] * fps].yield;
			});
		});

		// return the pattern
		Pbind(\instrument, \smplr_m3,
			[\delta, \startPos], rtn,
			\buf, buf,
			\rate, Pfunc({ clock.tempo }) / bps,
			\amp, amp,
			\dur, Pfunc({ clock.beatDur }) * Pkey(\delta)
		);
	};

	Ndef(\out).clear;
	Ndef(\out).play;
	s.sync;
	Ndef(\out)[0] = ~slcr.(buf: ~buf, beats: 4, beatDiv: 2, sliceProducer: ~sliceProducer, amp: 0.5);
	s.sync;
	Ndef(\out)[10] = \filter -> {arg in; FreeVerb.ar(in, 0.1, 0.5); };
};
)

I should mention that I had to restart my computer after messing with your code (which is no problem btw.), so just be cautious when testing.

1 Like

Thanks Thor! — that’s a massive step forward! - it worked in one go.
I can now focus on finetuning the code.
Excellent work! :slight_smile:

1 Like