2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-03-26 09:29:14 +00:00
|
|
|
|
2014-02-10 13:54:46 -05:00
|
|
|
#pragma once
|
2009-03-26 09:29:14 +00:00
|
|
|
|
2025-03-02 20:19:20 -05:00
|
|
|
#include <algorithm>
|
2016-01-14 01:19:25 -05:00
|
|
|
#include <array>
|
2015-05-09 23:50:45 -04:00
|
|
|
#include <atomic>
|
2025-03-02 20:19:20 -05:00
|
|
|
#include <bit>
|
|
|
|
#include <cmath>
|
2014-03-12 15:33:41 -04:00
|
|
|
|
2017-08-09 16:57:26 -03:00
|
|
|
#include "AudioCommon/SurroundDecoder.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "AudioCommon/WaveFile.h"
|
2016-01-14 01:19:25 -05:00
|
|
|
#include "Common/CommonTypes.h"
|
2023-08-16 21:37:12 +02:00
|
|
|
#include "Common/Config/Config.h"
|
2011-02-11 18:59:42 +00:00
|
|
|
|
2017-11-06 09:15:05 +01:00
|
|
|
class PointerWrap;
|
|
|
|
|
2017-06-26 14:41:12 -07:00
|
|
|
class Mixer final
|
2015-09-26 17:13:07 -04:00
|
|
|
{
|
2009-03-26 09:29:14 +00:00
|
|
|
public:
|
2025-03-02 20:19:20 -05:00
|
|
|
explicit Mixer(u32 BackendSampleRate);
|
2017-06-26 14:41:12 -07:00
|
|
|
~Mixer();
|
2010-07-23 23:51:34 +00:00
|
|
|
|
2017-11-06 09:15:05 +01:00
|
|
|
void DoState(PointerWrap& p);
|
|
|
|
|
2009-03-26 09:29:14 +00:00
|
|
|
// Called from audio threads
|
2025-03-02 20:19:20 -05:00
|
|
|
std::size_t Mix(s16* samples, std::size_t numSamples);
|
|
|
|
std::size_t MixSurround(float* samples, std::size_t num_samples);
|
2009-12-20 02:23:26 +00:00
|
|
|
|
2009-03-26 09:29:14 +00:00
|
|
|
// Called from main thread
|
2025-03-02 20:19:20 -05:00
|
|
|
void PushSamples(const s16* samples, std::size_t num_samples);
|
|
|
|
void PushStreamingSamples(const s16* samples, std::size_t num_samples);
|
|
|
|
void PushWiimoteSpeakerSamples(const s16* samples, std::size_t num_samples,
|
|
|
|
u32 sample_rate_divisor);
|
|
|
|
void PushSkylanderPortalSamples(const u8* samples, std::size_t num_samples);
|
|
|
|
void PushGBASamples(std::size_t device_number, const s16* samples, std::size_t num_samples);
|
2021-07-04 12:44:56 +02:00
|
|
|
|
2025-03-02 20:19:20 -05:00
|
|
|
u32 GetSampleRate() const { return m_output_sample_rate; }
|
2021-07-04 12:44:56 +02:00
|
|
|
|
2025-03-02 20:19:20 -05:00
|
|
|
void SetDMAInputSampleRateDivisor(u32 rate_divisor);
|
|
|
|
void SetStreamInputSampleRateDivisor(u32 rate_divisor);
|
|
|
|
void SetGBAInputSampleRateDivisors(std::size_t device_number, u32 rate_divisor);
|
2021-07-04 12:44:56 +02:00
|
|
|
|
2025-03-02 20:19:20 -05:00
|
|
|
void SetStreamingVolume(u32 lvolume, u32 rvolume);
|
|
|
|
void SetWiimoteSpeakerVolume(u32 lvolume, u32 rvolume);
|
|
|
|
void SetGBAVolume(std::size_t device_number, u32 lvolume, u32 rvolume);
|
2014-07-24 11:20:19 +08:00
|
|
|
|
2015-02-23 18:43:13 +01:00
|
|
|
void StartLogDTKAudio(const std::string& filename);
|
|
|
|
void StopLogDTKAudio();
|
2009-06-12 14:40:50 +00:00
|
|
|
|
2015-06-20 21:46:18 -04:00
|
|
|
void StartLogDSPAudio(const std::string& filename);
|
|
|
|
void StopLogDSPAudio();
|
2013-01-09 22:57:32 +11:00
|
|
|
|
2022-07-03 15:07:06 -07:00
|
|
|
// 54000000 doesn't work here as it doesn't evenly divide with 32000, but 108000000 does
|
|
|
|
static constexpr u64 FIXED_SAMPLE_RATE_DIVIDEND = 54000000 * 2;
|
|
|
|
|
2016-01-14 00:58:01 -05:00
|
|
|
private:
|
2025-03-02 20:19:20 -05:00
|
|
|
const std::size_t SURROUND_CHANNELS = 6;
|
2017-08-09 16:57:26 -03:00
|
|
|
|
2016-01-14 00:58:01 -05:00
|
|
|
class MixerFifo final
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
2025-03-02 20:19:20 -05:00
|
|
|
static constexpr std::size_t GRANULE_QUEUE_SIZE = 20;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static s16 ToShort(const T x)
|
|
|
|
{
|
|
|
|
return static_cast<s16>(std::clamp<T>(x, static_cast<T>(std::numeric_limits<s16>::min()),
|
|
|
|
static_cast<T>(std::numeric_limits<s16>::max())));
|
|
|
|
}
|
|
|
|
struct StereoPair final
|
|
|
|
{
|
|
|
|
float l = 0.f;
|
|
|
|
float r = 0.f;
|
|
|
|
|
|
|
|
constexpr StereoPair() = default;
|
|
|
|
constexpr explicit StereoPair(float mono) : l(mono), r(mono) {}
|
|
|
|
constexpr StereoPair(float left, float right) : l(left), r(right) {}
|
|
|
|
constexpr StereoPair(s16 left, s16 right) : l(left), r(right) {}
|
|
|
|
|
|
|
|
StereoPair operator+(const StereoPair& other) const
|
|
|
|
{
|
|
|
|
return StereoPair(l + other.l, r + other.r);
|
|
|
|
}
|
|
|
|
|
|
|
|
StereoPair& operator*=(const StereoPair& other)
|
|
|
|
{
|
|
|
|
l *= other.l;
|
|
|
|
r *= other.r;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr std::size_t GRANULE_BUFFER_SIZE = 256;
|
|
|
|
static constexpr std::size_t GRANULE_BUFFER_MASK = GRANULE_BUFFER_SIZE - 1;
|
|
|
|
static constexpr std::size_t GRANULE_BUFFER_BITS = std::countr_one(GRANULE_BUFFER_MASK);
|
|
|
|
static constexpr std::size_t GRANULE_BUFFER_FRAC_BITS = 32 - GRANULE_BUFFER_BITS;
|
|
|
|
|
|
|
|
using GranuleBuffer = std::array<StereoPair, GRANULE_BUFFER_SIZE>;
|
|
|
|
class Granule final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
constexpr Granule() = default;
|
|
|
|
constexpr Granule(const GranuleBuffer& input, std::size_t start_index);
|
|
|
|
|
|
|
|
static StereoPair InterpStereoPair(const Granule& front, const Granule& back, u32 frac);
|
|
|
|
|
|
|
|
Granule& operator*=(const StereoPair& x)
|
|
|
|
{
|
|
|
|
for (auto& sample : m_buffer)
|
|
|
|
sample *= x;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
GranuleBuffer m_buffer{};
|
|
|
|
};
|
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
public:
|
2025-03-02 20:19:20 -05:00
|
|
|
MixerFifo(Mixer* mixer, u32 sample_rate_divisor, bool little_endian)
|
2022-07-03 15:07:06 -07:00
|
|
|
: m_mixer(mixer), m_input_sample_rate_divisor(sample_rate_divisor),
|
|
|
|
m_little_endian(little_endian)
|
2016-06-24 10:43:46 +02:00
|
|
|
{
|
|
|
|
}
|
2017-11-06 09:15:05 +01:00
|
|
|
void DoState(PointerWrap& p);
|
2025-03-02 20:19:20 -05:00
|
|
|
void PushSamples(const s16* samples, std::size_t num_samples);
|
|
|
|
void Mix(s16* samples, std::size_t num_samples);
|
|
|
|
void SetInputSampleRateDivisor(u32 rate_divisor);
|
|
|
|
u32 GetInputSampleRateDivisor() const;
|
|
|
|
void SetVolume(u32 lvolume, u32 rvolume);
|
2022-06-10 21:27:10 -07:00
|
|
|
std::pair<s32, s32> GetVolume() const;
|
2016-01-14 03:00:47 -05:00
|
|
|
|
2014-04-10 18:28:19 -07:00
|
|
|
private:
|
2017-06-26 14:41:12 -07:00
|
|
|
Mixer* m_mixer;
|
2025-03-02 20:19:20 -05:00
|
|
|
u32 m_input_sample_rate_divisor;
|
2021-07-04 12:44:56 +02:00
|
|
|
bool m_little_endian;
|
2025-03-02 20:19:20 -05:00
|
|
|
|
|
|
|
std::size_t m_buffer_index = 0;
|
|
|
|
GranuleBuffer m_buffer{};
|
|
|
|
|
|
|
|
u32 m_current_index = 0;
|
|
|
|
Granule m_front, m_back;
|
|
|
|
|
|
|
|
std::array<Granule, GRANULE_QUEUE_SIZE> m_queue;
|
|
|
|
std::atomic<std::size_t> m_queue_head{0};
|
|
|
|
std::atomic<std::size_t> m_queue_tail{0};
|
|
|
|
std::atomic<bool> m_queue_looping{false};
|
|
|
|
std::size_t m_queue_fade_index = 0;
|
|
|
|
|
|
|
|
void Enqueue(const Granule& granule);
|
|
|
|
void Dequeue(Granule* granule);
|
|
|
|
|
2015-02-23 18:43:13 +01:00
|
|
|
// Volume ranges from 0-256
|
2016-01-14 01:19:25 -05:00
|
|
|
std::atomic<s32> m_LVolume{256};
|
|
|
|
std::atomic<s32> m_RVolume{256};
|
2025-03-02 20:19:20 -05:00
|
|
|
|
|
|
|
StereoPair m_quantization_error;
|
2014-04-10 18:28:19 -07:00
|
|
|
};
|
2017-04-10 17:44:17 +01:00
|
|
|
|
2022-01-06 16:43:43 +01:00
|
|
|
void RefreshConfig();
|
|
|
|
|
2022-07-03 15:07:06 -07:00
|
|
|
MixerFifo m_dma_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 32000, false};
|
|
|
|
MixerFifo m_streaming_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000, false};
|
|
|
|
MixerFifo m_wiimote_speaker_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 3000, true};
|
2023-03-05 19:19:00 +13:00
|
|
|
MixerFifo m_skylander_portal_mixer{this, FIXED_SAMPLE_RATE_DIVIDEND / 8000, true};
|
2022-07-03 15:07:06 -07:00
|
|
|
std::array<MixerFifo, 4> m_gba_mixers{MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000, true},
|
|
|
|
MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000, true},
|
|
|
|
MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000, true},
|
|
|
|
MixerFifo{this, FIXED_SAMPLE_RATE_DIVIDEND / 48000, true}};
|
2025-03-02 20:19:20 -05:00
|
|
|
u32 m_output_sample_rate;
|
2011-02-11 18:59:42 +00:00
|
|
|
|
2017-08-09 16:57:26 -03:00
|
|
|
AudioCommon::SurroundDecoder m_surround_decoder;
|
2017-04-10 17:44:17 +01:00
|
|
|
|
2015-06-20 21:48:50 -04:00
|
|
|
WaveFileWriter m_wave_writer_dtk;
|
|
|
|
WaveFileWriter m_wave_writer_dsp;
|
2013-10-29 01:23:17 -04:00
|
|
|
|
2016-01-14 01:19:25 -05:00
|
|
|
bool m_log_dtk_audio = false;
|
|
|
|
bool m_log_dsp_audio = false;
|
2009-03-26 09:29:14 +00:00
|
|
|
|
2022-01-06 16:43:43 +01:00
|
|
|
float m_config_emulation_speed;
|
2025-03-02 20:19:20 -05:00
|
|
|
bool m_audio_fill_gaps = true;
|
2022-01-06 16:43:43 +01:00
|
|
|
|
2023-08-16 21:37:12 +02:00
|
|
|
Config::ConfigChangedCallbackID m_config_changed_callback_id;
|
2015-02-23 18:43:13 +01:00
|
|
|
};
|