Randomness tied to Impulses in SynthDef

I realise this question is something of a meme, but I have, I think, a specific angle on it.

Here’s my code.

(SynthDef(\LFOexample, {|out|
	var sig = SinOsc.ar(440);
	var lfo = SinOsc.ar(Rand(0.1,10));
	var impulse = Impulse.ar(5);
	var env = EnvGen.kr(Env.perc(0.02, 4), impulse, doneAction:0);
	sig = sig * lfo *env;
	Out.ar(out, sig);
}).add;
)

Synth(\LFOexample);

What I would like to happen is that each time there is a new Impulse the LFO generates a new random value. I realise that will not happen because Rand…

generates this when the SynthDef first starts playing, and remains fixed for the duration of the synth’s existence.

I realise on the language side I could solve this problem easily by sending a new random value, but for long and boring reasons - that include wanting the ‘clock’ to be in the SynthDef - I don’t want to do this.

I want the SynthDef to generate a new random value each time there is a new Impulse. I’ve tried using LFNoise.ar to generate random values, but no luck, as it keeps generating new values out of sync with the Impulse.

Is there a way to do this, or am I barking up the wrong tree?

Thanks, Dom

Have you looked at TRand?

SynthDef(\LFOexample, {|out|
	var sig = SinOsc.ar(440);
	var impulse = Impulse.ar(5);
	var lfo = SinOsc.ar(TRand.ar(0.1, 10, impulse));
	var env = EnvGen.kr(Env.perc(0.02, 4), impulse, doneAction:0);
	sig = sig * lfo *env;
	Out.ar(out, sig);
}).add;

Or TExpRand for frequency values

Or more generally, you could use demand rate ugens, imo, they are highly underrated!

1 Like

Thank you all, that’s exactly what I was looking for. I’m glad it exists!

probably you dont want to change the frequency of the LFO but the amount of modulation per trigger. Currently your LFO does bipolar modulation, meaning its going from - 1 to 1, independent of its frequency.

I did actually want to change the LFO frequency. I’ve found it’s a very nice effect in generative settings to make the sounds less monotonous. Quite right with the bipolar modulation - thanks for picking up on that!