From e93a2bc14ebe4527c7cdaa01aec18d42077f31a5 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Wed, 22 May 2024 10:31:11 +0200 Subject: [PATCH] add config vars for audio thread runahead and sleep --- src/pc/configfile.c | 4 ++++ src/pc/configfile.h | 2 ++ src/pc/pc_main.c | 5 +++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pc/configfile.c b/src/pc/configfile.c index 7411d4f8..a342c09d 100644 --- a/src/pc/configfile.c +++ b/src/pc/configfile.c @@ -54,6 +54,8 @@ unsigned int configMasterVolume = MAX_VOLUME; // 0 - MAX_VOLUME unsigned int configMusicVolume = MAX_VOLUME; unsigned int configSfxVolume = MAX_VOLUME; unsigned int configEnvVolume = MAX_VOLUME; +float configAudioRunahead = 0.f; +float configAudioSleep = 1.f; // Keyboard mappings (VK_ values, by default keyboard/gamepad/mouse) unsigned int configKeyA[MAX_BINDS] = { 0x0026, 0x1000, 0x1103 }; @@ -106,6 +108,8 @@ static const struct ConfigOption options[] = { {.name = "music_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configMusicVolume}, {.name = "sfx_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configSfxVolume}, {.name = "env_volume", .type = CONFIG_TYPE_UINT, .uintValue = &configEnvVolume}, + {.name = "audio_sleep", .type = CONFIG_TYPE_FLOAT, .floatValue = &configAudioSleep}, + {.name = "audio_runahead", .type = CONFIG_TYPE_FLOAT, .floatValue = &configAudioRunahead}, {.name = "key_a", .type = CONFIG_TYPE_BIND, .uintValue = configKeyA}, {.name = "key_b", .type = CONFIG_TYPE_BIND, .uintValue = configKeyB}, {.name = "key_start", .type = CONFIG_TYPE_BIND, .uintValue = configKeyStart}, diff --git a/src/pc/configfile.h b/src/pc/configfile.h index b92ae7be..d053cbb0 100644 --- a/src/pc/configfile.h +++ b/src/pc/configfile.h @@ -23,6 +23,8 @@ extern unsigned int configMasterVolume; extern unsigned int configMusicVolume; extern unsigned int configSfxVolume; extern unsigned int configEnvVolume; +extern float configAudioRunahead; +extern float configAudioSleep; extern unsigned int configKeyA[]; extern unsigned int configKeyB[]; extern unsigned int configKeyStart[]; diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 98ddf78b..82dd954f 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -163,7 +163,8 @@ void* audio_thread() { set_sequence_player_volume(SEQ_PLAYER_SFX, (f32)configSfxVolume / 127.0f * master_mod); set_sequence_player_volume(SEQ_PLAYER_ENV, (f32)configEnvVolume / 127.0f * master_mod); - int samples_left = audio_api->buffered(); + int samples_left = audio_api->buffered() - (audio_api->get_desired_buffered() * configAudioRunahead); + if (samples_left < 0) samples_left = 0; u32 num_audio_samples = samples_left < audio_api->get_desired_buffered() ? SAMPLES_HIGH : SAMPLES_LOW; // printf("Audio samples: %d %u\n", samples_left, num_audio_samples); s16 audio_buffer[SAMPLES_HIGH * 2]; @@ -191,7 +192,7 @@ void* audio_thread() { end_time = sys_profile_time(); // Sleep for the remaining time - f64 nap_time = frametime_micro - (end_time - start_time); + f64 nap_time = (frametime_micro - (end_time - start_time)) * configAudioSleep; // printf("Audio thread nap time: %f\n", nap_time); if (nap_time > 0.0) sys_sleep(nap_time);