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"
|
2018-06-30 12:26:38 +00:00
|
|
|
#include "core/settings.h"
|
2017-12-20 18:44:32 +00:00
|
|
|
|
|
|
|
namespace AudioCore {
|
|
|
|
|
|
|
|
DspInterface::DspInterface() = default;
|
2018-09-08 20:07:28 +00:00
|
|
|
DspInterface::~DspInterface() = default;
|
2017-12-20 18:44:32 +00:00
|
|
|
|
2018-07-02 13:03:14 +00:00
|
|
|
void DspInterface::SetSink(const std::string& sink_id, const std::string& audio_device) {
|
2018-09-08 20:28:19 +00:00
|
|
|
sink.reset();
|
2017-12-20 18:44:32 +00:00
|
|
|
const SinkDetails& sink_details = GetSinkDetails(sink_id);
|
2018-07-02 13:03:14 +00:00
|
|
|
sink = sink_details.factory(audio_device);
|
2018-09-08 20:07:28 +00: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) {
|
|
|
|
if (perform_time_stretching == enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enable) {
|
2018-09-08 20:28:19 +00:00
|
|
|
flushing_time_stretcher = true;
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
perform_time_stretching = enable;
|
|
|
|
}
|
|
|
|
|
2018-06-30 12:26:38 +00:00
|
|
|
void DspInterface::OutputFrame(StereoFrame16& frame) {
|
2017-12-20 18:44:32 +00:00
|
|
|
if (!sink)
|
|
|
|
return;
|
|
|
|
|
2018-06-30 12:26:38 +00:00
|
|
|
// Implementation of the hardware volume slider with a dynamic range of 60 dB
|
2018-07-23 21:08:14 +00:00
|
|
|
double volume_scale_factor = std::exp(6.90775 * Settings::values.volume) * 0.001;
|
2018-09-06 20:03:28 +00:00
|
|
|
for (std::size_t i = 0; i < frame.size(); i++) {
|
2018-06-30 12:26:38 +00:00
|
|
|
frame[i][0] = static_cast<s16>(frame[i][0] * volume_scale_factor);
|
|
|
|
frame[i][1] = static_cast<s16>(frame[i][1] * volume_scale_factor);
|
|
|
|
}
|
|
|
|
|
2018-09-08 20:07:28 +00:00
|
|
|
fifo.Push(frame.data(), frame.size());
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 20:28:19 +00:00
|
|
|
void DspInterface::OutputCallback(s16* buffer, std::size_t num_frames) {
|
|
|
|
std::size_t frames_written;
|
|
|
|
if (perform_time_stretching) {
|
|
|
|
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 if (flushing_time_stretcher) {
|
|
|
|
time_stretcher.Flush();
|
|
|
|
frames_written = time_stretcher.Process(nullptr, 0, buffer, num_frames);
|
|
|
|
frames_written += fifo.Pop(buffer, num_frames - frames_written);
|
|
|
|
flushing_time_stretcher = false;
|
|
|
|
} else {
|
|
|
|
frames_written = fifo.Pop(buffer, num_frames);
|
|
|
|
}
|
2018-09-08 20:07:28 +00: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 20:07:28 +00:00
|
|
|
// Hold last emitted frame; this prevents popping.
|
2018-09-08 20:28:19 +00:00
|
|
|
for (std::size_t i = frames_written; i < num_frames; i++) {
|
2018-09-08 20:07:28 +00:00
|
|
|
std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16));
|
2017-12-20 18:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioCore
|