diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp index 5d1207bbe3..f35a671094 100644 --- a/Source/Core/AudioCommon/Mixer.cpp +++ b/Source/Core/AudioCommon/Mixer.cpp @@ -6,11 +6,11 @@ #include "AudioCommon/AudioCommon.h" #include "AudioCommon/Mixer.h" -#include "Common/CPUDetect.h" +#include "Common/CommonFuncs.h" +#include "Common/CommonTypes.h" #include "Common/MathUtil.h" +#include "Common/Logging/Log.h" #include "Core/ConfigManager.h" -#include "Core/Core.h" -#include "Core/HW/AudioInterface.h" // UGLINESS #include "Core/PowerPC/PowerPC.h" @@ -20,17 +20,15 @@ #endif CMixer::CMixer(unsigned int BackendSampleRate) - : m_dma_mixer(this, 32000) - , m_streaming_mixer(this, 48000) - , m_wiimote_speaker_mixer(this, 3000) - , m_sampleRate(BackendSampleRate) - , m_log_dtk_audio(false) - , m_log_dsp_audio(false) - , m_speed(0) + : m_sampleRate(BackendSampleRate) { INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized"); } +CMixer::~CMixer() +{ +} + // Executed from sound stream thread unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, bool consider_framelimit) { @@ -46,7 +44,7 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, boo u32 indexR = m_indexR.load(); u32 indexW = m_indexW.load(); - int low_waterwark = m_input_sample_rate * SConfig::GetInstance().iTimingVariance / 1000; + u32 low_waterwark = m_input_sample_rate * SConfig::GetInstance().iTimingVariance / 1000; low_waterwark = std::min(low_waterwark, MAX_SAMPLES / 2); float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2); diff --git a/Source/Core/AudioCommon/Mixer.h b/Source/Core/AudioCommon/Mixer.h index 4ced71f360..008e059e44 100644 --- a/Source/Core/AudioCommon/Mixer.h +++ b/Source/Core/AudioCommon/Mixer.h @@ -4,33 +4,25 @@ #pragma once +#include #include -#include -#include #include "AudioCommon/WaveFile.h" +#include "Common/CommonTypes.h" -// 16 bit Stereo -#define MAX_SAMPLES (1024 * 4) // 128 ms -#define INDEX_MASK (MAX_SAMPLES * 2 - 1) - -#define MAX_FREQ_SHIFT 200 // per 32000 Hz -#define CONTROL_FACTOR 0.2f // in freq_shift per fifo size offset -#define CONTROL_AVG 32 - -class CMixer +class CMixer final { public: - CMixer(unsigned int BackendSampleRate); - virtual ~CMixer() {} + explicit CMixer(unsigned int BackendSampleRate); + ~CMixer(); // Called from audio threads - virtual unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true); + unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true); // Called from main thread - virtual void PushSamples(const short* samples, unsigned int num_samples); - virtual void PushStreamingSamples(const short* samples, unsigned int num_samples); - virtual void PushWiimoteSpeakerSamples(const short* samples, unsigned int num_samples, unsigned int sample_rate); + void PushSamples(const short* samples, unsigned int num_samples); + void PushStreamingSamples(const short* samples, unsigned int num_samples); + void PushWiimoteSpeakerSamples(const short* samples, unsigned int num_samples, unsigned int sample_rate); unsigned int GetSampleRate() const { return m_sampleRate; } void SetDMAInputSampleRate(unsigned int rate); @@ -47,47 +39,48 @@ public: float GetCurrentSpeed() const { return m_speed.load(); } void UpdateSpeed(float val) { m_speed.store(val); } -protected: - class MixerFifo { +private: + static constexpr u32 MAX_SAMPLES = 1024 * 4; // 128 ms + static constexpr u32 INDEX_MASK = MAX_SAMPLES * 2 - 1; + static constexpr int MAX_FREQ_SHIFT = 200; // Per 32000 Hz + static constexpr float CONTROL_FACTOR = 0.2f; + static constexpr u32 CONTROL_AVG = 32; // In freq_shift per FIFO size offset + + class MixerFifo final + { public: - MixerFifo(CMixer *mixer, unsigned sample_rate) + MixerFifo(CMixer* mixer, unsigned sample_rate) : m_mixer(mixer) , m_input_sample_rate(sample_rate) - , m_indexW(0) - , m_indexR(0) - , m_LVolume(256) - , m_RVolume(256) - , m_numLeftI(0.0f) - , m_frac(0) { - 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 SetInputSampleRate(unsigned int rate); void SetVolume(unsigned int lvolume, unsigned int rvolume); private: - CMixer *m_mixer; + CMixer* m_mixer; unsigned m_input_sample_rate; - short m_buffer[MAX_SAMPLES * 2]; - std::atomic m_indexW; - std::atomic m_indexR; + std::array m_buffer{}; + std::atomic m_indexW{0}; + std::atomic m_indexR{0}; // Volume ranges from 0-256 - std::atomic m_LVolume; - std::atomic m_RVolume; - float m_numLeftI; - u32 m_frac; + std::atomic m_LVolume{256}; + std::atomic m_RVolume{256}; + float m_numLeftI = 0.0f; + u32 m_frac = 0; }; - MixerFifo m_dma_mixer; - MixerFifo m_streaming_mixer; - MixerFifo m_wiimote_speaker_mixer; + MixerFifo m_dma_mixer{this, 32000}; + MixerFifo m_streaming_mixer{this, 48000}; + MixerFifo m_wiimote_speaker_mixer{this, 3000}; unsigned int m_sampleRate; WaveFileWriter m_wave_writer_dtk; WaveFileWriter m_wave_writer_dsp; - bool m_log_dtk_audio; - bool m_log_dsp_audio; + bool m_log_dtk_audio = false; + bool m_log_dsp_audio = false; - std::atomic m_speed; // Current rate of the emulation (1.0 = 100% speed) + // Current rate of emulation (1.0 = 100% speed) + std::atomic m_speed{0.0f}; };