desc:Toy music box in Bb major
//tags: instrument generator synthesis
//author: Tale

// Copyright (C) 2022, 2023 Theo Niessink
// License: LGPL - http://www.gnu.org/licenses/lgpl.html

// Simple synth based on a cheap music box toy, originally in the Bb major
// scale, and with a range of D5..D7 (mapped to MIDI notes 52..76, or 50..74
// in chromatic mode).

slider1:0<0,1,1{C Major,Chromatic}>Input Scale
slider2:0<0,3,1{Slow,Medium Slow,Medium Fast,Fast}}>Pitch Mod Speed
slider3:13.0<0.0,50.0,0.1>Pitch Mod Depth (cent)
slider4:2000<20,5000,1>Decay (ms)
slider5:-6.0<-36.0,24.0,0.1>Volume (dB)
slider6:10.0<0.0,100.0,0.1>Ambience (%)

out_pin:Left
out_pin:Right

import array.jsfx-inc
import adsr.jsfx-inc
import lfo.jsfx-inc
import midi_queue.jsfx-inc
import poly_blep.jsfx-inc
import rc_filter.jsfx-inc
import zdf_filter.jsfx-inc

@init

function note_on(note, velocity)
  // global(note_buf*, osc*, freq_tbl, harmonic_tbl, adsr*)
  local(gain, ptr, harmonic)
(
  gain = (sqr(velocity) + 3) * /* 1/(127^2 + 3) */ 0.000061988594098685842;

  (ptr = note_buf.array_find(note)) < 0 ? (
    ptr = note_buf.array_add();

    ptr[0] = note;

    ptr[3] = /* osc.t = */ 0.0;
    ptr[4] = osc.lfo_setf(freq_tbl[note]) * 0.5;

    ptr[5] = /* gain * adsr.env */ 0.0;
    ptr[6] = gain;

    ptr[8] = (harmonic = harmonic_tbl[note]) * osc.dt < 0.5 ? harmonic : 0.0;

    adsr.adsr_reset();
  ) : (
    adsr.state = ptr[1];
    adsr.env = ptr[2];
  );

  ptr[1] = adsr.adsr_a(1.0);
  ptr[2] = adsr.env;

  ptr[7] = gain;
);

// Fast approximation of f(x) = 2^(2/12*(x/8192 - 1)) for 0 <= x <= 16384.
function pitch_bend(x)
(
  (/* (2*(2^(-2/12) + 2^(2/12)) - 4) / 16384^2 */ 0.000000000099545467270257221 * x +
   /* (4 - (3*2^(-2/12) + 2^(2/12))) / 16384 */ 0.000012502551102881415) * x +
   /* 2^(-2/12) */ 0.89089871814033930;
);

function early_ref_init(buf)
  // global(srate)
  instance(buf, len0, len1)
  local(ptr, step, delay)
(
  ptr = buf;
  step = 0.020 * srate;
  delay = 0.5;

  loop(11,
    ptr[0] = (delay += step)|0;
    ptr += 2;
  );

  len0 = ceil(delay - 0.5);
  len1 = ceil(16/44100 * srate);

  buf[ 1] = /* -(10^(/-47.3/20)) */ -0.0043151907682776522;  //  20 ms
  buf[ 3] = /* -(10^(/-41.7/20)) */ -0.0082224264994707114;  //  40 ms
  buf[ 5] = /* +(10^(/-36.1/20)) */ +0.015667510701081491;   //  60 ms
  buf[ 7] = /* +(10^(/-48.1/20)) */ +0.0039355007545577746;  //  80 ms
  buf[ 9] = /* +(10^(/-44.3/20)) */ +0.0060953689724016916;  // 100 ms
  buf[11] = /* +(10^(/-55.0/20)) */ +0.0017782794100389228;  // 120 ms
  buf[13] = /* +(10^(/-54.9/20)) */ +0.0017988709151287878;  // 140 ms
  buf[15] = /* +(10^(/-65.3/20)) */ +0.00054325033149243321; // 160 ms
  buf[17] = /* +(10^(/-46.3/20)) */ +0.0048417236758409934;  // 180 ms
  buf[19] = /* +(10^(/-56.5/20)) */ +0.0014962356560944334;  // 200 ms
  buf[21] = /* +(10^(/-58.0/20)) */ +0.0012589254117941672;  // 220 ms

  buf + len0 + len1;
);

function early_ref_process(in)
  instance(out0, idx0, len0, out1, idx1, len1)
  local(ptr, buf, idx, gain)
(
  out0 = 0.0;
  buf = (ptr = this.buf) + 22;

  loop(11,
    (idx = idx0 - ptr[0]) < 0 ? idx += len0;
    out0 += ptr[1] * buf[idx];
    ptr += 2;
  );

  buf[idx0] = in;
  (idx0 += 1) >= len0 ? idx0 = 0;

  buf += len0;
  out1 = buf[idx1];

  buf[idx1] = out0;
  (idx1 += 1) >= len1 ? idx1 = 0;
);

note_tbl = 0;

// Bb major
note_tbl[ 0] =  1; // D5
note_tbl[ 1] =  2; // Eb5
note_tbl[ 3] =  3; // F5
note_tbl[ 5] =  4; // G5
note_tbl[ 7] =  5; // A5
note_tbl[ 8] =  6; // Bb5
note_tbl[10] =  7; // C6
note_tbl[12] =  8; // D6
note_tbl[13] =  9; // Eb6
note_tbl[15] = 10; // F6
note_tbl[17] = 11; // G6
note_tbl[19] = 12; // A6
note_tbl[20] = 13; // Bb6
note_tbl[22] = 16; // C7
note_tbl[24] = 18; // D7

// Chromatic
note_tbl[25] =  1; // D5
note_tbl[26] =  2; // Eb5
note_tbl[27] = 19; // E5
note_tbl[28] =  3; // F5
note_tbl[29] = 20; // F#5
note_tbl[30] =  4; // G5
note_tbl[31] = 21; // G#5
note_tbl[32] =  5; // A5
note_tbl[33] =  6; // Bb5
note_tbl[34] = 22; // B5
note_tbl[35] =  7; // C6
note_tbl[36] = 23; // C#6
note_tbl[37] =  8; // D6
note_tbl[38] =  9; // Eb6
note_tbl[39] = 24; // E6
note_tbl[40] = 10; // F6
note_tbl[41] = 25; // F#6
note_tbl[42] = 11; // G6
note_tbl[43] = 26; // G#6
note_tbl[44] = 12; // A6
note_tbl[45] = 13; // Bb6
note_tbl[46] = 27; // B6
note_tbl[47] = 16; // C7
note_tbl[48] = 28; // C#7
note_tbl[49] = 18; // D7

freq_tbl = note_tbl + 50 - 1;

// Bb major
freq_tbl[ 1] = /* 440*2^( 4.95/12) */ 585.63570877844703; // D5
freq_tbl[ 2] = /* 440*2^( 6.05/12) */ 624.05370504586610; // Eb5
freq_tbl[ 3] = /* 440*2^( 7.99/12) */ 698.05313508953531; // F5
freq_tbl[ 4] = /* 440*2^(10.10/12) */ 788.53248488251816; // G5
freq_tbl[ 5] = /* 440*2^(12.11/12) */ 885.60918835397306; // A5
freq_tbl[ 6] = /* 440*2^(13.19/12) */ 942.61601339069027; // Bb5
freq_tbl[ 7] = /* 440*2^(15.09/12) */ 1051.9567775415026; // C6
freq_tbl[ 8] = /* 440*2^(17.01/12) */ 1175.3377773550204; // D6
freq_tbl[ 9] = /* 440*2^(18.29/12) */ 1265.5303402586934; // Eb6
freq_tbl[10] = /* 440*2^(20.06/12) */ 1401.7626560998308; // F6
freq_tbl[11] = /* 440*2^(22.04/12) */ 1571.6087394662727; // G6
freq_tbl[12] = /* 440*2^(24.24/12) */ 1784.5686844304513; // A6
freq_tbl[13] = /* 440*2^(25.14/12) */ 1879.7951181642187; // Bb6 (1)
freq_tbl[14] = /* 440*2^(25.20/12) */ 1886.3212940638760; // Bb6 (2)
freq_tbl[15] = /* 440*2^(25.26/12) */ 1892.8701272049856; // Bb6 (3)
freq_tbl[16] = /* 440*2^(27.21/12) */ 2118.5474311303943; // C7  (1)
freq_tbl[17] = /* 440*2^(27.15/12) */ 2111.2178138319874; // C7  (2)
freq_tbl[18] = /* 440*2^(28.94/12) */ 2341.1901200295851; // D7

// Chromatic
freq_tbl[19] = /* freq_tbl[ 2]*2^(1/12) */ 661.16186939427625; // E5
freq_tbl[20] = /* freq_tbl[ 3]*2^(1/12) */ 739.56153452916624; // F#5
freq_tbl[21] = /* freq_tbl[ 4]*2^(1/12) */ 835.42106643645690; // G#5
freq_tbl[22] = /* freq_tbl[ 6]*2^(1/12) */ 998.66687833952361; // B5
freq_tbl[23] = /* freq_tbl[ 7]*2^(1/12) */ 1114.5093826663532; // C#6
freq_tbl[24] = /* freq_tbl[ 9]*2^(1/12) */ 1340.7826902960471; // E6
freq_tbl[25] = /* freq_tbl[10]*2^(1/12) */ 1485.1158011888314; // F#6
freq_tbl[26] = /* freq_tbl[11]*2^(1/12) */ 1665.0614582370488; // G#6
freq_tbl[27] = /* freq_tbl[14]*2^(1/12) */ 1998.4877951647442; // B6
freq_tbl[28] = /* freq_tbl[17]*2^(1/12) */ 2236.7573579089039; // C#7

harmonic_tbl = freq_tbl + 29 - 1;

// Bb major
harmonic_tbl[ 1] = 13.0; // D5
harmonic_tbl[ 2] = 12.5; // Eb5
harmonic_tbl[ 3] = 11.5; // F5
harmonic_tbl[ 4] = 11.0; // G5
harmonic_tbl[ 5] = 10.5; // A5
harmonic_tbl[ 6] = 10.5; // Bb5
harmonic_tbl[ 7] =  9.5; // C6
harmonic_tbl[ 8] = 10.5; // D6
harmonic_tbl[ 9] = 10.5; // Eb6
harmonic_tbl[10] = 10.0; // F6
harmonic_tbl[11] =  8.5; // G6
harmonic_tbl[12] =  8.5; // A6
harmonic_tbl[13] =  8.5; // Bb6 (1)
harmonic_tbl[14] =  8.5; // Bb6 (2)
harmonic_tbl[15] =  8.5; // Bb6 (3)
harmonic_tbl[16] =  7.5; // C7  (1)
harmonic_tbl[17] =  8.0; // C7  (2)
harmonic_tbl[18] =  8.5; // D7

// Chromatic
harmonic_tbl[19] = /* harmonic_tbl[ 2] */ 12.5; // E5
harmonic_tbl[20] = /* harmonic_tbl[ 3] */ 11.5; // F#5
harmonic_tbl[21] = /* harmonic_tbl[ 4] */ 11.0; // G#5
harmonic_tbl[22] = /* harmonic_tbl[ 6] */ 10.5; // B5
harmonic_tbl[23] = /* harmonic_tbl[ 7] */  9.5; // C#6
harmonic_tbl[24] = /* harmonic_tbl[ 9] */ 10.5; // E6
harmonic_tbl[25] = /* harmonic_tbl[10] */ 10.0; // F#6
harmonic_tbl[26] = /* harmonic_tbl[11] */  8.5; // G#6
harmonic_tbl[27] = /* harmonic_tbl[14] */  8.5; // B6
harmonic_tbl[28] = /* harmonic_tbl[17] */  8.0; // C#7

er.buf = note_buf.array_init(harmonic_tbl + 29, 29, 9);
pb = 1.0;

osc.a = 1.0;

adsr.adsr_seta(0.003);
adsr.adsr_setd(0.010);
adsr.adsr_sets(0.3);

gain_smooth.a = env_smooth.a = adsr.a;

hp1.a = hp0.rc_setf_precise(1000.0);
hs.zdf_hs(5000.0, 2.0, 0.25);

er_smooth.a = vol_smooth.rc_sett(0.0165);
er_smooth.lp = vol_smooth.lp = -1.0;

er.early_ref_init(er.buf);

@slider

scale = note_tbl;
min_note = 50;

slider1 < 0.5 ? min_note += 2 : scale += 25;
max_note = min_note + 24;

fm_rate = (1 << min(max(0, slider2|0), 3)) * /* 16/(28*120) */ 0.0047619047619047619;
fm_depth = min(max(0.0, slider3), 50.0) * /* 0.015/26.0 */ 0.00057692307692307692;

adsr.adsr_setr(min(max(20, slider4), 5000) * 0.001);

vol_gain = exp(slider5 * /* log(10)/20 */ 0.11512925464970228);
er_gain = min(max(0.0, slider6), 100.0) * 0.3;


@block

lfo.lfo_setf(fm_rate * tempo);

vol_smooth.lp < 0.0 ? vol_smooth.lp = vol_gain;
er_smooth.lp < 0.0 ? er_smooth.lp = er_gain;

@sample

while(midi.midiq_recv()) (
  // Note On
  (midi.msg1 &= 0xF0) == 0x90 ? (
    midi.msg3 && midi.msg2 >= min_note && midi.msg2 <= max_note &&
    (note = scale[midi.msg2 - min_note]) > 0 ? (

      // Bb6
      note == 13 ? (
        note += rr0;
        (rr0 += 1) >= 3 ? rr0 = 0;
      ) :

      // C7
      note == 16 ? (
        note += rr1;
        rr1 = !rr1;
      );

      note_on(note, midi.msg3);
    );
  ) :

  // Pitch Bend
  midi.msg1 == 0xE0 ? (
    pb = pitch_bend(midi.msg3 * 128 + midi.msg2);
  );
);

fm = (lfo.lfo_para() * fm_depth + 1.0) * pb;
out = 0.0;

ptr = note_buf.array_first();
while(ptr >= 0) (

  adsr.state = ptr[1];
  adsr.env = ptr[2];

  adsr.adsr_process() ? (
    adsr.state == 4 ? adsr.adsr_r();
  
    ptr[1] = adsr.state;
    ptr[2] = adsr.env;

    osc.t = t = ptr[3];
    osc.dt = ptr[4] * fm;
    ptr[3] = osc.lfo_inc();

    env_smooth.lp = ptr[5];
    gain_smooth.lp = ptr[6];
    gain = ptr[7];

    ptr[5] = env_smooth.rc_lp(gain * adsr.env);
    ptr[6] = gain_smooth.rc_lp(gain);

    osc.t = t *= 2;
    osc.t -= osc.t|0;
    osc.dt *= 2;
    sample = osc.poly_para();

    (harmonic = ptr[8]) > 0.0 ? (
      osc.t = t * harmonic;
      osc.t -= osc.t|0;
      osc.dt *= harmonic;
      sample += osc.poly_cos() * (0.1 / gain_smooth.lp + 0.9) * sqr(adsr.env);
    );

    out += sample * env_smooth.lp;
  ) : (
    note_buf.array_remove(ptr);
    ptr -= note_buf.num;
  );

  ptr = note_buf.array_next(ptr);
);

out *= vol_smooth.rc_lp(vol_gain);

out = hp0.rc_hp_precise(out);
out = hp1.rc_hp_precise(out);
out = hs.zdf_svf(out);

er.early_ref_process(out);

er.out0 *= er_smooth.rc_lp(er_gain);
er.out1 *= er_smooth.lp;

spl0 += out + er.out0;
spl1 += out + er.out1;
