danigb / soundfont-player

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

Memory leak in sample-player (includes fix/PR) #102

Open sschmidTU opened 1 year ago

sschmidTU commented 1 year ago

Used memory increases the longer samples are played in one session. You'll see a lot of GainNode objects piling up. For those interested, I shared our fix here: https://github.com/danigb/sample-player/pull/8

In our library (OpenSheetMusicDisplay, early access audio player), we patch this fix into node_modules/sample-player, otherwise we'd have to wait for a new release of sample-player and soundfont-player.

You can do this hotfix like this: Add this in package.json under scripts:

{
  "prebuild": prebuildSamplePlayer,
  "prebuildSamplePlayer": "ncp src/SamplePlayerPatch/lib/ node_modules/sample-player/lib/",
}

prebuild is automatically executed with npm run build. (you can just put the bottom line into prebuild if you don't have other prebuild scripts, otherwise use npm-run-all in prebuild) (of course you need to put the patched .js files into src/SamplePlayerPatch/lib/)


Dependencies: ncp is a dependency you'll need to add: "ncp": "^2.0.0",

This is just cp (a copy command) that works on Mac, Linux and Windows. I'm not aware of another easy platform-agnostic way to do that. And it's 17.5KB.

Also npm-run-all, if you want to use it in prebuild: "npm-run-all": "^4.1.5", -> prebuild: npm-run-all myPrebuild prebuildSamplePlayer