Sound lags in calculations

I tried to add sound to the game, but got a problem.
If a function that requires a lot of processor resources is working during music playback, then the sound lags. I have no idea how to solve this. Is it possible to add asynchrony here? Or limit the function? This happens not only with sound, the whole game freezes. But with sound, this is most noticeable.

A simple example below.
Function calc() calculates the sum of primes. Found it on the Internet. The main thing is the demanding function.Simple project.zip (8.9 MB)

This is because built in audio is not played on its own thread currently. You can use either of these depending on your feature needs. extension-openal would be faster to use but defold-fmod has way more features posible.

6 Likes

Thank you so much. Music plays perfectly.

Would either of these extensions offer smoother playback on HTML5, or is that platform always problematic when it comes to audio?

All JavaScript code is single threaded by nature. I guess the webaudio subsystem too.

May be worth experimenting with. I remember webaudio supposed to be on its own thread, but maybe spectre/meltdown impacted that too.