From 3eb8564fd92ae2939275a40ea67c3475dadd431b Mon Sep 17 00:00:00 2001 From: Apaczer <94932128+Apaczer@users.noreply.github.com> Date: Mon, 10 Feb 2025 21:28:33 +0100 Subject: [PATCH 3/3] LIBRETRO: add "Framerate cap" option to reduce audio stutter via MM+ and: https://github.com/StupidHoroscope/libretro-scummvm-miyoo-backend/commit/6c3954f916bf430e7756afd8ca74618d48b56744 --- backends/platform/libretro/libretro.cpp | 32 +++++++++++++++++-- .../platform/libretro/libretro_core_options.h | 19 +++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/backends/platform/libretro/libretro.cpp b/backends/platform/libretro/libretro.cpp index 3a5bf84b4bc..1668182774b 100644 --- a/backends/platform/libretro/libretro.cpp +++ b/backends/platform/libretro/libretro.cpp @@ -55,6 +55,10 @@ static bool analog_response_is_quadratic = false; static float mouse_speed = 1.0f; static bool speed_hack_is_enabled = false; +static int cap_fps=50u; + +static size_t audio_buffer_length = 0; +static uint32* audio_buffer = NULL; char cmd_params[20][200]; char cmd_params_num; @@ -144,6 +148,11 @@ void retro_init (void) void retro_deinit(void) { + if (audio_buffer != NULL) + { + delete [] audio_buffer; + audio_buffer = NULL; + } } void parse_command_params(char* cmdline) @@ -258,6 +267,13 @@ static void update_variables(void) if (strcmp(var.value, "enabled") == 0) speed_hack_is_enabled = true; } + var.key = "scummvm_audio_cap_fps"; + var.value = NULL; + cap_fps = 50u; + if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value) + { + cap_fps = (int)atof(var.value); + } } static int retro_device = RETRO_DEVICE_JOYPAD; @@ -463,8 +479,18 @@ void retro_run (void) video_cb(screen.pixels, screen.w, screen.h, screen.pitch); /* Upload audio */ - static uint32 buf[735]; - int count = ((Audio::MixerImpl*)g_system->getMixer())->mixCallback((byte*)buf, 735*4); + // HACK: Reduce audio buffer under-run by capping framerate to lower value. + int samples_per_frame = 44100u / (cap_fps); + if (audio_buffer == NULL || samples_per_frame != audio_buffer_length) + { + if (audio_buffer != NULL) + { + delete [] audio_buffer; + } + audio_buffer_length = samples_per_frame; + audio_buffer = new uint32[audio_buffer_length]; + } + int count = ((Audio::MixerImpl*)g_system->getMixer())->mixCallback((byte*)audio_buffer, audio_buffer_length*4); #if defined(_3DS) /* Hack: 3DS will produce static noise * unless we manually send a zeroed @@ -478,7 +504,7 @@ void retro_run (void) } else #endif - audio_batch_cb((int16_t*)buf, count); + audio_batch_cb((int16_t*)audio_buffer, count); } #if defined(USE_LIBCO) diff --git a/backends/platform/libretro/libretro_core_options.h b/backends/platform/libretro/libretro_core_options.h index 0fbd01e86d8..a32c0e37529 100644 --- a/backends/platform/libretro/libretro_core_options.h +++ b/backends/platform/libretro/libretro_core_options.h @@ -138,6 +138,25 @@ struct retro_core_option_definition option_defs_us[] = { "disabled" #endif }, + { + "scummvm_audio_cap_fps", + "Framerate cap", + "Lock max framerate on resource-intensive games, to reduce audio crackling", + { + { "15", NULL }, + { "20", NULL }, + { "25", NULL }, + { "30", NULL }, + { "35", NULL }, + { "40", NULL }, + { "45", NULL }, + { "50", NULL }, + { "55", NULL }, + { "60", NULL }, + { NULL, NULL }, + }, + "50" + }, { NULL, NULL, NULL, {{0}}, NULL }, }; -- 2.45.2.windows.1