Tonejs / Tone.js

A Web Audio framework for making interactive music in the browser.
https://tonejs.github.io
MIT License
13.41k stars 977 forks source link

Updated Envelope class causes some clicking on TriggerAttack and problems with Bitcrusher #79

Closed lukephills closed 9 years ago

lukephills commented 9 years ago

Hey,

I've noticed a couple of undesirable effects with the changes to the Envelope in r5.

There's a click on the attack portion when TriggerAttack is called. It can be heard when the attack is set anywhere between 0.02 and 0.1.

Chain an Oscillator to a AmplitudeEnvelope and Bitcrusher. Turn the bits down and without triggering the attack on the env you can here the original tone sound. Some of the Oscillator output is escaping through the envelope.

tambien commented 9 years ago

Since the Envelope's decay / release uses setTargetAtTime, it is an exponential approach towards 0, but might never reach 0. The BitCrusher effect uses a WaveShaperNode which seems to be amplifying those very small values by interpolating them back and forth between two steps.

It seems like the fix is to make the _minOuput value of the envelope smaller. It seems to be working fine for me when i set it to 0.00001, so i'll be sure to make that change in the next update.

The click also seems to be pretty easily fixable. From my testing, it appears that scheduling things now is what is causing the click. So far, I've never gotten clicks when i try triggerAttack("+0.01"), only when i use it with no arguments which causes it to trigger immediately: triggerAttack().

If i put a very small delay, it works fine. My guess is that it's unable to schedule the event at the current block (128 samples) so delaying now events to the next block would fix the problem. Since 128 samples at 44.1k is only about ~0.003ms, the delay wouldn't be noticeable, but should fix that clicking problem.

I'll make these changes and you can let me know if it fixes your issues.

lukephills commented 9 years ago

Yes this fixed both problems. Amazing thanks!