2016-03-24 00:12:54 +00:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-26 12:23:25 +01:00
|
|
|
#include <algorithm>
|
2016-03-24 00:12:54 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
2017-12-20 18:44:32 +00:00
|
|
|
#include "audio_core/audio_types.h"
|
2016-09-21 07:52:38 +01:00
|
|
|
#include "audio_core/codec.h"
|
2016-03-24 00:12:54 +00:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/common_types.h"
|
|
|
|
|
2019-02-18 04:41:48 +00:00
|
|
|
namespace AudioCore::Codec {
|
2016-03-24 00:12:54 +00:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
StereoBuffer16 DecodeADPCM(const u8* const data, const std::size_t sample_count,
|
2016-09-18 01:38:01 +01:00
|
|
|
const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) {
|
2016-03-24 00:12:54 +00:00
|
|
|
// GC-ADPCM with scale factor and variable coefficients.
|
|
|
|
// Frames are 8 bytes long containing 14 samples each.
|
|
|
|
// Samples are 4 bits (one nibble) long.
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
constexpr std::size_t FRAME_LEN = 8;
|
|
|
|
constexpr std::size_t SAMPLES_PER_FRAME = 14;
|
2016-09-19 02:01:46 +01:00
|
|
|
constexpr std::array<int, 16> SIGNED_NIBBLES = {
|
2016-09-18 01:38:01 +01:00
|
|
|
{0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}};
|
2016-03-24 00:12:54 +00:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
const std::size_t ret_size =
|
2016-09-18 01:38:01 +01:00
|
|
|
sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two.
|
2016-03-24 00:12:54 +00:00
|
|
|
StereoBuffer16 ret(ret_size);
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
int yn1 = state.yn1, yn2 = state.yn2;
|
2016-03-24 00:12:54 +00:00
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
const std::size_t NUM_FRAMES =
|
2016-09-18 01:38:01 +01:00
|
|
|
(sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up.
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t framei = 0; framei < NUM_FRAMES; framei++) {
|
2016-03-24 00:12:54 +00:00
|
|
|
const int frame_header = data[framei * FRAME_LEN];
|
|
|
|
const int scale = 1 << (frame_header & 0xF);
|
|
|
|
const int idx = (frame_header >> 4) & 0x7;
|
|
|
|
|
|
|
|
// Coefficients are fixed point with 11 bits fractional part.
|
|
|
|
const int coef1 = adpcm_coeff[idx * 2 + 0];
|
|
|
|
const int coef2 = adpcm_coeff[idx * 2 + 1];
|
|
|
|
|
|
|
|
// Decodes an audio sample. One nibble produces one sample.
|
|
|
|
const auto decode_sample = [&](const int nibble) -> s16 {
|
|
|
|
const int xn = nibble * scale;
|
2016-09-18 01:38:01 +01:00
|
|
|
// We first transform everything into 11 bit fixed point, perform the second order
|
|
|
|
// digital filter, then transform back.
|
2016-03-24 00:12:54 +00:00
|
|
|
// 0x400 == 0.5 in 11 bit fixed point.
|
|
|
|
// Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2]
|
|
|
|
int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11;
|
|
|
|
// Clamp to output range.
|
2018-07-24 18:08:17 +01:00
|
|
|
val = std::clamp(val, -32768, 32767);
|
2016-03-24 00:12:54 +00:00
|
|
|
// Advance output feedback.
|
|
|
|
yn2 = yn1;
|
|
|
|
yn1 = val;
|
|
|
|
return (s16)val;
|
|
|
|
};
|
|
|
|
|
2018-09-06 21:03:28 +01:00
|
|
|
std::size_t outputi = framei * SAMPLES_PER_FRAME;
|
|
|
|
std::size_t datai = framei * FRAME_LEN + 1;
|
|
|
|
for (std::size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) {
|
2016-09-03 14:01:31 +01:00
|
|
|
const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]);
|
2016-03-24 00:12:54 +00:00
|
|
|
ret[outputi].fill(sample1);
|
|
|
|
outputi++;
|
|
|
|
|
2016-09-03 14:01:31 +01:00
|
|
|
const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]);
|
2016-03-24 00:12:54 +00:00
|
|
|
ret[outputi].fill(sample2);
|
|
|
|
outputi++;
|
|
|
|
|
|
|
|
datai++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.yn1 = yn1;
|
|
|
|
state.yn2 = yn2;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data,
|
2018-09-06 21:03:28 +01:00
|
|
|
const std::size_t sample_count) {
|
2016-03-24 00:12:54 +00:00
|
|
|
ASSERT(num_channels == 1 || num_channels == 2);
|
|
|
|
|
2018-02-25 12:42:15 +00:00
|
|
|
const auto decode_sample = [](u8 sample) {
|
|
|
|
return static_cast<s16>(static_cast<u16>(sample) << 8);
|
|
|
|
};
|
|
|
|
|
2016-03-24 00:12:54 +00:00
|
|
|
StereoBuffer16 ret(sample_count);
|
|
|
|
|
|
|
|
if (num_channels == 1) {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < sample_count; i++) {
|
2018-02-25 12:42:15 +00:00
|
|
|
ret[i].fill(decode_sample(data[i]));
|
2016-03-24 00:12:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < sample_count; i++) {
|
2018-02-25 12:42:15 +00:00
|
|
|
ret[i][0] = decode_sample(data[i * 2 + 0]);
|
|
|
|
ret[i][1] = decode_sample(data[i * 2 + 1]);
|
2016-03-24 00:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data,
|
2018-09-06 21:03:28 +01:00
|
|
|
const std::size_t sample_count) {
|
2016-03-24 00:12:54 +00:00
|
|
|
ASSERT(num_channels == 1 || num_channels == 2);
|
|
|
|
|
|
|
|
StereoBuffer16 ret(sample_count);
|
|
|
|
|
|
|
|
if (num_channels == 1) {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < sample_count; i++) {
|
2016-03-24 00:12:54 +00:00
|
|
|
s16 sample;
|
|
|
|
std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16));
|
|
|
|
ret[i].fill(sample);
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-06 21:03:28 +01:00
|
|
|
for (std::size_t i = 0; i < sample_count; ++i) {
|
2017-09-25 19:06:42 +01:00
|
|
|
std::memcpy(&ret[i], data + i * sizeof(s16) * 2, 2 * sizeof(s16));
|
|
|
|
}
|
2016-03-24 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-18 04:41:48 +00:00
|
|
|
} // namespace AudioCore::Codec
|