Skip to content

Commit

Permalink
Bugfix in the adsr
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Nov 15, 2024
1 parent 035043c commit 55a0b51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions js/packages/core/lib/envelopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ export function adsr(
let atkSamps = el.mul(a, el.sr());
let atkGate = el.le(el.counter(g), atkSamps);

let target = el.select(g, el.select(atkGate, 1.0, s), 0);
let t60 = el.select(g, el.select(atkGate, a, d), r);
let targetValue = el.select(g, el.select(atkGate, 1.0, s), 0);

// Clamp the values to a minimum of 0.1ms because a time constant of 0 yields
// a divide-by-zero in the pole calculation
let t60 = el.max(0.0001, el.select(g, el.select(atkGate, a, d), r));

// Accelerate the phase time when calculating the pole position to ensure
// we reach closer to the target value before moving to the next phase.
//
// See: https://ccrma.stanford.edu/~jos/mdft/Audio_Decay_Time_T60.html
let p = el.tau2pole(el.div(t60, 6.91));

return el.smooth(p, target);
return el.smooth(p, targetValue);
}
2 changes: 1 addition & 1 deletion js/packages/core/lib/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function ms2samps(t: ElemNode): NodeRepr_t {
}

/**
* Computes a real pole position giving exponential decay over t, where t is the time (in seconds) to decay 60dB.
* Computes a real pole position giving exponential decay over t, where t is the time (in seconds) to decay by 1/e.
*/
export function tau2pole(t: ElemNode): NodeRepr_t {
return el.exp(el.div(-1.0, el.mul(t, el.sr())));
Expand Down

0 comments on commit 55a0b51

Please sign in to comment.