summaryrefslogtreecommitdiffstats
path: root/src/audio_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core')
-rw-r--r--src/audio_core/audio_core.cpp4
-rw-r--r--src/audio_core/audio_core.h3
-rw-r--r--src/audio_core/hle/dsp.cpp45
-rw-r--r--src/audio_core/hle/dsp.h8
-rw-r--r--src/audio_core/null_sink.h2
-rw-r--r--src/audio_core/sdl2_sink.cpp6
-rw-r--r--src/audio_core/sdl2_sink.h2
-rw-r--r--src/audio_core/sink.h5
8 files changed, 59 insertions, 16 deletions
diff --git a/src/audio_core/audio_core.cpp b/src/audio_core/audio_core.cpp
index d42249ebd..8e19ec0c4 100644
--- a/src/audio_core/audio_core.cpp
+++ b/src/audio_core/audio_core.cpp
@@ -71,6 +71,10 @@ void SelectSink(std::string sink_id) {
DSP::HLE::SetSink(iter->factory());
}
+void EnableStretching(bool enable) {
+ DSP::HLE::EnableStretching(enable);
+}
+
void Shutdown() {
CoreTiming::UnscheduleEvent(tick_event, 0);
DSP::HLE::Shutdown();
diff --git a/src/audio_core/audio_core.h b/src/audio_core/audio_core.h
index f618361f3..7e678aba5 100644
--- a/src/audio_core/audio_core.h
+++ b/src/audio_core/audio_core.h
@@ -23,6 +23,9 @@ void AddAddressSpace(Kernel::VMManager& vm_manager);
/// Select the sink to use based on sink id.
void SelectSink(std::string sink_id);
+/// Enable/Disable stretching.
+void EnableStretching(bool enable);
+
/// Shutdown Audio Core
void Shutdown();
diff --git a/src/audio_core/hle/dsp.cpp b/src/audio_core/hle/dsp.cpp
index 0640e1eff..0cddeb82a 100644
--- a/src/audio_core/hle/dsp.cpp
+++ b/src/audio_core/hle/dsp.cpp
@@ -85,12 +85,45 @@ static StereoFrame16 GenerateCurrentFrame() {
// Audio output
+static bool perform_time_stretching = true;
static std::unique_ptr<AudioCore::Sink> sink;
static AudioCore::TimeStretcher time_stretcher;
+static void FlushResidualStretcherAudio() {
+ time_stretcher.Flush();
+ while (true) {
+ std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue());
+ if (residual_audio.empty())
+ break;
+ sink->EnqueueSamples(residual_audio.data(), residual_audio.size() / 2);
+ }
+}
+
static void OutputCurrentFrame(const StereoFrame16& frame) {
- time_stretcher.AddSamples(&frame[0][0], frame.size());
- sink->EnqueueSamples(time_stretcher.Process(sink->SamplesInQueue()));
+ if (perform_time_stretching) {
+ time_stretcher.AddSamples(&frame[0][0], frame.size());
+ std::vector<s16> stretched_samples = time_stretcher.Process(sink->SamplesInQueue());
+ sink->EnqueueSamples(stretched_samples.data(), stretched_samples.size() / 2);
+ } else {
+ constexpr size_t maximum_sample_latency = 1024; // about 32 miliseconds
+ if (sink->SamplesInQueue() > maximum_sample_latency) {
+ // This can occur if we're running too fast and samples are starting to back up.
+ // Just drop the samples.
+ return;
+ }
+
+ sink->EnqueueSamples(&frame[0][0], frame.size());
+ }
+}
+
+void EnableStretching(bool enable) {
+ if (perform_time_stretching == enable)
+ return;
+
+ if (!enable) {
+ FlushResidualStretcherAudio();
+ }
+ perform_time_stretching = enable;
}
// Public Interface
@@ -111,12 +144,8 @@ void Init() {
}
void Shutdown() {
- time_stretcher.Flush();
- while (true) {
- std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue());
- if (residual_audio.empty())
- break;
- sink->EnqueueSamples(residual_audio);
+ if (perform_time_stretching) {
+ FlushResidualStretcherAudio();
}
}
diff --git a/src/audio_core/hle/dsp.h b/src/audio_core/hle/dsp.h
index 9275cd7de..565f20b6f 100644
--- a/src/audio_core/hle/dsp.h
+++ b/src/audio_core/hle/dsp.h
@@ -544,5 +544,13 @@ bool Tick();
*/
void SetSink(std::unique_ptr<AudioCore::Sink> sink);
+/**
+ * Enables/Disables audio-stretching.
+ * Audio stretching is an enhancement that stretches audio to match emulation
+ * speed to prevent stuttering at the cost of some audio latency.
+ * @param enable true to enable, false to disable.
+ */
+void EnableStretching(bool enable);
+
} // namespace HLE
} // namespace DSP
diff --git a/src/audio_core/null_sink.h b/src/audio_core/null_sink.h
index faf0ee4e1..9931c4778 100644
--- a/src/audio_core/null_sink.h
+++ b/src/audio_core/null_sink.h
@@ -19,7 +19,7 @@ public:
return native_sample_rate;
}
- void EnqueueSamples(const std::vector<s16>&) override {}
+ void EnqueueSamples(const s16*, size_t) override {}
size_t SamplesInQueue() const override {
return 0;
diff --git a/src/audio_core/sdl2_sink.cpp b/src/audio_core/sdl2_sink.cpp
index dc75c04ee..311dd5b59 100644
--- a/src/audio_core/sdl2_sink.cpp
+++ b/src/audio_core/sdl2_sink.cpp
@@ -71,14 +71,12 @@ unsigned int SDL2Sink::GetNativeSampleRate() const {
return impl->sample_rate;
}
-void SDL2Sink::EnqueueSamples(const std::vector<s16>& samples) {
+void SDL2Sink::EnqueueSamples(const s16* samples, size_t sample_count) {
if (impl->audio_device_id <= 0)
return;
- ASSERT_MSG(samples.size() % 2 == 0, "Samples must be in interleaved stereo PCM16 format (size must be a multiple of two)");
-
SDL_LockAudioDevice(impl->audio_device_id);
- impl->queue.emplace_back(samples);
+ impl->queue.emplace_back(samples, samples + sample_count * 2);
SDL_UnlockAudioDevice(impl->audio_device_id);
}
diff --git a/src/audio_core/sdl2_sink.h b/src/audio_core/sdl2_sink.h
index 0f296b673..b13827214 100644
--- a/src/audio_core/sdl2_sink.h
+++ b/src/audio_core/sdl2_sink.h
@@ -18,7 +18,7 @@ public:
unsigned int GetNativeSampleRate() const override;
- void EnqueueSamples(const std::vector<s16>& samples) override;
+ void EnqueueSamples(const s16* samples, size_t sample_count) override;
size_t SamplesInQueue() const override;
diff --git a/src/audio_core/sink.h b/src/audio_core/sink.h
index 1c881c3d2..a06fc3dcc 100644
--- a/src/audio_core/sink.h
+++ b/src/audio_core/sink.h
@@ -23,9 +23,10 @@ public:
/**
* Feed stereo samples to sink.
- * @param samples Samples in interleaved stereo PCM16 format. Size of vector must be multiple of two.
+ * @param samples Samples in interleaved stereo PCM16 format.
+ * @param sample_count Number of samples.
*/
- virtual void EnqueueSamples(const std::vector<s16>& samples) = 0;
+ virtual void EnqueueSamples(const s16* samples, size_t sample_count) = 0;
/// Samples enqueued that have not been played yet.
virtual std::size_t SamplesInQueue() const = 0;