Merge pull request #3514 from lioncash/mixer

Mixer: Minor changes
This commit is contained in:
Mathew Maidment 2016-01-27 19:19:42 -05:00
commit 437b3db88e
2 changed files with 43 additions and 52 deletions

View File

@ -6,11 +6,11 @@
#include "AudioCommon/AudioCommon.h" #include "AudioCommon/AudioCommon.h"
#include "AudioCommon/Mixer.h" #include "AudioCommon/Mixer.h"
#include "Common/CPUDetect.h" #include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/MathUtil.h" #include "Common/MathUtil.h"
#include "Common/Logging/Log.h"
#include "Core/ConfigManager.h" #include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/HW/AudioInterface.h"
// UGLINESS // UGLINESS
#include "Core/PowerPC/PowerPC.h" #include "Core/PowerPC/PowerPC.h"
@ -20,17 +20,15 @@
#endif #endif
CMixer::CMixer(unsigned int BackendSampleRate) CMixer::CMixer(unsigned int BackendSampleRate)
: m_dma_mixer(this, 32000) : m_sampleRate(BackendSampleRate)
, 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)
{ {
INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized"); INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized");
} }
CMixer::~CMixer()
{
}
// Executed from sound stream thread // Executed from sound stream thread
unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples, bool consider_framelimit) 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 indexR = m_indexR.load();
u32 indexW = m_indexW.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); low_waterwark = std::min(low_waterwark, MAX_SAMPLES / 2);
float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2); float numLeft = (float)(((indexW - indexR) & INDEX_MASK) / 2);

View File

@ -4,33 +4,25 @@
#pragma once #pragma once
#include <array>
#include <atomic> #include <atomic>
#include <cstring>
#include <mutex>
#include "AudioCommon/WaveFile.h" #include "AudioCommon/WaveFile.h"
#include "Common/CommonTypes.h"
// 16 bit Stereo class CMixer final
#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
{ {
public: public:
CMixer(unsigned int BackendSampleRate); explicit CMixer(unsigned int BackendSampleRate);
virtual ~CMixer() {} ~CMixer();
// Called from audio threads // 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 // Called from main thread
virtual void PushSamples(const short* samples, unsigned int num_samples); void PushSamples(const short* samples, unsigned int num_samples);
virtual void PushStreamingSamples(const short* samples, unsigned int num_samples); void PushStreamingSamples(const short* samples, unsigned int num_samples);
virtual void PushWiimoteSpeakerSamples(const short* samples, unsigned int num_samples, unsigned int sample_rate); void PushWiimoteSpeakerSamples(const short* samples, unsigned int num_samples, unsigned int sample_rate);
unsigned int GetSampleRate() const { return m_sampleRate; } unsigned int GetSampleRate() const { return m_sampleRate; }
void SetDMAInputSampleRate(unsigned int rate); void SetDMAInputSampleRate(unsigned int rate);
@ -47,47 +39,48 @@ public:
float GetCurrentSpeed() const { return m_speed.load(); } float GetCurrentSpeed() const { return m_speed.load(); }
void UpdateSpeed(float val) { m_speed.store(val); } void UpdateSpeed(float val) { m_speed.store(val); }
protected: private:
class MixerFifo { 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: public:
MixerFifo(CMixer *mixer, unsigned sample_rate) MixerFifo(CMixer* mixer, unsigned sample_rate)
: m_mixer(mixer) : m_mixer(mixer)
, m_input_sample_rate(sample_rate) , 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); void PushSamples(const short* samples, unsigned int num_samples);
unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true); unsigned int Mix(short* samples, unsigned int numSamples, bool consider_framelimit = true);
void SetInputSampleRate(unsigned int rate); void SetInputSampleRate(unsigned int rate);
void SetVolume(unsigned int lvolume, unsigned int rvolume); void SetVolume(unsigned int lvolume, unsigned int rvolume);
private: private:
CMixer *m_mixer; CMixer* m_mixer;
unsigned m_input_sample_rate; unsigned m_input_sample_rate;
short m_buffer[MAX_SAMPLES * 2]; std::array<short, MAX_SAMPLES * 2> m_buffer{};
std::atomic<u32> m_indexW; std::atomic<u32> m_indexW{0};
std::atomic<u32> m_indexR; std::atomic<u32> m_indexR{0};
// Volume ranges from 0-256 // Volume ranges from 0-256
std::atomic<s32> m_LVolume; std::atomic<s32> m_LVolume{256};
std::atomic<s32> m_RVolume; std::atomic<s32> m_RVolume{256};
float m_numLeftI; float m_numLeftI = 0.0f;
u32 m_frac; u32 m_frac = 0;
}; };
MixerFifo m_dma_mixer; MixerFifo m_dma_mixer{this, 32000};
MixerFifo m_streaming_mixer; MixerFifo m_streaming_mixer{this, 48000};
MixerFifo m_wiimote_speaker_mixer; MixerFifo m_wiimote_speaker_mixer{this, 3000};
unsigned int m_sampleRate; unsigned int m_sampleRate;
WaveFileWriter m_wave_writer_dtk; WaveFileWriter m_wave_writer_dtk;
WaveFileWriter m_wave_writer_dsp; WaveFileWriter m_wave_writer_dsp;
bool m_log_dtk_audio; bool m_log_dtk_audio = false;
bool m_log_dsp_audio; bool m_log_dsp_audio = false;
std::atomic<float> m_speed; // Current rate of the emulation (1.0 = 100% speed) // Current rate of emulation (1.0 = 100% speed)
std::atomic<float> m_speed{0.0f};
}; };