diff options
| author | 2018-08-03 15:30:01 -0400 | |
|---|---|---|
| committer | 2018-08-04 18:22:58 -0400 | |
| commit | f1cb3903ac358183dcdc562ba19dc469b056e73f (patch) | |
| tree | 0179ee55d3c573cdd9189cb708e2b69e02a1b166 /src/audio_core/codec.cpp | |
| parent | cubeb_sink: Support variable sample_rate and num_channels. (diff) | |
| download | yuzu-f1cb3903ac358183dcdc562ba19dc469b056e73f.tar.gz yuzu-f1cb3903ac358183dcdc562ba19dc469b056e73f.tar.xz yuzu-f1cb3903ac358183dcdc562ba19dc469b056e73f.zip | |
audio_core: Port codec code from Citra for ADPCM decoding.
Diffstat (limited to 'src/audio_core/codec.cpp')
| -rw-r--r-- | src/audio_core/codec.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/audio_core/codec.cpp b/src/audio_core/codec.cpp new file mode 100644 index 000000000..c3021403f --- /dev/null +++ b/src/audio_core/codec.cpp | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // Copyright 2018 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | #include "audio_core/codec.h" | ||
| 8 | |||
| 9 | namespace AudioCore::Codec { | ||
| 10 | |||
| 11 | std::vector<s16> DecodeADPCM(const u8* const data, size_t size, const ADPCM_Coeff& coeff, | ||
| 12 | ADPCMState& state) { | ||
| 13 | // GC-ADPCM with scale factor and variable coefficients. | ||
| 14 | // Frames are 8 bytes long containing 14 samples each. | ||
| 15 | // Samples are 4 bits (one nibble) long. | ||
| 16 | |||
| 17 | constexpr size_t FRAME_LEN = 8; | ||
| 18 | constexpr size_t SAMPLES_PER_FRAME = 14; | ||
| 19 | constexpr std::array<int, 16> SIGNED_NIBBLES = { | ||
| 20 | {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}}; | ||
| 21 | |||
| 22 | const size_t sample_count = (size / FRAME_LEN) * SAMPLES_PER_FRAME; | ||
| 23 | const size_t ret_size = | ||
| 24 | sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two. | ||
| 25 | std::vector<s16> ret(ret_size); | ||
| 26 | |||
| 27 | int yn1 = state.yn1, yn2 = state.yn2; | ||
| 28 | |||
| 29 | const size_t NUM_FRAMES = | ||
| 30 | (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up. | ||
| 31 | for (size_t framei = 0; framei < NUM_FRAMES; framei++) { | ||
| 32 | const int frame_header = data[framei * FRAME_LEN]; | ||
| 33 | const int scale = 1 << (frame_header & 0xF); | ||
| 34 | const int idx = (frame_header >> 4) & 0x7; | ||
| 35 | |||
| 36 | // Coefficients are fixed point with 11 bits fractional part. | ||
| 37 | const int coef1 = coeff[idx * 2 + 0]; | ||
| 38 | const int coef2 = coeff[idx * 2 + 1]; | ||
| 39 | |||
| 40 | // Decodes an audio sample. One nibble produces one sample. | ||
| 41 | const auto decode_sample = [&](const int nibble) -> s16 { | ||
| 42 | const int xn = nibble * scale; | ||
| 43 | // We first transform everything into 11 bit fixed point, perform the second order | ||
| 44 | // digital filter, then transform back. | ||
| 45 | // 0x400 == 0.5 in 11 bit fixed point. | ||
| 46 | // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2] | ||
| 47 | int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11; | ||
| 48 | // Clamp to output range. | ||
| 49 | val = std::clamp<s32>(val, -32768, 32767); | ||
| 50 | // Advance output feedback. | ||
| 51 | yn2 = yn1; | ||
| 52 | yn1 = val; | ||
| 53 | return static_cast<s16>(val); | ||
| 54 | }; | ||
| 55 | |||
| 56 | size_t outputi = framei * SAMPLES_PER_FRAME; | ||
| 57 | size_t datai = framei * FRAME_LEN + 1; | ||
| 58 | for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) { | ||
| 59 | const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]); | ||
| 60 | ret[outputi] = sample1; | ||
| 61 | outputi++; | ||
| 62 | |||
| 63 | const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]); | ||
| 64 | ret[outputi] = sample2; | ||
| 65 | outputi++; | ||
| 66 | |||
| 67 | datai++; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | state.yn1 = yn1; | ||
| 72 | state.yn2 = yn2; | ||
| 73 | |||
| 74 | return ret; | ||
| 75 | } | ||
| 76 | |||
| 77 | } // namespace AudioCore::Codec | ||