From 2883af81be41b0e3ce3e58dd9d5d3b91bfd9bede Mon Sep 17 00:00:00 2001 From: geneotech Date: Thu, 25 Jul 2024 20:13:22 +0200 Subject: [PATCH] CrazyGames: limit audio command rate up to 4 ms. (configurable) --- cmake/web/assets/common.js | 16 +++++---- hypersomnia/content/menu/config.json | 2 +- hypersomnia/default_config.json | 5 +-- src/application/gui/settings_gui.cpp | 12 ++++--- src/augs/audio/audio_command_buffers.h | 33 +++++++++++++++---- src/make_canon_config.hpp | 8 ++++- .../audiovisual_state/audiovisual_state.cpp | 6 +++- .../systems/sound_system_settings.h | 3 ++ src/work.cpp | 2 +- 9 files changed, 64 insertions(+), 23 deletions(-) diff --git a/cmake/web/assets/common.js b/cmake/web/assets/common.js index ea00a8400..6e615c745 100644 --- a/cmake/web/assets/common.js +++ b/cmake/web/assets/common.js @@ -78,14 +78,16 @@ function setBrowserLocation(newLocation) { } function setBrowserLocation_cg(newLocation) { - const locStr = UTF8ToString(newLocation); + if (window.CrazyGames) { + const locStr = UTF8ToString(newLocation); - if (locStr === "") { - window.CrazyGames.SDK.game.hideInviteButton(); - } - else { - const link = window.CrazyGames.SDK.game.showInviteButton({ game: locStr }); - console.log("Invite button link", link); + if (locStr === "") { + window.CrazyGames.SDK.game.hideInviteButton(); + } + else { + const link = window.CrazyGames.SDK.game.showInviteButton({ game: locStr }); + console.log("Invite button link", link); + } } } diff --git a/hypersomnia/content/menu/config.json b/hypersomnia/content/menu/config.json index 27d61a24e..5fc9604b4 100644 --- a/hypersomnia/content/menu/config.json +++ b/hypersomnia/content/menu/config.json @@ -16,7 +16,7 @@ } }, "audio_volume": { - "music": 0.15 + "music": 0.35 }, "camera": { "averages_per_sec": 12 diff --git a/hypersomnia/default_config.json b/hypersomnia/default_config.json index 8aa9670f9..4f2373a1d 100644 --- a/hypersomnia/default_config.json +++ b/hypersomnia/default_config.json @@ -51,7 +51,7 @@ }, "audio": { - "mute_main_menu_background": true, + "mute_main_menu_background": false, "output_mode": "STEREO_HRTF", "output_device_name": "", "max_number_of_sound_sources": 4200, @@ -416,7 +416,8 @@ "gain_threshold_for_bullet_trace_sounds": 0.012, "max_short_sounds": 80, "processing_frequency": "EVERY_SINGLE_FRAME", - "custom_processing_frequency": 10 + "custom_processing_frequency": 10, + "max_audio_commands_per_frame_ms": 4.0 }, // self_update will be used by non-steam processes, diff --git a/src/application/gui/settings_gui.cpp b/src/application/gui/settings_gui.cpp index a917700c3..d68357dd5 100644 --- a/src/application/gui/settings_gui.cpp +++ b/src/application/gui/settings_gui.cpp @@ -846,7 +846,7 @@ void settings_gui_state::perform( revertable_enum("Output mode", config.audio.output_mode); - text_disabled("Set to Auto for speakers.\nFor headphones, HRTF is the best."); + text("Disable this for speakers!\nFor headphones, HRTF is the best."); { auto scope = scoped_indent(); @@ -865,7 +865,9 @@ void settings_gui_state::perform( #endif } +#if !PLATFORM_WEB text_disabled("If you experience a drop in sound quality with HRTF,\ntry setting the sample rate of your audio device to 44.1 kHz,\nor consider providing your own presets in detail/hrtf."); +#endif { @@ -894,6 +896,9 @@ void settings_gui_state::perform( { auto& scope_cfg = config.sound; +#if WEB_LOWEND + revertable_slider(SCOPE_CFG_NVP(max_audio_commands_per_frame_ms), 0.5f, 10.f); +#endif revertable_enum_radio(SCOPE_CFG_NVP(processing_frequency)); if (scope_cfg.processing_frequency == sound_processing_frequency::PERIODIC) { @@ -1895,16 +1900,15 @@ void settings_gui_state::perform( revertable_checkbox(SCOPE_CFG_NVP(measure_atlas_uploading)); } +#if !PLATFORM_WEB text("Content regeneration"); { auto indent = scoped_indent(); auto& scope_cfg = config.content_regeneration; - revertable_checkbox(SCOPE_CFG_NVP(regenerate_every_time)); revertable_checkbox(SCOPE_CFG_NVP(rescan_assets_on_window_focus)); -#if !PLATFORM_WEB const auto concurrency = std::thread::hardware_concurrency(); const auto t_max = concurrency * 2; @@ -1913,8 +1917,8 @@ void settings_gui_state::perform( revertable_slider(SCOPE_CFG_NVP(atlas_blitting_threads), 1u, t_max); revertable_slider(SCOPE_CFG_NVP(neon_regeneration_threads), 1u, t_max); -#endif } +#endif #if !PLATFORM_WEB ImGui::Separator(); diff --git a/src/augs/audio/audio_command_buffers.h b/src/augs/audio/audio_command_buffers.h index d743cee85..8a413ab3b 100644 --- a/src/augs/audio/audio_command_buffers.h +++ b/src/augs/audio/audio_command_buffers.h @@ -7,6 +7,7 @@ #include "augs/templates/thread_pool.h" #include "augs/audio/audio_command.h" #include "augs/audio/audio_backend.h" +#include "augs/log.h" #if WEB_SINGLETHREAD @@ -32,10 +33,34 @@ namespace augs { } int num_currently_processed_buffers() { - return 0; + if (buffer.empty()) { + return 0; + } + + return 1; } - auto submit_write_buffer() { + auto submit_write_buffer(const float max_processing_time_ms_v) { + augs::timer tm; + + std::size_t i = 0; + + for (; i < buffer.size(); ++i) { + backend.perform( + buffer.data() + i, + 1 + ); + + if (tm.get() >= max_processing_time_ms_v) { + // LOG("Sound overload (p: %x all: %x)", i, buffer.size()); + break; + } + } + + erase_first_n(buffer, i); + } + + void finish() { backend.perform( buffer.data(), buffer.size() @@ -44,10 +69,6 @@ namespace augs { buffer.clear(); } - void finish() { - - } - template void stop_sources_if(F&& pred) { backend.stop_sources_if(std::forward(pred)); diff --git a/src/make_canon_config.hpp b/src/make_canon_config.hpp index 856067f25..54541921e 100644 --- a/src/make_canon_config.hpp +++ b/src/make_canon_config.hpp @@ -95,12 +95,18 @@ inline void make_canon_config(config_json_table& result, bool is_dedicated_serve result.inventory_gui_controls.erase(augs::event::keys::key::V); result.sound.max_simultaneous_bullet_trace_sounds = 0; + result.content_regeneration.rescan_assets_on_window_focus = false; #endif #if WEB_LOWEND result.gui_fonts.gui.size_in_pixels = 18.0f; - result.audio.output_mode = audio_output_mode::AUTO; + /* + Should be fine as we're limiting the rate of audio commands + to avoid hiccups. + */ + + result.audio.output_mode = audio_output_mode::STEREO_HRTF; result.sound.max_short_sounds = 16; result.sound.processing_frequency = sound_processing_frequency::PERIODIC; diff --git a/src/view/audiovisual_state/audiovisual_state.cpp b/src/view/audiovisual_state/audiovisual_state.cpp index 68900ee93..0563492ff 100644 --- a/src/view/audiovisual_state/audiovisual_state.cpp +++ b/src/view/audiovisual_state/audiovisual_state.cpp @@ -391,7 +391,11 @@ void audiovisual_state::advance(const audiovisual_advance_input input) { ); if (input.audio_renderer != nullptr) { - input.command_buffers.submit_write_buffer(); + input.command_buffers.submit_write_buffer( +#if WEB_LOWEND + input.sound_settings.max_audio_commands_per_frame_ms +#endif + ); } }; diff --git a/src/view/audiovisual_state/systems/sound_system_settings.h b/src/view/audiovisual_state/systems/sound_system_settings.h index 76b1f9cca..5038476d9 100644 --- a/src/view/audiovisual_state/systems/sound_system_settings.h +++ b/src/view/audiovisual_state/systems/sound_system_settings.h @@ -38,6 +38,9 @@ struct sound_system_settings { sound_processing_frequency processing_frequency = sound_processing_frequency::EVERY_SIMULATION_STEP; int custom_processing_frequency = 10; +#if WEB_LOWEND + float max_audio_commands_per_frame_ms = 4.0f; +#endif // END GEN INTROSPECTOR bool operator==(const sound_system_settings& b) const = default; diff --git a/src/work.cpp b/src/work.cpp index 9cbefa40c..2c8f949d2 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -2040,7 +2040,7 @@ work_result work( #if PLATFORM_WEB const bool is_official_connect_string = ::is_official_webrtc_id(connect_string); -#if 1 +#if 0 /* Test */ (void)is_official_connect_string; const bool requires_sign_in = true;