summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon/Mixer.cpp
diff options
context:
space:
mode:
authorMarkus Wick <degasus@users.noreply.github.com>2017-04-12 20:27:26 +0200
committerGitHub <noreply@github.com>2017-04-12 20:27:26 +0200
commit2151858fb10adcd37c67c814c4675ca5ab3ac607 (patch)
tree675d28467827bc115dd58f012ba94054ee8c13fe /Source/Core/AudioCommon/Mixer.cpp
parent21544f8661986fd022d690600a2276f6aed59661 (diff)
parentcbaa00457a961b3aa0213da0f5282e13a69c610d (diff)
Merge pull request #5241 from MerryMage/stretch
Pitch-Preserving Audio Stretching
Diffstat (limited to 'Source/Core/AudioCommon/Mixer.cpp')
-rw-r--r--Source/Core/AudioCommon/Mixer.cpp135
1 files changed, 117 insertions, 18 deletions
diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp
index bdaaad812c..8149da088c 100644
--- a/Source/Core/AudioCommon/Mixer.cpp
+++ b/Source/Core/AudioCommon/Mixer.cpp
@@ -4,6 +4,7 @@
#include "AudioCommon/Mixer.h"
+#include <cmath>
#include <cstring>
#include "Common/CommonTypes.h"
@@ -15,6 +16,15 @@
CMixer::CMixer(unsigned int BackendSampleRate) : m_sampleRate(BackendSampleRate)
{
INFO_LOG(AUDIO_INTERFACE, "Mixer is initialized");
+
+ m_sound_touch.setChannels(2);
+ m_sound_touch.setSampleRate(BackendSampleRate);
+ m_sound_touch.setPitch(1.0);
+ m_sound_touch.setTempo(1.0);
+ m_sound_touch.setSetting(SETTING_USE_QUICKSEEK, 0);
+ m_sound_touch.setSetting(SETTING_SEQUENCE_MS, 62);
+ m_sound_touch.setSetting(SETTING_SEEKWINDOW_MS, 28);
+ m_sound_touch.setSetting(SETTING_OVERLAP_MS, 8);
}
CMixer::~CMixer()
@@ -37,26 +47,27 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples,
u32 indexR = m_indexR.load();
u32 indexW = m_indexW.load();
- 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);
- m_numLeftI = (numLeft + m_numLeftI * (CONTROL_AVG - 1)) / CONTROL_AVG;
- float offset = (m_numLeftI - low_waterwark) * CONTROL_FACTOR;
- if (offset > MAX_FREQ_SHIFT)
- offset = MAX_FREQ_SHIFT;
- if (offset < -MAX_FREQ_SHIFT)
- offset = -MAX_FREQ_SHIFT;
-
// render numleft sample pairs to samples[]
// advance indexR with sample position
// remember fractional offset
float emulationspeed = SConfig::GetInstance().m_EmulationSpeed;
- float aid_sample_rate = m_input_sample_rate + offset;
+ float aid_sample_rate = static_cast<float>(m_input_sample_rate);
if (consider_framelimit && emulationspeed > 0.0f)
{
- aid_sample_rate = aid_sample_rate * emulationspeed;
+ float numLeft = static_cast<float>(((indexW - indexR) & INDEX_MASK) / 2);
+
+ u32 low_waterwark = m_input_sample_rate * SConfig::GetInstance().iTimingVariance / 1000;
+ low_waterwark = std::min(low_waterwark, MAX_SAMPLES / 2);
+
+ m_numLeftI = (numLeft + m_numLeftI * (CONTROL_AVG - 1)) / CONTROL_AVG;
+ float offset = (m_numLeftI - low_waterwark) * CONTROL_FACTOR;
+ if (offset > MAX_FREQ_SHIFT)
+ offset = MAX_FREQ_SHIFT;
+ if (offset < -MAX_FREQ_SHIFT)
+ offset = -MAX_FREQ_SHIFT;
+
+ aid_sample_rate = (aid_sample_rate + offset) * emulationspeed;
}
const u32 ratio = (u32)(65536.0f * aid_sample_rate / (float)m_mixer->m_sampleRate);
@@ -88,6 +99,9 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples,
m_frac &= 0xffff;
}
+ // Actual number of samples written to the buffer without padding.
+ unsigned int actual_sample_count = currentSample / 2;
+
// Padding
short s[2];
s[0] = Common::swap16(m_buffer[(indexR - 1) & INDEX_MASK]);
@@ -106,22 +120,99 @@ unsigned int CMixer::MixerFifo::Mix(short* samples, unsigned int numSamples,
// Flush cached variable
m_indexR.store(indexR);
- return numSamples;
+ return actual_sample_count;
}
-unsigned int CMixer::Mix(short* samples, unsigned int num_samples, bool consider_framelimit)
+unsigned int CMixer::Mix(short* samples, unsigned int num_samples)
{
if (!samples)
return 0;
memset(samples, 0, num_samples * 2 * sizeof(short));
- m_dma_mixer.Mix(samples, num_samples, consider_framelimit);
- m_streaming_mixer.Mix(samples, num_samples, consider_framelimit);
- m_wiimote_speaker_mixer.Mix(samples, num_samples, consider_framelimit);
+ if (SConfig::GetInstance().m_audio_stretch)
+ {
+ unsigned int available_samples =
+ std::min(m_dma_mixer.AvailableSamples(), m_streaming_mixer.AvailableSamples());
+
+ m_stretch_buffer.fill(0);
+
+ m_dma_mixer.Mix(m_stretch_buffer.data(), available_samples, false);
+ m_streaming_mixer.Mix(m_stretch_buffer.data(), available_samples, false);
+ m_wiimote_speaker_mixer.Mix(m_stretch_buffer.data(), available_samples, false);
+
+ if (!m_is_stretching)
+ {
+ m_sound_touch.clear();
+ m_is_stretching = true;
+ }
+ StretchAudio(m_stretch_buffer.data(), available_samples, samples, 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_is_stretching = false;
+ }
+
return num_samples;
}
+void CMixer::StretchAudio(const short* in, unsigned int num_in, short* out, unsigned int num_out)
+{
+ const double time_delta = static_cast<double>(num_out) / m_sampleRate; // seconds
+
+ // We were given actual_samples number of samples, and num_samples were requested from us.
+ double current_ratio = static_cast<double>(num_in) / static_cast<double>(num_out);
+
+ const double max_latency = SConfig::GetInstance().m_audio_stretch_max_latency;
+ const double max_backlog = m_sampleRate * max_latency / 1000.0 / m_stretch_ratio;
+ const double backlog_fullness = m_sound_touch.numSamples() / max_backlog;
+ if (backlog_fullness > 5.0)
+ {
+ // Too many samples in backlog: Don't push anymore on
+ num_in = 0;
+ }
+
+ // We ideally want the backlog to be about 50% full.
+ // This gives some headroom both ways to prevent underflow and overflow.
+ // We tweak current_ratio to encourage this.
+ constexpr double tweak_time_scale = 0.5; // seconds
+ current_ratio *= 1.0 + 2.0 * (backlog_fullness - 0.5) * (time_delta / tweak_time_scale);
+
+ // This low-pass filter smoothes out variance in the calculated stretch ratio.
+ // The time-scale determines how responsive this filter is.
+ constexpr double lpf_time_scale = 1.0; // seconds
+ const double m_lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale);
+ m_stretch_ratio += m_lpf_gain * (current_ratio - m_stretch_ratio);
+
+ // Place a lower limit of 10% speed. When a game boots up, there will be
+ // many silence samples. These do not need to be timestretched.
+ m_stretch_ratio = std::max(m_stretch_ratio, 0.1);
+ m_sound_touch.setTempo(m_stretch_ratio);
+
+ DEBUG_LOG(AUDIO, "Audio stretching: samples:%u/%u ratio:%f backlog:%f gain: %f", num_in, num_out,
+ m_stretch_ratio, backlog_fullness, m_lpf_gain);
+
+ m_sound_touch.putSamples(in, num_in);
+
+ const size_t samples_received = m_sound_touch.receiveSamples(out, num_out);
+
+ if (samples_received != 0)
+ {
+ m_last_stretched_sample[0] = out[samples_received * 2 - 2];
+ m_last_stretched_sample[1] = out[samples_received * 2 - 1];
+ }
+
+ // Preform padding if we've run out of samples.
+ for (size_t i = samples_received; i < num_out; i++)
+ {
+ out[i * 2 + 0] = m_last_stretched_sample[0];
+ out[i * 2 + 1] = m_last_stretched_sample[1];
+ }
+}
+
void CMixer::MixerFifo::PushSamples(const short* samples, unsigned int num_samples)
{
// Cache access in non-volatile variable
@@ -295,3 +386,11 @@ void CMixer::MixerFifo::SetVolume(unsigned int lvolume, unsigned int rvolume)
m_LVolume.store(lvolume + (lvolume >> 7));
m_RVolume.store(rvolume + (rvolume >> 7));
}
+
+unsigned int CMixer::MixerFifo::AvailableSamples() const
+{
+ unsigned int samples_in_fifo = ((m_indexW.load() - m_indexR.load()) & INDEX_MASK) / 2;
+ if (samples_in_fifo <= 1)
+ return 0; // CMixer::MixerFifo::Mix always keeps one sample in the buffer.
+ return (samples_in_fifo - 1) * m_mixer->m_sampleRate / m_input_sample_rate;
+}