Skip to content

Commit

Permalink
Merge pull request #94268 from adamscott/fix-web-sample-playback-fini…
Browse files Browse the repository at this point in the history
…shed-signal-redux

Fix audio samples not being able to be "finished"
  • Loading branch information
akien-mga committed Jul 17, 2024
2 parents 0918fd2 + 2f5f84b commit 6d7ef2c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 18 deletions.
14 changes: 1 addition & 13 deletions platform/web/audio_driver_web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node>(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) {
Expand Down
4 changes: 0 additions & 4 deletions scene/audio/audio_stream_player_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ Ref<AudioStreamPlayback> AudioStreamPlayerInternal::play_basic() {
Ref<AudioSamplePlayback> 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()) {
Expand Down Expand Up @@ -262,9 +261,6 @@ void AudioStreamPlayerInternal::seek(float p_seconds) {
void AudioStreamPlayerInternal::stop() {
for (Ref<AudioStreamPlayback> &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();

Expand Down
1 change: 0 additions & 1 deletion servers/audio/audio_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class AudioSamplePlayback : public RefCounted {
public:
Ref<AudioStream> stream;

ObjectID player_id;
float offset = 0.0f;
Vector<AudioFrame> volume_vector;
StringName bus;
Expand Down
12 changes: 12 additions & 0 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,12 @@ void AudioServer::start_playback_stream(Ref<AudioStreamPlayback> p_playback, con
void AudioServer::stop_playback_stream(Ref<AudioStreamPlayback> 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;
Expand Down Expand Up @@ -1358,6 +1364,10 @@ void AudioServer::set_playback_highshelf_params(Ref<AudioStreamPlayback> p_playb
bool AudioServer::is_playback_active(Ref<AudioStreamPlayback> 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;
Expand Down Expand Up @@ -1818,11 +1828,13 @@ void AudioServer::unregister_sample(const Ref<AudioSample> &p_sample) {
void AudioServer::start_sample_playback(const Ref<AudioSamplePlayback> &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<AudioSamplePlayback> &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<AudioSamplePlayback> &p_playback, bool p_paused) {
Expand Down
2 changes: 2 additions & 0 deletions servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ class AudioServer : public Object {
friend class AudioDriver;
void _driver_process(int p_frames, int32_t *p_buffer);

LocalVector<Ref<AudioStreamPlayback>> sample_playback_list;

protected:
static void _bind_methods();

Expand Down

0 comments on commit 6d7ef2c

Please sign in to comment.