diff --git a/scene/2d/audio_stream_player_2d.cpp b/scene/2d/audio_stream_player_2d.cpp index 0cb431e6f..ac6adf8f5 100644 --- a/scene/2d/audio_stream_player_2d.cpp +++ b/scene/2d/audio_stream_player_2d.cpp @@ -275,6 +275,9 @@ void AudioStreamPlayer2D::_notification(int p_what) { } void AudioStreamPlayer2D::set_stream(Ref p_stream) { + // Instancing audio streams can cause large memory allocations, do it prior to AudioServer::lock. + Ref pre_instanced_playback = p_stream->instance_playback(); + AudioServer::get_singleton()->lock(); mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size()); @@ -288,7 +291,7 @@ void AudioStreamPlayer2D::set_stream(Ref p_stream) { if (p_stream.is_valid()) { stream = p_stream; - stream_playback = p_stream->instance_playback(); + stream_playback = pre_instanced_playback; } AudioServer::get_singleton()->unlock(); diff --git a/scene/3d/audio_stream_player_3d.cpp b/scene/3d/audio_stream_player_3d.cpp index 616abdfa8..7d869c2c5 100644 --- a/scene/3d/audio_stream_player_3d.cpp +++ b/scene/3d/audio_stream_player_3d.cpp @@ -627,6 +627,9 @@ void AudioStreamPlayer3D::_notification(int p_what) { } void AudioStreamPlayer3D::set_stream(Ref p_stream) { + // Instancing audio streams can cause large memory allocations, do it prior to AudioServer::lock. + Ref pre_instanced_playback = p_stream->instance_playback(); + AudioServer::get_singleton()->lock(); mix_buffer.resize(AudioServer::get_singleton()->thread_get_mix_buffer_size()); @@ -640,7 +643,7 @@ void AudioStreamPlayer3D::set_stream(Ref p_stream) { if (p_stream.is_valid()) { stream = p_stream; - stream_playback = p_stream->instance_playback(); + stream_playback = pre_instanced_playback; } AudioServer::get_singleton()->unlock(); diff --git a/scene/audio/audio_stream_player.cpp b/scene/audio/audio_stream_player.cpp index 66931aa45..21c620b4b 100644 --- a/scene/audio/audio_stream_player.cpp +++ b/scene/audio/audio_stream_player.cpp @@ -167,6 +167,9 @@ void AudioStreamPlayer::_notification(int p_what) { } void AudioStreamPlayer::set_stream(Ref p_stream) { + // Instancing audio streams can cause large memory allocations, do it prior to AudioServer::lock. + Ref pre_instanced_playback = p_stream->instance_playback(); + AudioServer::get_singleton()->lock(); if (active.is_set() && stream_playback.is_valid() && !stream_paused) { @@ -203,7 +206,7 @@ void AudioStreamPlayer::set_stream(Ref p_stream) { if (p_stream.is_valid()) { stream = p_stream; - stream_playback = p_stream->instance_playback(); + stream_playback = pre_instanced_playback; } AudioServer::get_singleton()->unlock();