Use a buffer's amplitude as a threshold to trigger synths

Hi everyone!

I wrote a very simple code -based on a topic from this forum- to use a buffer’s amplitude as a reference, and whenever the amplitude exceeds a threshold to trigger a percusive synth. Right now it is working when written in a {function}.play format:

b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");


(
{ var source, trig,
	pan, amp, env, freqs, amps, out;

	source = PlayBuf.ar(1, b, loop:1);
	trig = Amplitude.ar(source) > 0.2;

	pan = Rand(-1.0, 1.0);
	amp = Rand(0.002, 0.01);
	freqs = Array.fill(40, {ExpRand(4500, 18000)});
	amps = Array.fill(40, {LFDNoise1.ar(0.5) * amp});
	env = EnvGen.ar(Env.perc(0.001, Rand(0.01, 0.11)), trig);

	out = Mix(
		Pan2.ar(
			Ringz.ar(WhiteNoise.ar, freqs, Rand(0.02, 0.4), amps),
			pan));

	out = out*env;


	out

}.play
)

The problem is, as random frequencies and other arguments are important in the code, when the synth is triggered in the {function}.play format, random values are picked when evaluated and then they are always the same. What I’m trying to is to reformulate the code in a SynthDef format, so that every sound is triggered has its own new random values. I suppose the way to make it work would be using a routine, but maybe I’m wrong. I can’t seem to find what’s wrong with it, although it is probably something really dumb.
Here’s the code for my SynthDef + Routine approach:


(
SynthDef(\sourcesend, {arg out=0;
	var source;

	source = PlayBuf.ar(1, b);

	Out.ar(out, source)
}).add
)

//Synth(\sourcesend, [\out, 15], s, \addToTail);





(SynthDef(\cristal, {
	arg gateok=1;
	var //input, trig,
	pan, amp, env, freqs, amps, out;

	//input = In.ar(15);
	//trig = Amplitude.ar(input) > 0.0002;
	pan = Rand(-1.0, 1.0);
	amp = Rand(0.01, 0.15);
	freqs = Array.fill(40, {ExpRand(4500, 18000)});
	amps = Array.fill(40, {LFDNoise1.ar(0.5) * amp});
	env = EnvGen.ar(Env.perc(0.001, Rand(0.05, 0.11)), gate: gateok, doneAction:2);

	out = Mix(
		Pan2.ar(
			Ringz.ar(PinkNoise.ar, freqs, 0.09, amps),
			pan));

	out = out*env;

	Out.ar(0, out);

}).add
)

// Synth(\cristal);




(
Routine({


	var input, trig;
	input = In.ar(15);
	trig = Amplitude.ar(input);

	Synth(\sourcesend, [\out, 15], s, \addToTail);


	200.do({
		if( trig > 0.0002, { Synth(\cristal, [\gateok, 1], s) },
			{ Synth(\cristal, [\gateok, 0], s) }) ;
	});

	0.1.wait;

}).play
)



Thanks!

https://doc.sccode.org/Classes/TRand.html

1 Like

That makes everything a lot simpler, thanks a lot!

1 Like