codec.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  5. #include <cstddef>
  6. #include <cstring>
  7. #include <vector>
  8. #include "audio_core/codec.h"
  9. #include "common/assert.h"
  10. #include "common/common_types.h"
  11. #include "common/math_util.h"
  12. namespace Codec {
  13. StereoBuffer16 DecodeADPCM(const u8* const data, const size_t sample_count,
  14. const std::array<s16, 16>& adpcm_coeff, ADPCMState& state) {
  15. // GC-ADPCM with scale factor and variable coefficients.
  16. // Frames are 8 bytes long containing 14 samples each.
  17. // Samples are 4 bits (one nibble) long.
  18. constexpr size_t FRAME_LEN = 8;
  19. constexpr size_t SAMPLES_PER_FRAME = 14;
  20. constexpr std::array<int, 16> SIGNED_NIBBLES = {
  21. {0, 1, 2, 3, 4, 5, 6, 7, -8, -7, -6, -5, -4, -3, -2, -1}};
  22. const size_t ret_size =
  23. sample_count % 2 == 0 ? sample_count : sample_count + 1; // Ensure multiple of two.
  24. StereoBuffer16 ret(ret_size);
  25. int yn1 = state.yn1, yn2 = state.yn2;
  26. const size_t NUM_FRAMES =
  27. (sample_count + (SAMPLES_PER_FRAME - 1)) / SAMPLES_PER_FRAME; // Round up.
  28. for (size_t framei = 0; framei < NUM_FRAMES; framei++) {
  29. const int frame_header = data[framei * FRAME_LEN];
  30. const int scale = 1 << (frame_header & 0xF);
  31. const int idx = (frame_header >> 4) & 0x7;
  32. // Coefficients are fixed point with 11 bits fractional part.
  33. const int coef1 = adpcm_coeff[idx * 2 + 0];
  34. const int coef2 = adpcm_coeff[idx * 2 + 1];
  35. // Decodes an audio sample. One nibble produces one sample.
  36. const auto decode_sample = [&](const int nibble) -> s16 {
  37. const int xn = nibble * scale;
  38. // We first transform everything into 11 bit fixed point, perform the second order
  39. // digital filter, then transform back.
  40. // 0x400 == 0.5 in 11 bit fixed point.
  41. // Filter: y[n] = x[n] + 0.5 + c1 * y[n-1] + c2 * y[n-2]
  42. int val = ((xn << 11) + 0x400 + coef1 * yn1 + coef2 * yn2) >> 11;
  43. // Clamp to output range.
  44. val = MathUtil::Clamp(val, -32768, 32767);
  45. // Advance output feedback.
  46. yn2 = yn1;
  47. yn1 = val;
  48. return (s16)val;
  49. };
  50. size_t outputi = framei * SAMPLES_PER_FRAME;
  51. size_t datai = framei * FRAME_LEN + 1;
  52. for (size_t i = 0; i < SAMPLES_PER_FRAME && outputi < sample_count; i += 2) {
  53. const s16 sample1 = decode_sample(SIGNED_NIBBLES[data[datai] >> 4]);
  54. ret[outputi].fill(sample1);
  55. outputi++;
  56. const s16 sample2 = decode_sample(SIGNED_NIBBLES[data[datai] & 0xF]);
  57. ret[outputi].fill(sample2);
  58. outputi++;
  59. datai++;
  60. }
  61. }
  62. state.yn1 = yn1;
  63. state.yn2 = yn2;
  64. return ret;
  65. }
  66. static s16 SignExtendS8(u8 x) {
  67. // The data is actually signed PCM8.
  68. // We sign extend this to signed PCM16.
  69. return static_cast<s16>(static_cast<s8>(x));
  70. }
  71. StereoBuffer16 DecodePCM8(const unsigned num_channels, const u8* const data,
  72. const size_t sample_count) {
  73. ASSERT(num_channels == 1 || num_channels == 2);
  74. StereoBuffer16 ret(sample_count);
  75. if (num_channels == 1) {
  76. for (size_t i = 0; i < sample_count; i++) {
  77. ret[i].fill(SignExtendS8(data[i]));
  78. }
  79. } else {
  80. for (size_t i = 0; i < sample_count; i++) {
  81. ret[i][0] = SignExtendS8(data[i * 2 + 0]);
  82. ret[i][1] = SignExtendS8(data[i * 2 + 1]);
  83. }
  84. }
  85. return ret;
  86. }
  87. StereoBuffer16 DecodePCM16(const unsigned num_channels, const u8* const data,
  88. const size_t sample_count) {
  89. ASSERT(num_channels == 1 || num_channels == 2);
  90. StereoBuffer16 ret(sample_count);
  91. if (num_channels == 1) {
  92. for (size_t i = 0; i < sample_count; i++) {
  93. s16 sample;
  94. std::memcpy(&sample, data + i * sizeof(s16), sizeof(s16));
  95. ret[i].fill(sample);
  96. }
  97. } else {
  98. std::memcpy(ret.data(), data, sample_count * 2 * sizeof(u16));
  99. }
  100. return ret;
  101. }
  102. };