Core/AudioCommon: Add config caching to Mixer.

This commit is contained in:
Admiral H. Curtiss 2022-01-06 16:43:43 +01:00
parent ed1a1c1fae
commit eb5bb2993e
No known key found for this signature in database
GPG Key ID: F051B4C4044F33FB
2 changed files with 41 additions and 13 deletions

View File

@ -35,11 +35,15 @@ Mixer::Mixer(unsigned int BackendSampleRate)
m_surround_decoder(BackendSampleRate,
DPL2QualityToFrameBlockSize(Config::Get(Config::MAIN_DPL2_QUALITY)))
{
m_config_changed_callback_id = Config::AddConfigChangedCallback([this] { RefreshConfig(); });
RefreshConfig();
INFO_LOG_FMT(AUDIO_INTERFACE, "Mixer is initialized");
}
Mixer::~Mixer()
{
Config::RemoveConfigChangedCallback(m_config_changed_callback_id);
}
void Mixer::DoState(PointerWrap& p)
@ -53,7 +57,8 @@ void Mixer::DoState(PointerWrap& p)
// Executed from sound stream thread
unsigned int Mixer::MixerFifo::Mix(short* samples, unsigned int numSamples,
bool consider_framelimit)
bool consider_framelimit, float emulationspeed,
int timing_variance)
{
unsigned int currentSample = 0;
@ -71,13 +76,12 @@ unsigned int Mixer::MixerFifo::Mix(short* samples, unsigned int numSamples,
// advance indexR with sample position
// remember fractional offset
float emulationspeed = Config::Get(Config::MAIN_EMULATION_SPEED);
float aid_sample_rate = static_cast<float>(m_input_sample_rate);
if (consider_framelimit && emulationspeed > 0.0f)
{
float numLeft = static_cast<float>(((indexW - indexR) & INDEX_MASK) / 2);
u32 low_waterwark = m_input_sample_rate * Config::Get(Config::MAIN_TIMING_VARIANCE) / 1000;
u32 low_waterwark = m_input_sample_rate * timing_variance / 1000;
low_waterwark = std::min(low_waterwark, MAX_SAMPLES / 2);
m_numLeftI = (numLeft + m_numLeftI * (CONTROL_AVG - 1)) / CONTROL_AVG;
@ -154,18 +158,26 @@ unsigned int Mixer::Mix(short* samples, unsigned int num_samples)
memset(samples, 0, num_samples * 2 * sizeof(short));
if (Config::Get(Config::MAIN_AUDIO_STRETCH))
const float emulation_speed = m_config_emulation_speed;
const int timing_variance = m_config_timing_variance;
if (m_config_audio_stretch)
{
unsigned int available_samples =
std::min(m_dma_mixer.AvailableSamples(), m_streaming_mixer.AvailableSamples());
m_scratch_buffer.fill(0);
m_dma_mixer.Mix(m_scratch_buffer.data(), available_samples, false);
m_streaming_mixer.Mix(m_scratch_buffer.data(), available_samples, false);
m_wiimote_speaker_mixer.Mix(m_scratch_buffer.data(), available_samples, false);
m_dma_mixer.Mix(m_scratch_buffer.data(), available_samples, false, emulation_speed,
timing_variance);
m_streaming_mixer.Mix(m_scratch_buffer.data(), available_samples, false, emulation_speed,
timing_variance);
m_wiimote_speaker_mixer.Mix(m_scratch_buffer.data(), available_samples, false, emulation_speed,
timing_variance);
for (auto& mixer : m_gba_mixers)
mixer.Mix(m_scratch_buffer.data(), available_samples, false);
{
mixer.Mix(m_scratch_buffer.data(), available_samples, false, emulation_speed,
timing_variance);
}
if (!m_is_stretching)
{
@ -177,11 +189,11 @@ unsigned int Mixer::Mix(short* samples, unsigned int num_samples)
}
else
{
m_dma_mixer.Mix(samples, num_samples, true);
m_streaming_mixer.Mix(samples, num_samples, true);
m_wiimote_speaker_mixer.Mix(samples, num_samples, true);
m_dma_mixer.Mix(samples, num_samples, true, emulation_speed, timing_variance);
m_streaming_mixer.Mix(samples, num_samples, true, emulation_speed, timing_variance);
m_wiimote_speaker_mixer.Mix(samples, num_samples, true, emulation_speed, timing_variance);
for (auto& mixer : m_gba_mixers)
mixer.Mix(samples, num_samples, true);
mixer.Mix(samples, num_samples, true, emulation_speed, timing_variance);
m_is_stretching = false;
}
@ -385,6 +397,13 @@ void Mixer::StopLogDSPAudio()
}
}
void Mixer::RefreshConfig()
{
m_config_emulation_speed = Config::Get(Config::MAIN_EMULATION_SPEED);
m_config_timing_variance = Config::Get(Config::MAIN_TIMING_VARIANCE);
m_config_audio_stretch = Config::Get(Config::MAIN_AUDIO_STRETCH);
}
void Mixer::MixerFifo::DoState(PointerWrap& p)
{
p.Do(m_input_sample_rate);

View File

@ -69,7 +69,8 @@ private:
}
void DoState(PointerWrap& p);
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,
float emulationspeed, int timing_variance);
void SetInputSampleRate(unsigned int rate);
unsigned int GetInputSampleRate() const;
void SetVolume(unsigned int lvolume, unsigned int rvolume);
@ -89,6 +90,8 @@ private:
u32 m_frac = 0;
};
void RefreshConfig();
MixerFifo m_dma_mixer{this, 32000, false};
MixerFifo m_streaming_mixer{this, 48000, false};
MixerFifo m_wiimote_speaker_mixer{this, 3000, true};
@ -109,4 +112,10 @@ private:
// Current rate of emulation (1.0 = 100% speed)
std::atomic<float> m_speed{0.0f};
float m_config_emulation_speed;
int m_config_timing_variance;
bool m_config_audio_stretch;
size_t m_config_changed_callback_id;
};