Importing audioSceneComponent causes massive increase in memory usage

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.

cc @docEdub

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.

Could you share that specific mp3 so we can do some tests?

3 Likes

Sorry about the delay, got busy with other tasks.

I’d post a link, but I don’t remember where I downloaded it. Here are the parts (It’s too big to upload as a single file.)

ocean.mp3aa.zip (5 MB)
ocean.mp3ab.zip (5.0 MB)

To combine, download, remove .zip extension (UI won’t let me upload arbitrary extensions), then run cat ocean.mp3aa ocean.mp3ab > ocean.mp3.

1 Like

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.

2 Likes

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?

1 Like

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.

4 Likes

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.

3 Likes