How to sample a control bus on trigger?

I have a form of global frequency bus to control root frequency for many synths routed through a bus in a group. This frequency gradually shifts and changes through the piece, but I want some of the SynthDef-s that pattern these synths to just read the frequency once at the trigger time. So that the percussive notes, do not form weird slurs.

A short example is somewhat like this:

(
(
SynthDef(\percSynth, {|out, freq=440|
	var sig = SinOsc.ar(freq)*EnvGen.ar(Env.perc(),1,doneAction:Done.freeSelf);
	Out.ar(out,sig ! 2)}).add;
);
(
SynthDef(\otherSynth, {|out, freq=440|
	var sig = SinOsc.ar(2*freq)*EnvGen.ar(Env.new([0,1, 1, 0],[0.1,0.6, 0.2],[5,-5, -8]);,1,doneAction:Done.freeSelf);
	Out.ar(out,sig ! 2)}).add
);
)
(
// group these 2 synths and plug global frequency bus into them
(
g = Group.new;
~freqBus = Bus.new('control',numChannels: 1);
g.run(false);
Synth.new(\percSynth,[],g,\addToHead);
Synth.new(\otherSynth,[],g,\addToHead);
g.map(\freq, ~freqBus);
~freqBus.set(440);
);
g.run(true);
)

I would like the \percSynth to just read the frequency once instead of being subject to potential changes on ~freqBus, while the \otherSynth synth should stay the way it is, preferably still allowing me to group them in a similar way and set frequency in one go. For some more details, the plan is to add trigger argument t_gate to \percSynth so that it can be potentially re-triggerable and should capture the freq on every trigger.
Is there a way to do this?

Latch.kr(freq, Impulse.kr(0) + \gate.tr) in the SynthDef?

What is the reason behind the Impulse.kr(0) here? Doing something along the lines of Latch.kr(freq,t_trig) seems to work on a small scale. Is this some difference between trigger args and non trigger args?

Latch outputs 0 until it receives its first trigger, so it needs a jump start

1 Like