2017-12-20 18:44:32 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include "audio_core/dsp_interface.h"
|
|
|
|
#include "audio_core/sink.h"
|
|
|
|
#include "audio_core/sink_details.h"
|
|
|
|
#include "common/assert.h"
|
2022-12-08 11:27:25 +00:00
|
|
|
#include "common/settings.h"
|
2019-01-26 14:20:21 +00:00
|
|
|
#include "core/core.h"
|
|
|
|
#include "core/dumping/backend.h"
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
2023-11-26 20:06:59 +00:00
|
|
|
DspInterface::DspInterface(Core::System& system_) : system(system_) {}
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
DspInterface::~DspInterface() = default;
|
2017-12-20 18:44:32 +00:00
|
|
|
|
2023-05-01 20:17:45 +01:00
|
|
|
void DspInterface::SetSink(AudioCore::SinkType sink_type, std::string_view audio_device) {
|
2023-05-25 23:44:56 +01:00
|
|
|
// Dispose of the current sink first to avoid contention.
|
|
|
|
sink.reset();
|
|
|
|
|
2023-05-01 20:17:45 +01:00
|
|
|
sink = CreateSinkFromID(sink_type, audio_device);
|
2018-09-08 21:07:28 +01:00
|
|
|
sink->SetCallback(
|
|
|
|
[this](s16* buffer, std::size_t num_frames) { OutputCallback(buffer, num_frames); });
|
2017-12-20 18:44:32 +00:00
|
|
|
time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
|
|
|
|
}
|
|
|
|
|
|
|
|
Sink& DspInterface::GetSink() {
|
|
|
|
ASSERT(sink);
|
|
|
|
return *sink.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DspInterface::EnableStretching(bool enable) {
|
2023-11-26 20:06:59 +00:00
|
|
|
enable_time_stretching = enable;
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:19:36 +00:00
|
|
|
void DspInterface::OutputFrame(StereoFrame16 frame) {
|
2017-12-20 18:44:32 +00:00
|
|
|
if (!sink)
|
|
|
|
return;
|
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
fifo.Push(frame.data(), frame.size());
|
2019-01-26 14:20:21 +00:00
|
|
|
|
2023-11-26 20:06:59 +00:00
|
|
|
auto video_dumper = system.GetVideoDumper();
|
2023-06-17 00:06:18 +01:00
|
|
|
if (video_dumper && video_dumper->IsDumping()) {
|
|
|
|
video_dumper->AddAudioFrame(std::move(frame));
|
2019-01-26 14:20:21 +00:00
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:19:58 +00:00
|
|
|
void DspInterface::OutputSample(std::array<s16, 2> sample) {
|
|
|
|
if (!sink)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fifo.Push(&sample, 1);
|
2019-01-26 14:20:21 +00:00
|
|
|
|
2023-11-26 20:06:59 +00:00
|
|
|
auto video_dumper = system.GetVideoDumper();
|
2023-06-17 00:06:18 +01:00
|
|
|
if (video_dumper && video_dumper->IsDumping()) {
|
|
|
|
video_dumper->AddAudioSample(std::move(sample));
|
2019-01-26 14:20:21 +00:00
|
|
|
}
|
2018-12-06 17:19:58 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 21:28:19 +01:00
|
|
|
void DspInterface::OutputCallback(s16* buffer, std::size_t num_frames) {
|
2023-11-26 20:06:59 +00:00
|
|
|
// Determine if we should stretch based on the current emulation speed.
|
|
|
|
const auto perf_stats = system.GetLastPerfStats();
|
|
|
|
const auto should_stretch = enable_time_stretching && perf_stats.emulation_speed <= 95;
|
|
|
|
if (performing_time_stretching && !should_stretch) {
|
|
|
|
// If we just stopped stretching, flush the stretcher before returning to normal output.
|
|
|
|
flushing_time_stretcher = true;
|
|
|
|
}
|
|
|
|
performing_time_stretching = should_stretch;
|
|
|
|
|
2023-10-25 01:22:10 +01:00
|
|
|
std::size_t frames_written = 0;
|
2023-11-26 20:06:59 +00:00
|
|
|
if (performing_time_stretching) {
|
2018-09-08 21:28:19 +01:00
|
|
|
const std::vector<s16> in{fifo.Pop()};
|
|
|
|
const std::size_t num_in{in.size() / 2};
|
|
|
|
frames_written = time_stretcher.Process(in.data(), num_in, buffer, num_frames);
|
|
|
|
} else {
|
2023-10-25 01:22:10 +01:00
|
|
|
if (flushing_time_stretcher) {
|
|
|
|
time_stretcher.Flush();
|
|
|
|
frames_written = time_stretcher.Process(nullptr, 0, buffer, num_frames);
|
|
|
|
flushing_time_stretcher = false;
|
|
|
|
|
|
|
|
// Make sure any frames that did not fit are cleared from the time stretcher,
|
|
|
|
// so that they do not bleed into the next time the stretcher is enabled.
|
|
|
|
time_stretcher.Clear();
|
|
|
|
}
|
|
|
|
frames_written += fifo.Pop(buffer, num_frames - frames_written);
|
2018-09-08 21:28:19 +01:00
|
|
|
}
|
2018-09-08 21:07:28 +01:00
|
|
|
|
|
|
|
if (frames_written > 0) {
|
|
|
|
std::memcpy(&last_frame[0], buffer + 2 * (frames_written - 1), 2 * sizeof(s16));
|
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
|
2018-09-08 21:07:28 +01:00
|
|
|
// Hold last emitted frame; this prevents popping.
|
2018-09-08 21:28:19 +01:00
|
|
|
for (std::size_t i = frames_written; i < num_frames; i++) {
|
2018-09-08 21:07:28 +01:00
|
|
|
std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16));
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
2018-09-08 22:21:49 +01:00
|
|
|
|
2022-05-20 18:17:37 +01:00
|
|
|
// Implementation of the hardware volume slider
|
|
|
|
// A cubic curve is used to approximate a linear change in human-perceived loudness
|
2022-11-04 19:25:57 +00:00
|
|
|
const float linear_volume = std::clamp(Settings::Volume(), 0.0f, 1.0f);
|
2018-09-08 22:21:49 +01:00
|
|
|
if (linear_volume != 1.0) {
|
2022-05-20 18:17:37 +01:00
|
|
|
const float volume_scale_factor = linear_volume * linear_volume * linear_volume;
|
2018-09-08 22:21:49 +01:00
|
|
|
for (std::size_t i = 0; i < num_frames; i++) {
|
|
|
|
buffer[i * 2 + 0] = static_cast<s16>(buffer[i * 2 + 0] * volume_scale_factor);
|
|
|
|
buffer[i * 2 + 1] = static_cast<s16>(buffer[i * 2 + 1] * volume_scale_factor);
|
|
|
|
}
|
|
|
|
}
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|