infojunkie / musicxml-player

A TypeScript component that loads and plays MusicXML files in the browser using Web Audio and Web MIDI.
https://blog.karimratib.me/demos/musicxml/
GNU General Public License v3.0
34 stars 6 forks source link

Missed notes using `webaudiofont` #9

Closed infojunkie closed 1 year ago

infojunkie commented 1 year ago

When many notes are played simultaneously (e.g. a MIDI file with full accompaniment) some notes are dropped seemingly randomly.

infojunkie commented 1 year ago

I made many experiments to find the root cause of this behaviour. My latest conclusion is that it seems related to calling envelope.cancel() inside the note cutoff scheduler: when I suppress that call, the notes are no longer cut off (as expected), but no notes are subsequently missed.

Another observation has to do with the refresh interval in the note cutoff scheduler: the longer the interval, the more notes are missed. This is counter-intuitive, because I was expecting more notes to be elongated, and less notes to be dropped.

My current theory is that the envelopes that are put back in the pool after being canceled have not been thoroughly "cleaned". The module author alludes to this in a similar issue but never elaborates on the answer.

infojunkie commented 1 year ago

In the branch above, I am overriding the envelope.cancel() function to cut the note short, but still avoid putting the envelope back into the pool by setting its onset to a value that will never be picked up. More listening is needed until I am satisfied this fixes the missed notes.

Also, this approach would put a limit to the maximum amount of notes that be played, since a new envelope will need to be allocated for each.

infojunkie commented 1 year ago

After repeated listening, there was no longer dropped notes. But as expected, the whole sound system started glitching after multiple repetitions and eventually went completely silent - only a page reload fixed it. I will need to add a more thorough cleanup mechanism in place.

infojunkie commented 1 year ago

Still appearing in latest demo.