From 2f5f84b4701b80ea3cf6067d8f15035f221918b1 Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Fri, 12 Jul 2024 10:49:59 -0400 Subject: [PATCH] Fix audio samples not being able to be "finished" --- platform/web/audio_driver_web.cpp | 14 +------------- scene/audio/audio_stream_player_internal.cpp | 4 ---- servers/audio/audio_stream.h | 1 - servers/audio_server.cpp | 12 ++++++++++++ servers/audio_server.h | 2 ++ 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/platform/web/audio_driver_web.cpp b/platform/web/audio_driver_web.cpp index b24c6cb1fdf..22487d2756a 100644 --- a/platform/web/audio_driver_web.cpp +++ b/platform/web/audio_driver_web.cpp @@ -65,19 +65,7 @@ void AudioDriverWeb::_sample_playback_finished_callback(const char *p_playback_o return; } - Object *player_object = ObjectDB::get_instance(playback->player_id); - if (player_object == nullptr) { - return; - } - Node *player = Object::cast_to(player_object); - if (player == nullptr) { - return; - } - - const StringName finished = SNAME("finished"); - if (player->has_signal(finished)) { - player->emit_signal(finished); - } + AudioServer::get_singleton()->stop_sample_playback(playback); } void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) { diff --git a/scene/audio/audio_stream_player_internal.cpp b/scene/audio/audio_stream_player_internal.cpp index 6653e01f258..36c14e03d54 100644 --- a/scene/audio/audio_stream_player_internal.cpp +++ b/scene/audio/audio_stream_player_internal.cpp @@ -152,7 +152,6 @@ Ref AudioStreamPlayerInternal::play_basic() { Ref sample_playback; sample_playback.instantiate(); sample_playback->stream = stream; - sample_playback->player_id = node->get_instance_id(); stream_playback->set_sample_playback(sample_playback); } } else if (!stream->is_meta_stream()) { @@ -262,9 +261,6 @@ void AudioStreamPlayerInternal::seek(float p_seconds) { void AudioStreamPlayerInternal::stop() { for (Ref &playback : stream_playbacks) { AudioServer::get_singleton()->stop_playback_stream(playback); - if (_is_sample() && playback->get_sample_playback().is_valid()) { - AudioServer::get_singleton()->stop_sample_playback(playback->get_sample_playback()); - } } stream_playbacks.clear(); diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 91491093815..0ca4777d5c4 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -49,7 +49,6 @@ class AudioSamplePlayback : public RefCounted { public: Ref stream; - ObjectID player_id; float offset = 0.0f; Vector volume_vector; StringName bus; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index fefb8bfd413..54840adcae1 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -1220,6 +1220,12 @@ void AudioServer::start_playback_stream(Ref p_playback, con void AudioServer::stop_playback_stream(Ref p_playback) { ERR_FAIL_COND(p_playback.is_null()); + // Handle sample playback. + if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) { + AudioServer::get_singleton()->stop_sample_playback(p_playback->get_sample_playback()); + return; + } + AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback); if (!playback_node) { return; @@ -1358,6 +1364,10 @@ void AudioServer::set_playback_highshelf_params(Ref p_playb bool AudioServer::is_playback_active(Ref p_playback) { ERR_FAIL_COND_V(p_playback.is_null(), false); + if (p_playback->get_is_sample() && p_playback->get_sample_playback().is_valid()) { + return sample_playback_list.has(p_playback->get_sample_playback()); + } + AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback); if (!playback_node) { return false; @@ -1818,11 +1828,13 @@ void AudioServer::unregister_sample(const Ref &p_sample) { void AudioServer::start_sample_playback(const Ref &p_playback) { ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); AudioDriver::get_singleton()->start_sample_playback(p_playback); + sample_playback_list.ordered_insert(p_playback); } void AudioServer::stop_sample_playback(const Ref &p_playback) { ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); AudioDriver::get_singleton()->stop_sample_playback(p_playback); + sample_playback_list.erase(p_playback); } void AudioServer::set_sample_playback_pause(const Ref &p_playback, bool p_paused) { diff --git a/servers/audio_server.h b/servers/audio_server.h index 4825e243364..84c091e3205 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -329,6 +329,8 @@ private: friend class AudioDriver; void _driver_process(int p_frames, int32_t *p_buffer); + LocalVector> sample_playback_list; + protected: static void _bind_methods();