MoritzBrueckner / aura

A fast and lightweight 3D audio engine for Kha.
41 stars 3 forks source link

Fix audio crashing in HTML5 when canvas is resized or tabs are changed #8

Closed moisesjpelaez closed 3 months ago

MoritzBrueckner commented 6 months ago

Thanks a lot! I can confirm an issue that's related to NaN values in the doppler code which might be the same that you are experiencing, however, I'm not yet sure what exactly the cause is. I assume it is timing/framerate-related, maybe resizing the window causes a small lag that then changes the listener/source object velocities in a way that causes a division by zero, or something similar.

While your changes certainly seem to fix the error, at a first glance it looks to me like they might just fix the symptoms and not the underlying cause, so before merging this I want to spend some more time debugging the issue to really know what happens.

Do you already know more perhaps?

To make sure we're talking about the same issue: In my case, I get a failed assertion caused by dopplerRatio being NaN:

Error: Uncaught 
[Aura] Failed assertion:
    Expression: (targetSamplePos >= 0.0) in aura.utils.Resampler.sampleAtTargetPositionLerp at aura/utils/Resampler.hx:66
Stack:
Called from module at aura_utils_AuraAssertionException.haxe_exceptions_PosException [as __class__] ([...]/build/debug-html5/kha.js:6073:17)
Called from .new aura_utils_AuraAssertionException ([...]/build/debug-html5/kha.js line 6093 column 31)
Called from Function.aura_utils_Assert.throwAssertionError ([...]/build/debug-html5/kha.js line 5955 column 8)
Called from Function.aura_utils_Resampler.sampleAtTargetPositionLerp ([...]/build/debug-html5/kha.js line 6670 column 21)
Called from aura_channels_UncompBufferResamplingChannel.nextSamples ([...]/build/debug-html5/kha.js line 2521 column 44)
Called from aura_channels_MixChannel.nextSamples ([...]/build/debug-html5/kha.js line 2019 column 12)
Called from aura_channels_MixChannel.nextSamples ([...]/build/debug-html5/kha.js line 2019 column 12)
Called from Function.aura_Aura.audioCallback ([...]/build/debug-html5/kha.js line 966 column 10)
Called from ScriptProcessorNode.kha_audio2_Audio.processingNode.onaudioprocess ([...]/build/debug-html5/kha.js line 13180 column 21)

(The above error comes from the debug-html5 target using Electron, but the same happens with html5 in Firefox)

moisesjpelaez commented 6 months ago

No problem! I got that same error as well when I was resizing my browser or setting it to full-screen (in Chrome). I don't know what's causing the division by zero or dopplerRatio resulting in a negative value.

MoritzBrueckner commented 3 months ago

Hi, I finally had some time for this and pushed some changes at https://github.com/MoritzBrueckner/aura/commit/374800abd009398885e7eda0d40a631df12f16fc that should hopefully fix your issue. I would appreciate if you would test them and report back, thanks :)

While your PR fixed the "visible" part of the issue, the actual cause for the issue was indeed a division by zero that would happen when calculating the panner's velocity multiple times within the same time step. Apparently, resizing the browser window can trip up Kha's scheduler which provides the time, and thus the issue became evident in that case. But even without resizing, the previous behavior was problematic as a user could just update the position twice within a time step and the same would have happened.

Thank you for your patience!

moisesjpelaez commented 3 months ago

Thanks! These days I'm busy with one game plus other things but I'll likely be testing it again around next month in another project.