(
SynthDef(\wtable_vosc_dual, {
	var env, sig, freq, buf, wtmod, wtpos;

	var wtbufnumRange = [ ~wtbufnums.minItem, ~wtbufnums.maxItem ];

	// Main controls
	var gate    = \gate.kr(1);
	freq        = \freq.kr(120) / 2;
	buf         = \bufn.kr(wtbufnumRange[2]);
	wtmod       = \wtmodbufn.kr(wtbufnumRange[2]);

	// Envelope
	env = EnvGen.kr(
		Env.adsr(
			\attack.kr(0.1),
			\decay.kr(0.2),
			\sustain.kr(3.0),
			\release.kr(1.0)
		),
		gate,
		doneAction: 2
	).lag(\envLag.kr(0.1));


	wtpos = (
		VOsc.ar(wtmod, \wtmodfreq.kr(0), \phase.kr(0), \wtmodamp.kr(1)
		)
		+ env.pow(1.4)
		+ buf
	).lag(1.8)
	.mod(wtbufnumRange[1] - wtbufnumRange[0])
	+ wtbufnumRange[0];

	sig = VOsc.ar(wtpos, freq, \phase.kr(0), 1) * env * \amp.kr(0.2);

	// sig = BPF.ar(sig, \lpf.ar(800), \q.ar(1.0));
	sig = BLowShelf.ar(sig, \lpf.ar(800), \q.ar(1.0));

	sig = AllpassN.ar(sig, 1, 1, 1);

	Out.ar(\out.kr(0), sig ! 2);
}).add;
)

x = Synth(\wtable_vosc_dual, [\freq, 70.midicps]);


(
Pbind(
    \instrument, \wtable_vosc_dual,      // Choose SynthDef
    \amp, Pwhite(0.2, 0.8, inf),     // Random amplitude (0.2 to 0.8)
 /*   \mod1, Pwhite(5, 40, inf),       // Random mod1 value (5 to 40)
    \mod2, Pwhite(0.01, 0.3, inf),   // Random mod2 value (0.01 to 0.3)
    \mod3, Pwhite(1, 10, inf),       // Random mod3 value (1 to 10)
    \mod4, Pwhite(0.1, 0.8, inf),    // Random mod4 value (0.1 to 0.8)
    \mod5, Pwhite(0.01, 0.4, inf),   // Random mod5 value (0.01 to 0.4)*/
	\freq, Pwhite(40, 70, inf),
    \dur, Pseq([1], inf)    // Sequence of durations
).play;
)


(
~chords = (
    "Csus4": [0, 5, 7],        // Root, fourth, fifth
    "Cmaj7": [0, 4, 7, 11],
    "Cmin7": [0, 3, 7, 10],
    "C5": [0, 7],              // Power chord
    "C": [0, 4, 7]
);
)

(
~chords = (
    \Csus4: [0, 5, 7],
    \Cmaj7: [0, 4, 7, 11],
    \Cmin7: [0, 3, 7, 10],
    \C5: [0, 7],
    \C: [0, 4, 7]
);
)



(
Pbind(
    \instrument, \wtable_vosc_dual,
    \amp, 0.5,
    \dur, 1,
    \midinote, Pseq([
        ~chords["Csus4"] + 60,   // Transpose to MIDI note 60 (C4)
    ].flat, inf)
).play;
)


x = Synth(\wtable_vosc_dual [\freq, Pseq((~chords["Csus4"] + 60).midicps, inf)]);


(
~chords = (
	\Csus4: [[2, 4]],
	\Cmaj7: [[0, 4, 7, 11]],
	\Cmin7: [[0, 3, 7, 10]],
	\C5: [[0, 7]],
	\C: [[0, 4, 7]]
);

Pbind(
    \instrument, \wtable_vosc_dual,
    \amp, 0.4,
	\dur, Pwhite(0.4, 0.8),
	// add freq ratio
	\midinote, Pwhite(40, 80),
	// \midinote, Pseq((~chords[\Cmin7, ~chords[\Cmaj7]] + 60), inf)
).play;
)

(
~chords = (
	/*\Csus4: [[ 3, 5]],
	// \Cmaj7: [0, 1, 3, 7],
	\Cmin7: [[3, 7]],
	\whatever: [3, 5],
	\whateer: [12, 15],*/

	\Fm: [[0, 3, 7]],
	\Bbm: [[-1, 2, 6]],   // Relative to C
	\F: [[0, 4, 7]],
	\Eb: [[-4, 0, 3]],
	\Ebm: [[-4, -1, 3]]


/*	\C5: [0, 7],
	\C: [0, 4, 7],*/
);

~chordKeys = ~chords.keys;

Pbind(
    \instrument, \wtable_vosc_dual,
    \amp, 0.4,
    \dur, Pwhite(0.1, 1),
    \midinote, Pfunc({
        var chordName = ~chordKeys.choose; // Randomly pick a chord
        var chord = ~chords[chordName];    // Get its intervals
        var note = chord.choose + 60;      // Pick one and transpose
        note;
    }),
	\attack, Pwhite(0.01, 0.1),
	\decay, Pwhite(0.1, 0.3),
	\sustain, Pwhite(0.2, 0.8),
	\release, Pwhite(0.1, 2.0),
	\lpf, Pwhite(800, 2000),
	\q, Pwhite(0.3, 1.0),
).play;
)


(
~freygish = [0, 1, 4, 5, 7, 8, 10]; // Freygish scale in C
~root = 60; // Middle C

Pbind(
    \instrument, \wtable_vosc_dual,
    \amp, 0.4,
    \dur, Pwhite(1, 2),
    \midinote, Pfunc({
        ~root + ~freygish.choose;
    })
).play;
)



(
~chords = (
	\Csus4: [0, 5, 7],
	\Cmaj7: [0, 4, 7, 11],
	\Cmin7: [0, 3, 7, 10],
	\C5: [0, 7],
	\C: [0, 4, 7]
);

Pbind(
    \instrument, \wtable_vosc_dual,
    \amp, 0.4,
    \dur, 1,
    \midinote, Pcollect(
        Pseq([\Cmin7, \Cmaj7, \C5], inf),   // sequence of chord names
		{ |chordName| ~chords[chordName] + 60 }  // map to MIDI notes
    )
).play;
)
