From 0522e842c44b1edbcd1f54ee82e6e49c42e06424 Mon Sep 17 00:00:00 2001 From: doyle Date: Tue, 31 Oct 2023 00:02:34 +1100 Subject: [PATCH] fp: Switch to sokol audio to fix audio bugs --- External/tely | 2 +- feely_pona.cpp | 3 -- feely_pona_build.cpp | 81 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/External/tely b/External/tely index d822446..07b3737 160000 --- a/External/tely +++ b/External/tely @@ -1 +1 @@ -Subproject commit d82244653e856bbf4f83c6338207405aa4595e46 +Subproject commit 07b3737cf4e5e68be355630228375c5b5873513f diff --git a/feely_pona.cpp b/feely_pona.cpp index a2020cb..d0e8602 100644 --- a/feely_pona.cpp +++ b/feely_pona.cpp @@ -4429,7 +4429,6 @@ TELY_OS_DLL_FUNCTION void TELY_OS_DLLFrameUpdate(TELY_OS *os) { TELY_OSInput *input = &os->input; - TELY_Assets *assets = &os->assets; TELY_Renderer *renderer = &os->renderer; FP_Game *game = DQN_CAST(FP_Game *) os->user_data; @@ -4491,6 +4490,4 @@ void TELY_OS_DLLFrameUpdate(TELY_OS *os) } FP_Render(game, os, renderer, audio); - - TELY_Audio_MixPlaybackSamples(audio, assets); } diff --git a/feely_pona_build.cpp b/feely_pona_build.cpp index 2180837..1fc4234 100644 --- a/feely_pona_build.cpp +++ b/feely_pona_build.cpp @@ -229,6 +229,38 @@ int main(int argc, char const **argv) } } + // NOTE: sokol_audio ================================================================================ + Dqn_Str8 sokol_audio_source_code_file = Dqn_FsPath_ConvertF(scratch.arena, "%.*s/External/sokol/sokol_audio.c", DQN_STR_FMT(tely_dir)); + uint64_t sokol_audio_pc_timings[2] = {}; + Dqn_List sokol_audio_pc_output_files = Dqn_List_Init(scratch.arena, 16); + { + sokol_audio_pc_timings[0] = Dqn_OS_PerfCounterNow(); + DQN_DEFER { sokol_audio_pc_timings[1] = Dqn_OS_PerfCounterNow(); }; + + Dqn_CPPBuildContext build_context = {}; + build_context.compiler = Dqn_CPPBuildCompiler_MSVC; + build_context.compile_files = Dqn_Slice_InitCArrayCopy(scratch.arena, { + Dqn_CPPBuildCompileFile{ + {}, // Args + sokol_audio_source_code_file, + }, + }); + + Dqn_List_Add(&sokol_audio_pc_output_files, DQN_STR8("sokol_audio.obj")); + + Dqn_List compile_flags = Dqn_List_InitSliceCopy(scratch.arena, 16, common_compile_flags); + Dqn_List_Add(&compile_flags, DQN_STR8("/c")); + build_context.compile_flags = Dqn_List_ToSliceCopy(&compile_flags, scratch.arena); + build_context.build_dir = build_dir; + + if (dry_run) { + Dqn_Str8 cmd = Dqn_CPPBuild_ToCommandLine(build_context, Dqn_CPPBuildMode_AlwaysRebuild, scratch.allocator); + Dqn_Print_StdLnF(Dqn_PrintStd_Out, "%.*s\n", DQN_STR_FMT(cmd)); + } else { + Dqn_CPPBuild_ExecOrAbort(build_context, Dqn_CPPBuildMode_CacheBuild); + } + } + // NOTE: QOI Converter ========================================================================= uint64_t qoi_converter_timings[2] = {}; { @@ -286,6 +318,8 @@ int main(int argc, char const **argv) { for (Dqn_ListIterator it = {}; Dqn_List_Iterate(&raylib_pc_output_files, &it, 0); ) Dqn_List_Add(&link_flags, *it.data); + for (Dqn_ListIterator it = {}; Dqn_List_Iterate(&sokol_audio_pc_output_files, &it, 0); ) + Dqn_List_Add(&link_flags, *it.data); Dqn_List_Add(&link_flags, DQN_STR8("gdi32.lib")); Dqn_List_Add(&link_flags, DQN_STR8("opengl32.lib")); Dqn_List_Add(&link_flags, DQN_STR8("winmm.lib")); @@ -434,9 +468,9 @@ int main(int argc, char const **argv) DQN_DEFER { raylib_emscripten_timings[1] = Dqn_OS_PerfCounterNow(); }; // NOTE: Setup build context =========================================================== - Dqn_List raylib_emscripten_output_files = Dqn_List_Init(scratch.arena, 16); - Dqn_CPPBuildContext raylib_emscripten_build_context = {}; - raylib_emscripten_build_context.compiler = Dqn_CPPBuildCompiler_GCC; + Dqn_List emscripten_obj_files = Dqn_List_Init(scratch.arena, 16); + Dqn_CPPBuildContext raylib_emscripten_build_context = {}; + raylib_emscripten_build_context.compiler = Dqn_CPPBuildCompiler_GCC; for (Dqn_Str8 base_file : raylib_base_files) { Dqn_Str8 file_stem = Dqn_Str8_FileNameNoExtension(base_file); @@ -461,7 +495,7 @@ int main(int argc, char const **argv) raylib_emscripten_build_context.compile_files = Dqn_Slice_InitCArrayCopy(scratch.arena, {build_file}); raylib_emscripten_build_context.compile_flags = Dqn_List_ToSliceCopy(&compile_flags, scratch.arena); raylib_emscripten_build_context.build_dir = build_dir; - Dqn_List_Add(&raylib_emscripten_output_files, build_file.output_file_path); + Dqn_List_Add(&emscripten_obj_files, build_file.output_file_path); if (dry_run) { Dqn_Str8 cmd = Dqn_CPPBuild_ToCommandLine(raylib_emscripten_build_context, Dqn_CPPBuildMode_AlwaysRebuild, scratch.allocator); @@ -471,13 +505,45 @@ int main(int argc, char const **argv) } } - // NOTE: Build the wasm raylib library ================================================= + { + Dqn_Str8 base_file = sokol_audio_source_code_file; + Dqn_Str8 file_stem = Dqn_Str8_FileNameNoExtension(base_file); + + // NOTE: Append "emscripten" suffix to the object files + Dqn_CPPBuildCompileFile build_file = {}; + build_file.input_file_path = base_file; + build_file.output_file_path = Dqn_Str8_InitF(scratch.allocator, "sokol_audio_emscripten.o"); + + Dqn_List compile_flags = Dqn_List_InitCArrayCopy(scratch.arena, 32, { + DQN_STR8("cmd"), + DQN_STR8("/C"), + DQN_STR8("emcc.bat"), + DQN_STR8("-c"), // Compile and assemble, but do not link + DQN_STR8("-Wall"), + DQN_STR8("-Os"), // Optimize for size + }); + Dqn_List_AddList(&compile_flags, build_specific_compile_flags); + + raylib_emscripten_build_context.compile_files = Dqn_Slice_InitCArrayCopy(scratch.arena, {build_file}); + raylib_emscripten_build_context.compile_flags = Dqn_List_ToSliceCopy(&compile_flags, scratch.arena); + raylib_emscripten_build_context.build_dir = build_dir; + Dqn_List_Add(&emscripten_obj_files, build_file.output_file_path); + + if (dry_run) { + Dqn_Str8 cmd = Dqn_CPPBuild_ToCommandLine(raylib_emscripten_build_context, Dqn_CPPBuildMode_AlwaysRebuild, scratch.allocator); + Dqn_Print_StdLnF(Dqn_PrintStd_Out, "%.*s\n", DQN_STR_FMT(cmd)); + } else { + Dqn_CPPBuild_ExecOrAbort(raylib_emscripten_build_context, Dqn_CPPBuildMode_CacheBuild); + } + } + + // NOTE: Build the wasm raylib+sokol_audio library ===================================== { Dqn_Str8Builder builder = {}; builder.allocator = scratch.allocator; Dqn_Str8Builder_AppendF(&builder, "cmd /C emar.bat rcs %.*s", DQN_STR_FMT(raylib_emscripten_lib_name)); - for (Dqn_ListIterator it = {}; Dqn_List_Iterate(&raylib_emscripten_output_files, &it, 0); ) + for (Dqn_ListIterator it = {}; Dqn_List_Iterate(&emscripten_obj_files, &it, 0); ) Dqn_Str8Builder_AppendF(&builder, " %.*s", DQN_STR_FMT(*it.data)); Dqn_Str8 cmd = Dqn_Str8Builder_Build(&builder, scratch.allocator); @@ -602,7 +668,8 @@ int main(int argc, char const **argv) build_timings[1] = Dqn_OS_PerfCounterNow(); Dqn_Print_StdLnF(Dqn_PrintStd_Out, "\n-- Dqn_CPPBuild Timings (%.2fms)", Dqn_OS_PerfCounterMs(build_timings[0], build_timings[1])); Dqn_Print_StdLnF(Dqn_PrintStd_Out, " robocopy: %.2fms", Dqn_OS_PerfCounterMs(robocopy_timings[0], robocopy_timings[1])); - Dqn_Print_StdLnF(Dqn_PrintStd_Out, " raylib: %.2fms", Dqn_OS_PerfCounterMs(raylib_pc_timings[0], raylib_pc_timings[1])); + Dqn_Print_StdLnF(Dqn_PrintStd_Out, " raylib: %.2fms", Dqn_OS_PerfCounterMs(raylib_pc_timings[0], raylib_pc_timings[1])); + Dqn_Print_StdLnF(Dqn_PrintStd_Out, " sokol_audio: %.2fms", Dqn_OS_PerfCounterMs(sokol_audio_pc_timings[0], sokol_audio_pc_timings[1])); Dqn_Print_StdLnF(Dqn_PrintStd_Out, " qoi_converter: %.2fms", Dqn_OS_PerfCounterMs(qoi_converter_timings[0], qoi_converter_timings[1])); Dqn_Print_StdLnF(Dqn_PrintStd_Out, " feely pona sprite packer: %.2fms", Dqn_OS_PerfCounterMs(feely_pona_sprite_packer_timings[0], feely_pona_sprite_packer_timings[1])); Dqn_Print_StdLnF(Dqn_PrintStd_Out, " feely pona (no dll): %.2fms", Dqn_OS_PerfCounterMs(feely_pona_no_dll_timings[0], feely_pona_no_dll_timings[1]));