~ir = Buffer.read(s, "/home/lcoogan/snd/ir/ForestScaleModel/ForestScaleModel/IR_ScaleModel/S1R1_ScaleModel.wav");
~ir = Buffer.read(s, "/home/lcoogan/snd/ir/r1-nuclear-reactor-hall/stereo/r1_ortf-48k.wav");


b = Buffer.read(s, "/home/lcoogan/snd/samples/scarlett/scarlett singing.wav");
b.play;

{Convolution.ar(b, ~ir)}.play;


s.options.memSize = 8192;


(
SynthDef(\bellFM, {
    |freq = 440, amp = 0.1, gate = 1, ratioIndex = 0, out = 0, irBuf = 0, wet = 0.5, lfoRate = 20, lfoDepth = 1|
    var car, mod, env, mFreq, mIndex, tunedFreq, tuning, dry, reverb, sig, lfo;

    tuning = [13/12, 55/48, 5/4, 65/48, 11/8, 143/96, 13/8, 5/3, 11/6, 2/1];
    tunedFreq = freq * Select.kr(ratioIndex.clip(0, tuning.size - 1), tuning);

    env = EnvGen.kr(Env.perc(0.01, 1.0, 1, 4), gate, doneAction: 2);

    mFreq = tunedFreq * 6.7;
    mIndex = env * 5;
    mod = Pulse.ar(mFreq) * (tunedFreq * mIndex);
    dry = SinOsc.ar(tunedFreq + mod) * env * amp;

    // Initialize sig with the dry signal
	// sig = dry;

	sig =

    // Create LFO and modulate
    lfo = SinOsc.kr(lfoRate, 0, lfoDepth); // Low-frequency oscillator

	lfo = LFNoise0.kr(lfoRate * lfoDepth);
    sig = sig + lfo;  // Add LFO to the signal

    // Convolve and scale
    reverb = Convolution2.ar(dry, ~ir, 512);
    // Mix dry and wet signals
    sig = XFade2.ar(dry, reverb, (wet * 2) + lfo); // -1 = all dry, +1 = all wet

    Out.ar(out, sig.dup);
}).add;
)


(
SynthDef(\pluckedSine, {
    |out=0, freq=220, amp=0.4, decay=3, coef=0.5|
    var trig, burst, pluck;

    trig = Impulse.kr(0); // trigger manually

    burst = SinOsc.ar(freq) * EnvGen.kr(Env.perc(0.001, 0.05), trig);

    pluck = Pluck.ar(
        burst,
        trig,
        0.2,        // max delay time
        1 / freq,   // delay time = 1 / freq
        decay,
        coef
    );

    Out.ar(out, pluck.dup * amp);
}).add;
)



Synth(\bellFM, [\irBuf, ~ir.bufnum]);




Synth(\bellFM, [\freq, 440]);
Synth(\bellFM, [\freq, 660]);
Synth(\bellFM, [\freq, 550]);


(
SynthDef(\pluckedSine, {
    |out = 0, freq = 220, amp = 0.4, decay = 3, coef = 0.5, wet = 0.5, lfoRate = 3, lfoDepth = 1, irBuf = 0|
    var trig, burst, pluck, reverb, lfo, sig;

    trig = Impulse.kr(0); // trigger manually

    burst = LFSaw.ar(freq) * EnvGen.kr(Env.perc(0.001, 0.05), trig);

    pluck = Pluck.ar(
        burst,
        trig,
        0.2,        // max delay time
        1 / freq,   // delay time = 1 / freq
        decay,
        coef
    );

    // Initialize sig with the pluck signal
    sig = pluck;

    // LFO modulation (Low-frequency noise oscillator)
    lfo = LFNoise0.kr(lfoRate);  // Low-frequency noise oscillator
    sig = sig + lfo * lfoDepth;  // Apply LFO to the signal

    // Convolve and scale
	// reverb = Convolution2.ar(sig, irBuf, 512);  // Apply convolution reverb
    sig = XFade2.ar(sig, reverb, (wet * 2) + 0.1); // Mix dry and wet signals

	// sig = GVerb.ar(sig);

    // Output the signal
    Out.ar(out, sig.dup);
}).add;
)

