From 83480765f130753694122fb6aabeb05b90fd7c9e Mon Sep 17 00:00:00 2001 From: Brandon Wright Date: Thu, 7 Jun 2018 15:49:41 -0500 Subject: [PATCH] Sanity check input rate better. --- apu/apu.cpp | 2 +- gtk/src/gtk_s9xwindow.cpp | 21 ++++++++++++++++----- gtk/src/gtk_sound.cpp | 2 +- snes9x.cpp | 8 +++++--- unix/unix.cpp | 2 +- win32/wconfig.cpp | 4 ++-- win32/win32_display.cpp | 21 ++++++++++++++++----- win32/win32_sound.cpp | 6 +++++- 8 files changed, 47 insertions(+), 19 deletions(-) diff --git a/apu/apu.cpp b/apu/apu.cpp index 5f10a2aa..50dec00f 100644 --- a/apu/apu.cpp +++ b/apu/apu.cpp @@ -199,7 +199,7 @@ #include "snes/snes.hpp" -#define APU_DEFAULT_INPUT_RATE 31987 // 60hz +#define APU_DEFAULT_INPUT_RATE 31950 // ~ 59.94Hz #define APU_MINIMUM_SAMPLE_COUNT 512 #define APU_MINIMUM_SAMPLE_BLOCK 128 #define APU_NUMERATOR_NTSC 15664 diff --git a/gtk/src/gtk_s9xwindow.cpp b/gtk/src/gtk_s9xwindow.cpp index 88494801..facab641 100644 --- a/gtk/src/gtk_s9xwindow.cpp +++ b/gtk/src/gtk_s9xwindow.cpp @@ -1620,12 +1620,23 @@ Snes9xWindow::get_refresh_rate (void) int Snes9xWindow::get_auto_input_rate (void) { - double new_input_rate = get_refresh_rate () * 32040.0 / 60.09881389744051 + 0.5; + double refresh_rate = get_refresh_rate (); - if (new_input_rate > 32040.0 * 1.05) - new_input_rate = 32040.0; - if (new_input_rate < 32040.0 * 0.95) - new_input_rate = 32040.0 * 0.95; + if (refresh_rate == 0.0) + return 0; + + // Try for a close multiple of 60hz + if (refresh_rate > 119.0 && refresh_rate < 121.0) + refresh_rate /= 2.0; + if (refresh_rate > 179.0 && refresh_rate < 181.0) + refresh_rate /= 3.0; + if (refresh_rate > 239.0 && refresh_rate < 241.0) + refresh_rate /= 4.0; + + double new_input_rate = refresh_rate * 32040.0 / 60.09881389744051 + 0.5; + + if (new_input_rate > 32040.0 * 1.05 || new_input_rate < 32040.0 * 0.95) + new_input_rate = 0.0; return new_input_rate; } diff --git a/gtk/src/gtk_sound.cpp b/gtk/src/gtk_sound.cpp index 55078483..ee33e3d1 100644 --- a/gtk/src/gtk_sound.cpp +++ b/gtk/src/gtk_sound.cpp @@ -133,7 +133,7 @@ S9xPortSoundInit (void) } else { - Settings.SoundInputRate = CLAMP (gui_config->sound_input_rate, 8000, 48000); + Settings.SoundInputRate = CLAMP (gui_config->sound_input_rate, 31700, 32300); } Settings.SoundPlaybackRate = playback_rates[gui_config->sound_playback_rate]; diff --git a/snes9x.cpp b/snes9x.cpp index 2826985d..78947e44 100644 --- a/snes9x.cpp +++ b/snes9x.cpp @@ -424,7 +424,7 @@ void S9xLoadConfigFiles (char **argv, int argc) Settings.Stereo = conf.GetBool("Sound::Stereo", true); Settings.ReverseStereo = conf.GetBool("Sound::ReverseStereo", false); Settings.SoundPlaybackRate = conf.GetUInt("Sound::Rate", 32000); - Settings.SoundInputRate = conf.GetUInt("Sound::InputRate", 32000); + Settings.SoundInputRate = conf.GetUInt("Sound::InputRate", 31950); Settings.Mute = conf.GetBool("Sound::Mute", false); Settings.DynamicRateControl = conf.GetBool("Sound::DynamicRateControl", false); Settings.DynamicRateLimit = conf.GetInt ("Sound::DynamicRateLimit", 5); @@ -692,8 +692,10 @@ char * S9xParseArgs (char **argv, int argc) if (i + 1 < argc) { Settings.SoundInputRate = atoi(argv[++i]); - if (Settings.SoundInputRate < 8192) - Settings.SoundInputRate = 8192; + if (Settings.SoundInputRate < 31700) + Settings.SoundInputRate = 31700; + if (Settings.SoundInputRate > 32300) + Settings.SoundInputRate = 32300; } else S9xUsage(); diff --git a/unix/unix.cpp b/unix/unix.cpp index 6f4b26fb..c0c79596 100644 --- a/unix/unix.cpp +++ b/unix/unix.cpp @@ -1671,7 +1671,7 @@ int main (int argc, char **argv) Settings.SixteenBitSound = TRUE; Settings.Stereo = TRUE; Settings.SoundPlaybackRate = 32000; - Settings.SoundInputRate = 32000; + Settings.SoundInputRate = 31950; Settings.SupportHiRes = TRUE; Settings.Transparency = TRUE; Settings.AutoDisplayMessages = TRUE; diff --git a/win32/wconfig.cpp b/win32/wconfig.cpp index ac18b801..5415bc97 100644 --- a/win32/wconfig.cpp +++ b/win32/wconfig.cpp @@ -982,8 +982,8 @@ void WinRegisterConfigItems() AddIntC("Sync", Settings.SoundSync, 1, "1 to sync emulation to sound output, 0 to disable."); AddBool2("Stereo", Settings.Stereo, true); AddBool("SixteenBitSound", Settings.SixteenBitSound, true); - AddUIntC("Rate", Settings.SoundPlaybackRate, 32000, "sound playback quality, in Hz"); - AddUIntC("InputRate", Settings.SoundInputRate, 31900, "for each 'Input rate' samples generated by the SNES, 'Playback rate' samples will produced. If you experience crackling you can try to lower this setting."); + AddUIntC("Rate", Settings.SoundPlaybackRate, 44100, "sound playback quality, in Hz"); + AddUIntC("InputRate", Settings.SoundInputRate, 31950, "for each 'Input rate' samples generated by the SNES, 'Playback rate' samples will produced. If you experience crackling you can try to lower this setting."); AddBoolC("ReverseStereo", Settings.ReverseStereo, false, "true to swap speaker outputs"); AddBoolC("Mute", GUI.Mute, false, "true to mute sound output (does not disable the sound CPU)"); AddBool("DynamicRateControl", Settings.DynamicRateControl, false); diff --git a/win32/win32_display.cpp b/win32/win32_display.cpp index 0c9339a9..c15d7541 100644 --- a/win32/win32_display.cpp +++ b/win32/win32_display.cpp @@ -752,12 +752,23 @@ double WinGetRefreshRate(void) int WinGetAutomaticInputRate(void) { - double newInputRate = WinGetRefreshRate() * 32040.0 / 60.09881389744051 + 0.5; + double refreshRate = WinGetRefreshRate(); - if (newInputRate > 32040.0 * 1.05) - newInputRate = 32040.0; - if (newInputRate < 32040.0 * 0.95) - newInputRate = 32040.0 * 0.95; + if (refreshRate == 0.0) + return 0; + + // Try for a close multiple of 60hz + if (refreshRate > 119.0 && refreshRate < 121.0) + refreshRate /= 2.0; + if (refreshRate > 179.0 && refreshRate < 181.0) + refreshRate /= 3.0; + if (refreshRate > 239.0 && refreshRate < 241.0) + refreshRate /= 4.0; + + double newInputRate = refreshRate * 32040.0 / 60.09881389744051 + 0.5; + + if (newInputRate > 32040.0 * 1.05 || newInputRate < 32040.0 * 0.95) + newInputRate = 0.0; return newInputRate; } diff --git a/win32/win32_sound.cpp b/win32/win32_sound.cpp index e36e3e6b..87ab1a1f 100644 --- a/win32/win32_sound.cpp +++ b/win32/win32_sound.cpp @@ -224,9 +224,13 @@ bool ReInitSound() if (rate) Settings.SoundInputRate = rate; else + { GUI.AutomaticInputRate = false; + Settings.SoundInputRate = 31950; + } } - Settings.SoundInputRate = CLAMP(Settings.SoundInputRate,8000, 48000); + + Settings.SoundInputRate = CLAMP(Settings.SoundInputRate,31700, 32300); Settings.SoundPlaybackRate = CLAMP(Settings.SoundPlaybackRate,8000, 48000); S9xSetSoundMute(GUI.Mute); if(S9xSoundOutput)