1 // Copyright 2017 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #include <cstddef>
6 #include "audio_core/dsp_interface.h"
7 #include "audio_core/sink.h"
8 #include "audio_core/sink_details.h"
9 #include "common/assert.h"
10 #include "core/core.h"
11 #include "core/dumping/backend.h"
12 #include "core/settings.h"
13 
14 namespace AudioCore {
15 
16 DspInterface::DspInterface() = default;
17 DspInterface::~DspInterface() = default;
18 
SetSink(const std::string & sink_id,const std::string & audio_device)19 void DspInterface::SetSink(const std::string& sink_id, const std::string& audio_device) {
20     sink = CreateSinkFromID(Settings::values.sink_id, Settings::values.audio_device_id);
21     sink->SetCallback(
22         [this](s16* buffer, std::size_t num_frames) { OutputCallback(buffer, num_frames); });
23     time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
24 }
25 
GetSink()26 Sink& DspInterface::GetSink() {
27     ASSERT(sink);
28     return *sink.get();
29 }
30 
EnableStretching(bool enable)31 void DspInterface::EnableStretching(bool enable) {
32     if (perform_time_stretching == enable)
33         return;
34 
35     if (!enable) {
36         flushing_time_stretcher = true;
37     }
38     perform_time_stretching = enable;
39 }
40 
OutputFrame(StereoFrame16 frame)41 void DspInterface::OutputFrame(StereoFrame16 frame) {
42     if (!sink)
43         return;
44 
45     fifo.Push(frame.data(), frame.size());
46 
47     if (Core::System::GetInstance().VideoDumper().IsDumping()) {
48         Core::System::GetInstance().VideoDumper().AddAudioFrame(std::move(frame));
49     }
50 }
51 
OutputSample(std::array<s16,2> sample)52 void DspInterface::OutputSample(std::array<s16, 2> sample) {
53     if (!sink)
54         return;
55 
56     fifo.Push(&sample, 1);
57 
58     if (Core::System::GetInstance().VideoDumper().IsDumping()) {
59         Core::System::GetInstance().VideoDumper().AddAudioSample(std::move(sample));
60     }
61 }
62 
OutputCallback(s16 * buffer,std::size_t num_frames)63 void DspInterface::OutputCallback(s16* buffer, std::size_t num_frames) {
64     std::size_t frames_written;
65     if (perform_time_stretching) {
66         const std::vector<s16> in{fifo.Pop()};
67         const std::size_t num_in{in.size() / 2};
68         frames_written = time_stretcher.Process(in.data(), num_in, buffer, num_frames);
69     } else if (flushing_time_stretcher) {
70         time_stretcher.Flush();
71         frames_written = time_stretcher.Process(nullptr, 0, buffer, num_frames);
72         frames_written += fifo.Pop(buffer, num_frames - frames_written);
73         flushing_time_stretcher = false;
74     } else {
75         frames_written = fifo.Pop(buffer, num_frames);
76     }
77 
78     if (frames_written > 0) {
79         std::memcpy(&last_frame[0], buffer + 2 * (frames_written - 1), 2 * sizeof(s16));
80     }
81 
82     // Hold last emitted frame; this prevents popping.
83     for (std::size_t i = frames_written; i < num_frames; i++) {
84         std::memcpy(buffer + 2 * i, &last_frame[0], 2 * sizeof(s16));
85     }
86 
87     // Implementation of the hardware volume slider with a dynamic range of 60 dB
88     const float linear_volume = std::clamp(Settings::values.volume, 0.0f, 1.0f);
89     if (linear_volume != 1.0) {
90         const float volume_scale_factor =
91             linear_volume == 0 ? 0 : std::exp(6.90775f * linear_volume) * 0.001f;
92         for (std::size_t i = 0; i < num_frames; i++) {
93             buffer[i * 2 + 0] = static_cast<s16>(buffer[i * 2 + 0] * volume_scale_factor);
94             buffer[i * 2 + 1] = static_cast<s16>(buffer[i * 2 + 1] * volume_scale_factor);
95         }
96     }
97 }
98 
99 } // namespace AudioCore
100