From 67c110c4d4ea7a0a033d4ef7a85cb2da8108cafb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 4 Oct 2026 12:39:12 +0000 Subject: [PATCH] Fix WASM audio wiring and m_defer_enterframe load check Co-authored-by: ClausValcaTD <193546948+ClausValcaTD@users.noreply.github.com> --- src/wasm_api.cpp | 88 ++++++++++++++++++- .../gameswf/gameswf/gameswf_sprite.cpp | 2 +- 2 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/wasm_api.cpp b/src/wasm_api.cpp index b0879bd..3a78b3b 100644 --- a/src/wasm_api.cpp +++ b/src/wasm_api.cpp @@ -5,6 +5,7 @@ #include "gameswf/gameswf_player.h" #include "gameswf/gameswf_movie_def.h" #include "gameswf/gameswf_types.h" +#include "audio_mixer.h" #include #include #include @@ -19,6 +20,86 @@ uint32_t g_wasm_height = 600; static gameswf::player* g_player = nullptr; static gameswf::root* g_movie = nullptr; +static AudioMixer g_audio_mixer; + +struct WasmSoundHandler : public gameswf::sound_handler { + struct SoundData { + std::vector samples; + int sample_rate = 44100; + bool stereo = true; + bool is_stream = false; + }; + + std::vector m_sounds; + + void reset() { + m_sounds.clear(); + } + + virtual bool is_open() override { + return true; + } + + virtual int create_sound( + void* data, + int data_bytes, + int sample_count, + format_type format, + int sample_rate, + bool stereo) override + { + int id = static_cast(m_sounds.size()); + m_sounds.push_back(SoundData()); + m_sounds[id].sample_rate = sample_rate; + m_sounds[id].stereo = stereo; + + if (data && data_bytes > 0) { + m_sounds[id].is_stream = false; + size_t num_samples = data_bytes / sizeof(int16_t); + const int16_t* pcm = static_cast(data); + m_sounds[id].samples.assign(pcm, pcm + num_samples); + } else { + m_sounds[id].is_stream = true; + } + return id; + } + + virtual int load_sound(const char* url) override { + return -1; + } + + virtual void append_sound(int sound_handle, void* data, int data_bytes) override { + if (sound_handle >= 0 && sound_handle < static_cast(m_sounds.size())) { + if (data && data_bytes > 0) { + size_t num_samples = data_bytes / sizeof(int16_t); + const int16_t* pcm = static_cast(data); + m_sounds[sound_handle].samples.insert( + m_sounds[sound_handle].samples.end(), pcm, pcm + num_samples); + g_audio_mixer.queue_samples(pcm, num_samples); + } + } + } + + virtual void play_sound(gameswf::as_object* listener_obj, int sound_handle, int loop_count) override { + if (sound_handle >= 0 && sound_handle < static_cast(m_sounds.size())) { + const auto& snd = m_sounds[sound_handle]; + if (!snd.is_stream && !snd.samples.empty()) { + int loops = (loop_count < 0) ? 1 : (loop_count + 1); + for (int l = 0; l < loops; ++l) { + g_audio_mixer.queue_samples(snd.samples.data(), snd.samples.size()); + } + } + } + } + + virtual void set_volume(int sound_handle, int volume) override {} + virtual void set_max_volume(int vol) override {} + virtual void stop_sound(int sound_handle) override {} + virtual void stop_all_sounds() override {} + virtual void delete_sound(int sound_handle) override {} +}; + +static WasmSoundHandler g_sound_handler; extern "C" { @@ -31,7 +112,9 @@ int wasm_load_swf(const uint8_t* data, size_t size) { } gameswf::set_render_handler(create_render_handler_wasm()); - gameswf::set_sound_handler(nullptr); + g_audio_mixer.reset(); + g_sound_handler.reset(); + gameswf::set_sound_handler(&g_sound_handler); tu_file* in = create_gameswf_tu_file_mem(data, size); if (!in) return 0; @@ -89,8 +172,7 @@ uint32_t wasm_get_player_hp(void) { return 0; } size_t wasm_get_audio_samples(int16_t* out_buffer, size_t num_frames) { if (!out_buffer || num_frames == 0) return 0; - std::memset(out_buffer, 0, num_frames * 2 * sizeof(int16_t)); - return num_frames; + return g_audio_mixer.render_frame(out_buffer, num_frames); } } // extern "C" diff --git a/third_party/gameswf/gameswf/gameswf_sprite.cpp b/third_party/gameswf/gameswf/gameswf_sprite.cpp index 0b1fd99..f3ed026 100644 --- a/third_party/gameswf/gameswf/gameswf_sprite.cpp +++ b/third_party/gameswf/gameswf/gameswf_sprite.cpp @@ -329,7 +329,7 @@ namespace gameswf character* sib = parent_sp->m_display_list.get_character(i); if (sib && sib != this) { sprite_instance* sib_sp = cast_to(sib); - if (sib_sp && sib_sp->get_character_def() && sib_sp->get_frame_count() > 0 && sib_sp->get_loaded_bytes() == 0) { + if (sib_sp && sib_sp->get_character_def() && sib_sp->get_frame_count() > 0 && sib_sp->m_on_event_load_called == false) { skip_enterframe = true; break; }