/**
 * Description: This library is able to play soundfonts (sf2 files) with the Web Audio API..
 *
 * @package     midi-Synth
 * @category    module js
 * @author      felixroos <https://github.com/felixroos/sfumato>
 * @copyright   sfumato
 * @license     GPL-2.0-or-later
 */

if (typeof AudioParam !== "undefined") {
  AudioParam.prototype.dahdsr = function(
    time,
    min,
    max,
    delay,
    attack,
    hold,
    decay,
    sustain,
    release
  ) {
    attack = Math.max(precision(attack, 4), 0.001)
    decay = Math.max(precision(decay, 4), 0.001)
    release = precision(release, 4)
    min = Math.max(min, 0.001)
    // console.log(min, '-', max);
    // console.log('A', attack, 'H', hold, 'D', decay, 'S', sustain, 'R', release);
    let t = time
    this.setValueAtTime(min, t) // origin time
    // delay
    this.setValueAtTime(min, (t += delay))
    // attack
    if(0 < max)this.exponentialRampToValueAtTime(max, (t += attack)) // attack
    // hold
    if(0 < max)this.setValueAtTime(max, (t += hold))
    // decay
    if(0 < max)this.exponentialRampToValueAtTime(
      Math.max(sustain * max, 0.001),
      (t += decay)
    )

    /*
    When 96 dB (0.04) of attenuation is reached in the final gain amplifier, an abrupt jump to zero gain (infinite dB
    of attenuation) occurs. In a 16-bit system, this jump is inaudible.
    */
    // release function
    return (end, value) => {
      if (!navigator.userAgent.includes("Firefox"))this.cancelAndHoldAtTime(end)
      const target = Math.max(value ?? min, 0.001) // 0 is not allowed here..
      this.exponentialRampToValueAtTime(target, end + release) // release
    }
  }
}
