surikov / webaudiofont

Use full GM set of musical instruments to play MIDI and single sounds or effects. Support for reverberation and equaliser. No plugins, no Flash. Pure HTML5 implementation compatible with desktop and mobile browser. See live examples.
https://surikov.github.io/webaudiofont/
GNU General Public License v3.0
891 stars 92 forks source link

envelope.cancel() Should include a "when" parameter #62

Closed dannybullo closed 4 years ago

dannybullo commented 4 years ago

Hi! I'm building an application where I schedule events in the future, in the technique explained in "A Tale of Tow Clocks.". I have Note Ons and Note Offs. Instead of scheduling NoteOn(when, duration), I do:

NoteOn(when) NoteOff(aLaterWhen)

As in 'endless flute', I do:

var envelope = player.queueWaveTable( audioContext, audioContext.destination, instrumentName, when, pitch, 999, 0.5 );

The problem is that envelope.cancel() does not have a "when" parameter. Do you think you can add it? I tried adding it without success. Note that I don't want to JUST cut the sound at "when". I want to simulate good NoteOff...Same as if the envelope had the correct Duration, instead of 999.

Thank you very much and I really enjoy your lib!

Danny Bullo

surikov commented 4 years ago

Use envelope.cancel if you need to stop the sound suddenly. Use when and duration to schedule exact time for start and stop. See https://github.com/surikov/webaudiofont/wiki/2.1.-queueWaveTable

You don't need to abort notes while playing music. See example https://surikov.github.io/webaudiofont/examples/realtime.html

Why do you need to schedule a cancel of a note?

dannybullo commented 4 years ago

@surikov : Obviously, conceptually there are 2 ways to play a 1.5 second sound: 1) NoteOn(Note, 0) // 0 means NOW NoteOff(Note, 1.5) // 1.5 Seconds in the future

2) NoteOn(Note, 0, 1.5) // 0 means NOW, 1.5 is Duration

In your example , you know the duration of each note.

In my Case, I have a sequencer that contains Events, such as NoteOn and NoteOff. I do not store Duration in NoteOns as I have the future NoteOff. Sure I can store it (NoteOff time - NoteOn time). But prefer not to as I do other things with NoteOff events... I've seen other libraries that DO have when parameter in stop() https://github.com/danigb/soundfont-player/blob/master/dist/soundfont-player.js#L865

I think it gives a little more flexibility. If you are interested, I can make a Pull Request. Got it working.

Thanks a lot @surikov !

surikov commented 4 years ago

You know the exact start time and duration. Change your code to use it. “Cancel” is a sound cancellation function. Do not use it when playing music.

You can fork WebAudioFont and implement any function you want. Remember that in order to stop a note properly, you must recalculate the attenuation of the sound not just abort it. Also you need to recalculate ADSR envelope and tone portamento if note uses it.

dannybullo commented 4 years ago

@surikov : Yes, we usually know the exact start time and duration. There is an exception: When Playing LIVE/ Real Time. In those cases, what is the best way to handle it?

In you "Endless Flute" Example, you create the note with duration 999, and call CANCEL() as soon as the user releases the note (AKA NoteOff)

How to handle it properly, respecting the ADSR Envelope, in that case?

Thank you! ;)