Start / Stop Ndef and routing

Hi,

I have an Ndef(\pulsingSound) playing through Ndef(\effect), the first is then routed to the second one.

When I stop Ndef(\pulsingSound) and then play it again, the sound is not routed through Ndef(\effect)
The only way I found to stop and play it, while keeping the previous routing, is to use the .pause and .resume methods.
is this the fitting solution for such a problem or there other better soultions?

(
// Define the effect chain Ndef with some reverb
Ndef(\effect, {
    |in|
    var input = In.ar(in, 2);  // Accept stereo input

    // Simple reverb effect for demonstration
    var output = FreeVerb.ar(input, mix: 1.0, room: 0.8, damp: 0.2) * 0.5;

    LeakDC.ar(output);  // Remove any DC offset
}).play;
)
(
// Define a pulsating sound source Ndef
Ndef(\pulsingSound, {
    |trig = 0.5|  // Trigger control for the pulse rate
    var pulseEnv, signal;

    pulseEnv = EnvGen.kr(Env.perc(0.01, 0.2, 1, -4), Impulse.kr(trig));
    signal = SinOsc.ar(440, 0, pulseEnv * 0.3); // Pulsating sine wave at 440Hz
    signal;
}).play;
)
// Routing the sound source through the effect chain
Ndef(\effect) << Ndef(\pulsingSound);

Ndef(\pulsingSound).pause
Ndef(\pulsingSound).resume

Instead of using the B << A routing technique, you could consider embedding the source Ndef directly into the effect Ndef. I’m not saying this is necessarily the better option, but it does preserve the routing after toggling the source off/on. I like being able to explicitly see what’s being passed into an Ndef effect by simply looking at its source code.

Eli

s.boot;

(
Ndef(\effect, {
	var input = Ndef.ar(\pulsingSound, 1); // embed source Ndef directly
	var output = FreeVerb.ar(input, mix: 1.0, room: 0.8, damp: 0.2) * 0.5;
	LeakDC.ar(output);
}).play;
)

(
Ndef(\pulsingSound, { |trig = 0.5|
	var pulseEnv, signal;
	pulseEnv = EnvGen.kr(Env.perc(0.01, 0.2, 1, -4), Impulse.kr(trig));
	signal = SinOsc.ar(440, 0, pulseEnv * 0.3);
}).play;
)

Ndef(\pulsingSound).end;

Ndef(\pulsingSound).play;

thanks for your answers and suggestions!

that makes a lot of sense, but what if I have multiple Ndefs, which I would like to route inside the effect?

Should it be something like this or are there better solutions?

(
Ndef(\effect, {
	var input = Mix.new([Ndef.ar(\pulsingSound, 1), Ndef.ar(\pulsingSound1, 1), Ndef.ar(\pulsingSound2, 1)]; // embed source Ndef directly
	var output = FreeVerb.ar(input, mix: 1.0, room: 0.8, damp: 0.2) * 0.5;
	LeakDC.ar(output);
}).play;
)

Yes, I think that’s pretty much what I’d do. I would also consider spacing the Ndefs onto separate lines to make them easier to comment out. You can also use the plus operator to simply add the Ndefs together.

1 Like