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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <array>
#include "audio_core/algorithm/interpolate.h"
#include "audio_core/codec.h"
#include "audio_core/common.h"
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
namespace Core::Memory {
class Memory;
}
namespace AudioCore {
class BehaviorInfo;
class VoiceContext;
enum class SampleFormat : u8 {
Invalid = 0,
Pcm8 = 1,
Pcm16 = 2,
Pcm24 = 3,
Pcm32 = 4,
PcmFloat = 5,
Adpcm = 6,
};
enum class PlayState : u8 {
Started = 0,
Stopped = 1,
Paused = 2,
};
enum class ServerPlayState {
Play = 0,
Stop = 1,
RequestStop = 2,
Paused = 3,
};
struct BiquadFilterParameter {
bool enabled{};
INSERT_PADDING_BYTES(1);
std::array<s16, 3> numerator{};
std::array<s16, 2> denominator{};
};
static_assert(sizeof(BiquadFilterParameter) == 0xc, "BiquadFilterParameter is an invalid size");
struct WaveBuffer {
u64_le buffer_address{};
u64_le buffer_size{};
s32_le start_sample_offset{};
s32_le end_sample_offset{};
u8 is_looping{};
u8 end_of_stream{};
u8 sent_to_server{};
INSERT_PADDING_BYTES(5);
u64 context_address{};
u64 context_size{};
INSERT_PADDING_BYTES(8);
};
static_assert(sizeof(WaveBuffer) == 0x38, "WaveBuffer is an invalid size");
struct ServerWaveBuffer {
VAddr buffer_address{};
std::size_t buffer_size{};
s32 start_sample_offset{};
s32 end_sample_offset{};
bool is_looping{};
bool end_of_stream{};
VAddr context_address{};
std::size_t context_size{};
bool sent_to_dsp{true};
};
struct BehaviorFlags {
BitField<0, 1, u16> is_played_samples_reset_at_loop_point;
BitField<1, 1, u16> is_pitch_and_src_skipped;
};
static_assert(sizeof(BehaviorFlags) == 0x4, "BehaviorFlags is an invalid size");
struct VoiceState {
s64 played_sample_count{};
s32 offset{};
s32 wave_buffer_index{};
std::array<bool, AudioCommon::MAX_WAVE_BUFFERS> is_wave_buffer_valid{};
s32 wave_buffer_consumed{};
std::array<s32, AudioCommon::MAX_SAMPLE_HISTORY> sample_history{};
s32 fraction{};
VAddr context_address{};
Codec::ADPCM_Coeff coeff{};
Codec::ADPCMState context{};
std::array<s64, 2> biquad_filter_state{};
std::array<s32, AudioCommon::MAX_MIX_BUFFERS> previous_samples{};
u32 external_context_size{};
bool is_external_context_used{};
bool voice_dropped{};
// TODO(ogniK): Hack until ADPCM streaming is implemented
std::vector<s16> adpcm_samples{};
};
class VoiceChannelResource {
public:
struct InParams {
s32_le id{};
std::array<float_le, AudioCommon::MAX_MIX_BUFFERS> mix_volume{};
bool in_use{};
INSERT_PADDING_BYTES(11);
};
static_assert(sizeof(VoiceChannelResource::InParams) == 0x70, "InParams is an invalid size");
};
class ServerVoiceChannelResource {
public:
explicit ServerVoiceChannelResource(s32 id);
~ServerVoiceChannelResource();
bool InUse() const;
float GetCurrentMixVolumeAt(std::size_t i) const;
float GetLastMixVolumeAt(std::size_t i) const;
void Update(VoiceChannelResource::InParams& in_params);
void UpdateLastMixVolumes();
const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& GetCurrentMixVolume() const;
const std::array<float, AudioCommon::MAX_MIX_BUFFERS>& GetLastMixVolume() const;
private:
s32 id{};
std::array<float, AudioCommon::MAX_MIX_BUFFERS> mix_volume{};
std::array<float, AudioCommon::MAX_MIX_BUFFERS> last_mix_volume{};
bool in_use{};
};
class VoiceInfo {
public:
struct InParams {
s32_le id{};
u32_le node_id{};
u8 is_new{};
u8 is_in_use{};
PlayState play_state{};
SampleFormat sample_format{};
s32_le sample_rate{};
s32_le priority{};
s32_le sorting_order{};
s32_le channel_count{};
float_le pitch{};
float_le volume{};
std::array<BiquadFilterParameter, 2> biquad_filter{};
s32_le wave_buffer_count{};
s16_le wave_buffer_head{};
INSERT_PADDING_BYTES(6);
u64_le additional_params_address{};
u64_le additional_params_size{};
s32_le mix_id{};
s32_le splitter_info_id{};
std::array<WaveBuffer, 4> wave_buffer{};
std::array<u32_le, 6> voice_channel_resource_ids{};
// TODO(ogniK): Remaining flags
u8 is_voice_drop_flag_clear_requested{};
u8 wave_buffer_flush_request_count{};
INSERT_PADDING_BYTES(2);
BehaviorFlags behavior_flags{};
INSERT_PADDING_BYTES(16);
};
static_assert(sizeof(VoiceInfo::InParams) == 0x170, "InParams is an invalid size");
struct OutParams {
u64_le played_sample_count{};
u32_le wave_buffer_consumed{};
u8 voice_dropped{};
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(VoiceInfo::OutParams) == 0x10, "OutParams is an invalid size");
};
class ServerVoiceInfo {
public:
struct InParams {
bool in_use{};
bool is_new{};
bool should_depop{};
SampleFormat sample_format{};
s32 sample_rate{};
s32 channel_count{};
s32 id{};
s32 node_id{};
s32 mix_id{};
ServerPlayState current_playstate{};
ServerPlayState last_playstate{};
s32 priority{};
s32 sorting_order{};
float pitch{};
float volume{};
float last_volume{};
std::array<BiquadFilterParameter, AudioCommon::MAX_BIQUAD_FILTERS> biquad_filter{};
s32 wave_buffer_count{};
s16 wave_bufffer_head{};
INSERT_PADDING_BYTES(2);
BehaviorFlags behavior_flags{};
VAddr additional_params_address{};
std::size_t additional_params_size{};
std::array<ServerWaveBuffer, AudioCommon::MAX_WAVE_BUFFERS> wave_buffer{};
std::array<s32, AudioCommon::MAX_CHANNEL_COUNT> voice_channel_resource_id{};
s32 splitter_info_id{};
u8 wave_buffer_flush_request_count{};
bool voice_drop_flag{};
bool buffer_mapped{};
std::array<bool, AudioCommon::MAX_BIQUAD_FILTERS> was_biquad_filter_enabled{};
};
struct OutParams {
s64 played_sample_count{};
s32 wave_buffer_consumed{};
};
ServerVoiceInfo();
~ServerVoiceInfo();
void Initialize();
void UpdateParameters(const VoiceInfo::InParams& voice_in, BehaviorInfo& behavior_info);
void UpdateWaveBuffers(const VoiceInfo::InParams& voice_in,
std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states,
BehaviorInfo& behavior_info);
void UpdateWaveBuffer(ServerWaveBuffer& out_wavebuffer, const WaveBuffer& in_wave_buffer,
SampleFormat sample_format, bool is_buffer_valid,
BehaviorInfo& behavior_info);
void WriteOutStatus(VoiceInfo::OutParams& voice_out, VoiceInfo::InParams& voice_in,
std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& voice_states);
const InParams& GetInParams() const;
InParams& GetInParams();
const OutParams& GetOutParams() const;
OutParams& GetOutParams();
bool ShouldSkip() const;
bool UpdateForCommandGeneration(VoiceContext& voice_context);
void ResetResources(VoiceContext& voice_context);
bool UpdateParametersForCommandGeneration(
std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states);
void FlushWaveBuffers(u8 flush_count,
std::array<VoiceState*, AudioCommon::MAX_CHANNEL_COUNT>& dsp_voice_states,
s32 channel_count);
private:
std::vector<s16> stored_samples;
InParams in_params{};
OutParams out_params{};
bool HasValidWaveBuffer(const VoiceState* state) const;
};
class VoiceContext {
public:
VoiceContext(std::size_t voice_count);
~VoiceContext();
std::size_t GetVoiceCount() const;
ServerVoiceChannelResource& GetChannelResource(std::size_t i);
const ServerVoiceChannelResource& GetChannelResource(std::size_t i) const;
VoiceState& GetState(std::size_t i);
const VoiceState& GetState(std::size_t i) const;
VoiceState& GetDspSharedState(std::size_t i);
const VoiceState& GetDspSharedState(std::size_t i) const;
ServerVoiceInfo& GetInfo(std::size_t i);
const ServerVoiceInfo& GetInfo(std::size_t i) const;
ServerVoiceInfo& GetSortedInfo(std::size_t i);
const ServerVoiceInfo& GetSortedInfo(std::size_t i) const;
s32 DecodePcm16(s32* output_buffer, ServerWaveBuffer* wave_buffer, s32 channel,
s32 channel_count, s32 buffer_offset, s32 sample_count,
Core::Memory::Memory& memory);
void SortInfo();
void UpdateStateByDspShared();
private:
std::size_t voice_count{};
std::vector<ServerVoiceChannelResource> voice_channel_resources{};
std::vector<VoiceState> voice_states{};
std::vector<VoiceState> dsp_voice_states{};
std::vector<ServerVoiceInfo> voice_info{};
std::vector<ServerVoiceInfo*> sorted_voice_info{};
};
} // namespace AudioCore
|