Skip to content

Commit

Permalink
CrazyGames: limit audio command rate up to 4 ms. (configurable)
Browse files Browse the repository at this point in the history
  • Loading branch information
geneotech committed Jul 25, 2024
1 parent 45c6ebf commit 2883af8
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 23 deletions.
16 changes: 9 additions & 7 deletions cmake/web/assets/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion hypersomnia/content/menu/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
},
"audio_volume": {
"music": 0.15
"music": 0.35
},
"camera": {
"averages_per_sec": 12
Expand Down
5 changes: 3 additions & 2 deletions hypersomnia/default_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions src/application/gui/settings_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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

{

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -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();
Expand Down
33 changes: 27 additions & 6 deletions src/augs/audio/audio_command_buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<std::chrono::milliseconds>() >= 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()
Expand All @@ -44,10 +69,6 @@ namespace augs {
buffer.clear();
}

void finish() {

}

template <class F>
void stop_sources_if(F&& pred) {
backend.stop_sources_if(std::forward<F>(pred));
Expand Down
8 changes: 7 additions & 1 deletion src/make_canon_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/view/audiovisual_state/audiovisual_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/view/audiovisual_state/systems/sound_system_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/work.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2883af8

Please sign in to comment.