summaryrefslogtreecommitdiff
path: root/src/audio_core/hle
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/hle')
-rw-r--r--src/audio_core/hle/common.h34
-rw-r--r--src/audio_core/hle/dsp.cpp172
-rw-r--r--src/audio_core/hle/dsp.h595
-rw-r--r--src/audio_core/hle/filter.cpp117
-rw-r--r--src/audio_core/hle/filter.h117
-rw-r--r--src/audio_core/hle/mixers.cpp210
-rw-r--r--src/audio_core/hle/mixers.h61
-rw-r--r--src/audio_core/hle/pipe.cpp177
-rw-r--r--src/audio_core/hle/pipe.h63
-rw-r--r--src/audio_core/hle/source.cpp349
-rw-r--r--src/audio_core/hle/source.h149
11 files changed, 0 insertions, 2044 deletions
diff --git a/src/audio_core/hle/common.h b/src/audio_core/hle/common.h
deleted file mode 100644
index 7fbc3ad9a..000000000
--- a/src/audio_core/hle/common.h
+++ /dev/null
@@ -1,34 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <algorithm>
8#include <array>
9#include "common/common_types.h"
10
11namespace DSP {
12namespace HLE {
13
14constexpr int num_sources = 24;
15constexpr int samples_per_frame = 160; ///< Samples per audio frame at native sample rate
16
17/// The final output to the speakers is stereo. Preprocessing output in Source is also stereo.
18using StereoFrame16 = std::array<std::array<s16, 2>, samples_per_frame>;
19
20/// The DSP is quadraphonic internally.
21using QuadFrame32 = std::array<std::array<s32, 4>, samples_per_frame>;
22
23/**
24 * This performs the filter operation defined by FilterT::ProcessSample on the frame in-place.
25 * FilterT::ProcessSample is called sequentially on the samples.
26 */
27template <typename FrameT, typename FilterT>
28void FilterFrame(FrameT& frame, FilterT& filter) {
29 std::transform(frame.begin(), frame.end(), frame.begin(),
30 [&filter](const auto& sample) { return filter.ProcessSample(sample); });
31}
32
33} // namespace HLE
34} // namespace DSP
diff --git a/src/audio_core/hle/dsp.cpp b/src/audio_core/hle/dsp.cpp
deleted file mode 100644
index 260b182ed..000000000
--- a/src/audio_core/hle/dsp.cpp
+++ /dev/null
@@ -1,172 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <memory>
7#include "audio_core/hle/dsp.h"
8#include "audio_core/hle/mixers.h"
9#include "audio_core/hle/pipe.h"
10#include "audio_core/hle/source.h"
11#include "audio_core/sink.h"
12#include "audio_core/time_stretch.h"
13
14namespace DSP {
15namespace HLE {
16
17// Region management
18
19DspMemory g_dsp_memory;
20
21static size_t CurrentRegionIndex() {
22 // The region with the higher frame counter is chosen unless there is wraparound.
23 // This function only returns a 0 or 1.
24 u16 frame_counter_0 = g_dsp_memory.region_0.frame_counter;
25 u16 frame_counter_1 = g_dsp_memory.region_1.frame_counter;
26
27 if (frame_counter_0 == 0xFFFFu && frame_counter_1 != 0xFFFEu) {
28 // Wraparound has occurred.
29 return 1;
30 }
31
32 if (frame_counter_1 == 0xFFFFu && frame_counter_0 != 0xFFFEu) {
33 // Wraparound has occurred.
34 return 0;
35 }
36
37 return (frame_counter_0 > frame_counter_1) ? 0 : 1;
38}
39
40static SharedMemory& ReadRegion() {
41 return CurrentRegionIndex() == 0 ? g_dsp_memory.region_0 : g_dsp_memory.region_1;
42}
43
44static SharedMemory& WriteRegion() {
45 return CurrentRegionIndex() != 0 ? g_dsp_memory.region_0 : g_dsp_memory.region_1;
46}
47
48// Audio processing and mixing
49
50static std::array<Source, num_sources> sources = {
51 Source(0), Source(1), Source(2), Source(3), Source(4), Source(5), Source(6), Source(7),
52 Source(8), Source(9), Source(10), Source(11), Source(12), Source(13), Source(14), Source(15),
53 Source(16), Source(17), Source(18), Source(19), Source(20), Source(21), Source(22), Source(23),
54};
55static Mixers mixers;
56
57static StereoFrame16 GenerateCurrentFrame() {
58 SharedMemory& read = ReadRegion();
59 SharedMemory& write = WriteRegion();
60
61 std::array<QuadFrame32, 3> intermediate_mixes = {};
62
63 // Generate intermediate mixes
64 for (size_t i = 0; i < num_sources; i++) {
65 write.source_statuses.status[i] =
66 sources[i].Tick(read.source_configurations.config[i], read.adpcm_coefficients.coeff[i]);
67 for (size_t mix = 0; mix < 3; mix++) {
68 sources[i].MixInto(intermediate_mixes[mix], mix);
69 }
70 }
71
72 // Generate final mix
73 write.dsp_status = mixers.Tick(read.dsp_configuration, read.intermediate_mix_samples,
74 write.intermediate_mix_samples, intermediate_mixes);
75
76 StereoFrame16 output_frame = mixers.GetOutput();
77
78 // Write current output frame to the shared memory region
79 for (size_t samplei = 0; samplei < output_frame.size(); samplei++) {
80 for (size_t channeli = 0; channeli < output_frame[0].size(); channeli++) {
81 write.final_samples.pcm16[samplei][channeli] = s16_le(output_frame[samplei][channeli]);
82 }
83 }
84
85 return output_frame;
86}
87
88// Audio output
89
90static bool perform_time_stretching = true;
91static std::unique_ptr<AudioCore::Sink> sink;
92static AudioCore::TimeStretcher time_stretcher;
93
94static void FlushResidualStretcherAudio() {
95 time_stretcher.Flush();
96 while (true) {
97 std::vector<s16> residual_audio = time_stretcher.Process(sink->SamplesInQueue());
98 if (residual_audio.empty())
99 break;
100 sink->EnqueueSamples(residual_audio.data(), residual_audio.size() / 2);
101 }
102}
103
104static void OutputCurrentFrame(const StereoFrame16& frame) {
105 if (perform_time_stretching) {
106 time_stretcher.AddSamples(&frame[0][0], frame.size());
107 std::vector<s16> stretched_samples = time_stretcher.Process(sink->SamplesInQueue());
108 sink->EnqueueSamples(stretched_samples.data(), stretched_samples.size() / 2);
109 } else {
110 constexpr size_t maximum_sample_latency = 2048; // about 64 miliseconds
111 if (sink->SamplesInQueue() > maximum_sample_latency) {
112 // This can occur if we're running too fast and samples are starting to back up.
113 // Just drop the samples.
114 return;
115 }
116
117 sink->EnqueueSamples(&frame[0][0], frame.size());
118 }
119}
120
121void EnableStretching(bool enable) {
122 if (perform_time_stretching == enable)
123 return;
124
125 if (!enable) {
126 FlushResidualStretcherAudio();
127 }
128 perform_time_stretching = enable;
129}
130
131// Public Interface
132
133void Init() {
134 DSP::HLE::ResetPipes();
135
136 for (auto& source : sources) {
137 source.Reset();
138 }
139
140 mixers.Reset();
141
142 time_stretcher.Reset();
143 if (sink) {
144 time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
145 }
146}
147
148void Shutdown() {
149 if (perform_time_stretching) {
150 FlushResidualStretcherAudio();
151 }
152}
153
154bool Tick() {
155 StereoFrame16 current_frame = {};
156
157 // TODO: Check dsp::DSP semaphore (which indicates emulated application has finished writing to
158 // shared memory region)
159 current_frame = GenerateCurrentFrame();
160
161 OutputCurrentFrame(current_frame);
162
163 return true;
164}
165
166void SetSink(std::unique_ptr<AudioCore::Sink> sink_) {
167 sink = std::move(sink_);
168 time_stretcher.SetOutputSampleRate(sink->GetNativeSampleRate());
169}
170
171} // namespace HLE
172} // namespace DSP
diff --git a/src/audio_core/hle/dsp.h b/src/audio_core/hle/dsp.h
deleted file mode 100644
index 94ce48863..000000000
--- a/src/audio_core/hle/dsp.h
+++ /dev/null
@@ -1,595 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <memory>
10#include <type_traits>
11#include "audio_core/hle/common.h"
12#include "common/bit_field.h"
13#include "common/common_funcs.h"
14#include "common/common_types.h"
15#include "common/swap.h"
16
17namespace AudioCore {
18class Sink;
19}
20
21namespace DSP {
22namespace HLE {
23
24// The application-accessible region of DSP memory consists of two parts. Both are marked as IO and
25// have Read/Write permissions.
26//
27// First Region: 0x1FF50000 (Size: 0x8000)
28// Second Region: 0x1FF70000 (Size: 0x8000)
29//
30// The DSP reads from each region alternately based on the frame counter for each region much like a
31// double-buffer. The frame counter is located as the very last u16 of each region and is
32// incremented each audio tick.
33
34constexpr u32 region0_offset = 0x50000;
35constexpr u32 region1_offset = 0x70000;
36
37/**
38 * The DSP is native 16-bit. The DSP also appears to be big-endian. When reading 32-bit numbers from
39 * its memory regions, the higher and lower 16-bit halves are swapped compared to the little-endian
40 * layout of the ARM11. Hence from the ARM11's point of view the memory space appears to be
41 * middle-endian.
42 *
43 * Unusually this does not appear to be an issue for floating point numbers. The DSP makes the more
44 * sensible choice of keeping that little-endian. There are also some exceptions such as the
45 * IntermediateMixSamples structure, which is little-endian.
46 *
47 * This struct implements the conversion to and from this middle-endianness.
48 */
49struct u32_dsp {
50 u32_dsp() = default;
51 operator u32() const {
52 return Convert(storage);
53 }
54 void operator=(u32 new_value) {
55 storage = Convert(new_value);
56 }
57
58private:
59 static constexpr u32 Convert(u32 value) {
60 return (value << 16) | (value >> 16);
61 }
62 u32_le storage;
63};
64#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)
65static_assert(std::is_trivially_copyable<u32_dsp>::value, "u32_dsp isn't trivially copyable");
66#endif
67
68// There are 15 structures in each memory region. A table of them in the order they appear in memory
69// is presented below:
70//
71// # First Region DSP Address Purpose Control
72// 5 0x8400 DSP Status DSP
73// 9 0x8410 DSP Debug Info DSP
74// 6 0x8540 Final Mix Samples DSP
75// 2 0x8680 Source Status [24] DSP
76// 8 0x8710 Compressor Table Application
77// 4 0x9430 DSP Configuration Application
78// 7 0x9492 Intermediate Mix Samples DSP + App
79// 1 0x9E92 Source Configuration [24] Application
80// 3 0xA792 Source ADPCM Coefficients [24] Application
81// 10 0xA912 Surround Sound Related
82// 11 0xAA12 Surround Sound Related
83// 12 0xAAD2 Surround Sound Related
84// 13 0xAC52 Surround Sound Related
85// 14 0xAC5C Surround Sound Related
86// 0 0xBFFF Frame Counter Application
87//
88// #: This refers to the order in which they appear in the DspPipe::Audio DSP pipe.
89// See also: DSP::HLE::PipeRead.
90//
91// Note that the above addresses do vary slightly between audio firmwares observed; the addresses
92// are not fixed in stone. The addresses above are only an examplar; they're what this
93// implementation does and provides to applications.
94//
95// Application requests the DSP service to convert DSP addresses into ARM11 virtual addresses using
96// the ConvertProcessAddressFromDspDram service call. Applications seem to derive the addresses for
97// the second region via:
98// second_region_dsp_addr = first_region_dsp_addr | 0x10000
99//
100// Applications maintain most of its own audio state, the memory region is used mainly for
101// communication and not storage of state.
102//
103// In the documentation below, filter and effect transfer functions are specified in the z domain.
104// (If you are more familiar with the Laplace transform, z = exp(sT). The z domain is the digital
105// frequency domain, just like how the s domain is the analog frequency domain.)
106
107#define INSERT_PADDING_DSPWORDS(num_words) INSERT_PADDING_BYTES(2 * (num_words))
108
109// GCC versions < 5.0 do not implement std::is_trivially_copyable.
110// Excluding MSVC because it has weird behaviour for std::is_trivially_copyable.
111#if (__GNUC__ >= 5) || defined(__clang__)
112#define ASSERT_DSP_STRUCT(name, size) \
113 static_assert(std::is_standard_layout<name>::value, \
114 "DSP structure " #name " doesn't use standard layout"); \
115 static_assert(std::is_trivially_copyable<name>::value, \
116 "DSP structure " #name " isn't trivially copyable"); \
117 static_assert(sizeof(name) == (size), "Unexpected struct size for DSP structure " #name)
118#else
119#define ASSERT_DSP_STRUCT(name, size) \
120 static_assert(std::is_standard_layout<name>::value, \
121 "DSP structure " #name " doesn't use standard layout"); \
122 static_assert(sizeof(name) == (size), "Unexpected struct size for DSP structure " #name)
123#endif
124
125struct SourceConfiguration {
126 struct Configuration {
127 /// These dirty flags are set by the application when it updates the fields in this struct.
128 /// The DSP clears these each audio frame.
129 union {
130 u32_le dirty_raw;
131
132 BitField<0, 1, u32_le> format_dirty;
133 BitField<1, 1, u32_le> mono_or_stereo_dirty;
134 BitField<2, 1, u32_le> adpcm_coefficients_dirty;
135 /// Tends to be set when a looped buffer is queued.
136 BitField<3, 1, u32_le> partial_embedded_buffer_dirty;
137 BitField<4, 1, u32_le> partial_reset_flag;
138
139 BitField<16, 1, u32_le> enable_dirty;
140 BitField<17, 1, u32_le> interpolation_dirty;
141 BitField<18, 1, u32_le> rate_multiplier_dirty;
142 BitField<19, 1, u32_le> buffer_queue_dirty;
143 BitField<20, 1, u32_le> loop_related_dirty;
144 /// Tends to also be set when embedded buffer is updated.
145 BitField<21, 1, u32_le> play_position_dirty;
146 BitField<22, 1, u32_le> filters_enabled_dirty;
147 BitField<23, 1, u32_le> simple_filter_dirty;
148 BitField<24, 1, u32_le> biquad_filter_dirty;
149 BitField<25, 1, u32_le> gain_0_dirty;
150 BitField<26, 1, u32_le> gain_1_dirty;
151 BitField<27, 1, u32_le> gain_2_dirty;
152 BitField<28, 1, u32_le> sync_dirty;
153 BitField<29, 1, u32_le> reset_flag;
154 BitField<30, 1, u32_le> embedded_buffer_dirty;
155 };
156
157 // Gain control
158
159 /**
160 * Gain is between 0.0-1.0. This determines how much will this source appear on each of the
161 * 12 channels that feed into the intermediate mixers. Each of the three intermediate mixers
162 * is fed two left and two right channels.
163 */
164 float_le gain[3][4];
165
166 // Interpolation
167
168 /// Multiplier for sample rate. Resampling occurs with the selected interpolation method.
169 float_le rate_multiplier;
170
171 enum class InterpolationMode : u8 {
172 Polyphase = 0,
173 Linear = 1,
174 None = 2,
175 };
176
177 InterpolationMode interpolation_mode;
178 INSERT_PADDING_BYTES(1); ///< Interpolation related
179
180 // Filters
181
182 /**
183 * This is the simplest normalized first-order digital recursive filter.
184 * The transfer function of this filter is:
185 * H(z) = b0 / (1 - a1 z^-1)
186 * Note the feedbackward coefficient is negated.
187 * Values are signed fixed point with 15 fractional bits.
188 */
189 struct SimpleFilter {
190 s16_le b0;
191 s16_le a1;
192 };
193
194 /**
195 * This is a normalised biquad filter (second-order).
196 * The transfer function of this filter is:
197 * H(z) = (b0 + b1 z^-1 + b2 z^-2) / (1 - a1 z^-1 - a2 z^-2)
198 * Nintendo chose to negate the feedbackward coefficients. This differs from standard
199 * notation as in: https://ccrma.stanford.edu/~jos/filters/Direct_Form_I.html
200 * Values are signed fixed point with 14 fractional bits.
201 */
202 struct BiquadFilter {
203 s16_le a2;
204 s16_le a1;
205 s16_le b2;
206 s16_le b1;
207 s16_le b0;
208 };
209
210 union {
211 u16_le filters_enabled;
212 BitField<0, 1, u16_le> simple_filter_enabled;
213 BitField<1, 1, u16_le> biquad_filter_enabled;
214 };
215
216 SimpleFilter simple_filter;
217 BiquadFilter biquad_filter;
218
219 // Buffer Queue
220
221 /// A buffer of audio data from the application, along with metadata about it.
222 struct Buffer {
223 /// Physical memory address of the start of the buffer
224 u32_dsp physical_address;
225
226 /// This is length in terms of samples.
227 /// Note that in different buffer formats a sample takes up different number of bytes.
228 u32_dsp length;
229
230 /// ADPCM Predictor (4 bits) and Scale (4 bits)
231 union {
232 u16_le adpcm_ps;
233 BitField<0, 4, u16_le> adpcm_scale;
234 BitField<4, 4, u16_le> adpcm_predictor;
235 };
236
237 /// ADPCM Historical Samples (y[n-1] and y[n-2])
238 u16_le adpcm_yn[2];
239
240 /// This is non-zero when the ADPCM values above are to be updated.
241 u8 adpcm_dirty;
242
243 /// Is a looping buffer.
244 u8 is_looping;
245
246 /// This value is shown in SourceStatus::previous_buffer_id when this buffer has
247 /// finished. This allows the emulated application to tell what buffer is currently
248 /// playing.
249 u16_le buffer_id;
250
251 INSERT_PADDING_DSPWORDS(1);
252 };
253
254 u16_le buffers_dirty; ///< Bitmap indicating which buffers are dirty (bit i -> buffers[i])
255 Buffer buffers[4]; ///< Queued Buffers
256
257 // Playback controls
258
259 u32_dsp loop_related;
260 u8 enable;
261 INSERT_PADDING_BYTES(1);
262 u16_le sync; ///< Application-side sync (See also: SourceStatus::sync)
263 u32_dsp play_position; ///< Position. (Units: number of samples)
264 INSERT_PADDING_DSPWORDS(2);
265
266 // Embedded Buffer
267 // This buffer is often the first buffer to be used when initiating audio playback,
268 // after which the buffer queue is used.
269
270 u32_dsp physical_address;
271
272 /// This is length in terms of samples.
273 /// Note a sample takes up different number of bytes in different buffer formats.
274 u32_dsp length;
275
276 enum class MonoOrStereo : u16_le {
277 Mono = 1,
278 Stereo = 2,
279 };
280
281 enum class Format : u16_le {
282 PCM8 = 0,
283 PCM16 = 1,
284 ADPCM = 2,
285 };
286
287 union {
288 u16_le flags1_raw;
289 BitField<0, 2, MonoOrStereo> mono_or_stereo;
290 BitField<2, 2, Format> format;
291 BitField<5, 1, u16_le> fade_in;
292 };
293
294 /// ADPCM Predictor (4 bit) and Scale (4 bit)
295 union {
296 u16_le adpcm_ps;
297 BitField<0, 4, u16_le> adpcm_scale;
298 BitField<4, 4, u16_le> adpcm_predictor;
299 };
300
301 /// ADPCM Historical Samples (y[n-1] and y[n-2])
302 u16_le adpcm_yn[2];
303
304 union {
305 u16_le flags2_raw;
306 BitField<0, 1, u16_le> adpcm_dirty; ///< Has the ADPCM info above been changed?
307 BitField<1, 1, u16_le> is_looping; ///< Is this a looping buffer?
308 };
309
310 /// Buffer id of embedded buffer (used as a buffer id in SourceStatus to reference this
311 /// buffer).
312 u16_le buffer_id;
313 };
314
315 Configuration config[num_sources];
316};
317ASSERT_DSP_STRUCT(SourceConfiguration::Configuration, 192);
318ASSERT_DSP_STRUCT(SourceConfiguration::Configuration::Buffer, 20);
319
320struct SourceStatus {
321 struct Status {
322 u8 is_enabled; ///< Is this channel enabled? (Doesn't have to be playing anything.)
323 u8 current_buffer_id_dirty; ///< Non-zero when current_buffer_id changes
324 u16_le sync; ///< Is set by the DSP to the value of SourceConfiguration::sync
325 u32_dsp buffer_position; ///< Number of samples into the current buffer
326 u16_le current_buffer_id; ///< Updated when a buffer finishes playing
327 INSERT_PADDING_DSPWORDS(1);
328 };
329
330 Status status[num_sources];
331};
332ASSERT_DSP_STRUCT(SourceStatus::Status, 12);
333
334struct DspConfiguration {
335 /// These dirty flags are set by the application when it updates the fields in this struct.
336 /// The DSP clears these each audio frame.
337 union {
338 u32_le dirty_raw;
339
340 BitField<8, 1, u32_le> mixer1_enabled_dirty;
341 BitField<9, 1, u32_le> mixer2_enabled_dirty;
342 BitField<10, 1, u32_le> delay_effect_0_dirty;
343 BitField<11, 1, u32_le> delay_effect_1_dirty;
344 BitField<12, 1, u32_le> reverb_effect_0_dirty;
345 BitField<13, 1, u32_le> reverb_effect_1_dirty;
346
347 BitField<16, 1, u32_le> volume_0_dirty;
348
349 BitField<24, 1, u32_le> volume_1_dirty;
350 BitField<25, 1, u32_le> volume_2_dirty;
351 BitField<26, 1, u32_le> output_format_dirty;
352 BitField<27, 1, u32_le> limiter_enabled_dirty;
353 BitField<28, 1, u32_le> headphones_connected_dirty;
354 };
355
356 /// The DSP has three intermediate audio mixers. This controls the volume level (0.0-1.0) for
357 /// each at the final mixer.
358 float_le volume[3];
359
360 INSERT_PADDING_DSPWORDS(3);
361
362 enum class OutputFormat : u16_le {
363 Mono = 0,
364 Stereo = 1,
365 Surround = 2,
366 };
367
368 OutputFormat output_format;
369
370 u16_le limiter_enabled; ///< Not sure of the exact gain equation for the limiter.
371 u16_le headphones_connected; ///< Application updates the DSP on headphone status.
372 INSERT_PADDING_DSPWORDS(4); ///< TODO: Surround sound related
373 INSERT_PADDING_DSPWORDS(2); ///< TODO: Intermediate mixer 1/2 related
374 u16_le mixer1_enabled;
375 u16_le mixer2_enabled;
376
377 /**
378 * This is delay with feedback.
379 * Transfer function:
380 * H(z) = a z^-N / (1 - b z^-1 + a g z^-N)
381 * where
382 * N = frame_count * samples_per_frame
383 * g, a and b are fixed point with 7 fractional bits
384 */
385 struct DelayEffect {
386 /// These dirty flags are set by the application when it updates the fields in this struct.
387 /// The DSP clears these each audio frame.
388 union {
389 u16_le dirty_raw;
390 BitField<0, 1, u16_le> enable_dirty;
391 BitField<1, 1, u16_le> work_buffer_address_dirty;
392 BitField<2, 1, u16_le> other_dirty; ///< Set when anything else has been changed
393 };
394
395 u16_le enable;
396 INSERT_PADDING_DSPWORDS(1);
397 u16_le outputs;
398 /// The application allocates a block of memory for the DSP to use as a work buffer.
399 u32_dsp work_buffer_address;
400 /// Frames to delay by
401 u16_le frame_count;
402
403 // Coefficients
404 s16_le g; ///< Fixed point with 7 fractional bits
405 s16_le a; ///< Fixed point with 7 fractional bits
406 s16_le b; ///< Fixed point with 7 fractional bits
407 };
408
409 DelayEffect delay_effect[2];
410
411 struct ReverbEffect {
412 INSERT_PADDING_DSPWORDS(26); ///< TODO
413 };
414
415 ReverbEffect reverb_effect[2];
416
417 INSERT_PADDING_DSPWORDS(4);
418};
419ASSERT_DSP_STRUCT(DspConfiguration, 196);
420ASSERT_DSP_STRUCT(DspConfiguration::DelayEffect, 20);
421ASSERT_DSP_STRUCT(DspConfiguration::ReverbEffect, 52);
422
423struct AdpcmCoefficients {
424 /// Coefficients are signed fixed point with 11 fractional bits.
425 /// Each source has 16 coefficients associated with it.
426 s16_le coeff[num_sources][16];
427};
428ASSERT_DSP_STRUCT(AdpcmCoefficients, 768);
429
430struct DspStatus {
431 u16_le unknown;
432 u16_le dropped_frames;
433 INSERT_PADDING_DSPWORDS(0xE);
434};
435ASSERT_DSP_STRUCT(DspStatus, 32);
436
437/// Final mixed output in PCM16 stereo format, what you hear out of the speakers.
438/// When the application writes to this region it has no effect.
439struct FinalMixSamples {
440 s16_le pcm16[samples_per_frame][2];
441};
442ASSERT_DSP_STRUCT(FinalMixSamples, 640);
443
444/// DSP writes output of intermediate mixers 1 and 2 here.
445/// Writes to this region by the application edits the output of the intermediate mixers.
446/// This seems to be intended to allow the application to do custom effects on the ARM11.
447/// Values that exceed s16 range will be clipped by the DSP after further processing.
448struct IntermediateMixSamples {
449 struct Samples {
450 s32_le pcm32[4][samples_per_frame]; ///< Little-endian as opposed to DSP middle-endian.
451 };
452
453 Samples mix1;
454 Samples mix2;
455};
456ASSERT_DSP_STRUCT(IntermediateMixSamples, 5120);
457
458/// Compressor table
459struct Compressor {
460 INSERT_PADDING_DSPWORDS(0xD20); ///< TODO
461};
462
463/// There is no easy way to implement this in a HLE implementation.
464struct DspDebug {
465 INSERT_PADDING_DSPWORDS(0x130);
466};
467ASSERT_DSP_STRUCT(DspDebug, 0x260);
468
469struct SharedMemory {
470 /// Padding
471 INSERT_PADDING_DSPWORDS(0x400);
472
473 DspStatus dsp_status;
474
475 DspDebug dsp_debug;
476
477 FinalMixSamples final_samples;
478
479 SourceStatus source_statuses;
480
481 Compressor compressor;
482
483 DspConfiguration dsp_configuration;
484
485 IntermediateMixSamples intermediate_mix_samples;
486
487 SourceConfiguration source_configurations;
488
489 AdpcmCoefficients adpcm_coefficients;
490
491 struct {
492 INSERT_PADDING_DSPWORDS(0x100);
493 } unknown10;
494
495 struct {
496 INSERT_PADDING_DSPWORDS(0xC0);
497 } unknown11;
498
499 struct {
500 INSERT_PADDING_DSPWORDS(0x180);
501 } unknown12;
502
503 struct {
504 INSERT_PADDING_DSPWORDS(0xA);
505 } unknown13;
506
507 struct {
508 INSERT_PADDING_DSPWORDS(0x13A3);
509 } unknown14;
510
511 u16_le frame_counter;
512};
513ASSERT_DSP_STRUCT(SharedMemory, 0x8000);
514
515union DspMemory {
516 std::array<u8, 0x80000> raw_memory;
517 struct {
518 u8 unused_0[0x50000];
519 SharedMemory region_0;
520 u8 unused_1[0x18000];
521 SharedMemory region_1;
522 u8 unused_2[0x8000];
523 };
524};
525static_assert(offsetof(DspMemory, region_0) == region0_offset,
526 "DSP region 0 is at the wrong offset");
527static_assert(offsetof(DspMemory, region_1) == region1_offset,
528 "DSP region 1 is at the wrong offset");
529
530extern DspMemory g_dsp_memory;
531
532// Structures must have an offset that is a multiple of two.
533static_assert(offsetof(SharedMemory, frame_counter) % 2 == 0,
534 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
535static_assert(offsetof(SharedMemory, source_configurations) % 2 == 0,
536 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
537static_assert(offsetof(SharedMemory, source_statuses) % 2 == 0,
538 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
539static_assert(offsetof(SharedMemory, adpcm_coefficients) % 2 == 0,
540 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
541static_assert(offsetof(SharedMemory, dsp_configuration) % 2 == 0,
542 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
543static_assert(offsetof(SharedMemory, dsp_status) % 2 == 0,
544 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
545static_assert(offsetof(SharedMemory, final_samples) % 2 == 0,
546 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
547static_assert(offsetof(SharedMemory, intermediate_mix_samples) % 2 == 0,
548 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
549static_assert(offsetof(SharedMemory, compressor) % 2 == 0,
550 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
551static_assert(offsetof(SharedMemory, dsp_debug) % 2 == 0,
552 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
553static_assert(offsetof(SharedMemory, unknown10) % 2 == 0,
554 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
555static_assert(offsetof(SharedMemory, unknown11) % 2 == 0,
556 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
557static_assert(offsetof(SharedMemory, unknown12) % 2 == 0,
558 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
559static_assert(offsetof(SharedMemory, unknown13) % 2 == 0,
560 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
561static_assert(offsetof(SharedMemory, unknown14) % 2 == 0,
562 "Structures in DSP::HLE::SharedMemory must be 2-byte aligned");
563
564#undef INSERT_PADDING_DSPWORDS
565#undef ASSERT_DSP_STRUCT
566
567/// Initialize DSP hardware
568void Init();
569
570/// Shutdown DSP hardware
571void Shutdown();
572
573/**
574 * Perform processing and updates state of current shared memory buffer.
575 * This function is called every audio tick before triggering the audio interrupt.
576 * @return Whether an audio interrupt should be triggered this frame.
577 */
578bool Tick();
579
580/**
581 * Set the output sink. This must be called before calling Tick().
582 * @param sink The sink to which audio will be output to.
583 */
584void SetSink(std::unique_ptr<AudioCore::Sink> sink);
585
586/**
587 * Enables/Disables audio-stretching.
588 * Audio stretching is an enhancement that stretches audio to match emulation
589 * speed to prevent stuttering at the cost of some audio latency.
590 * @param enable true to enable, false to disable.
591 */
592void EnableStretching(bool enable);
593
594} // namespace HLE
595} // namespace DSP
diff --git a/src/audio_core/hle/filter.cpp b/src/audio_core/hle/filter.cpp
deleted file mode 100644
index b24a79b89..000000000
--- a/src/audio_core/hle/filter.cpp
+++ /dev/null
@@ -1,117 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <cstddef>
7#include "audio_core/hle/common.h"
8#include "audio_core/hle/dsp.h"
9#include "audio_core/hle/filter.h"
10#include "common/common_types.h"
11#include "common/math_util.h"
12
13namespace DSP {
14namespace HLE {
15
16void SourceFilters::Reset() {
17 Enable(false, false);
18}
19
20void SourceFilters::Enable(bool simple, bool biquad) {
21 simple_filter_enabled = simple;
22 biquad_filter_enabled = biquad;
23
24 if (!simple)
25 simple_filter.Reset();
26 if (!biquad)
27 biquad_filter.Reset();
28}
29
30void SourceFilters::Configure(SourceConfiguration::Configuration::SimpleFilter config) {
31 simple_filter.Configure(config);
32}
33
34void SourceFilters::Configure(SourceConfiguration::Configuration::BiquadFilter config) {
35 biquad_filter.Configure(config);
36}
37
38void SourceFilters::ProcessFrame(StereoFrame16& frame) {
39 if (!simple_filter_enabled && !biquad_filter_enabled)
40 return;
41
42 if (simple_filter_enabled) {
43 FilterFrame(frame, simple_filter);
44 }
45
46 if (biquad_filter_enabled) {
47 FilterFrame(frame, biquad_filter);
48 }
49}
50
51// SimpleFilter
52
53void SourceFilters::SimpleFilter::Reset() {
54 y1.fill(0);
55 // Configure as passthrough.
56 a1 = 0;
57 b0 = 1 << 15;
58}
59
60void SourceFilters::SimpleFilter::Configure(
61 SourceConfiguration::Configuration::SimpleFilter config) {
62
63 a1 = config.a1;
64 b0 = config.b0;
65}
66
67std::array<s16, 2> SourceFilters::SimpleFilter::ProcessSample(const std::array<s16, 2>& x0) {
68 std::array<s16, 2> y0;
69 for (size_t i = 0; i < 2; i++) {
70 const s32 tmp = (b0 * x0[i] + a1 * y1[i]) >> 15;
71 y0[i] = MathUtil::Clamp(tmp, -32768, 32767);
72 }
73
74 y1 = y0;
75
76 return y0;
77}
78
79// BiquadFilter
80
81void SourceFilters::BiquadFilter::Reset() {
82 x1.fill(0);
83 x2.fill(0);
84 y1.fill(0);
85 y2.fill(0);
86 // Configure as passthrough.
87 a1 = a2 = b1 = b2 = 0;
88 b0 = 1 << 14;
89}
90
91void SourceFilters::BiquadFilter::Configure(
92 SourceConfiguration::Configuration::BiquadFilter config) {
93
94 a1 = config.a1;
95 a2 = config.a2;
96 b0 = config.b0;
97 b1 = config.b1;
98 b2 = config.b2;
99}
100
101std::array<s16, 2> SourceFilters::BiquadFilter::ProcessSample(const std::array<s16, 2>& x0) {
102 std::array<s16, 2> y0;
103 for (size_t i = 0; i < 2; i++) {
104 const s32 tmp = (b0 * x0[i] + b1 * x1[i] + b2 * x2[i] + a1 * y1[i] + a2 * y2[i]) >> 14;
105 y0[i] = MathUtil::Clamp(tmp, -32768, 32767);
106 }
107
108 x2 = x1;
109 x1 = x0;
110 y2 = y1;
111 y1 = y0;
112
113 return y0;
114}
115
116} // namespace HLE
117} // namespace DSP
diff --git a/src/audio_core/hle/filter.h b/src/audio_core/hle/filter.h
deleted file mode 100644
index 5350e2857..000000000
--- a/src/audio_core/hle/filter.h
+++ /dev/null
@@ -1,117 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include "audio_core/hle/common.h"
9#include "audio_core/hle/dsp.h"
10#include "common/common_types.h"
11
12namespace DSP {
13namespace HLE {
14
15/// Preprocessing filters. There is an independent set of filters for each Source.
16class SourceFilters final {
17public:
18 SourceFilters() {
19 Reset();
20 }
21
22 /// Reset internal state.
23 void Reset();
24
25 /**
26 * Enable/Disable filters
27 * See also: SourceConfiguration::Configuration::simple_filter_enabled,
28 * SourceConfiguration::Configuration::biquad_filter_enabled.
29 * @param simple If true, enables the simple filter. If false, disables it.
30 * @param biquad If true, enables the biquad filter. If false, disables it.
31 */
32 void Enable(bool simple, bool biquad);
33
34 /**
35 * Configure simple filter.
36 * @param config Configuration from DSP shared memory.
37 */
38 void Configure(SourceConfiguration::Configuration::SimpleFilter config);
39
40 /**
41 * Configure biquad filter.
42 * @param config Configuration from DSP shared memory.
43 */
44 void Configure(SourceConfiguration::Configuration::BiquadFilter config);
45
46 /**
47 * Processes a frame in-place.
48 * @param frame Audio samples to process. Modified in-place.
49 */
50 void ProcessFrame(StereoFrame16& frame);
51
52private:
53 bool simple_filter_enabled;
54 bool biquad_filter_enabled;
55
56 struct SimpleFilter {
57 SimpleFilter() {
58 Reset();
59 }
60
61 /// Resets internal state.
62 void Reset();
63
64 /**
65 * Configures this filter with application settings.
66 * @param config Configuration from DSP shared memory.
67 */
68 void Configure(SourceConfiguration::Configuration::SimpleFilter config);
69
70 /**
71 * Processes a single stereo PCM16 sample.
72 * @param x0 Input sample
73 * @return Output sample
74 */
75 std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0);
76
77 private:
78 // Configuration
79 s32 a1, b0;
80 // Internal state
81 std::array<s16, 2> y1;
82 } simple_filter;
83
84 struct BiquadFilter {
85 BiquadFilter() {
86 Reset();
87 }
88
89 /// Resets internal state.
90 void Reset();
91
92 /**
93 * Configures this filter with application settings.
94 * @param config Configuration from DSP shared memory.
95 */
96 void Configure(SourceConfiguration::Configuration::BiquadFilter config);
97
98 /**
99 * Processes a single stereo PCM16 sample.
100 * @param x0 Input sample
101 * @return Output sample
102 */
103 std::array<s16, 2> ProcessSample(const std::array<s16, 2>& x0);
104
105 private:
106 // Configuration
107 s32 a1, a2, b0, b1, b2;
108 // Internal state
109 std::array<s16, 2> x1;
110 std::array<s16, 2> x2;
111 std::array<s16, 2> y1;
112 std::array<s16, 2> y2;
113 } biquad_filter;
114};
115
116} // namespace HLE
117} // namespace DSP
diff --git a/src/audio_core/hle/mixers.cpp b/src/audio_core/hle/mixers.cpp
deleted file mode 100644
index 6cc81dfca..000000000
--- a/src/audio_core/hle/mixers.cpp
+++ /dev/null
@@ -1,210 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstddef>
6
7#include "audio_core/hle/common.h"
8#include "audio_core/hle/dsp.h"
9#include "audio_core/hle/mixers.h"
10#include "common/assert.h"
11#include "common/logging/log.h"
12#include "common/math_util.h"
13
14namespace DSP {
15namespace HLE {
16
17void Mixers::Reset() {
18 current_frame.fill({});
19 state = {};
20}
21
22DspStatus Mixers::Tick(DspConfiguration& config, const IntermediateMixSamples& read_samples,
23 IntermediateMixSamples& write_samples,
24 const std::array<QuadFrame32, 3>& input) {
25 ParseConfig(config);
26
27 AuxReturn(read_samples);
28 AuxSend(write_samples, input);
29
30 MixCurrentFrame();
31
32 return GetCurrentStatus();
33}
34
35void Mixers::ParseConfig(DspConfiguration& config) {
36 if (!config.dirty_raw) {
37 return;
38 }
39
40 if (config.mixer1_enabled_dirty) {
41 config.mixer1_enabled_dirty.Assign(0);
42 state.mixer1_enabled = config.mixer1_enabled != 0;
43 LOG_TRACE(Audio_DSP, "mixers mixer1_enabled = %hu", config.mixer1_enabled);
44 }
45
46 if (config.mixer2_enabled_dirty) {
47 config.mixer2_enabled_dirty.Assign(0);
48 state.mixer2_enabled = config.mixer2_enabled != 0;
49 LOG_TRACE(Audio_DSP, "mixers mixer2_enabled = %hu", config.mixer2_enabled);
50 }
51
52 if (config.volume_0_dirty) {
53 config.volume_0_dirty.Assign(0);
54 state.intermediate_mixer_volume[0] = config.volume[0];
55 LOG_TRACE(Audio_DSP, "mixers volume[0] = %f", config.volume[0]);
56 }
57
58 if (config.volume_1_dirty) {
59 config.volume_1_dirty.Assign(0);
60 state.intermediate_mixer_volume[1] = config.volume[1];
61 LOG_TRACE(Audio_DSP, "mixers volume[1] = %f", config.volume[1]);
62 }
63
64 if (config.volume_2_dirty) {
65 config.volume_2_dirty.Assign(0);
66 state.intermediate_mixer_volume[2] = config.volume[2];
67 LOG_TRACE(Audio_DSP, "mixers volume[2] = %f", config.volume[2]);
68 }
69
70 if (config.output_format_dirty) {
71 config.output_format_dirty.Assign(0);
72 state.output_format = config.output_format;
73 LOG_TRACE(Audio_DSP, "mixers output_format = %zu",
74 static_cast<size_t>(config.output_format));
75 }
76
77 if (config.headphones_connected_dirty) {
78 config.headphones_connected_dirty.Assign(0);
79 // Do nothing. (Note: Whether headphones are connected does affect coefficients used for
80 // surround sound.)
81 LOG_TRACE(Audio_DSP, "mixers headphones_connected=%hu", config.headphones_connected);
82 }
83
84 if (config.dirty_raw) {
85 LOG_DEBUG(Audio_DSP, "mixers remaining_dirty=%x", config.dirty_raw);
86 }
87
88 config.dirty_raw = 0;
89}
90
91static s16 ClampToS16(s32 value) {
92 return static_cast<s16>(MathUtil::Clamp(value, -32768, 32767));
93}
94
95static std::array<s16, 2> AddAndClampToS16(const std::array<s16, 2>& a,
96 const std::array<s16, 2>& b) {
97 return {ClampToS16(static_cast<s32>(a[0]) + static_cast<s32>(b[0])),
98 ClampToS16(static_cast<s32>(a[1]) + static_cast<s32>(b[1]))};
99}
100
101void Mixers::DownmixAndMixIntoCurrentFrame(float gain, const QuadFrame32& samples) {
102 // TODO(merry): Limiter. (Currently we're performing final mixing assuming a disabled limiter.)
103
104 switch (state.output_format) {
105 case OutputFormat::Mono:
106 std::transform(
107 current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(),
108 [gain](const std::array<s16, 2>& accumulator,
109 const std::array<s32, 4>& sample) -> std::array<s16, 2> {
110 // Downmix to mono
111 s16 mono = ClampToS16(static_cast<s32>(
112 (gain * sample[0] + gain * sample[1] + gain * sample[2] + gain * sample[3]) /
113 2));
114 // Mix into current frame
115 return AddAndClampToS16(accumulator, {mono, mono});
116 });
117 return;
118
119 case OutputFormat::Surround:
120 // TODO(merry): Implement surround sound.
121 // fallthrough
122
123 case OutputFormat::Stereo:
124 std::transform(
125 current_frame.begin(), current_frame.end(), samples.begin(), current_frame.begin(),
126 [gain](const std::array<s16, 2>& accumulator,
127 const std::array<s32, 4>& sample) -> std::array<s16, 2> {
128 // Downmix to stereo
129 s16 left = ClampToS16(static_cast<s32>(gain * sample[0] + gain * sample[2]));
130 s16 right = ClampToS16(static_cast<s32>(gain * sample[1] + gain * sample[3]));
131 // Mix into current frame
132 return AddAndClampToS16(accumulator, {left, right});
133 });
134 return;
135 }
136
137 UNREACHABLE_MSG("Invalid output_format %zu", static_cast<size_t>(state.output_format));
138}
139
140void Mixers::AuxReturn(const IntermediateMixSamples& read_samples) {
141 // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to
142 // QuadFrame32.
143
144 if (state.mixer1_enabled) {
145 for (size_t sample = 0; sample < samples_per_frame; sample++) {
146 for (size_t channel = 0; channel < 4; channel++) {
147 state.intermediate_mix_buffer[1][sample][channel] =
148 read_samples.mix1.pcm32[channel][sample];
149 }
150 }
151 }
152
153 if (state.mixer2_enabled) {
154 for (size_t sample = 0; sample < samples_per_frame; sample++) {
155 for (size_t channel = 0; channel < 4; channel++) {
156 state.intermediate_mix_buffer[2][sample][channel] =
157 read_samples.mix2.pcm32[channel][sample];
158 }
159 }
160 }
161}
162
163void Mixers::AuxSend(IntermediateMixSamples& write_samples,
164 const std::array<QuadFrame32, 3>& input) {
165 // NOTE: read_samples.mix{1,2}.pcm32 annoyingly have their dimensions in reverse order to
166 // QuadFrame32.
167
168 state.intermediate_mix_buffer[0] = input[0];
169
170 if (state.mixer1_enabled) {
171 for (size_t sample = 0; sample < samples_per_frame; sample++) {
172 for (size_t channel = 0; channel < 4; channel++) {
173 write_samples.mix1.pcm32[channel][sample] = input[1][sample][channel];
174 }
175 }
176 } else {
177 state.intermediate_mix_buffer[1] = input[1];
178 }
179
180 if (state.mixer2_enabled) {
181 for (size_t sample = 0; sample < samples_per_frame; sample++) {
182 for (size_t channel = 0; channel < 4; channel++) {
183 write_samples.mix2.pcm32[channel][sample] = input[2][sample][channel];
184 }
185 }
186 } else {
187 state.intermediate_mix_buffer[2] = input[2];
188 }
189}
190
191void Mixers::MixCurrentFrame() {
192 current_frame.fill({});
193
194 for (size_t mix = 0; mix < 3; mix++) {
195 DownmixAndMixIntoCurrentFrame(state.intermediate_mixer_volume[mix],
196 state.intermediate_mix_buffer[mix]);
197 }
198
199 // TODO(merry): Compressor. (We currently assume a disabled compressor.)
200}
201
202DspStatus Mixers::GetCurrentStatus() const {
203 DspStatus status;
204 status.unknown = 0;
205 status.dropped_frames = 0;
206 return status;
207}
208
209} // namespace HLE
210} // namespace DSP
diff --git a/src/audio_core/hle/mixers.h b/src/audio_core/hle/mixers.h
deleted file mode 100644
index bf4e865ae..000000000
--- a/src/audio_core/hle/mixers.h
+++ /dev/null
@@ -1,61 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include "audio_core/hle/common.h"
9#include "audio_core/hle/dsp.h"
10
11namespace DSP {
12namespace HLE {
13
14class Mixers final {
15public:
16 Mixers() {
17 Reset();
18 }
19
20 void Reset();
21
22 DspStatus Tick(DspConfiguration& config, const IntermediateMixSamples& read_samples,
23 IntermediateMixSamples& write_samples, const std::array<QuadFrame32, 3>& input);
24
25 StereoFrame16 GetOutput() const {
26 return current_frame;
27 }
28
29private:
30 StereoFrame16 current_frame = {};
31
32 using OutputFormat = DspConfiguration::OutputFormat;
33
34 struct {
35 std::array<float, 3> intermediate_mixer_volume = {};
36
37 bool mixer1_enabled = false;
38 bool mixer2_enabled = false;
39 std::array<QuadFrame32, 3> intermediate_mix_buffer = {};
40
41 OutputFormat output_format = OutputFormat::Stereo;
42
43 } state;
44
45 /// INTERNAL: Update our internal state based on the current config.
46 void ParseConfig(DspConfiguration& config);
47 /// INTERNAL: Read samples from shared memory that have been modified by the ARM11.
48 void AuxReturn(const IntermediateMixSamples& read_samples);
49 /// INTERNAL: Write samples to shared memory for the ARM11 to modify.
50 void AuxSend(IntermediateMixSamples& write_samples, const std::array<QuadFrame32, 3>& input);
51 /// INTERNAL: Mix current_frame.
52 void MixCurrentFrame();
53 /// INTERNAL: Downmix from quadraphonic to stereo based on status.output_format and accumulate
54 /// into current_frame.
55 void DownmixAndMixIntoCurrentFrame(float gain, const QuadFrame32& samples);
56 /// INTERNAL: Generate DspStatus based on internal state.
57 DspStatus GetCurrentStatus() const;
58};
59
60} // namespace HLE
61} // namespace DSP
diff --git a/src/audio_core/hle/pipe.cpp b/src/audio_core/hle/pipe.cpp
deleted file mode 100644
index 24074a514..000000000
--- a/src/audio_core/hle/pipe.cpp
+++ /dev/null
@@ -1,177 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <array>
6#include <vector>
7#include "audio_core/hle/dsp.h"
8#include "audio_core/hle/pipe.h"
9#include "common/assert.h"
10#include "common/common_types.h"
11#include "common/logging/log.h"
12#include "core/hle/service/dsp_dsp.h"
13
14namespace DSP {
15namespace HLE {
16
17static DspState dsp_state = DspState::Off;
18
19static std::array<std::vector<u8>, NUM_DSP_PIPE> pipe_data;
20
21void ResetPipes() {
22 for (auto& data : pipe_data) {
23 data.clear();
24 }
25 dsp_state = DspState::Off;
26}
27
28std::vector<u8> PipeRead(DspPipe pipe_number, u32 length) {
29 const size_t pipe_index = static_cast<size_t>(pipe_number);
30
31 if (pipe_index >= NUM_DSP_PIPE) {
32 LOG_ERROR(Audio_DSP, "pipe_number = %zu invalid", pipe_index);
33 return {};
34 }
35
36 if (length > UINT16_MAX) { // Can only read at most UINT16_MAX from the pipe
37 LOG_ERROR(Audio_DSP, "length of %u greater than max of %u", length, UINT16_MAX);
38 return {};
39 }
40
41 std::vector<u8>& data = pipe_data[pipe_index];
42
43 if (length > data.size()) {
44 LOG_WARNING(
45 Audio_DSP,
46 "pipe_number = %zu is out of data, application requested read of %u but %zu remain",
47 pipe_index, length, data.size());
48 length = static_cast<u32>(data.size());
49 }
50
51 if (length == 0)
52 return {};
53
54 std::vector<u8> ret(data.begin(), data.begin() + length);
55 data.erase(data.begin(), data.begin() + length);
56 return ret;
57}
58
59size_t GetPipeReadableSize(DspPipe pipe_number) {
60 const size_t pipe_index = static_cast<size_t>(pipe_number);
61
62 if (pipe_index >= NUM_DSP_PIPE) {
63 LOG_ERROR(Audio_DSP, "pipe_number = %zu invalid", pipe_index);
64 return 0;
65 }
66
67 return pipe_data[pipe_index].size();
68}
69
70static void WriteU16(DspPipe pipe_number, u16 value) {
71 const size_t pipe_index = static_cast<size_t>(pipe_number);
72
73 std::vector<u8>& data = pipe_data.at(pipe_index);
74 // Little endian
75 data.emplace_back(value & 0xFF);
76 data.emplace_back(value >> 8);
77}
78
79static void AudioPipeWriteStructAddresses() {
80 // These struct addresses are DSP dram addresses.
81 // See also: DSP_DSP::ConvertProcessAddressFromDspDram
82 static const std::array<u16, 15> struct_addresses = {
83 0x8000 + offsetof(SharedMemory, frame_counter) / 2,
84 0x8000 + offsetof(SharedMemory, source_configurations) / 2,
85 0x8000 + offsetof(SharedMemory, source_statuses) / 2,
86 0x8000 + offsetof(SharedMemory, adpcm_coefficients) / 2,
87 0x8000 + offsetof(SharedMemory, dsp_configuration) / 2,
88 0x8000 + offsetof(SharedMemory, dsp_status) / 2,
89 0x8000 + offsetof(SharedMemory, final_samples) / 2,
90 0x8000 + offsetof(SharedMemory, intermediate_mix_samples) / 2,
91 0x8000 + offsetof(SharedMemory, compressor) / 2,
92 0x8000 + offsetof(SharedMemory, dsp_debug) / 2,
93 0x8000 + offsetof(SharedMemory, unknown10) / 2,
94 0x8000 + offsetof(SharedMemory, unknown11) / 2,
95 0x8000 + offsetof(SharedMemory, unknown12) / 2,
96 0x8000 + offsetof(SharedMemory, unknown13) / 2,
97 0x8000 + offsetof(SharedMemory, unknown14) / 2,
98 };
99
100 // Begin with a u16 denoting the number of structs.
101 WriteU16(DspPipe::Audio, static_cast<u16>(struct_addresses.size()));
102 // Then write the struct addresses.
103 for (u16 addr : struct_addresses) {
104 WriteU16(DspPipe::Audio, addr);
105 }
106 // Signal that we have data on this pipe.
107 Service::DSP_DSP::SignalPipeInterrupt(DspPipe::Audio);
108}
109
110void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer) {
111 switch (pipe_number) {
112 case DspPipe::Audio: {
113 if (buffer.size() != 4) {
114 LOG_ERROR(Audio_DSP, "DspPipe::Audio: Unexpected buffer length %zu was written",
115 buffer.size());
116 return;
117 }
118
119 enum class StateChange {
120 Initialize = 0,
121 Shutdown = 1,
122 Wakeup = 2,
123 Sleep = 3,
124 };
125
126 // The difference between Initialize and Wakeup is that Input state is maintained
127 // when sleeping but isn't when turning it off and on again. (TODO: Implement this.)
128 // Waking up from sleep garbles some of the structs in the memory region. (TODO:
129 // Implement this.) Applications store away the state of these structs before
130 // sleeping and reset it back after wakeup on behalf of the DSP.
131
132 switch (static_cast<StateChange>(buffer[0])) {
133 case StateChange::Initialize:
134 LOG_INFO(Audio_DSP, "Application has requested initialization of DSP hardware");
135 ResetPipes();
136 AudioPipeWriteStructAddresses();
137 dsp_state = DspState::On;
138 break;
139 case StateChange::Shutdown:
140 LOG_INFO(Audio_DSP, "Application has requested shutdown of DSP hardware");
141 dsp_state = DspState::Off;
142 break;
143 case StateChange::Wakeup:
144 LOG_INFO(Audio_DSP, "Application has requested wakeup of DSP hardware");
145 ResetPipes();
146 AudioPipeWriteStructAddresses();
147 dsp_state = DspState::On;
148 break;
149 case StateChange::Sleep:
150 LOG_INFO(Audio_DSP, "Application has requested sleep of DSP hardware");
151 UNIMPLEMENTED();
152 dsp_state = DspState::Sleeping;
153 break;
154 default:
155 LOG_ERROR(Audio_DSP,
156 "Application has requested unknown state transition of DSP hardware %hhu",
157 buffer[0]);
158 dsp_state = DspState::Off;
159 break;
160 }
161
162 return;
163 }
164 default:
165 LOG_CRITICAL(Audio_DSP, "pipe_number = %zu unimplemented",
166 static_cast<size_t>(pipe_number));
167 UNIMPLEMENTED();
168 return;
169 }
170}
171
172DspState GetDspState() {
173 return dsp_state;
174}
175
176} // namespace HLE
177} // namespace DSP
diff --git a/src/audio_core/hle/pipe.h b/src/audio_core/hle/pipe.h
deleted file mode 100644
index ac053c029..000000000
--- a/src/audio_core/hle/pipe.h
+++ /dev/null
@@ -1,63 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <cstddef>
8#include <vector>
9#include "common/common_types.h"
10
11namespace DSP {
12namespace HLE {
13
14/// Reset the pipes by setting pipe positions back to the beginning.
15void ResetPipes();
16
17enum class DspPipe {
18 Debug = 0,
19 Dma = 1,
20 Audio = 2,
21 Binary = 3,
22};
23constexpr size_t NUM_DSP_PIPE = 8;
24
25/**
26 * Reads `length` bytes from the DSP pipe identified with `pipe_number`.
27 * @note Can read up to the maximum value of a u16 in bytes (65,535).
28 * @note IF an error is encoutered with either an invalid `pipe_number` or `length` value, an empty
29 * vector will be returned.
30 * @note IF `length` is set to 0, an empty vector will be returned.
31 * @note IF `length` is greater than the amount of data available, this function will only read the
32 * available amount.
33 * @param pipe_number a `DspPipe`
34 * @param length the number of bytes to read. The max is 65,535 (max of u16).
35 * @returns a vector of bytes from the specified pipe. On error, will be empty.
36 */
37std::vector<u8> PipeRead(DspPipe pipe_number, u32 length);
38
39/**
40 * How much data is left in pipe
41 * @param pipe_number The Pipe ID
42 * @return The amount of data remaning in the pipe. This is the maximum length PipeRead will return.
43 */
44size_t GetPipeReadableSize(DspPipe pipe_number);
45
46/**
47 * Write to a DSP pipe.
48 * @param pipe_number The Pipe ID
49 * @param buffer The data to write to the pipe.
50 */
51void PipeWrite(DspPipe pipe_number, const std::vector<u8>& buffer);
52
53enum class DspState {
54 Off,
55 On,
56 Sleeping,
57};
58
59/// Get the state of the DSP
60DspState GetDspState();
61
62} // namespace HLE
63} // namespace DSP
diff --git a/src/audio_core/hle/source.cpp b/src/audio_core/hle/source.cpp
deleted file mode 100644
index c12287700..000000000
--- a/src/audio_core/hle/source.cpp
+++ /dev/null
@@ -1,349 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <array>
7#include "audio_core/codec.h"
8#include "audio_core/hle/common.h"
9#include "audio_core/hle/source.h"
10#include "audio_core/interpolate.h"
11#include "common/assert.h"
12#include "common/logging/log.h"
13#include "core/memory.h"
14
15namespace DSP {
16namespace HLE {
17
18SourceStatus::Status Source::Tick(SourceConfiguration::Configuration& config,
19 const s16_le (&adpcm_coeffs)[16]) {
20 ParseConfig(config, adpcm_coeffs);
21
22 if (state.enabled) {
23 GenerateFrame();
24 }
25
26 return GetCurrentStatus();
27}
28
29void Source::MixInto(QuadFrame32& dest, size_t intermediate_mix_id) const {
30 if (!state.enabled)
31 return;
32
33 const std::array<float, 4>& gains = state.gain.at(intermediate_mix_id);
34 for (size_t samplei = 0; samplei < samples_per_frame; samplei++) {
35 // Conversion from stereo (current_frame) to quadraphonic (dest) occurs here.
36 dest[samplei][0] += static_cast<s32>(gains[0] * current_frame[samplei][0]);
37 dest[samplei][1] += static_cast<s32>(gains[1] * current_frame[samplei][1]);
38 dest[samplei][2] += static_cast<s32>(gains[2] * current_frame[samplei][0]);
39 dest[samplei][3] += static_cast<s32>(gains[3] * current_frame[samplei][1]);
40 }
41}
42
43void Source::Reset() {
44 current_frame.fill({});
45 state = {};
46}
47
48void Source::ParseConfig(SourceConfiguration::Configuration& config,
49 const s16_le (&adpcm_coeffs)[16]) {
50 if (!config.dirty_raw) {
51 return;
52 }
53
54 if (config.reset_flag) {
55 config.reset_flag.Assign(0);
56 Reset();
57 LOG_TRACE(Audio_DSP, "source_id=%zu reset", source_id);
58 }
59
60 if (config.partial_reset_flag) {
61 config.partial_reset_flag.Assign(0);
62 state.input_queue = std::priority_queue<Buffer, std::vector<Buffer>, BufferOrder>{};
63 LOG_TRACE(Audio_DSP, "source_id=%zu partial_reset", source_id);
64 }
65
66 if (config.enable_dirty) {
67 config.enable_dirty.Assign(0);
68 state.enabled = config.enable != 0;
69 LOG_TRACE(Audio_DSP, "source_id=%zu enable=%d", source_id, state.enabled);
70 }
71
72 if (config.sync_dirty) {
73 config.sync_dirty.Assign(0);
74 state.sync = config.sync;
75 LOG_TRACE(Audio_DSP, "source_id=%zu sync=%u", source_id, state.sync);
76 }
77
78 if (config.rate_multiplier_dirty) {
79 config.rate_multiplier_dirty.Assign(0);
80 state.rate_multiplier = config.rate_multiplier;
81 LOG_TRACE(Audio_DSP, "source_id=%zu rate=%f", source_id, state.rate_multiplier);
82
83 if (state.rate_multiplier <= 0) {
84 LOG_ERROR(Audio_DSP, "Was given an invalid rate multiplier: source_id=%zu rate=%f",
85 source_id, state.rate_multiplier);
86 state.rate_multiplier = 1.0f;
87 // Note: Actual firmware starts producing garbage if this occurs.
88 }
89 }
90
91 if (config.adpcm_coefficients_dirty) {
92 config.adpcm_coefficients_dirty.Assign(0);
93 std::transform(adpcm_coeffs, adpcm_coeffs + state.adpcm_coeffs.size(),
94 state.adpcm_coeffs.begin(),
95 [](const auto& coeff) { return static_cast<s16>(coeff); });
96 LOG_TRACE(Audio_DSP, "source_id=%zu adpcm update", source_id);
97 }
98
99 if (config.gain_0_dirty) {
100 config.gain_0_dirty.Assign(0);
101 std::transform(config.gain[0], config.gain[0] + state.gain[0].size(), state.gain[0].begin(),
102 [](const auto& coeff) { return static_cast<float>(coeff); });
103 LOG_TRACE(Audio_DSP, "source_id=%zu gain 0 update", source_id);
104 }
105
106 if (config.gain_1_dirty) {
107 config.gain_1_dirty.Assign(0);
108 std::transform(config.gain[1], config.gain[1] + state.gain[1].size(), state.gain[1].begin(),
109 [](const auto& coeff) { return static_cast<float>(coeff); });
110 LOG_TRACE(Audio_DSP, "source_id=%zu gain 1 update", source_id);
111 }
112
113 if (config.gain_2_dirty) {
114 config.gain_2_dirty.Assign(0);
115 std::transform(config.gain[2], config.gain[2] + state.gain[2].size(), state.gain[2].begin(),
116 [](const auto& coeff) { return static_cast<float>(coeff); });
117 LOG_TRACE(Audio_DSP, "source_id=%zu gain 2 update", source_id);
118 }
119
120 if (config.filters_enabled_dirty) {
121 config.filters_enabled_dirty.Assign(0);
122 state.filters.Enable(config.simple_filter_enabled.ToBool(),
123 config.biquad_filter_enabled.ToBool());
124 LOG_TRACE(Audio_DSP, "source_id=%zu enable_simple=%hu enable_biquad=%hu", source_id,
125 config.simple_filter_enabled.Value(), config.biquad_filter_enabled.Value());
126 }
127
128 if (config.simple_filter_dirty) {
129 config.simple_filter_dirty.Assign(0);
130 state.filters.Configure(config.simple_filter);
131 LOG_TRACE(Audio_DSP, "source_id=%zu simple filter update", source_id);
132 }
133
134 if (config.biquad_filter_dirty) {
135 config.biquad_filter_dirty.Assign(0);
136 state.filters.Configure(config.biquad_filter);
137 LOG_TRACE(Audio_DSP, "source_id=%zu biquad filter update", source_id);
138 }
139
140 if (config.interpolation_dirty) {
141 config.interpolation_dirty.Assign(0);
142 state.interpolation_mode = config.interpolation_mode;
143 LOG_TRACE(Audio_DSP, "source_id=%zu interpolation_mode=%zu", source_id,
144 static_cast<size_t>(state.interpolation_mode));
145 }
146
147 if (config.format_dirty || config.embedded_buffer_dirty) {
148 config.format_dirty.Assign(0);
149 state.format = config.format;
150 LOG_TRACE(Audio_DSP, "source_id=%zu format=%zu", source_id,
151 static_cast<size_t>(state.format));
152 }
153
154 if (config.mono_or_stereo_dirty || config.embedded_buffer_dirty) {
155 config.mono_or_stereo_dirty.Assign(0);
156 state.mono_or_stereo = config.mono_or_stereo;
157 LOG_TRACE(Audio_DSP, "source_id=%zu mono_or_stereo=%zu", source_id,
158 static_cast<size_t>(state.mono_or_stereo));
159 }
160
161 u32_dsp play_position = {};
162 if (config.play_position_dirty && config.play_position != 0) {
163 config.play_position_dirty.Assign(0);
164 play_position = config.play_position;
165 // play_position applies only to the embedded buffer, and defaults to 0 w/o a dirty bit
166 // This will be the starting sample for the first time the buffer is played.
167 }
168
169 if (config.embedded_buffer_dirty) {
170 config.embedded_buffer_dirty.Assign(0);
171 state.input_queue.emplace(Buffer{
172 config.physical_address,
173 config.length,
174 static_cast<u8>(config.adpcm_ps),
175 {config.adpcm_yn[0], config.adpcm_yn[1]},
176 config.adpcm_dirty.ToBool(),
177 config.is_looping.ToBool(),
178 config.buffer_id,
179 state.mono_or_stereo,
180 state.format,
181 false,
182 play_position,
183 false,
184 });
185 LOG_TRACE(Audio_DSP, "enqueuing embedded addr=0x%08x len=%u id=%hu start=%u",
186 config.physical_address, config.length, config.buffer_id,
187 static_cast<u32>(config.play_position));
188 }
189
190 if (config.loop_related_dirty && config.loop_related != 0) {
191 config.loop_related_dirty.Assign(0);
192 LOG_WARNING(Audio_DSP, "Unhandled complex loop with loop_related=0x%08x",
193 static_cast<u32>(config.loop_related));
194 }
195
196 if (config.buffer_queue_dirty) {
197 config.buffer_queue_dirty.Assign(0);
198 for (size_t i = 0; i < 4; i++) {
199 if (config.buffers_dirty & (1 << i)) {
200 const auto& b = config.buffers[i];
201 state.input_queue.emplace(Buffer{
202 b.physical_address,
203 b.length,
204 static_cast<u8>(b.adpcm_ps),
205 {b.adpcm_yn[0], b.adpcm_yn[1]},
206 b.adpcm_dirty != 0,
207 b.is_looping != 0,
208 b.buffer_id,
209 state.mono_or_stereo,
210 state.format,
211 true,
212 {}, // 0 in u32_dsp
213 false,
214 });
215 LOG_TRACE(Audio_DSP, "enqueuing queued %zu addr=0x%08x len=%u id=%hu", i,
216 b.physical_address, b.length, b.buffer_id);
217 }
218 }
219 config.buffers_dirty = 0;
220 }
221
222 if (config.dirty_raw) {
223 LOG_DEBUG(Audio_DSP, "source_id=%zu remaining_dirty=%x", source_id, config.dirty_raw);
224 }
225
226 config.dirty_raw = 0;
227}
228
229void Source::GenerateFrame() {
230 current_frame.fill({});
231
232 if (state.current_buffer.empty() && !DequeueBuffer()) {
233 state.enabled = false;
234 state.buffer_update = true;
235 state.current_buffer_id = 0;
236 return;
237 }
238
239 size_t frame_position = 0;
240
241 state.current_sample_number = state.next_sample_number;
242 while (frame_position < current_frame.size()) {
243 if (state.current_buffer.empty() && !DequeueBuffer()) {
244 break;
245 }
246
247 switch (state.interpolation_mode) {
248 case InterpolationMode::None:
249 AudioInterp::None(state.interp_state, state.current_buffer, state.rate_multiplier,
250 current_frame, frame_position);
251 break;
252 case InterpolationMode::Linear:
253 AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier,
254 current_frame, frame_position);
255 break;
256 case InterpolationMode::Polyphase:
257 // TODO(merry): Implement polyphase interpolation
258 LOG_DEBUG(Audio_DSP, "Polyphase interpolation unimplemented; falling back to linear");
259 AudioInterp::Linear(state.interp_state, state.current_buffer, state.rate_multiplier,
260 current_frame, frame_position);
261 break;
262 default:
263 UNIMPLEMENTED();
264 break;
265 }
266 }
267 state.next_sample_number += static_cast<u32>(frame_position);
268
269 state.filters.ProcessFrame(current_frame);
270}
271
272bool Source::DequeueBuffer() {
273 ASSERT_MSG(state.current_buffer.empty(),
274 "Shouldn't dequeue; we still have data in current_buffer");
275
276 if (state.input_queue.empty())
277 return false;
278
279 Buffer buf = state.input_queue.top();
280
281 // if we're in a loop, the current sound keeps playing afterwards, so leave the queue alone
282 if (!buf.is_looping) {
283 state.input_queue.pop();
284 }
285
286 if (buf.adpcm_dirty) {
287 state.adpcm_state.yn1 = buf.adpcm_yn[0];
288 state.adpcm_state.yn2 = buf.adpcm_yn[1];
289 }
290
291 const u8* const memory = Memory::GetPhysicalPointer(buf.physical_address);
292 if (memory) {
293 const unsigned num_channels = buf.mono_or_stereo == MonoOrStereo::Stereo ? 2 : 1;
294 switch (buf.format) {
295 case Format::PCM8:
296 state.current_buffer = Codec::DecodePCM8(num_channels, memory, buf.length);
297 break;
298 case Format::PCM16:
299 state.current_buffer = Codec::DecodePCM16(num_channels, memory, buf.length);
300 break;
301 case Format::ADPCM:
302 DEBUG_ASSERT(num_channels == 1);
303 state.current_buffer =
304 Codec::DecodeADPCM(memory, buf.length, state.adpcm_coeffs, state.adpcm_state);
305 break;
306 default:
307 UNIMPLEMENTED();
308 break;
309 }
310 } else {
311 LOG_WARNING(Audio_DSP,
312 "source_id=%zu buffer_id=%hu length=%u: Invalid physical address 0x%08X",
313 source_id, buf.buffer_id, buf.length, buf.physical_address);
314 state.current_buffer.clear();
315 return true;
316 }
317
318 // the first playthrough starts at play_position, loops start at the beginning of the buffer
319 state.current_sample_number = (!buf.has_played) ? buf.play_position : 0;
320 state.next_sample_number = state.current_sample_number;
321 state.current_buffer_id = buf.buffer_id;
322 state.buffer_update = buf.from_queue && !buf.has_played;
323
324 buf.has_played = true;
325
326 LOG_TRACE(Audio_DSP, "source_id=%zu buffer_id=%hu from_queue=%s current_buffer.size()=%zu",
327 source_id, buf.buffer_id, buf.from_queue ? "true" : "false",
328 state.current_buffer.size());
329 return true;
330}
331
332SourceStatus::Status Source::GetCurrentStatus() {
333 SourceStatus::Status ret;
334
335 // Applications depend on the correct emulation of
336 // current_buffer_id_dirty and current_buffer_id to synchronise
337 // audio with video.
338 ret.is_enabled = state.enabled;
339 ret.current_buffer_id_dirty = state.buffer_update ? 1 : 0;
340 state.buffer_update = false;
341 ret.current_buffer_id = state.current_buffer_id;
342 ret.buffer_position = state.current_sample_number;
343 ret.sync = state.sync;
344
345 return ret;
346}
347
348} // namespace HLE
349} // namespace DSP
diff --git a/src/audio_core/hle/source.h b/src/audio_core/hle/source.h
deleted file mode 100644
index c4d2debc2..000000000
--- a/src/audio_core/hle/source.h
+++ /dev/null
@@ -1,149 +0,0 @@
1// Copyright 2016 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <array>
8#include <queue>
9#include <vector>
10#include "audio_core/codec.h"
11#include "audio_core/hle/common.h"
12#include "audio_core/hle/dsp.h"
13#include "audio_core/hle/filter.h"
14#include "audio_core/interpolate.h"
15#include "common/common_types.h"
16
17namespace DSP {
18namespace HLE {
19
20/**
21 * This module performs:
22 * - Buffer management
23 * - Decoding of buffers
24 * - Buffer resampling and interpolation
25 * - Per-source filtering (SimpleFilter, BiquadFilter)
26 * - Per-source gain
27 * - Other per-source processing
28 */
29class Source final {
30public:
31 explicit Source(size_t source_id_) : source_id(source_id_) {
32 Reset();
33 }
34
35 /// Resets internal state.
36 void Reset();
37
38 /**
39 * This is called once every audio frame. This performs per-source processing every frame.
40 * @param config The new configuration we've got for this Source from the application.
41 * @param adpcm_coeffs ADPCM coefficients to use if config tells us to use them (may contain
42 * invalid values otherwise).
43 * @return The current status of this Source. This is given back to the emulated application via
44 * SharedMemory.
45 */
46 SourceStatus::Status Tick(SourceConfiguration::Configuration& config,
47 const s16_le (&adpcm_coeffs)[16]);
48
49 /**
50 * Mix this source's output into dest, using the gains for the `intermediate_mix_id`-th
51 * intermediate mixer.
52 * @param dest The QuadFrame32 to mix into.
53 * @param intermediate_mix_id The id of the intermediate mix whose gains we are using.
54 */
55 void MixInto(QuadFrame32& dest, size_t intermediate_mix_id) const;
56
57private:
58 const size_t source_id;
59 StereoFrame16 current_frame;
60
61 using Format = SourceConfiguration::Configuration::Format;
62 using InterpolationMode = SourceConfiguration::Configuration::InterpolationMode;
63 using MonoOrStereo = SourceConfiguration::Configuration::MonoOrStereo;
64
65 /// Internal representation of a buffer for our buffer queue
66 struct Buffer {
67 PAddr physical_address;
68 u32 length;
69 u8 adpcm_ps;
70 std::array<u16, 2> adpcm_yn;
71 bool adpcm_dirty;
72 bool is_looping;
73 u16 buffer_id;
74
75 MonoOrStereo mono_or_stereo;
76 Format format;
77
78 bool from_queue;
79 u32_dsp play_position; // = 0;
80 bool has_played; // = false;
81 };
82
83 struct BufferOrder {
84 bool operator()(const Buffer& a, const Buffer& b) const {
85 // Lower buffer_id comes first.
86 return a.buffer_id > b.buffer_id;
87 }
88 };
89
90 struct {
91
92 // State variables
93
94 bool enabled = false;
95 u16 sync = 0;
96
97 // Mixing
98
99 std::array<std::array<float, 4>, 3> gain = {};
100
101 // Buffer queue
102
103 std::priority_queue<Buffer, std::vector<Buffer>, BufferOrder> input_queue;
104 MonoOrStereo mono_or_stereo = MonoOrStereo::Mono;
105 Format format = Format::ADPCM;
106
107 // Current buffer
108
109 u32 current_sample_number = 0;
110 u32 next_sample_number = 0;
111 AudioInterp::StereoBuffer16 current_buffer;
112
113 // buffer_id state
114
115 bool buffer_update = false;
116 u32 current_buffer_id = 0;
117
118 // Decoding state
119
120 std::array<s16, 16> adpcm_coeffs = {};
121 Codec::ADPCMState adpcm_state = {};
122
123 // Resampling state
124
125 float rate_multiplier = 1.0;
126 InterpolationMode interpolation_mode = InterpolationMode::Polyphase;
127 AudioInterp::State interp_state = {};
128
129 // Filter state
130
131 SourceFilters filters;
132
133 } state;
134
135 // Internal functions
136
137 /// INTERNAL: Update our internal state based on the current config.
138 void ParseConfig(SourceConfiguration::Configuration& config, const s16_le (&adpcm_coeffs)[16]);
139 /// INTERNAL: Generate the current audio output for this frame based on our internal state.
140 void GenerateFrame();
141 /// INTERNAL: Dequeues a buffer and does preprocessing on it (decoding, resampling). Puts it
142 /// into current_buffer.
143 bool DequeueBuffer();
144 /// INTERNAL: Generates a SourceStatus::Status based on our internal state.
145 SourceStatus::Status GetCurrentStatus();
146};
147
148} // namespace HLE
149} // namespace DSP