import "@babylonjs/core/Audio/audioSceneComponent"; causes my app’s memory usage to go from about 180MB to over 1GB. Tested in Chrome (macOS), Safari (macOS), Safari (iOS).
There’s no issue in playgrounds, but I’m not sure how to replicate it in a playground, because there’s no direct control over imports.
On macOS Safari using a barebones webpack-based app.ts I’m only seeing a 35 MB memory increase when adding import "@babylonjs/core/Audio/audioSceneComponent";
Can you tell us more about your setup? Are you using Webpack? Something else? Can we see the code, or can you repro it in a public github repo?
You’re right, the audio component is not a problem. The issue seems to be a specific large mp3 file (10MB). I swapped it with a different, smaller file, and the memory issues went away.
But… I would still like to know how a 10MB file causes a ~750MB spike memory usage, and only in BJS apps. Interestingly, playing the same mp3 in a Chrome tab (by dragging) only costs 35MB.
The .mp3 file you’re using is over an hour long. That’s why it’s blowing up the memory usage. The browser is decompressing it and filling a huge memory buffer of uncompressed 32 bit PCM wave data from it. Is there a reason you need it to be that long? A well-crafted one or two minute loop should be fine for an ocean sound.
No, there’s no good reason at all… I just grabbed something as a placeholder.
But that file takes only ~35MB when playing in a browser tab by itself. Why does it take up so much more in BJS?
With Web Audio API, to use a compressed audio file you must load the compressed audio file into memory and then use decodeAudioData to convert the compressed file to an AudioBuffer , consisting of float arrays internally—essentially PCM. While it is done automatically it is quite memory intensive.
When playing it in a browser tab by itself, the .mp3 is being streamed as-is without being decompressed and stored in an uncompressed PCM buffer. You can try using the streaming option in the Sound constructor to get similar results as the browser, but .mp3’s and streaming audio files have issues with looping back to the beginning cleanly, and I don’t think the streaming option works in Safari, so I think your best option is to edit that .mp3 down to a minute or two long and keep using the Sound constructor without the streaming option.