From 24c78ed2f4c1a1e6cc21cb031ea56b24ba9516f8 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 31 Jan 2015 12:12:01 +0100 Subject: [PATCH] Add option to turn off the synchronizing audio resampler. Only available if output sample rate is 44100Hz. --- Core/Config.cpp | 1 + Core/Config.h | 1 + Core/HW/StereoResampler.cpp | 70 ++++++++++++++++++++++--------------- UI/GameSettingsScreen.cpp | 5 +++ 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 6b3241fa3f..235533a940 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -484,6 +484,7 @@ static ConfigSetting soundSettings[] = { ConfigSetting("AudioBackend", &g_Config.iAudioBackend, 0, true, true), ConfigSetting("AudioLatency", &g_Config.iAudioLatency, 1, true, true), ConfigSetting("SoundSpeedHack", &g_Config.bSoundSpeedHack, false, true, true), + ConfigSetting("AudioResampler", &g_Config.bAudioResampler, true, true, true), ConfigSetting(false), }; diff --git a/Core/Config.h b/Core/Config.h index 107567a821..33274e1636 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -209,6 +209,7 @@ public: bool bShowDebugStats; bool bShowAudioDebug; + bool bAudioResampler; //Analog stick tilting //the base x and y tilt. this inclination is treated as (0,0) and the tilt input diff --git a/Core/HW/StereoResampler.cpp b/Core/HW/StereoResampler.cpp index 5a9cd33312..44bc66b806 100644 --- a/Core/HW/StereoResampler.cpp +++ b/Core/HW/StereoResampler.cpp @@ -24,6 +24,7 @@ #include "Common/ChunkFile.h" #include "Common/MathUtil.h" #include "Common/Atomics.h" +#include "Core/Config.h" #include "Core/HW/StereoResampler.h" #include "Core/HLE/__sceAudio.h" #include "Core/System.h" @@ -89,38 +90,51 @@ unsigned int StereoResampler::MixerFifo::Mix(short* samples, unsigned int numSam u32 indexR = Common::AtomicLoad(m_indexR); u32 indexW = Common::AtomicLoad(m_indexW); - // Drift prevention mechanism - float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2); - m_numLeftI = (numLeft + m_numLeftI*(CONTROL_AVG - 1)) / CONTROL_AVG; - float offset = (m_numLeftI - LOW_WATERMARK) * CONTROL_FACTOR; - if (offset > MAX_FREQ_SHIFT) offset = MAX_FREQ_SHIFT; - if (offset < -MAX_FREQ_SHIFT) offset = -MAX_FREQ_SHIFT; + // We force on the audio resampler if the output sample rate doesn't match the input. + if (!g_Config.bAudioResampler && sample_rate == m_input_sample_rate) { + for (; currentSample < numSamples * 2 && ((indexW - indexR) & INDEX_MASK) > 2; currentSample += 2) { + u32 indexR2 = indexR + 2; //next sample + s16 l1 = m_buffer[indexR & INDEX_MASK]; //current + s16 r1 = m_buffer[(indexR + 1) & INDEX_MASK]; //current + samples[currentSample] = l1; + samples[currentSample + 1] = r1; + indexR += 2; + } + aid_sample_rate_ = sample_rate; + } else { + // Drift prevention mechanism + float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2); + m_numLeftI = (numLeft + m_numLeftI*(CONTROL_AVG - 1)) / CONTROL_AVG; + float offset = (m_numLeftI - LOW_WATERMARK) * CONTROL_FACTOR; + if (offset > MAX_FREQ_SHIFT) offset = MAX_FREQ_SHIFT; + if (offset < -MAX_FREQ_SHIFT) offset = -MAX_FREQ_SHIFT; - aid_sample_rate_ = m_input_sample_rate + offset; + aid_sample_rate_ = m_input_sample_rate + offset; - /* Hm? - u32 framelimit = SConfig::GetInstance().m_Framelimit; - if (consider_framelimit && framelimit > 1) { - aid_sample_rate = aid_sample_rate * (framelimit - 1) * 5 / 59.994; - }*/ + /* Hm? + u32 framelimit = SConfig::GetInstance().m_Framelimit; + if (consider_framelimit && framelimit > 1) { + aid_sample_rate = aid_sample_rate * (framelimit - 1) * 5 / 59.994; + }*/ - const u32 ratio = (u32)(65536.0f * aid_sample_rate_ / (float)sample_rate); + const u32 ratio = (u32)(65536.0f * aid_sample_rate_ / (float)sample_rate); - // TODO: consider a higher-quality resampling algorithm. - // TODO: Add a fast path for 1:1. - for (; currentSample < numSamples * 2 && ((indexW - indexR) & INDEX_MASK) > 2; currentSample += 2) { - u32 indexR2 = indexR + 2; //next sample - s16 l1 = m_buffer[indexR & INDEX_MASK]; //current - s16 r1 = m_buffer[(indexR + 1) & INDEX_MASK]; //current - s16 l2 = m_buffer[indexR2 & INDEX_MASK]; //next - s16 r2 = m_buffer[(indexR2 + 1) & INDEX_MASK]; //next - int sampleL = ((l1 << 16) + (l2 - l1) * (u16)m_frac) >> 16; - int sampleR = ((r1 << 16) + (r2 - r1) * (u16)m_frac) >> 16; - samples[currentSample] = sampleL; - samples[currentSample + 1] = sampleR; - m_frac += ratio; - indexR += 2 * (u16)(m_frac >> 16); - m_frac &= 0xffff; + // TODO: consider a higher-quality resampling algorithm. + // TODO: Add a fast path for 1:1. + for (; currentSample < numSamples * 2 && ((indexW - indexR) & INDEX_MASK) > 2; currentSample += 2) { + u32 indexR2 = indexR + 2; //next sample + s16 l1 = m_buffer[indexR & INDEX_MASK]; //current + s16 r1 = m_buffer[(indexR + 1) & INDEX_MASK]; //current + s16 l2 = m_buffer[indexR2 & INDEX_MASK]; //next + s16 r2 = m_buffer[(indexR2 + 1) & INDEX_MASK]; //next + int sampleL = ((l1 << 16) + (l2 - l1) * (u16)m_frac) >> 16; + int sampleR = ((r1 << 16) + (r2 - r1) * (u16)m_frac) >> 16; + samples[currentSample] = sampleL; + samples[currentSample + 1] = sampleR; + m_frac += ratio; + indexR += 2 * (u16)(m_frac >> 16); + m_frac &= 0xffff; + } } int realSamples = currentSample; diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 9a49d06252..2220b1a1fe 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -348,7 +348,12 @@ void GameSettingsScreen::CreateViews() { static const char *latency[] = { "Low", "Medium", "High" }; PopupMultiChoice *lowAudio = audioSettings->Add(new PopupMultiChoice(&g_Config.iAudioLatency, a->T("Audio Latency"), latency, 0, ARRAY_SIZE(latency), gs, screenManager())); + lowAudio->SetEnabledPtr(&g_Config.bEnableSound); + if (System_GetPropertyInt(SYSPROP_AUDIO_SAMPLE_RATE) == 44100) { + CheckBox *resampling = audioSettings->Add(new CheckBox(&g_Config.bAudioResampler, a->T("Audio sync", "Audio sync (using resampling)"))); + resampling->SetEnabledPtr(&g_Config.bEnableSound); + } audioSettings->Add(new ItemHeader(a->T("Audio hacks"))); audioSettings->Add(new CheckBox(&g_Config.bSoundSpeedHack, a->T("Sound speed hack (DOA etc.)")));