(
SynthDef(\mellotron, {
    arg freq = 440, amp =0.3, gate = 1, pan = 0, vibratoRate = 1, vibratoDepth = 0.15,
        noiseLevel = 0.2, ringModFreq = 3, lpfFreq = 1e3, res = 1, lpfLfoRate = 1, lpfLfoDepth = 100,
        detune = 0.005, fineDetune1, fineDetune2;

    var sig, osc1, osc2, vibrato, noise, env, lpfMod, freq1, freq2;

		var tuning = [
 38.70968,
 116.12903,
 154.83871,
 232.25806,
 309.67742,
 348.38710,
 425.80645,
 503.22581,
 541.93548,
 619.35484,
 658.06452,
 735.48387,
 812.90323,
 851.61290,
 929.03226,
 1006.45161,
 1045.16129,
 1122.58065,
 2/1
];

	var tuningRatios = tuning * freq / 8;  // Scale the whole array once
freq1 = tuningRatios[0];
freq2 = tuningRatios[1];

    vibrato = SinOsc.kr(vibratoRate, 0, vibratoDepth).range(0.98, 1.02);

	freq1 = (freq * (0.99))/8 * tuning ;
	freq2 = (freq * (1.0))/3 * tuning ;

	// Apply the tuning ratios directly to each frequency



/*    osc1 = Mix([
        Saw.ar(freq1 * vibrato),
        Saw.ar(freq2 * vibrato)
    ]) * 0.5;

	// noise = WhiteNoise.ar(noiseLevel);
    osc1 = Mix([osc1 * 0.5]);

osc2 = Mix([
        Saw.ar(freq1 * ringModFreq),
        Saw.ar(freq2 * ringModFreq)
    ]) * 0.5;*/

	// // Directly combine the oscillators instead of using Mix
	osc1 = LFTri.ar(freq1 * vibrato);
	osc2 = Saw.ar(freq1 * ringModFreq) + Saw.ar(freq2 * ringModFreq);



// Apply the final mix
sig = osc1 + osc2 * 0.5;

	// sig = Fold.ar(sig, -20, 20); // EMF: wavefolding only happens if the input signal amplitude exceeds the bounds provided. osc has a pretty normal range and won't ever exceed ±20. You can see in the previous line (LFDNoise3) I'm only amplifying it by a maximum factor of 1.5. It's actually small boundary values with Fold that cause audible results (change to the folding min/max to -0.1, +0.1 and you'll notice the sound changes significantly)



    sig = Mix([osc1 + osc2]);

	lpfMod = SinOsc.kr(lpfLfoRate).range(lpfFreq - lpfLfoDepth, lpfFreq + lpfLfoDepth);
    sig = RLPF.ar(sig, lpfMod, res);

	sig = BPF.ar(sig, 200);

    env = EnvGen.kr(Env.adsr(0.2, 3, 1, 2), gate, doneAction: 2);

    sig = sig * env * amp;


	// sig = sig.tanh(8);
	sig = BLowShelf.ar(sig, 300, 1);

	// sig = Splay.ar(sig, 0.1, );




	sig = LeakDC.ar(sig);

	// sig = FreeVerb.ar(sig, 0.33, 2);
	// sig = GVerb.ar(sig, 40, 3, 0.5, 0.5, 20, 0, 1, 1);
	// sig = CombL.ar(sig, 0.01, 0.2, 1.0);

    Out.ar(0, Pan2.ar(sig, pan));
}).add;
)

(
Pbind(
    \instrument, \mellotron,
    \freq, Pseq([


		[65, 69, 71],   // F Major (F, A, C)
		[80, 75,],
		[90, 70],
		[90, 70],
		[80, 65]

    ].midicps, inf),  // Use MIDI note numbers for frequencies
	\dur, Pseq([2, 0.5, 0.5, 1],inf),
    \vibratoRate, 5,  // Adding more vibrato (changeable)
    \vibratoDepth, 0.15,  // Keep vibrato depth as is
    \lpfFreq, 1200,  // LPF for smoothing
    \lpfLfoRate, 0.1,  // Slow LFO modulation for LPF
    \lpfLfoDepth, 100,  // Depth of LPF modulation
	// \detune, -12,  // Slight detuning for realism
    \pan, Pseq([0.5, -0.5], inf),  // Panning left and right for stereo spread
).play;
)
