Debian Codegraph @

Source/Core/AudioCommon/Mixer.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "AudioCommon/Mixer.h"
5
6 #include <algorithm>
7 #include <cmath>
8 #include <cstring>
9 #include <span>
10
11 #include "AudioCommon/Enums.h"
12 #include "Common/ChunkFile.h"
13 #include "Common/CommonTypes.h"
14 #include "Common/Logging/Log.h"
15 #include "Common/MathUtil.h"
16 #include "Common/Swap.h"
17 #include "Core/Config/MainSettings.h"
18 #include "Core/Core.h"
19 #include "Core/System.h"
20
21 static u32 DPL2QualityToFrameBlockSize(AudioCommon::DPL2Quality quality)
22 {
23 switch (quality)
24 {
25 case AudioCommon::DPL2Quality::Lowest:
26 return 512;
27 case AudioCommon::DPL2Quality::Low:
28 return 1024;
29 case AudioCommon::DPL2Quality::Highest:
30 return 4096;
31 default:
32 return 2048;
33 }
34 }
35
36 Mixer::Mixer(u32 BackendSampleRate)
37 : m_output_sample_rate(BackendSampleRate),
38 m_surround_decoder(BackendSampleRate,
39 DPL2QualityToFrameBlockSize(Config::Get(Config::MAIN_DPL2_QUALITY)))
40 {
41 m_config_changed_callback_id = Config::AddConfigChangedCallback([this] { RefreshConfig(); });
42 RefreshConfig();
43
44 INFO_LOG_FMT(AUDIO_INTERFACE, "Mixer is initialized");
45 }
46
47 Mixer::~Mixer()
48 {
49 Config::RemoveConfigChangedCallback(m_config_changed_callback_id);
50 }
51
52 void Mixer::DoState(PointerWrap& p)
53 {
54 m_dma_mixer.DoState(p);
55 m_streaming_mixer.DoState(p);
56 for (auto& mixer : m_wiimote_speaker_mixers)
57 mixer.DoState(p);
58 m_skylander_portal_mixer.DoState(p);
59 for (auto& mixer : m_gba_mixers)
60 mixer.DoState(p);
61 }
62
63 // Executed from sound stream thread
64 void Mixer::MixerFifo::Mix(s16* samples, std::size_t num_samples)
65 {
66 constexpr u32 INDEX_HALF = 0x80000000;
67 constexpr DT_s FADE_IN_RC = DT_s(0.008);
68 constexpr DT_s FADE_OUT_RC = DT_s(0.064);
69
70 // We need at least a double because the index jump has 24 bits of fractional precision.
71 const double out_sample_rate = m_mixer->m_output_sample_rate;
72 double in_sample_rate = double(m_input_sample_rate_dividend) / m_input_sample_rate_divisor;
73
74 const double emulation_speed = m_mixer->m_config_emulation_speed;
75 if (!m_mixer->m_config_audio_preserve_pitch && 0 < emulation_speed && emulation_speed != 1.0)
76 in_sample_rate *= emulation_speed;
77
78 const double base = static_cast<double>(1 << GRANULE_FRAC_BITS);
79 const u32 index_jump = std::lround(base * in_sample_rate / out_sample_rate);
80
81 // These fade in / out multiplier are tuned to match a constant
82 // fade speed regardless of the input or the output sample rate.
83 const float fade_in_mul = -std::expm1(-DT_s(1.0) / (out_sample_rate * FADE_IN_RC));
84 const float fade_out_mul = -std::expm1(-DT_s(1.0) / (out_sample_rate * FADE_OUT_RC));
85
86 const StereoPair volume{m_LVolume.load() / 256.0f, m_RVolume.load() / 256.0f};
87
88 // Calculate the ideal length of the granule queue.
89 const std::size_t buffer_size_ms = m_mixer->m_config_audio_buffer_ms;
90 const std::size_t buffer_size_samples = std::llround(buffer_size_ms * in_sample_rate / 1000.0);
91
92 // Limit the possible queue sizes to any number between 4 and 64.
93 const std::size_t buffer_size_granules =
94 std::clamp((buffer_size_samples) / (GRANULE_SIZE >> 1), static_cast<std::size_t>(4),
95 static_cast<std::size_t>(MAX_GRANULE_QUEUE_SIZE));
96
97 bool fade_audio = m_queue_fading.load(std::memory_order_relaxed);
98
99 m_granule_queue_size.store(buffer_size_granules, std::memory_order_relaxed);
100
101 while (num_samples-- > 0)
102 {
103 // The indexes for the front and back buffers are offset by 50% of the granule size.
104 // We use the modular nature of 32-bit integers to wrap around the granule size.
105 m_current_index += index_jump;
106 const u32 front_index = m_current_index;
107 const u32 back_index = m_current_index + INDEX_HALF;
108
109 // If either index is less than the index jump, that means we reached
110 // the end of the of the buffer and need to load the next granule.
111 if (front_index < index_jump)
112 fade_audio = Dequeue(&m_front);
113 else if (back_index < index_jump)
114 fade_audio = Dequeue(&m_back);
115
116 // The Granules are pre-windowed, so we can just add them together
117 const std::size_t ft = front_index >> GRANULE_FRAC_BITS;
118 const std::size_t bt = back_index >> GRANULE_FRAC_BITS;
119 const StereoPair s0 = m_front[(ft - 2) & GRANULE_MASK] + m_back[(bt - 2) & GRANULE_MASK];
120 const StereoPair s1 = m_front[(ft - 1) & GRANULE_MASK] + m_back[(bt - 1) & GRANULE_MASK];
121 const StereoPair s2 = m_front[(ft + 0) & GRANULE_MASK] + m_back[(bt + 0) & GRANULE_MASK];
122 const StereoPair s3 = m_front[(ft + 1) & GRANULE_MASK] + m_back[(bt + 1) & GRANULE_MASK];
123 const StereoPair s4 = m_front[(ft + 2) & GRANULE_MASK] + m_back[(bt + 2) & GRANULE_MASK];
124 const StereoPair s5 = m_front[(ft + 3) & GRANULE_MASK] + m_back[(bt + 3) & GRANULE_MASK];
125
126 // Polynomial Interpolators for High-Quality Resampling of
127 // Over Sampled Audio by Olli Niemitalo, October 2001.
128 // Page 43 -- 6-point, 3rd-order Hermite:
129 // https://yehar.com/blog/wp-content/uploads/2009/08/deip.pdf
130 const u32 t_frac = m_current_index & ((1 << GRANULE_FRAC_BITS) - 1);
131 const float t1 = t_frac / static_cast<float>(1 << GRANULE_FRAC_BITS);
132 const float t2 = t1 * t1;
133 const float t3 = t2 * t1;
134
135 StereoPair sample = (s0 * StereoPair{(+0.0f + 1.0f * t1 - 2.0f * t2 + 1.0f * t3) / 12.0f} +
136 s1 * StereoPair{(+0.0f - 8.0f * t1 + 15.0f * t2 - 7.0f * t3) / 12.0f} +
137 s2 * StereoPair{(+3.0f + 0.0f * t1 - 7.0f * t2 + 4.0f * t3) / 3.0f} +
138 s3 * StereoPair{(+0.0f + 2.0f * t1 + 5.0f * t2 - 4.0f * t3) / 3.0f} +
139 s4 * StereoPair{(+0.0f - 1.0f * t1 - 6.0f * t2 + 7.0f * t3) / 12.0f} +
140 s5 * StereoPair{(+0.0f + 0.0f * t1 + 1.0f * t2 - 1.0f * t3) / 12.0f});
141
142 // Apply Fade In / Fade Out depending on if we are looping
143 if (fade_audio)
144 m_fade_volume += fade_out_mul * (0.0f - m_fade_volume);
145 else
146 m_fade_volume += fade_in_mul * (1.0f - m_fade_volume);
147
148 // Apply the fade volume and the regular volume to the sample
149 sample = sample * volume * StereoPair{m_fade_volume};
150
151 // This quantization method prevents accumulated error but does not do noise shaping.
152 sample.l += samples[0] - m_quantization_error.l;
153 samples[0] = MathUtil::SaturatingCast<s16>(std::lround(sample.l));
154 m_quantization_error.l = std::clamp(samples[0] - sample.l, -1.0f, 1.0f);
155
156 sample.r += samples[1] - m_quantization_error.r;
157 samples[1] = MathUtil::SaturatingCast<s16>(std::lround(sample.r));
158 m_quantization_error.r = std::clamp(samples[1] - sample.r, -1.0f, 1.0f);
159
160 samples += 2;
161 }
162 }
163
164 std::size_t Mixer::Mix(s16* samples, std::size_t num_samples)
165 {
166 if (!samples)
167 return 0;
168
169 memset(samples, 0, num_samples * 2 * sizeof(s16));
170
171 m_dma_mixer.Mix(samples, num_samples);
172 m_streaming_mixer.Mix(samples, num_samples);
173 for (std::size_t i = 0; i < m_wiimote_speaker_mixers.size(); ++i)
174 {
175 if (!m_config_wiimote_routing_enabled || !m_config_wiimote_output_enabled[i])
176 m_wiimote_speaker_mixers[i].Mix(samples, num_samples);
177 }
178 m_skylander_portal_mixer.Mix(samples, num_samples);
179 for (auto& mixer : m_gba_mixers)
180 mixer.Mix(samples, num_samples);
181
182 return num_samples;
183 }
184
185 std::size_t Mixer::MixSurround(float* samples, std::size_t num_samples)
186 {
187 if (!num_samples)
188 return 0;
189
190 memset(samples, 0, num_samples * SURROUND_CHANNELS * sizeof(float));
191
192 const std::size_t needed_frames =
193 m_surround_decoder.QueryFramesNeededForSurroundOutput(num_samples);
194
195 constexpr std::size_t max_samples = 0x8000;
196 ASSERT_MSG(AUDIO, needed_frames <= max_samples,
197 "needed_frames would overflow m_scratch_buffer: {} -> {} > {}", num_samples,
198 needed_frames, max_samples);
199
200 std::array<s16, max_samples> buffer;
201 const std::size_t available_frames = Mix(buffer.data(), static_cast<std::size_t>(needed_frames));
202 if (available_frames != needed_frames)
203 {
204 ERROR_LOG_FMT(AUDIO,
205 "Error decoding surround frames: needed {} frames for {} samples but got {}",
206 needed_frames, num_samples, available_frames);
207 return 0;
208 }
209
210 m_surround_decoder.PutFrames(buffer.data(), needed_frames);
211 m_surround_decoder.ReceiveFrames(samples, num_samples);
212
213 return num_samples;
214 }
215
216 void Mixer::PushSamples(const s16* samples, std::size_t num_samples)
217 {
218 if (IsOutputSampleRateValid())
219 {
220 // Big-endian RL-orderered stereo samples.
221
222 const s16* ptr = samples;
223 for (std::size_t i = 0; i != num_samples; ++i)
224 {
225 m_dma_mixer.PushSample(Common::swap16(ptr[1]), Common::swap16(ptr[0]));
226 ptr += 2;
227 }
228 }
229
230 if (m_log_dsp_audio)
231 {
232 const s32 sample_rate_divisor = m_dma_mixer.GetInputSampleRateDivisor();
233 auto const volume = m_dma_mixer.GetVolume();
234 m_wave_writer_dsp.AddStereoSamplesBE(samples, static_cast<u32>(num_samples),
235 sample_rate_divisor, volume.first, volume.second);
236 }
237 }
238
239 void Mixer::PushStreamingSamples(const s16* samples, std::size_t num_samples)
240 {
241 if (IsOutputSampleRateValid())
242 {
243 // Big-endian RL-orderered stereo samples.
244
245 const s16* ptr = samples;
246 for (std::size_t i = 0; i != num_samples; ++i)
247 {
248 m_streaming_mixer.PushSample(Common::swap16(ptr[1]), Common::swap16(ptr[0]));
249 ptr += 2;
250 }
251 }
252
253 if (m_log_dtk_audio)
254 {
255 const s32 sample_rate_divisor = m_streaming_mixer.GetInputSampleRateDivisor();
256 const auto volume = m_streaming_mixer.GetVolume();
257 m_wave_writer_dtk.AddStereoSamplesBE(samples, static_cast<u32>(num_samples),
258 sample_rate_divisor, volume.first, volume.second);
259 }
260 }
261
262 void Mixer::PushWiimoteSpeakerSamples(std::size_t wiimote_index, const s16* samples,
263 std::size_t num_samples, u32 sample_rate_divisor)
264 {
265 if (!IsOutputSampleRateValid() || wiimote_index >= m_wiimote_speaker_mixers.size())
266 return;
267
268 // WiimoteEmu produces host-endian mono samples.
269
270 m_wiimote_speaker_mixers[wiimote_index].SetInputSampleRateDivisor(sample_rate_divisor);
271
272 for (const s16 sample : std::span{samples, num_samples})
273 {
274 m_wiimote_speaker_mixers[wiimote_index].PushSample(sample, sample);
275 }
276 }
277
278 std::size_t Mixer::MixWiimoteSpeaker(std::size_t wiimote_index, s16* samples,
279 std::size_t num_samples)
280 {
281 if (!samples || wiimote_index >= m_wiimote_speaker_mixers.size())
282 return 0;
283
284 memset(samples, 0, num_samples * 2 * sizeof(s16));
285 m_wiimote_speaker_mixers[wiimote_index].Mix(samples, num_samples);
286 return num_samples;
287 }
288
289 void Mixer::PushSkylanderPortalSamples(const u8* samples, std::size_t num_samples)
290 {
291 if (!IsOutputSampleRateValid())
292 return;
293
294 // Skylander samples are always supplied as 64 bytes, 32 x 16 bit samples
295 // The portal speaker is 1 channel, so duplicate and play as stereo audio
296
297 while (num_samples--)
298 {
299 // Little-endian data.
300 const s16 sample = u16(samples[0] | u16(samples[1] << 8u));
301 m_skylander_portal_mixer.PushSample(sample, sample);
302 samples += 2;
303 }
304 }
305
306 void Mixer::PushGBASamples(std::size_t device_number, const s16* samples, std::size_t num_samples)
307 {
308 if (!IsOutputSampleRateValid())
309 return;
310
311 // Integrated GBA pushes host-endian LR-ordered stereo samples.
312
313 while (num_samples--)
314 {
315 m_gba_mixers[device_number].PushSample(samples[0], samples[1]);
316 samples += 2;
317 }
318 }
319
320 void Mixer::SetDMAInputSampleRateDivisor(u32 rate_divisor)
321 {
322 m_dma_mixer.SetInputSampleRateDivisor(rate_divisor);
323 }
324
325 void Mixer::SetStreamInputSampleRateDivisor(u32 rate_divisor)
326 {
327 m_streaming_mixer.SetInputSampleRateDivisor(rate_divisor);
328 }
329
330 void Mixer::SetGBAInputSampleRate(std::size_t device_number, u32 sample_rate)
331 {
332 m_gba_mixers[device_number].SetInputSampleRateDivisor(GBA_SAMPLE_RATE_DIVIDEND / sample_rate);
333 }
334
335 void Mixer::SetStreamingVolume(u32 lvolume, u32 rvolume)
336 {
337 m_streaming_mixer.SetVolume(std::clamp<u32>(lvolume, 0x00, 0xff),
338 std::clamp<u32>(rvolume, 0x00, 0xff));
339 }
340
341 void Mixer::SetWiimoteSpeakerVolume(std::size_t wiimote_index, u32 lvolume, u32 rvolume)
342 {
343 if (wiimote_index < m_wiimote_speaker_mixers.size())
344 m_wiimote_speaker_mixers[wiimote_index].SetVolume(lvolume, rvolume);
345 }
346
347 void Mixer::SetGBAVolume(std::size_t device_number, u32 lvolume, u32 rvolume)
348 {
349 m_gba_mixers[device_number].SetVolume(lvolume, rvolume);
350 }
351
352 void Mixer::StartLogDTKAudio(const std::string& filename)
353 {
354 if (!m_log_dtk_audio)
355 {
356 const bool success =
357 m_wave_writer_dtk.Start(filename, m_streaming_mixer.GetInputSampleRateDivisor());
358 if (success)
359 {
360 m_log_dtk_audio = true;
361 m_wave_writer_dtk.SetSkipSilence(false);
362 NOTICE_LOG_FMT(AUDIO, "Starting DTK Audio logging");
363 }
364 else
365 {
366 m_wave_writer_dtk.Stop();
367 NOTICE_LOG_FMT(AUDIO, "Unable to start DTK Audio logging");
368 }
369 }
370 else
371 {
372 WARN_LOG_FMT(AUDIO, "DTK Audio logging has already been started");
373 }
374 }
375
376 void Mixer::StopLogDTKAudio()
377 {
378 if (m_log_dtk_audio)
379 {
380 m_log_dtk_audio = false;
381 m_wave_writer_dtk.Stop();
382 NOTICE_LOG_FMT(AUDIO, "Stopping DTK Audio logging");
383 }
384 else
385 {
386 WARN_LOG_FMT(AUDIO, "DTK Audio logging has already been stopped");
387 }
388 }
389
390 void Mixer::StartLogDSPAudio(const std::string& filename)
391 {
392 if (!m_log_dsp_audio)
393 {
394 const bool success = m_wave_writer_dsp.Start(filename, m_dma_mixer.GetInputSampleRateDivisor());
395 if (success)
396 {
397 m_log_dsp_audio = true;
398 m_wave_writer_dsp.SetSkipSilence(false);
399 NOTICE_LOG_FMT(AUDIO, "Starting DSP Audio logging");
400 }
401 else
402 {
403 m_wave_writer_dsp.Stop();
404 NOTICE_LOG_FMT(AUDIO, "Unable to start DSP Audio logging");
405 }
406 }
407 else
408 {
409 WARN_LOG_FMT(AUDIO, "DSP Audio logging has already been started");
410 }
411 }
412
413 void Mixer::StopLogDSPAudio()
414 {
415 if (m_log_dsp_audio)
416 {
417 m_log_dsp_audio = false;
418 m_wave_writer_dsp.Stop();
419 NOTICE_LOG_FMT(AUDIO, "Stopping DSP Audio logging");
420 }
421 else
422 {
423 WARN_LOG_FMT(AUDIO, "DSP Audio logging has already been stopped");
424 }
425 }
426
427 void Mixer::RefreshConfig()
428 {
429 m_config_emulation_speed = Config::Get(Config::MAIN_EMULATION_SPEED);
430 m_config_audio_preserve_pitch = Config::Get(Config::MAIN_AUDIO_PRESERVE_PITCH);
431 m_config_fill_audio_gaps = Config::Get(Config::MAIN_AUDIO_FILL_GAPS);
432 m_config_audio_buffer_ms = Config::Get(Config::MAIN_AUDIO_BUFFER_SIZE);
433 m_config_wiimote_routing_enabled = Config::Get(Config::MAIN_WIIMOTE_AUDIO_ROUTING_ENABLED);
434 for (std::size_t i = 0; i < m_config_wiimote_output_enabled.size(); ++i)
435 m_config_wiimote_output_enabled[i] = Config::Get(Config::MAIN_WIIMOTE_AUDIO_OUTPUT_ENABLED[i]);
436 }
437
438 void Mixer::MixerFifo::DoState(PointerWrap& p)
439 {
440 p.Do(m_input_sample_rate_divisor);
441 p.Do(m_LVolume);
442 p.Do(m_RVolume);
443 }
444
445 void Mixer::MixerFifo::SetInputSampleRateDividend(u32 rate_dividend)
446 {
447 m_input_sample_rate_dividend = rate_dividend;
448 }
449
450 u32 Mixer::MixerFifo::GetInputSampleRateDividend() const
451 {
452 return m_input_sample_rate_dividend;
453 }
454
455 void Mixer::MixerFifo::SetInputSampleRateDivisor(u32 rate_divisor)
456 {
457 m_input_sample_rate_divisor = rate_divisor;
458 }
459
460 u32 Mixer::MixerFifo::GetInputSampleRateDivisor() const
461 {
462 return m_input_sample_rate_divisor;
463 }
464
465 void Mixer::MixerFifo::SetVolume(u32 lvolume, u32 rvolume)
466 {
467 m_LVolume.store(lvolume + (lvolume >> 7));
468 m_RVolume.store(rvolume + (rvolume >> 7));
469 }
470
471 std::pair<s32, s32> Mixer::MixerFifo::GetVolume() const
472 {
473 return std::make_pair(m_LVolume.load(), m_RVolume.load());
474 }
475
476 void Mixer::MixerFifo::Enqueue()
477 {
478 // import numpy as np
479 // import scipy.signal as signal
480 // window = np.convolve(np.ones(128), signal.windows.dpss(128 + 1, 4))
481 // window /= (window[:len(window) // 2] + window[len(window) // 2:]).max()
482 // elements = ", ".join([f"{x:.10f}f" for x in window])
483 // print(f'constexpr std::array<StereoPair, GRANULE_SIZE> GRANULE_WINDOW = {{ {elements}
484 // }};')
485 static constexpr std::array<StereoPair, GRANULE_SIZE> GRANULE_WINDOW = {
486 0.0000016272f, 0.0000050749f, 0.0000113187f, 0.0000216492f, 0.0000377350f, 0.0000616906f,
487 0.0000961509f, 0.0001443499f, 0.0002102045f, 0.0002984010f, 0.0004144844f, 0.0005649486f,
488 0.0007573262f, 0.0010002765f, 0.0013036694f, 0.0016786636f, 0.0021377783f, 0.0026949534f,
489 0.0033656000f, 0.0041666352f, 0.0051165029f, 0.0062351752f, 0.0075441359f, 0.0090663409f,
490 0.0108261579f, 0.0128492811f, 0.0151626215f, 0.0177941726f, 0.0207728499f, 0.0241283062f,
491 0.0278907219f, 0.0320905724f, 0.0367583739f, 0.0419244083f, 0.0476184323f, 0.0538693708f,
492 0.0607049996f, 0.0681516192f, 0.0762337261f, 0.0849736833f, 0.0943913952f, 0.1045039915f,
493 0.1153255250f, 0.1268666867f, 0.1391345431f, 0.1521323012f, 0.1658591025f, 0.1803098534f,
494 0.1954750915f, 0.2113408944f, 0.2278888303f, 0.2450959552f, 0.2629348550f, 0.2813737361f,
495 0.3003765625f, 0.3199032396f, 0.3399098438f, 0.3603488941f, 0.3811696664f, 0.4023185434f,
496 0.4237393998f, 0.4453740162f, 0.4671625177f, 0.4890438330f, 0.5109561670f, 0.5328374823f,
497 0.5546259838f, 0.5762606002f, 0.5976814566f, 0.6188303336f, 0.6396511059f, 0.6600901562f,
498 0.6800967604f, 0.6996234375f, 0.7186262639f, 0.7370651450f, 0.7549040448f, 0.7721111697f,
499 0.7886591056f, 0.8045249085f, 0.8196901466f, 0.8341408975f, 0.8478676988f, 0.8608654569f,
500 0.8731333133f, 0.8846744750f, 0.8954960085f, 0.9056086048f, 0.9150263167f, 0.9237662739f,
501 0.9318483808f, 0.9392950004f, 0.9461306292f, 0.9523815677f, 0.9580755917f, 0.9632416261f,
502 0.9679094276f, 0.9721092781f, 0.9758716938f, 0.9792271501f, 0.9822058274f, 0.9848373785f,
503 0.9871507189f, 0.9891738421f, 0.9909336591f, 0.9924558641f, 0.9937648248f, 0.9948834971f,
504 0.9958333648f, 0.9966344000f, 0.9973050466f, 0.9978622217f, 0.9983213364f, 0.9986963306f,
505 0.9989997235f, 0.9992426738f, 0.9994350514f, 0.9995855156f, 0.9997015990f, 0.9997897955f,
506 0.9998556501f, 0.9999038491f, 0.9999383094f, 0.9999622650f, 0.9999783508f, 0.9999886813f,
507 0.9999949251f, 0.9999983728f, 0.9999983728f, 0.9999949251f, 0.9999886813f, 0.9999783508f,
508 0.9999622650f, 0.9999383094f, 0.9999038491f, 0.9998556501f, 0.9997897955f, 0.9997015990f,
509 0.9995855156f, 0.9994350514f, 0.9992426738f, 0.9989997235f, 0.9986963306f, 0.9983213364f,
510 0.9978622217f, 0.9973050466f, 0.9966344000f, 0.9958333648f, 0.9948834971f, 0.9937648248f,
511 0.9924558641f, 0.9909336591f, 0.9891738421f, 0.9871507189f, 0.9848373785f, 0.9822058274f,
512 0.9792271501f, 0.9758716938f, 0.9721092781f, 0.9679094276f, 0.9632416261f, 0.9580755917f,
513 0.9523815677f, 0.9461306292f, 0.9392950004f, 0.9318483808f, 0.9237662739f, 0.9150263167f,
514 0.9056086048f, 0.8954960085f, 0.8846744750f, 0.8731333133f, 0.8608654569f, 0.8478676988f,
515 0.8341408975f, 0.8196901466f, 0.8045249085f, 0.7886591056f, 0.7721111697f, 0.7549040448f,
516 0.7370651450f, 0.7186262639f, 0.6996234375f, 0.6800967604f, 0.6600901562f, 0.6396511059f,
517 0.6188303336f, 0.5976814566f, 0.5762606002f, 0.5546259838f, 0.5328374823f, 0.5109561670f,
518 0.4890438330f, 0.4671625177f, 0.4453740162f, 0.4237393998f, 0.4023185434f, 0.3811696664f,
519 0.3603488941f, 0.3399098438f, 0.3199032396f, 0.3003765625f, 0.2813737361f, 0.2629348550f,
520 0.2450959552f, 0.2278888303f, 0.2113408944f, 0.1954750915f, 0.1803098534f, 0.1658591025f,
521 0.1521323012f, 0.1391345431f, 0.1268666867f, 0.1153255250f, 0.1045039915f, 0.0943913952f,
522 0.0849736833f, 0.0762337261f, 0.0681516192f, 0.0607049996f, 0.0538693708f, 0.0476184323f,
523 0.0419244083f, 0.0367583739f, 0.0320905724f, 0.0278907219f, 0.0241283062f, 0.0207728499f,
524 0.0177941726f, 0.0151626215f, 0.0128492811f, 0.0108261579f, 0.0090663409f, 0.0075441359f,
525 0.0062351752f, 0.0051165029f, 0.0041666352f, 0.0033656000f, 0.0026949534f, 0.0021377783f,
526 0.0016786636f, 0.0013036694f, 0.0010002765f, 0.0007573262f, 0.0005649486f, 0.0004144844f,
527 0.0002984010f, 0.0002102045f, 0.0001443499f, 0.0000961509f, 0.0000616906f, 0.0000377350f,
528 0.0000216492f, 0.0000113187f, 0.0000050749f, 0.0000016272f};
529
530 const std::size_t head = m_queue_head.load(std::memory_order_acquire);
531
532 // Check if we run out of space in the circular queue. (rare)
533 const std::size_t next_head = (head + 1) & GRANULE_QUEUE_MASK;
534 if (next_head == m_queue_tail.load(std::memory_order_acquire))
535 {
536 WARN_LOG_FMT(AUDIO,
537 "Granule Queue has completely filled and audio samples are being dropped. "
538 "This should not happen unless the audio backend has stopped requesting audio.");
539 return;
540 }
541
542 // By preconstructing the granule window, we have the best chance of
543 // the compiler optimizing this loop using SIMD instructions.
544 const std::size_t start_index = m_next_buffer_index;
545 for (std::size_t i = 0; i < GRANULE_SIZE; ++i)
546 m_queue[head][i] = m_next_buffer[(i + start_index) & GRANULE_MASK] * GRANULE_WINDOW[i];
547
548 m_queue_head.store(next_head, std::memory_order_release);
549 m_queue_fading.store(false, std::memory_order_relaxed);
550 m_queue_looping.store(false, std::memory_order_relaxed);
551 }
552
553 bool Mixer::MixerFifo::Dequeue(Granule* granule)
554 {
555 const std::size_t granule_queue_size = m_granule_queue_size.load(std::memory_order_relaxed);
556 const std::size_t head = m_queue_head.load(std::memory_order_acquire);
557 std::size_t tail = m_queue_tail.load(std::memory_order_acquire);
558
559 // Checks to see if the queue has gotten too long.
560 if (granule_queue_size < ((head - tail) & GRANULE_QUEUE_MASK))
561 {
562 // Jump the playhead to half the queue size behind the head.
563 const std::size_t gap = (granule_queue_size >> 1) + 1;
564 tail = (head - gap) & GRANULE_QUEUE_MASK;
565 }
566
567 // Checks to see if the queue is empty.
568 std::size_t next_tail = (tail + 1) & GRANULE_QUEUE_MASK;
569 if (next_tail == head)
570 {
571 // Only fill gaps when running to prevent stutter on pause.
572 const bool is_running = Core::GetState(Core::System::GetInstance()) == Core::State::Running;
573 if (m_mixer->m_config_fill_audio_gaps && is_running)
574 {
575 // Jump the playhead to half the queue size behind the head.
576 // This provides smoother audio playback than suddenly stopping.
577 const std::size_t gap = std::max<std::size_t>(2, granule_queue_size >> 1) - 1;
578 next_tail = (head - gap) & GRANULE_QUEUE_MASK;
579
580 bool looping = m_queue_looping.load(std::memory_order_relaxed);
581 m_queue_fading.store(looping, std::memory_order_relaxed);
582 m_queue_looping.store(true, std::memory_order_relaxed);
583 }
584 else
585 {
586 std::fill(granule->begin(), granule->end(), StereoPair{0.0f, 0.0f});
587 m_queue_fading.store(false, std::memory_order_relaxed);
588 m_queue_looping.store(false, std::memory_order_relaxed);
589 return false;
590 }
591 }
592
593 *granule = m_queue[tail];
594 m_queue_tail.store(next_tail, std::memory_order_release);
595
596 return m_queue_fading.load(std::memory_order_relaxed);
597 }