summaryrefslogtreecommitdiff
path: root/src/audio_core/codec.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/codec.cpp')
-rw-r--r--src/audio_core/codec.cpp127
1 files changed, 0 insertions, 127 deletions
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp
deleted file mode 100644
index 6fba9fdae..000000000
--- a/src/audio_core/codec.cpp
+++ /dev/null
@@ -1,127 +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 <cstring>
8#include <vector>
9#include "audio_core/codec.h"
10#include "common/assert.h"
11#include "common/common_types.h"
12#include "common/math_util.h"
13
14namespace Codec {
15
16StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count,
17 const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) {
18 // GC-ADPCM with scale factor and variable coefficients.
19 // Frames are 8 bytes long containing 14 samples each.
20 // Samples are 4 bits (one nibble) long.
21
22 constexpr size_t FRAME_LEN = 8;
23 constexpr size_t SAMPLES_PER_FRAME = 14;
24 constexpr std::array<int, 16> SIGNED_NIBBLES = {
25 {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}};
26
27 const size_t ret_size =
28 sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two.
29 StereoBuffer16 ret(ret_size);
30
31 int yn1 = state.yn1, yn2 = state.yn2;
32
33 const size_t NUM_FRAMES =
34 (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up.
35 for (size_t framei = 0; framei < NUM_FRAMES; framei++) {
36 const int frame_header = data[framei * FRAME_LEN];
37 const int scale = 1 << (frame_header & 0xF);
38 const int idx = (frame_header >> 4) & 0x7;
39
40 // Coefficients are fixed point with 11 bits fractional part.
41 const int coef1 = adpcm_coeff[idx * 2 + 0];
42 const int coef2 = adpcm_coeff[idx * 2 + 1];
43
44 // Decodes an audio sample. One nibble produces one sample.
45 const auto decode_sample = [&](const int nibble) -> s16 {
46 const int xn = nibble * scale;
47 // We first transform everything into 11 bit fixed point, perform the second order
48 // digital filter, then transform back.
49 // 0x400 == 0.5 in 11 bit fixed point.
50 // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2]
51 int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11;
52 // Clamp to output range.
53 val = MathUtil::Clamp(val, -32768, 32767);
54 // Advance output feedback.
55 yn2 = yn1;
56 yn1 = val;
57 return (s16)val;
58 };
59
60 size_t outputi = framei * SAMPLES_PER_FRAME;
61 size_t datai = framei * FRAME_LEN + 1;
62 for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) {
63 const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]);
64 ret[outputi].fill(sample1);
65 outputi++;
66
67 const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]);
68 ret[outputi].fill(sample2);
69 outputi++;
70
71 datai++;
72 }
73 }
74
75 state.yn1 = yn1;
76 state.yn2 = yn2;
77
78 return ret;
79}
80
81static s16 SignExtendS8(u8 x) {
82 // The data is actually signed PCM8.
83 // We sign extend this to signed PCM16.
84 return static_cast<s16>(static_cast<s8>(x));
85}
86
87StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data,
88 const size_t sample_count) {
89 ASSERT(num_channels == 1 || num_channels == 2);
90
91 StereoBuffer16 ret(sample_count);
92
93 if (num_channels == 1) {
94 for (size_t i = 0; i < sample_count; i++) {
95 ret[i].fill(SignExtendS8(data[i]));
96 }
97 } else {
98 for (size_t i = 0; i < sample_count; i++) {
99 ret[i][0] = SignExtendS8(data[i * 2 + 0]);
100 ret[i][1] = SignExtendS8(data[i * 2 + 1]);
101 }
102 }
103
104 return ret;
105}
106
107StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data,
108 const size_t sample_count) {
109 ASSERT(num_channels == 1 || num_channels == 2);
110
111 StereoBuffer16 ret(sample_count);
112
113 if (num_channels == 1) {
114 for (size_t i = 0; i < sample_count; i++) {
115 s16 sample;
116 std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16));
117 ret[i].fill(sample);
118 }
119 } else {
120 for (size_t i = 0; i < sample_count; ++i) {
121 std::memcpy(&ret[i], data + i * sizeof(s16) * 2, 2 * sizeof(s16));
122 }
123 }
124
125 return ret;
126}
127};