From ca0203a1ccaa9aa84bb3f74fa0548924438a1a40 Mon Sep 17 00:00:00 2001 From: magumagu Date: Thu, 26 Jun 2014 16:15:18 -0700 Subject: [PATCH] AudioCommon: restore support for DTK volume setting. --- Source/Core/AudioCommon/Mixer.cpp | 20 +++++++++++++++++++- Source/Core/AudioCommon/Mixer.h | 7 +++++++ Source/Core/Core/HW/AudioInterface.cpp | 7 ++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp index ac8a78ae7e..388eb56b0d 100644 --- a/Source/Core/AudioCommon/Mixer.cpp +++ b/Source/Core/AudioCommon/Mixer.cpp @@ -54,6 +54,9 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo static u32 frac = 0; const u32 ratio = (u32)( 65536.0f * aid_sample_rate / (float)m_mixer->m_sampleRate ); + s32 lvolume = m_LVolume; + s32 rvolume = m_RVolume; + // TODO: consider a higher-quality resampling algorithm. for (; currentSample < numSamples*2 && ((indexW-indexR) & INDEX_MASK) > 2; currentSample+=2) { u32 indexR2 = indexR + 2; //next sample @@ -61,6 +64,7 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo s16 l1 = Common::swap16(m_buffer[indexR & INDEX_MASK]); //current s16 l2 = Common::swap16(m_buffer[indexR2 & INDEX_MASK]); //next int sampleL = ((l1 << 16) + (l2 - l1) * (u16)frac) >> 16; + sampleL = (sampleL * lvolume) >> 8; sampleL += samples[currentSample + 1]; MathUtil::Clamp(&sampleL, -32767, 32767); samples[currentSample+1] = sampleL; @@ -68,6 +72,7 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo s16 r1 = Common::swap16(m_buffer[(indexR + 1) & INDEX_MASK]); //current s16 r2 = Common::swap16(m_buffer[(indexR2 + 1) & INDEX_MASK]); //next int sampleR = ((r1 << 16) + (r2 - r1) * (u16)frac) >> 16; + sampleR = (sampleR * rvolume) >> 8; sampleR += samples[currentSample]; MathUtil::Clamp(&sampleR, -32767, 32767); samples[currentSample] = sampleR; @@ -81,7 +86,9 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo short s[2]; s[0] = Common::swap16(m_buffer[(indexR - 1) & INDEX_MASK]); s[1] = Common::swap16(m_buffer[(indexR - 2) & INDEX_MASK]); - for (; currentSample < numSamples*2; currentSample+=2) + s[0] = (s[0] * lvolume) >> 8; + s[1] = (s[1] * rvolume) >> 8; + for (; currentSample < numSamples * 2; currentSample += 2) { int sampleR = s[0] + samples[currentSample]; MathUtil::Clamp(&sampleR, -32767, 32767); @@ -174,3 +181,14 @@ void CMixer::PushStreamingSamples(const short *samples, unsigned int num_samples { m_streaming_mixer.PushSamples(samples, num_samples); } + +void CMixer::SetStreamingVolume(unsigned int lvolume, unsigned int rvolume) +{ + m_streaming_mixer.SetVolume(lvolume, rvolume); +} + +void CMixer::MixerFifo::SetVolume(unsigned int lvolume, unsigned int rvolume) +{ + m_LVolume = lvolume + (lvolume >> 7); + m_RVolume = rvolume + (rvolume >> 7); +} diff --git a/Source/Core/AudioCommon/Mixer.h b/Source/Core/AudioCommon/Mixer.h index a796633205..6400cf8da4 100644 --- a/Source/Core/AudioCommon/Mixer.h +++ b/Source/Core/AudioCommon/Mixer.h @@ -41,6 +41,7 @@ public: virtual void PushSamples(const short* samples, unsigned int num_samples); virtual void PushStreamingSamples(const short* samples, unsigned int num_samples); unsigned int GetSampleRate() const { return m_sampleRate; } + void SetStreamingVolume(unsigned int lvolume, unsigned int rvolume); void SetThrottle(bool use) { m_throttle = use;} @@ -88,17 +89,23 @@ protected: , m_indexW(0) , m_indexR(0) , m_numLeftI(0.0f) + , m_LVolume(256) + , m_RVolume(256) { memset(m_buffer, 0, sizeof(m_buffer)); } void PushSamples(const short* samples, unsigned int num_samples); unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true); + void SetVolume(unsigned int lvolume, unsigned int rvolume); private: CMixer *m_mixer; unsigned m_input_sample_rate; short m_buffer[MAX_SAMPLES * 2]; volatile u32 m_indexW; volatile u32 m_indexR; + // Volume ranges from 0-256 + volatile s32 m_LVolume; + volatile s32 m_RVolume; float m_numLeftI; }; MixerFifo m_dma_mixer; diff --git a/Source/Core/Core/HW/AudioInterface.cpp b/Source/Core/Core/HW/AudioInterface.cpp index 519b84d1f0..2b9746dbc8 100644 --- a/Source/Core/Core/HW/AudioInterface.cpp +++ b/Source/Core/Core/HW/AudioInterface.cpp @@ -50,6 +50,8 @@ This file mainly deals with the [Drive I/F], however [AIDFR] controls TODO maybe the files should be merged? */ +#include "AudioCommon/AudioCommon.h" + #include "Common/Common.h" #include "Common/MathUtil.h" @@ -232,7 +234,10 @@ void RegisterMMIO(MMIO::Mapping* mmio, u32 base) mmio->Register(base | AI_VOLUME_REGISTER, MMIO::DirectRead(&m_Volume.hex), - MMIO::DirectWrite(&m_Volume.hex) + MMIO::ComplexWrite([](u32, u32 val) { + m_Volume.hex = val; + soundStream->GetMixer()->SetStreamingVolume(m_Volume.left, m_Volume.right); + }) ); mmio->Register(base | AI_SAMPLE_COUNTER,