3333#include " godot_audio.h"
3434
3535#include " core/config/project_settings.h"
36+ #include " core/object/object.h"
37+ #include " scene/main/node.h"
3638#include " servers/audio/audio_stream.h"
3739
3840#include < emscripten.h>
@@ -51,6 +53,33 @@ void AudioDriverWeb::_latency_update_callback(float p_latency) {
5153 AudioDriverWeb::audio_context.output_latency = p_latency;
5254}
5355
56+ void AudioDriverWeb::_sample_playback_finished_callback (const char *p_playback_object_id) {
57+ const ObjectID playback_id = ObjectID (String::to_int (p_playback_object_id));
58+
59+ Object *playback_object = ObjectDB::get_instance (playback_id);
60+ if (playback_object == nullptr ) {
61+ return ;
62+ }
63+ Ref<AudioSamplePlayback> playback = Object::cast_to<AudioSamplePlayback>(playback_object);
64+ if (playback.is_null ()) {
65+ return ;
66+ }
67+
68+ Object *player_object = ObjectDB::get_instance (playback->player_id );
69+ if (player_object == nullptr ) {
70+ return ;
71+ }
72+ Node *player = Object::cast_to<Node>(player_object);
73+ if (player == nullptr ) {
74+ return ;
75+ }
76+
77+ const StringName finished = SNAME (" finished" );
78+ if (player->has_signal (finished)) {
79+ player->emit_signal (finished);
80+ }
81+ }
82+
5483void AudioDriverWeb::_audio_driver_process (int p_from, int p_samples) {
5584 int32_t *stream_buffer = reinterpret_cast <int32_t *>(output_rb);
5685 const int max_samples = memarr_len (output_rb);
@@ -132,6 +161,9 @@ Error AudioDriverWeb::init() {
132161 if (!input_rb) {
133162 return ERR_OUT_OF_MEMORY;
134163 }
164+
165+ godot_audio_sample_set_finished_callback (&_sample_playback_finished_callback);
166+
135167 return OK;
136168}
137169
0 commit comments