From 027546ee8a6d095662861b2ea3c4cd00c7420da9 Mon Sep 17 00:00:00 2001 From: Ellen Poe Date: Tue, 22 Mar 2022 05:58:17 -0700 Subject: [PATCH] [3.x] instance audio streams before AudioServer::lock call --- scene/2d/audio_stream_player_2d.cpp | 5 ++++- scene/3d/audio_stream_player_3d.cpp | 5 ++++- scene/audio/audio_stream_player.cpp | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/scene/2d/audio_stream_player_2d.cpp b/scene/2d/audio_stream_player_2d.cpp index 7377b1428ff6..48507e0532bf 100644 --- a/scene/2d/audio_stream_player_2d.cpp +++ b/scene/2d/audio_stream_player_2d.cpp @@ -271,6 +271,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()); @@ -284,7 +287,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 31483634aa6c..e0e86ece401e 100644 --- a/scene/3d/audio_stream_player_3d.cpp +++ b/scene/3d/audio_stream_player_3d.cpp @@ -622,6 +622,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()); @@ -635,7 +638,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 c6e7e3c50f68..f3ecb8aa4ca9 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();