summaryrefslogtreecommitdiff
path: root/Source/Core/AudioCommon
diff options
context:
space:
mode:
authorshuffle2 <godisgovernment@gmail.com>2017-06-05 20:09:18 -0700
committerGitHub <noreply@github.com>2017-06-05 20:09:18 -0700
commit0b00477c8ae297e166d9e778810cecf67d3d760f (patch)
treeee4bc1f0ff27b3249fc48eb1a75572757f82ad0e /Source/Core/AudioCommon
parent98eb192226e91c4121a120371ea38b827c19f3ce (diff)
parent14c3d4716fa4abfa7cc70f056676f935ba302c8a (diff)
Merge pull request #5311 from ligfx/mixerdpl2
AudioCommon: Move DPL2 decoding into Mixer
Diffstat (limited to 'Source/Core/AudioCommon')
-rw-r--r--Source/Core/AudioCommon/AudioCommon.vcxproj2
-rw-r--r--Source/Core/AudioCommon/AudioCommon.vcxproj.filters2
-rw-r--r--Source/Core/AudioCommon/AudioStretcher.cpp89
-rw-r--r--Source/Core/AudioCommon/AudioStretcher.h28
-rw-r--r--Source/Core/AudioCommon/CMakeLists.txt1
-rw-r--r--Source/Core/AudioCommon/CubebStream.cpp22
-rw-r--r--Source/Core/AudioCommon/Mixer.cpp84
-rw-r--r--Source/Core/AudioCommon/Mixer.h14
-rw-r--r--Source/Core/AudioCommon/OpenALStream.cpp32
-rw-r--r--Source/Core/AudioCommon/PulseAudioStream.cpp19
10 files changed, 169 insertions, 124 deletions
diff --git a/Source/Core/AudioCommon/AudioCommon.vcxproj b/Source/Core/AudioCommon/AudioCommon.vcxproj
index 6ab07e55f4..dad6e891ed 100644
--- a/Source/Core/AudioCommon/AudioCommon.vcxproj
+++ b/Source/Core/AudioCommon/AudioCommon.vcxproj
@@ -38,6 +38,7 @@
<ItemGroup>
<ClCompile Include="aldlist.cpp" />
<ClCompile Include="AudioCommon.cpp" />
+ <ClCompile Include="AudioStretcher.cpp" />
<ClCompile Include="CubebStream.cpp" />
<ClCompile Include="CubebUtils.cpp" />
<ClCompile Include="DPL2Decoder.cpp" />
@@ -54,6 +55,7 @@
<ClInclude Include="aldlist.h" />
<ClInclude Include="AlsaSoundStream.h" />
<ClInclude Include="AudioCommon.h" />
+ <ClInclude Include="AudioStretcher.h" />
<ClInclude Include="CoreAudioSoundStream.h" />
<ClInclude Include="CubebStream.h" />
<ClInclude Include="CubebUtils.h" />
diff --git a/Source/Core/AudioCommon/AudioCommon.vcxproj.filters b/Source/Core/AudioCommon/AudioCommon.vcxproj.filters
index 35ba0a6c7d..661c6ce0ea 100644
--- a/Source/Core/AudioCommon/AudioCommon.vcxproj.filters
+++ b/Source/Core/AudioCommon/AudioCommon.vcxproj.filters
@@ -8,6 +8,7 @@
<ItemGroup>
<ClCompile Include="aldlist.cpp" />
<ClCompile Include="AudioCommon.cpp" />
+ <ClCompile Include="AudioStretcher.cpp" />
<ClCompile Include="CubebUtils.cpp" />
<ClCompile Include="DPL2Decoder.cpp" />
<ClCompile Include="Mixer.cpp" />
@@ -31,6 +32,7 @@
<ItemGroup>
<ClInclude Include="aldlist.h" />
<ClInclude Include="AudioCommon.h" />
+ <ClInclude Include="AudioStretcher.h" />
<ClInclude Include="CubebUtils.h" />
<ClInclude Include="DPL2Decoder.h" />
<ClInclude Include="Mixer.h" />
diff --git a/Source/Core/AudioCommon/AudioStretcher.cpp b/Source/Core/AudioCommon/AudioStretcher.cpp
new file mode 100644
index 0000000000..88721b0e35
--- /dev/null
+++ b/Source/Core/AudioCommon/AudioStretcher.cpp
@@ -0,0 +1,89 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#include <algorithm>
+#include <cmath>
+#include <cstddef>
+
+#include "AudioCommon/AudioStretcher.h"
+#include "Common/Logging/Log.h"
+#include "Core/ConfigManager.h"
+
+namespace AudioCommon
+{
+AudioStretcher::AudioStretcher(unsigned int sample_rate) : m_sample_rate(sample_rate)
+{
+ m_sound_touch.setChannels(2);
+ m_sound_touch.setSampleRate(sample_rate);
+ 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);
+}
+
+void AudioStretcher::Clear()
+{
+ m_sound_touch.clear();
+}
+
+void AudioStretcher::ProcessSamples(const short* in, unsigned int num_in, unsigned int num_out)
+{
+ const double time_delta = static_cast<double>(num_out) / m_sample_rate; // 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_sample_rate * 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 lpf_gain = 1.0 - std::exp(-time_delta / lpf_time_scale);
+ m_stretch_ratio += 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, lpf_gain);
+
+ m_sound_touch.putSamples(in, num_in);
+}
+
+void AudioStretcher::GetStretchedSamples(short* out, unsigned int num_out)
+{
+ 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];
+ }
+
+ // Perform 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];
+ }
+}
+
+} // namespace AudioCommon
diff --git a/Source/Core/AudioCommon/AudioStretcher.h b/Source/Core/AudioCommon/AudioStretcher.h
new file mode 100644
index 0000000000..b914fb924b
--- /dev/null
+++ b/Source/Core/AudioCommon/AudioStretcher.h
@@ -0,0 +1,28 @@
+// Copyright 2017 Dolphin Emulator Project
+// Licensed under GPLv2+
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <array>
+
+#include <soundtouch/SoundTouch.h>
+
+namespace AudioCommon
+{
+class AudioStretcher
+{
+public:
+ explicit AudioStretcher(unsigned int sample_rate);
+ void ProcessSamples(const short* in, unsigned int num_in, unsigned int num_out);
+ void GetStretchedSamples(short* out, unsigned int num_out);
+ void Clear();
+
+private:
+ unsigned int m_sample_rate;
+ std::array<short, 2> m_last_stretched_sample = {};
+ soundtouch::SoundTouch m_sound_touch;
+ double m_stretch_ratio = 1.0;
+};
+
+} // AudioCommon
diff --git a/Source/Core/AudioCommon/CMakeLists.txt b/Source/Core/AudioCommon/CMakeLists.txt
index 94eb45c9ed..7ede29eb41 100644
--- a/Source/Core/AudioCommon/CMakeLists.txt
+++ b/Source/Core/AudioCommon/CMakeLists.txt
@@ -1,5 +1,6 @@
set(SRCS
AudioCommon.cpp
+ AudioStretcher.cpp
CubebStream.cpp
CubebUtils.cpp
DPL2Decoder.cpp
diff --git a/Source/Core/AudioCommon/CubebStream.cpp b/Source/Core/AudioCommon/CubebStream.cpp
index c9052e29c9..272060cd74 100644
--- a/Source/Core/AudioCommon/CubebStream.cpp
+++ b/Source/Core/AudioCommon/CubebStream.cpp
@@ -21,29 +21,9 @@ long CubebStream::DataCallback(cubeb_stream* stream, void* user_data, const void
auto* self = static_cast<CubebStream*>(user_data);
if (self->m_stereo)
- {
self->m_mixer->Mix(static_cast<short*>(output_buffer), num_frames);
- }
else
- {
- size_t required_capacity = num_frames * 2;
- if (required_capacity > self->m_short_buffer.capacity() ||
- required_capacity > self->m_floatstereo_buffer.capacity())
- {
- INFO_LOG(AUDIO, "Expanding conversion buffers size: %li frames", num_frames);
- self->m_short_buffer.reserve(required_capacity);
- self->m_floatstereo_buffer.reserve(required_capacity);
- }
-
- self->m_mixer->Mix(self->m_short_buffer.data(), num_frames);
-
- // s16 to float
- for (size_t i = 0; i < static_cast<size_t>(num_frames) * 2; ++i)
- self->m_floatstereo_buffer[i] = self->m_short_buffer[i] / static_cast<float>(1 << 15);
-
- // DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR
- DPL2Decode(self->m_floatstereo_buffer.data(), num_frames, static_cast<float*>(output_buffer));
- }
+ self->m_mixer->MixSurround(static_cast<float*>(output_buffer), num_frames);
return num_frames;
}
diff --git a/Source/Core/AudioCommon/Mixer.cpp b/Source/Core/AudioCommon/Mixer.cpp
index 8149da088c..659deb819f 100644
--- a/Source/Core/AudioCommon/Mixer.cpp
+++ b/Source/Core/AudioCommon/Mixer.cpp
@@ -7,24 +7,18 @@
#include <cmath>
#include <cstring>
+#include "AudioCommon/DPL2Decoder.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
#include "Common/MathUtil.h"
#include "Common/Swap.h"
#include "Core/ConfigManager.h"
-CMixer::CMixer(unsigned int BackendSampleRate) : m_sampleRate(BackendSampleRate)
+CMixer::CMixer(unsigned int BackendSampleRate)
+ : m_sampleRate(BackendSampleRate), m_stretcher(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);
+ DPL2Reset();
}
CMixer::~CMixer()
@@ -135,18 +129,19 @@ unsigned int CMixer::Mix(short* samples, unsigned int num_samples)
unsigned int available_samples =
std::min(m_dma_mixer.AvailableSamples(), m_streaming_mixer.AvailableSamples());
- m_stretch_buffer.fill(0);
+ m_scratch_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);
+ 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);
if (!m_is_stretching)
{
- m_sound_touch.clear();
+ m_stretcher.Clear();
m_is_stretching = true;
}
- StretchAudio(m_stretch_buffer.data(), available_samples, samples, num_samples);
+ m_stretcher.ProcessSamples(m_scratch_buffer.data(), available_samples, num_samples);
+ m_stretcher.GetStretchedSamples(samples, num_samples);
}
else
{
@@ -159,58 +154,25 @@ unsigned int CMixer::Mix(short* samples, unsigned int num_samples)
return num_samples;
}
-void CMixer::StretchAudio(const short* in, unsigned int num_in, short* out, unsigned int num_out)
+unsigned int CMixer::MixSurround(float* samples, unsigned int num_samples)
{
- const double time_delta = static_cast<double>(num_out) / m_sampleRate; // seconds
+ if (!num_samples)
+ return 0;
- // 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);
+ memset(samples, 0, num_samples * 6 * sizeof(float));
- 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)
+ // Mix() may also use m_scratch_buffer internally, but is safe because it alternates reads and
+ // writes.
+ unsigned int available_samples = Mix(m_scratch_buffer.data(), num_samples);
+ for (size_t i = 0; i < static_cast<size_t>(available_samples) * 2; ++i)
{
- // Too many samples in backlog: Don't push anymore on
- num_in = 0;
+ m_float_conversion_buffer[i] =
+ m_scratch_buffer[i] / static_cast<float>(std::numeric_limits<short>::max());
}
- // 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);
+ DPL2Decode(m_float_conversion_buffer.data(), available_samples, samples);
- 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];
- }
+ return available_samples;
}
void CMixer::MixerFifo::PushSamples(const short* samples, unsigned int num_samples)
diff --git a/Source/Core/AudioCommon/Mixer.h b/Source/Core/AudioCommon/Mixer.h
index f2127cceba..2707df3383 100644
--- a/Source/Core/AudioCommon/Mixer.h
+++ b/Source/Core/AudioCommon/Mixer.h
@@ -7,12 +7,10 @@
#include <array>
#include <atomic>
+#include "AudioCommon/AudioStretcher.h"
#include "AudioCommon/WaveFile.h"
#include "Common/CommonTypes.h"
-#include <soundtouch/STTypes.h>
-#include <soundtouch/SoundTouch.h>
-
class CMixer final
{
public:
@@ -21,6 +19,7 @@ public:
// Called from audio threads
unsigned int Mix(short* samples, unsigned int numSamples);
+ unsigned int MixSurround(float* samples, unsigned int num_samples);
// Called from main thread
void PushSamples(const short* samples, unsigned int num_samples);
@@ -75,18 +74,15 @@ private:
u32 m_frac = 0;
};
- void StretchAudio(const short* in, unsigned int num_in, short* out, unsigned int num_out);
-
MixerFifo m_dma_mixer{this, 32000};
MixerFifo m_streaming_mixer{this, 48000};
MixerFifo m_wiimote_speaker_mixer{this, 3000};
unsigned int m_sampleRate;
bool m_is_stretching = false;
- soundtouch::SoundTouch m_sound_touch;
- double m_stretch_ratio = 1.0;
- std::array<short, 2> m_last_stretched_sample = {};
- std::array<short, MAX_SAMPLES * 2> m_stretch_buffer;
+ AudioCommon::AudioStretcher m_stretcher;
+ std::array<short, MAX_SAMPLES * 2> m_scratch_buffer;
+ std::array<float, MAX_SAMPLES * 2> m_float_conversion_buffer;
WaveFileWriter m_wave_writer_dtk;
WaveFileWriter m_wave_writer_dsp;
diff --git a/Source/Core/AudioCommon/OpenALStream.cpp b/Source/Core/AudioCommon/OpenALStream.cpp
index 3d4bc90e1d..ca343be005 100644
--- a/Source/Core/AudioCommon/OpenALStream.cpp
+++ b/Source/Core/AudioCommon/OpenALStream.cpp
@@ -6,7 +6,6 @@
#include <cstring>
#include <thread>
-#include "AudioCommon/DPL2Decoder.h"
#include "AudioCommon/OpenALStream.h"
#include "AudioCommon/aldlist.h"
#include "Common/Logging/Log.h"
@@ -66,9 +65,6 @@ bool OpenALStream::Start()
PanicAlertT("OpenAL: can't find sound devices");
}
- // Initialize DPL2 parameters
- DPL2Reset();
-
return bReturn;
}
@@ -228,23 +224,18 @@ void OpenALStream::SoundLoop()
numBuffersQueued -= numBuffersProcessed;
}
- // DPL2 accepts 240 samples minimum (FWRDURATION)
- unsigned int minSamples = surround_capable ? 240 : 0;
-
unsigned int numSamples = OAL_MAX_SAMPLES;
- numSamples = m_mixer->Mix(realtimeBuffer, numSamples);
-
- // Convert the samples from short to float
- for (u32 i = 0; i < numSamples * STEREO_CHANNELS; ++i)
- sampleBuffer[i] = static_cast<float>(realtimeBuffer[i]) / (1 << 15);
-
- if (numSamples <= minSamples)
- continue;
if (surround_capable)
{
+ // DPL2 accepts 240 samples minimum (FWRDURATION)
+ unsigned int minSamples = 240;
+
float dpl2[OAL_MAX_SAMPLES * OAL_MAX_BUFFERS * SURROUND_CHANNELS];
- DPL2Decode(sampleBuffer, numSamples, dpl2);
+ numSamples = m_mixer->MixSurround(dpl2, numSamples);
+
+ if (numSamples < minSamples)
+ continue;
// zero-out the subwoofer channel - DPL2Decode generates a pretty
// good 5.0 but not a good 5.1 output. Sadly there is not a 5.0
@@ -311,6 +302,15 @@ void OpenALStream::SoundLoop()
}
else
{
+ numSamples = m_mixer->Mix(realtimeBuffer, numSamples);
+
+ // Convert the samples from short to float
+ for (u32 i = 0; i < numSamples * STEREO_CHANNELS; ++i)
+ sampleBuffer[i] = static_cast<float>(realtimeBuffer[i]) / (1 << 15);
+
+ if (!numSamples)
+ continue;
+
if (float32_capable)
{
alBufferData(uiBuffers[nextBuffer], AL_FORMAT_STEREO_FLOAT32, sampleBuffer,
diff --git a/Source/Core/AudioCommon/PulseAudioStream.cpp b/Source/Core/AudioCommon/PulseAudioStream.cpp
index e6e6a45ce2..827816d4e8 100644
--- a/Source/Core/AudioCommon/PulseAudioStream.cpp
+++ b/Source/Core/AudioCommon/PulseAudioStream.cpp
@@ -4,7 +4,6 @@
#include <cstring>
-#include "AudioCommon/DPL2Decoder.h"
#include "AudioCommon/PulseAudioStream.h"
#include "Common/CommonTypes.h"
#include "Common/Logging/Log.h"
@@ -30,9 +29,6 @@ bool PulseAudio::Start()
m_run_thread.Set();
m_thread = std::thread(&PulseAudio::SoundLoop, this);
- // Initialize DPL2 parameters
- DPL2Reset();
-
return true;
}
@@ -194,23 +190,12 @@ void PulseAudio::WriteCallback(pa_stream* s, size_t length)
}
else
{
- // get a floating point mix
- s16 s16buffer_stereo[frames * 2];
- m_mixer->Mix(s16buffer_stereo, frames); // implicitly mixes to 16-bit stereo
-
- float floatbuffer_stereo[frames * 2];
- // s16 to float
- for (int i = 0; i < frames * 2; ++i)
- {
- floatbuffer_stereo[i] = s16buffer_stereo[i] / float(1 << 15);
- }
-
if (m_channels == 5) // Extract dpl2/5.0 Surround
{
float floatbuffer_6chan[frames * 6];
- // DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR
- DPL2Decode(floatbuffer_stereo, frames, floatbuffer_6chan);
+ m_mixer->MixSurround(floatbuffer_6chan, frames);
+ // DPL2Decode output: LEFTFRONT, RIGHTFRONT, CENTREFRONT, (sub), LEFTREAR, RIGHTREAR
// Discard the subwoofer channel - DPL2Decode generates a pretty
// good 5.0 but not a good 5.1 output.
const int dpl2_to_5chan[] = {0, 1, 2, 4, 5};