danigb / soundfont-player

Quick soundfont loader and player for browser
MIT License
453 stars 60 forks source link

event listener added on each event callback #101

Open err0r500 opened 2 years ago

err0r500 commented 2 years ago

Hi,

I try to loop on a sample by adding a onended event to it (once). The problem is : when run in a browser, the JS event listeners count increment on each loop. It seems that something can't be garbage collected but I can't figure out what.

Here's a minimal example that reproduces the issue (I try to avoid references to global vars in the play function, without success):

const AudioContext = window.AudioContext || window.webkitAudioContext || false;
const ac = new AudioContext();

const play = (organ, now) => {
  organ.play("C2").stop(now + 3);
};

Soundfont.instrument(ac, "drawbar_organ").then((organ) => {
  organ.on("ended", () => play(organ, ac.currentTime));

  play(organ, ac.currentTime);
});