Crossfade between two pbinds

Hi

I would like to be able to crossfade back and forth between two pbinds like one would crossfade between two signals with the XFade2 UGen.

As a starting point…

(
SynthDef(\sine, {

	arg freq = 300, out = 0;
	var snd, env;

	env = EnvGen.ar(Env([0, 1, 0], [0.2, 0.3]), doneAction: 2);

	snd = SinOsc.ar(freq, mul: 0.3) * env;

	Out.ar(out, snd);

}).add
)


(
SynthDef(\saw, {

	arg freq = 200, out = 0;
	var snd, env;

	env = EnvGen.ar(Env([0, 0.5, 0], [0.3, 0.4]), doneAction: 2);

	snd = LFSaw.ar(freq, mul: 0.2) * env;

	Out.ar(out, snd);

}).add
)


(
u = Pbind (
	\instrument, \sine,
	\dur, 0.5,
)
)

(
v = Pbind (
	\instrument, \saw,
	\dur, 0.8,
)
)

u.play
v.play

Thanks!

This is easiest with Pdef, e.g., see here (this thread is almost exactly 10 years old and I thought I posted the day before yesterday :slight_smile::slightly_frowning_face:)

https://www.listarc.bham.ac.uk/lists/sc-users-2011/msg03814.html

Cool!

What I would like to achieve is to crossfade between the two via Open Stage Control which is an application to send osc data.

I could use a button which would execute

Pdef(\x, q);

at state 1 and

Pdef(\x, p);

at state 0.

But I actually I would like to use a fader which might stop at 80% of one Pdef and 20% of the other Pdef if that makes sense to you.

I can’t quite see at the moment how to do this.

Basically you can decide to do the mixing explicitely (via busses) or by using JITLib stuff. You can blend and morph presets. I’m not a JITLib expert, so someone might want to chime in.

Does anyone have an idea?

I’m not sure if I quite understood what you want, but if so, then this works fine for me, although there may be other, better ways to do this:

(
SynthDef(\sine, {

    arg freq = 300, out = 0, pan = 0;
    var snd, env;

    env = EnvGen.ar(Env([0, 1, 0], [0.2, 0.3]), doneAction: 2);

    snd = SinOsc.ar(freq, mul: 0.3) * env;
    snd = Pan2.ar(snd, pan);
    Out.ar(out, snd);

}).add;

SynthDef(\saw, {

    arg freq = 200, out = 0, pan = 0;
    var snd, env;

    env = EnvGen.ar(Env([0, 0.5, 0], [0.3, 0.4]), doneAction: 2);

    snd = LFSaw.ar(freq, mul: 0.2) * env;
    snd = Pan2.ar(snd, pan);
    Out.ar(out, snd);

}).add;

SynthDef(\mixer, {
        arg uIn = 0, vIn = 0, mix = 0, out = 0;
        var uSnd, vSnd, snd;

        uSnd = In.ar(uIn, 2);
        vSnd = In.ar(vIn, 2);
        snd = XFade2.ar(uSnd, vSnd, mix.linlin(0, 1, -1, 1));
        Out.ar(out, snd);

}).add;
)

(
~uOut = Bus.audio(s, 2);
~vOut = Bus.audio(s, 2);
)

(
u = Pbind (
    \instrument, \sine,
    \dur, 0.5,
    \out, ~uOut
);

v = Pbind (
    \instrument, \saw,
    \dur, 0.8,
    \out, ~vOut
)
)

(
x = Synth.tail(nil, \mixer, [uIn: ~uOut, vIn: ~vOut, mix: 0.5]);
u.play;
v.play;
)

x.set(\mix, 0);
x.set(\mix, 1);


Thanks a lot. That’s what I was looking for!