Crossfade between Buffers with BufRd Vosc style

hey, I would like to crossfade between several buffers filled with One Cycle Waveforms within a BufRd. The reason for not using Vosc is 1.) wavetable format and 2.) to have the abilitly to maybe combine the setup with BufWr later on. I managed to crossfade between two BufRds with two Phasors using SelectX and was trying out some of the DX Ugens as well. In the end i would like to use the Signals in the Buffers also as LFO Sources to drive the Crossfade between them. Im not sure which DX Ugen is right for this specific task and how to implement the Signals in the Buffers as curve arguments. thanks a lot :slight_smile:

(
b = Buffer.loadCollection(s, Signal.sineFill(512, [1]), 1);
c = Buffer.alloc(s, 512, 1, { |buf| buf.chebyMsg([1,0,1,1,0,1])});
)

(
SynthDef(\crossFade, {
	arg out=0, pan=0, amp=0.25, sndBuf1=0, sndBuf2=1, freq=150;

	var bufFrames1 = BufFrames.ir(sndBuf1);
	var bufFrames2 = BufFrames.ir(sndBuf2);
	var sampleDur = SampleDur.ir;
	var sig, playbuf1, playbuf2, pos1, pos2;

	var crossFade = MouseX.kr(0, 1);

	pos1 = Phasor.ar(0, freq * bufFrames1 * sampleDur, 0, bufFrames1);
	pos2 = Phasor.ar(0, freq * bufFrames2 * sampleDur, 0, bufFrames2);

	playbuf1 = BufRd.ar(1, sndBuf1, pos1, interpolation:4);
	playbuf2 = BufRd.ar(1, sndBuf2, pos2, interpolation:4);

	sig = SelectX.ar(crossFade, [playbuf1, playbuf2]);
/*
	sig = DXMix.ar(
        Dseq([0, 1], inf),
        `[
			BufRd.ar(1, sndBuf1, pos1, interpolation:4),
			BufRd.ar(1, sndBuf2, pos2, interpolation:4)
        ],
		fadeTime: 0.5,
		//sine: 0,
		equalPower: 0, //set to zero to use curve argument
		//power: 0,
		curve: curve,
		allowTypeSeq: 1, //set to 1 to enable sequencing of sine, equalPower, power and curve
    );
*/
/*
	sig = DXFan.ar(
        Dseq([0, 1], inf),
        `[
			BufRd.ar(1, sndBuf1, pos1, interpolation:4),
			BufRd.ar(1, sndBuf2, pos2, interpolation:4)
        ],
        //size: 2,
        fadeTime: 3,
       //maxWidth: 4,
        //width: 3
    );

	sig = Mix(sig);
*/

	sig = Pan2.ar(sig, pan, amp);
	Out.ar(out, sig);
}).add;
)

Synth(\crossFade, [\sndBuf1, b, \sndBuf2, c, \amp, 0.25]);

I was also trying to just use one Phasor and one BufRd with an Array of two Buffers bufArray which crashes the server (i think probably because of BufFrames.ir(bufArray), i dont know):

(
b = Buffer.loadCollection(s, Signal.sineFill(512, [1]), 1);
c = Buffer.alloc(s, 512, 1, { |buf| buf.chebyMsg([1,0,1,1,0,1])});
)

(
SynthDef(\crossFade, {
	arg out=0, pan=0, amp=0.25, sndBuf1=0, sndBuf2=1, freq=150;

	/*
	var bufArray = Mix(DXFan.ar(
        Dseq([0, 1], inf),
        `[
			sndBuf1,
			sndBuf2
        ],
        fadeTime: 3,
	));
*/	
	var crossFade = MouseX.kr(0, 1);
	var bufArray = SelectX.kr(crossFade, [sndBuf1, sndBuf2]);
	
	var bufFrames = BufFrames.ir(bufArray);
	var sampleDur = SampleDur.ir;
	var sig, pos;

	pos = Phasor.ar(0, freq * bufFrames * sampleDur, 0, bufFrames);
	sig = BufRd.ar(1, bufArray, pos, interpolation:4);

	sig = Pan2.ar(sig, pan, amp);
	Out.ar(out, sig);
}).add;
)

Synth(\crossFade, [\sndBuf1, b, \sndBuf2, c, \amp, 0.25]);

The math here is not that complicated – but every time I have to reproduce it, I make every possible mistake, and the next time I have to do it, I make every possible mistake again… anyway this works.

s.boot;

b = Buffer.allocConsecutive(8, s, 2048, 1, { |buf, i|
	buf.sine1Msg((1 .. ((i+1) * 10)).reciprocal, asWavetable: false)
});

(
a = { |freq = 440, firstbuf|
	var mod = SinOsc.kr(0.1).range(0, 7);
	var evenIndex = min(7.999, mod.round(2));
	var oddIndex = mod.trunc(2) + 1;
	var phase = Phasor.ar(0, freq * SampleDur.ir, 0, 1) * BufFrames.ir(firstbuf);
	var sig = BufRd.ar(1, firstbuf + [evenIndex, oddIndex], phase);
	// LinXFade because I made the buffers to be phase correlated
	// if they are not correlated, use XFade2
	sig = LinXFade2.ar(sig[0], sig[1], mod.fold(0, 1) * 2 - 1);
	(sig * 0.1).dup
}.play(args: [freq: 100, firstbuf: b.first]);
)

(fold(0, 1) always confuses me – at first I wrote fold(0, 2), but this has a cycle of 4. fold(0, 1) has the desired cycle of 2.)

hjh

2 Likes

but every time I have to reproduce it, I make every possible mistake, and the next time I have to do it, I make every possible mistake again

thanks so much for doing it again nevertheless. its working perfectly :slight_smile:
everything im dealing with right now seems to be about odd and even haha.

(fold(0, 1) always confuses me

i made so many plots of .fold and .wrap the last days. my relationship with them is still far from friendship i guess haha