-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
audio-output.cpp
102 lines (83 loc) · 3.22 KB
/
audio-output.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "audio-output.h"
#include <numeric>
#include <SDL3/SDL.h>
#define CLOWNRESAMPLER_ASSERT SDL_assert
#define CLOWNRESAMPLER_FABS SDL_fabs
#define CLOWNRESAMPLER_SIN SDL_sin
#define CLOWNRESAMPLER_ZERO(buffer, size) SDL_memset(buffer, 0, size)
#define CLOWNRESAMPLER_MEMMOVE SDL_memmove
#define MIXER_IMPLEMENTATION
#define MIXER_ASSERT SDL_assert
#define MIXER_FREE SDL_free
#define MIXER_CALLOC SDL_calloc
#define MIXER_MEMMOVE SDL_memmove
#define MIXER_MEMSET SDL_memset
#include "clownmdemu-frontend-common/mixer.h"
Mixer_Constant AudioOutput::mixer_constant;
bool AudioOutput::mixer_constant_initialised;
AudioOutput::AudioOutput()
: device(MIXER_FM_CHANNEL_COUNT, sample_rate, total_buffer_frames)
{
// Initialise the mixer.
if (!mixer_constant_initialised)
{
mixer_constant_initialised = true;
Mixer_Constant_Initialise(&mixer_constant);
}
Mixer_State_Initialise(&mixer_state, sample_rate, pal_mode, low_pass_filter);
}
AudioOutput::~AudioOutput()
{
Mixer_State_Deinitialise(&mixer_state);
}
void AudioOutput::MixerBegin()
{
// TODO: Given that SDL3 boasts its own internal mixer, perhaps we should rely on that instead of our own solution.
if (mixer_update_pending)
{
mixer_update_pending = false;
Mixer_State_Deinitialise(&mixer_state);
Mixer_State_Initialise(&mixer_state, sample_rate, pal_mode, low_pass_filter);
}
Mixer_Begin(&mixer);
}
void AudioOutput::MixerEnd()
{
const cc_u32f target_frames = GetTargetFrames();
const cc_u32f queued_frames = device.GetTotalQueuedFrames();
rolling_average_buffer[rolling_average_buffer_index] = queued_frames;
rolling_average_buffer_index = (rolling_average_buffer_index + 1) % rolling_average_buffer.size();
// If there is too much audio, just drop it because the dynamic rate control will be unable to handle it.
if (queued_frames < target_frames * 2)
{
const auto callback = [](void* const user_data, const cc_s16l* const audio_samples, const std::size_t total_frames)
{
AudioOutput *audio_output = static_cast<AudioOutput*>(user_data);
audio_output->device.QueueFrames(audio_samples, total_frames);
};
// Hans-Kristian Arntzen's Dynamic Rate Control formula.
// https://github.com/libretro/docs/blob/master/archive/ratecontrol.pdf
const cc_u32f divisor = target_frames * 0x100; // The number here is the inverse of the formula's 'd' value.
Mixer_End(&mixer, queued_frames - target_frames + divisor, divisor, callback, this);
}
}
cc_s16l* AudioOutput::MixerAllocateFMSamples(const std::size_t total_frames)
{
return Mixer_AllocateFMSamples(&mixer, total_frames);
}
cc_s16l* AudioOutput::MixerAllocatePSGSamples(const std::size_t total_frames)
{
return Mixer_AllocatePSGSamples(&mixer, total_frames);
}
cc_s16l* AudioOutput::MixerAllocatePCMSamples(const std::size_t total_frames)
{
return Mixer_AllocatePCMSamples(&mixer, total_frames);
}
cc_s16l* AudioOutput::MixerAllocateCDDASamples(const std::size_t total_frames)
{
return Mixer_AllocateCDDASamples(&mixer, total_frames);
}
cc_u32f AudioOutput::GetAverageFrames() const
{
return std::accumulate(rolling_average_buffer.cbegin(), rolling_average_buffer.cend(), cc_u32f(0)) / rolling_average_buffer.size();
}