summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar MerryMage2016-03-24 00:12:54 +0000
committerGravatar MerryMage2016-03-24 03:46:59 +0000
commitefd1c3f8c3f649b0fa3fec0b236e9a748fc34e98 (patch)
tree566d3fda83b4cb2272b1bf8113ac13e130b4fdcb
parentMerge pull request #1302 from Subv/save_fix (diff)
downloadyuzu-efd1c3f8c3f649b0fa3fec0b236e9a748fc34e98.tar.gz
yuzu-efd1c3f8c3f649b0fa3fec0b236e9a748fc34e98.tar.xz
yuzu-efd1c3f8c3f649b0fa3fec0b236e9a748fc34e98.zip
DSP: Implement audio codecs (PCM8, PCM16, ADPCM)
Diffstat (limited to '')
-rw-r--r--src/audio_core/CMakeLists.txt2
-rw-r--r--src/audio_core/codec.cpp122
-rw-r--r--src/audio_core/codec.h50
3 files changed, 174 insertions, 0 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt
index b0d1c7eb6..c4bad8cb0 100644
--- a/src/audio_core/CMakeLists.txt
+++ b/src/audio_core/CMakeLists.txt
@@ -1,11 +1,13 @@
1set(SRCS 1set(SRCS
2 audio_core.cpp 2 audio_core.cpp
3 codec.cpp
3 hle/dsp.cpp 4 hle/dsp.cpp
4 hle/pipe.cpp 5 hle/pipe.cpp
5 ) 6 )
6 7
7set(HEADERS 8set(HEADERS
8 audio_core.h 9 audio_core.h
10 codec.h
9 hle/dsp.h 11 hle/dsp.h
10 hle/pipe.h 12 hle/pipe.h
11 sink.h 13 sink.h
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp
new file mode 100644
index 000000000..ab65514b7
--- /dev/null
+++ b/src/audio_core/codec.cpp
@@ -0,0 +1,122 @@
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
10#include "audio_core/codec.h"
11
12#include "common/assert.h"
13#include "common/common_types.h"
14#include "common/math_util.h"
15
16namespace Codec {
17
18StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) {
19 // GC-ADPCM with scale factor and variable coefficients.
20 // Frames are 8 bytes long containing 14 samples each.
21 // Samples are 4 bits (one nibble) long.
22
23 constexpr size_t FRAME_LEN = 8;
24 constexpr size_t SAMPLES_PER_FRAME = 14;
25 constexpr std::array<int, 16> SIGNED_NIBBLES {{ 0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1 }};
26
27 const size_t ret_size = sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two.
28 StereoBuffer16 ret(ret_size);
29
30 int yn1 = state.yn1,
31 yn2 = state.yn2;
32
33 const size_t NUM_FRAMES = (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up.
34 for (size_t framei = 0; framei < NUM_FRAMES; framei++) {
35 const int frame_header = data[framei * FRAME_LEN];
36 const int scale = 1 << (frame_header & 0xF);
37 const int idx = (frame_header >> 4) & 0x7;
38
39 // Coefficients are fixed point with 11 bits fractional part.
40 const int coef1 = adpcm_coeff[idx * 2 + 0];
41 const int coef2 = adpcm_coeff[idx * 2 + 1];
42
43 // Decodes an audio sample. One nibble produces one sample.
44 const auto decode_sample = [&](const int nibble) -> s16 {
45 const int xn = nibble * scale;
46 // We first transform everything into 11 bit fixed point, perform the second order digital filter, then transform back.
47 // 0x400 == 0.5 in 11 bit fixed point.
48 // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2]
49 int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11;
50 // Clamp to output range.
51 val = MathUtil::Clamp(val, -32768, 32767);
52 // Advance output feedback.
53 yn2 = yn1;
54 yn1 = val;
55 return (s16)val;
56 };
57
58 size_t outputi = framei * SAMPLES_PER_FRAME;
59 size_t datai = framei * FRAME_LEN + 1;
60 for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) {
61 const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]);
62 ret[outputi].fill(sample1);
63 outputi++;
64
65 const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]);
66 ret[outputi].fill(sample2);
67 outputi++;
68
69 datai++;
70 }
71 }
72
73 state.yn1 = yn1;
74 state.yn2 = yn2;
75
76 return ret;
77}
78
79static s16 SignExtendS8(u8 x) {
80 // The data is actually signed PCM8.
81 // We sign extend this to signed PCM16.
82 return static_cast<s16>(static_cast<s8>(x));
83}
84
85StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, const size_t sample_count) {
86 ASSERT(num_channels == 1 || num_channels == 2);
87
88 StereoBuffer16 ret(sample_count);
89
90 if (num_channels == 1) {
91 for (size_t i = 0; i < sample_count; i++) {
92 ret[i].fill(SignExtendS8(data[i]));
93 }
94 } else {
95 for (size_t i = 0; i < sample_count; i++) {
96 ret[i][0] = SignExtendS8(data[i * 2 + 0]);
97 ret[i][1] = SignExtendS8(data[i * 2 + 1]);
98 }
99 }
100
101 return ret;
102}
103
104StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, const size_t sample_count) {
105 ASSERT(num_channels == 1 || num_channels == 2);
106
107 StereoBuffer16 ret(sample_count);
108
109 if (num_channels == 1) {
110 for (size_t i = 0; i < sample_count; i++) {
111 s16 sample;
112 std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16));
113 ret[i].fill(sample);
114 }
115 } else {
116 std::memcpy(ret.data(), data, sample_count * 2 * sizeof(u16));
117 }
118
119 return ret;
120}
121
122};
diff --git a/src/audio_core/codec.h b/src/audio_core/codec.h
new file mode 100644
index 000000000..e695f2edc
--- /dev/null
+++ b/src/audio_core/codec.h
@@ -0,0 +1,50 @@
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 <vector>
9
10#include "common/common_types.h"
11
12namespace Codec {
13
14/// A variable length buffer of signed PCM16 stereo samples.
15using StereoBuffer16 = std::vector<std::array<s16, 2>>;
16
17/// See: Codec::DecodeADPCM
18struct ADPCMState {
19 // Two historical samples from previous processed buffer,
20 // required for ADPCM decoding
21 s16 yn1; ///< y[n-1]
22 s16 yn2; ///< y[n-2]
23};
24
25/**
26 * @param data Pointer to buffer that contains ADPCM data to decode
27 * @param sample_count Length of buffer in terms of number of samples
28 * @param adpcm_coeff ADPCM coefficients
29 * @param state ADPCM state, this is updated with new state
30 * @return Decoded stereo signed PCM16 data, sample_count in length
31 */
32StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count, const std::array<s16, 16>& adpcm_coeff, ADPCMState& state);
33
34/**
35 * @param num_channels Number of channels
36 * @param data Pointer to buffer that contains PCM8 data to decode
37 * @param sample_count Length of buffer in terms of number of samples
38 * @return Decoded stereo signed PCM16 data, sample_count in length
39 */
40StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data, const size_t sample_count);
41
42/**
43 * @param num_channels Number of channels
44 * @param data Pointer to buffer that contains PCM16 data to decode
45 * @param sample_count Length of buffer in terms of number of samples
46 * @return Decoded stereo signed PCM16 data, sample_count in length
47 */
48StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data, const size_t sample_count);
49
50};