desc:Mono synth with ADSR

// Copyright (C) 2013-2017 Theo Niessink <theo@taletn.com>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

slider1:3<0,5000,1>Attack (ms)
slider2:1000<1,15000,1>Decay (ms)
slider3:-12.0<-120.0,24.0,1.0>Sustain (dB)
slider4:500<0,5000,1>Release (ms)

import Tale/adsr.jsfx-inc
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc

@slider

adsr.adsr_seta(slider1 * 0.001);
adsr.adsr_setd(slider2 * 0.001);
adsr.adsr_sets(exp(log(10)/20 * slider3));
adsr.adsr_setr(slider4 * 0.001);

@sample

while(midi.midiq_recv()) (
  midi.msg1 &= 0xF0;

  // Note On
  midi.msg1 == 0x90 && midi.msg3 ? (
    osc.poly_setf(note = midi.msg2, 440);
    // Attack
    adsr.adsr_a(0.5 * midi.msg3 / 128);
  ) :

  // Note Off
  midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
    // Release
    midi.msg2 == note ? adsr.adsr_r();
  );
);

// Process ADSR envelope and apply it to oscillator.
adsr.adsr_process() ? spl0 = spl1 = adsr.env * osc.poly_saw();
