Debian Codegraph @

Source/Core/AudioCommon/Mixer.h

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <atomic>
8 #include <bit>
9
10 #include "AudioCommon/SurroundDecoder.h"
11 #include "AudioCommon/WaveFile.h"
12 #include "Common/CommonTypes.h"
13 #include "Common/Config/Config.h"
14 #include "Common/Inline.h"
15
16 class PointerWrap;
17
18 class Mixer final
19 {
20 public:
21 explicit Mixer(u32 BackendSampleRate);
22 ~Mixer();
23
24 void DoState(PointerWrap& p);
25
26 // Called from audio threads
27 std::size_t Mix(s16* samples, std::size_t numSamples);
28 std::size_t MixSurround(float* samples, std::size_t num_samples);
29
30 // Called from main thread
31 void PushSamples(const s16* samples, std::size_t num_samples);
32 void PushStreamingSamples(const s16* samples, std::size_t num_samples);
33 void PushWiimoteSpeakerSamples(std::size_t wiimote_index, const s16* samples,
34 std::size_t num_samples, u32 sample_rate_divisor);
35 void PushSkylanderPortalSamples(const u8* samples, std::size_t num_samples);
36 void PushGBASamples(std::size_t device_number, const s16* samples, std::size_t num_samples);
37
38 u32 GetSampleRate() const { return m_output_sample_rate; }
39 void SetSampleRate(u32 output_sample_rate) { m_output_sample_rate = output_sample_rate; }
40
41 // Note: NullSoundStream sets the sample rate to 0.
42 bool IsOutputSampleRateValid() const { return m_output_sample_rate != 0; }
43
44 void SetDMAInputSampleRateDivisor(u32 rate_divisor);
45 void SetStreamInputSampleRateDivisor(u32 rate_divisor);
46 void SetGBAInputSampleRate(std::size_t device_number, u32 sample_rate);
47
48 void SetStreamingVolume(u32 lvolume, u32 rvolume);
49 void SetWiimoteSpeakerVolume(std::size_t wiimote_index, u32 lvolume, u32 rvolume);
50 std::size_t MixWiimoteSpeaker(std::size_t wiimote_index, s16* samples, std::size_t num_samples);
51 void SetGBAVolume(std::size_t device_number, u32 lvolume, u32 rvolume);
52
53 void StartLogDTKAudio(const std::string& filename);
54 void StopLogDTKAudio();
55
56 void StartLogDSPAudio(const std::string& filename);
57 void StopLogDSPAudio();
58
59 // 54000000 doesn't work here as it doesn't evenly divide with 32000, but 108000000 does
60 static constexpr u64 FIXED_SAMPLE_RATE_DIVIDEND = 54000000 * 2;
61
62 private:
63 const std::size_t SURROUND_CHANNELS = 6;
64
65 class MixerFifo final
66 {
67 static constexpr std::size_t MAX_GRANULE_QUEUE_SIZE = 256;
68 static constexpr std::size_t GRANULE_QUEUE_MASK = MAX_GRANULE_QUEUE_SIZE - 1;
69
70 struct StereoPair final
71 {
72 float l = 0.f;
73 float r = 0.f;
74
75 constexpr StereoPair() = default;
76 constexpr StereoPair(const StereoPair&) = default;
77 constexpr StereoPair& operator=(const StereoPair&) = default;
78 constexpr StereoPair(StereoPair&&) = default;
79 constexpr StereoPair& operator=(StereoPair&&) = default;
80
81 constexpr StereoPair(float mono) : l(mono), r(mono) {}
82 constexpr StereoPair(float left, float right) : l(left), r(right) {}
83 constexpr StereoPair(s16 left, s16 right) : l(left), r(right) {}
84
85 StereoPair operator+(const StereoPair& other) const
86 {
87 return StereoPair(l + other.l, r + other.r);
88 }
89
90 StereoPair operator*(const StereoPair& other) const
91 {
92 return StereoPair(l * other.l, r * other.r);
93 }
94 };
95
96 static constexpr std::size_t GRANULE_SIZE = 256;
97 static constexpr std::size_t GRANULE_OVERLAP = GRANULE_SIZE / 2;
98 static constexpr std::size_t GRANULE_MASK = GRANULE_SIZE - 1;
99 static constexpr std::size_t GRANULE_BITS = std::countr_one(GRANULE_MASK);
100 static constexpr std::size_t GRANULE_FRAC_BITS = 32 - GRANULE_BITS;
101
102 using Granule = std::array<StereoPair, GRANULE_SIZE>;
103
104 public:
105 MixerFifo(Mixer* mixer, u32 sample_rate_divisor,
106 u32 sample_rate_dividend = FIXED_SAMPLE_RATE_DIVIDEND)
107 : m_mixer(mixer), m_input_sample_rate_dividend(sample_rate_dividend),
108 m_input_sample_rate_divisor(sample_rate_divisor)
109 {
110 }
111 void DoState(PointerWrap& p);
112
113 DOLPHIN_FORCE_INLINE void PushSample(s16 left, s16 right)
114 {
115 m_next_buffer[m_next_buffer_index] = {left, right};
116 m_next_buffer_index = (m_next_buffer_index + 1) & GRANULE_MASK;
117
118 // The granules overlap by 50%, so we need to enqueue the
119 // next buffer every time we fill half of the samples.
120 if (m_next_buffer_index == 0 || m_next_buffer_index == m_next_buffer.size() / 2)
121 Enqueue();
122 }
123
124 void Mix(s16* samples, std::size_t num_samples);
125
126 void SetInputSampleRateDividend(u32 rate_dividend);
127 u32 GetInputSampleRateDividend() const;
128
129 void SetInputSampleRateDivisor(u32 rate_divisor);
130 u32 GetInputSampleRateDivisor() const;
131
132 void SetVolume(u32 lvolume, u32 rvolume);
133 std::pair<s32, s32> GetVolume() const;
134
135 private:
136 Mixer* m_mixer;
137
138 // All non-GBA MixerFifo instances use FIXED_SAMPLE_RATE_DIVIDEND.
139 u32 m_input_sample_rate_dividend;
140 u32 m_input_sample_rate_divisor;
141
142 Granule m_next_buffer{};
143 std::size_t m_next_buffer_index = 0;
144
145 u32 m_current_index = 0;
146 Granule m_front, m_back;
147
148 std::atomic<std::size_t> m_granule_queue_size{20};
149 std::array<Granule, MAX_GRANULE_QUEUE_SIZE> m_queue;
150 std::atomic<std::size_t> m_queue_head{0};
151 std::atomic<std::size_t> m_queue_tail{0};
152 std::atomic<bool> m_queue_fading{false};
153 std::atomic<bool> m_queue_looping{false};
154 float m_fade_volume = 1.0;
155
156 void Enqueue();
157 bool Dequeue(Granule* granule);
158
159 // Volume ranges from 0-256
160 std::atomic<s32> m_LVolume{256};
161 std::atomic<s32> m_RVolume{256};
162
163 StereoPair m_quantization_error;
164 };
165
166 void RefreshConfig();
167
168 MixerFifo m_dma_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 32000};
169 MixerFifo m_streaming_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000};
170 std::array<MixerFifo, 4> m_wiimote_speaker_mixers{
171 MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 3000},
172 MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 3000},
173 MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 3000},
174 MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 3000},
175 };
176 MixerFifo m_skylander_portal_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 8000};
177
178 // GBAs generally use a 65536 sample rate which is not a factor of our FIXED_SAMPLE_RATE_DIVIDEND.
179 static constexpr u32 GBA_SAMPLE_RATE_DIVIDEND = 0x1000000;
180
181 std::array<MixerFifo, 4> m_gba_mixers{
182 MixerFifo{this, GBA_SAMPLE_RATE_DIVIDEND / 65536, GBA_SAMPLE_RATE_DIVIDEND},
183 MixerFifo{this, GBA_SAMPLE_RATE_DIVIDEND / 65536, GBA_SAMPLE_RATE_DIVIDEND},
184 MixerFifo{this, GBA_SAMPLE_RATE_DIVIDEND / 65536, GBA_SAMPLE_RATE_DIVIDEND},
185 MixerFifo{this, GBA_SAMPLE_RATE_DIVIDEND / 65536, GBA_SAMPLE_RATE_DIVIDEND},
186 };
187 u32 m_output_sample_rate;
188
189 AudioCommon::SurroundDecoder m_surround_decoder;
190
191 WaveFileWriter m_wave_writer_dtk;
192 WaveFileWriter m_wave_writer_dsp;
193
194 bool m_log_dtk_audio = false;
195 bool m_log_dsp_audio = false;
196
197 float m_config_emulation_speed;
198 bool m_config_audio_preserve_pitch;
199 bool m_config_fill_audio_gaps;
200 int m_config_audio_buffer_ms;
201 bool m_config_wiimote_routing_enabled = false;
202 std::array<bool, 4> m_config_wiimote_output_enabled{};
203
204 Config::ConfigChangedCallbackID m_config_changed_callback_id;
205 };