Merge pull request #94268 from adamscott/fix-web-sample-playback-finished-signal-redux

Fix audio samples not being able to be "finished"
This commit is contained in:
Rémi Verschelde 2024-07-17 18:33:42 +02:00
commit 6d7ef2c33a
No known key found for this signature in database
GPG Key ID: C3336907360768E1
5 changed files with 15 additions and 18 deletions

View File

@ -65,19 +65,7 @@ void AudioDriverWeb::_sample_playback_finished_callback(const char *p_playback_o
return; return;
} }
Object *player_object = ObjectDB::get_instance(playback->player_id); AudioServer::get_singleton()->stop_sample_playback(playback);
if (player_object == nullptr) {
return;
}
Node *player = Object::cast_to<Node>(player_object);
if (player == nullptr) {
return;
}
const StringName finished = SNAME("finished");
if (player->has_signal(finished)) {
player->emit_signal(finished);
}
} }
void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) { void AudioDriverWeb::_audio_driver_process(int p_from, int p_samples) {

View File

@ -152,7 +152,6 @@ Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
Ref<AudioSamplePlayback> sample_playback; Ref<AudioSamplePlayback> sample_playback;
sample_playback.instantiate(); sample_playback.instantiate();
sample_playback->stream = stream; sample_playback->stream = stream;
sample_playback->player_id = node->get_instance_id();
stream_playback->set_sample_playback(sample_playback); stream_playback->set_sample_playback(sample_playback);
} }
} else if (!stream->is_meta_stream()) { } else if (!stream->is_meta_stream()) {
@ -262,9 +261,6 @@ void AudioStreamPlayerInternal::seek(float p_seconds) {
void AudioStreamPlayerInternal::stop() { void AudioStreamPlayerInternal::stop() {
for (Ref<AudioStreamPlayback> &playback : stream_playbacks) { for (Ref<AudioStreamPlayback> &playback : stream_playbacks) {
AudioServer::get_singleton()->stop_playback_stream(playback); 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(); stream_playbacks.clear();

View File

@ -49,7 +49,6 @@ class AudioSamplePlayback : public RefCounted {
public: public:
Ref<AudioStream> stream; Ref<AudioStream> stream;
ObjectID player_id;
float offset = 0.0f; float offset = 0.0f;
Vector<AudioFrame> volume_vector; Vector<AudioFrame> volume_vector;
StringName bus; StringName bus;

View File

@ -1220,6 +1220,12 @@ void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, con
void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> p_playback) { void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> p_playback) {
ERR_FAIL_COND(p_playback.is_null()); 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); AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
if (!playback_node) { if (!playback_node) {
return; return;
@ -1358,6 +1364,10 @@ void AudioServer::set_playback_highshelf_params(Ref<AudioStreamPlayback> p_playb
bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) { bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> p_playback) {
ERR_FAIL_COND_V(p_playback.is_null(), false); 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); AudioStreamPlaybackListNode *playback_node = _find_playback_list_node(p_playback);
if (!playback_node) { if (!playback_node) {
return false; return false;
@ -1818,11 +1828,13 @@ void AudioServer::unregister_sample(const Ref<AudioSample> &p_sample) {
void AudioServer::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { void AudioServer::start_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
AudioDriver::get_singleton()->start_sample_playback(p_playback); AudioDriver::get_singleton()->start_sample_playback(p_playback);
sample_playback_list.ordered_insert(p_playback);
} }
void AudioServer::stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) { void AudioServer::stop_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {
ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null."); ERR_FAIL_COND_MSG(p_playback.is_null(), "Parameter p_playback is null.");
AudioDriver::get_singleton()->stop_sample_playback(p_playback); AudioDriver::get_singleton()->stop_sample_playback(p_playback);
sample_playback_list.erase(p_playback);
} }
void AudioServer::set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) { void AudioServer::set_sample_playback_pause(const Ref<AudioSamplePlayback> &p_playback, bool p_paused) {

View File

@ -329,6 +329,8 @@ private:
friend class AudioDriver; friend class AudioDriver;
void _driver_process(int p_frames, int32_t *p_buffer); void _driver_process(int p_frames, int32_t *p_buffer);
LocalVector<Ref<AudioStreamPlayback>> sample_playback_list;
protected: protected:
static void _bind_methods(); static void _bind_methods();