diff options
Diffstat (limited to '')
| -rw-r--r-- | src/audio_core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/audio_core/interpolate.cpp | 85 | ||||
| -rw-r--r-- | src/audio_core/interpolate.h | 41 |
3 files changed, 128 insertions, 0 deletions
diff --git a/src/audio_core/CMakeLists.txt b/src/audio_core/CMakeLists.txt index c08ce69e0..a965af291 100644 --- a/src/audio_core/CMakeLists.txt +++ b/src/audio_core/CMakeLists.txt | |||
| @@ -4,6 +4,7 @@ set(SRCS | |||
| 4 | hle/dsp.cpp | 4 | hle/dsp.cpp |
| 5 | hle/filter.cpp | 5 | hle/filter.cpp |
| 6 | hle/pipe.cpp | 6 | hle/pipe.cpp |
| 7 | interpolate.cpp | ||
| 7 | ) | 8 | ) |
| 8 | 9 | ||
| 9 | set(HEADERS | 10 | set(HEADERS |
| @@ -13,6 +14,7 @@ set(HEADERS | |||
| 13 | hle/dsp.h | 14 | hle/dsp.h |
| 14 | hle/filter.h | 15 | hle/filter.h |
| 15 | hle/pipe.h | 16 | hle/pipe.h |
| 17 | interpolate.h | ||
| 16 | sink.h | 18 | sink.h |
| 17 | ) | 19 | ) |
| 18 | 20 | ||
diff --git a/src/audio_core/interpolate.cpp b/src/audio_core/interpolate.cpp new file mode 100644 index 000000000..fcd3aa066 --- /dev/null +++ b/src/audio_core/interpolate.cpp | |||
| @@ -0,0 +1,85 @@ | |||
| 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 "audio_core/interpolate.h" | ||
| 6 | |||
| 7 | #include "common/assert.h" | ||
| 8 | #include "common/math_util.h" | ||
| 9 | |||
| 10 | namespace AudioInterp { | ||
| 11 | |||
| 12 | // Calculations are done in fixed point with 24 fractional bits. | ||
| 13 | // (This is not verified. This was chosen for minimal error.) | ||
| 14 | constexpr u64 scale_factor = 1 << 24; | ||
| 15 | constexpr u64 scale_mask = scale_factor - 1; | ||
| 16 | |||
| 17 | /// Here we step over the input in steps of rate_multiplier, until we consume all of the input. | ||
| 18 | /// Three adjacent samples are passed to fn each step. | ||
| 19 | template <typename Function> | ||
| 20 | static StereoBuffer16 StepOverSamples(State& state, const StereoBuffer16& input, float rate_multiplier, Function fn) { | ||
| 21 | ASSERT(rate_multiplier > 0); | ||
| 22 | |||
| 23 | if (input.size() < 2) | ||
| 24 | return {}; | ||
| 25 | |||
| 26 | StereoBuffer16 output; | ||
| 27 | output.reserve(static_cast<size_t>(input.size() / rate_multiplier)); | ||
| 28 | |||
| 29 | u64 step_size = static_cast<u64>(rate_multiplier * scale_factor); | ||
| 30 | |||
| 31 | u64 fposition = 0; | ||
| 32 | const u64 max_fposition = input.size() * scale_factor; | ||
| 33 | |||
| 34 | while (fposition < 1 * scale_factor) { | ||
| 35 | u64 fraction = fposition & scale_mask; | ||
| 36 | |||
| 37 | output.push_back(fn(fraction, state.xn2, state.xn1, input[0])); | ||
| 38 | |||
| 39 | fposition += step_size; | ||
| 40 | } | ||
| 41 | |||
| 42 | while (fposition < 2 * scale_factor) { | ||
| 43 | u64 fraction = fposition & scale_mask; | ||
| 44 | |||
| 45 | output.push_back(fn(fraction, state.xn1, input[0], input[1])); | ||
| 46 | |||
| 47 | fposition += step_size; | ||
| 48 | } | ||
| 49 | |||
| 50 | while (fposition < max_fposition) { | ||
| 51 | u64 fraction = fposition & scale_mask; | ||
| 52 | |||
| 53 | size_t index = static_cast<size_t>(fposition / scale_factor); | ||
| 54 | output.push_back(fn(fraction, input[index - 2], input[index - 1], input[index])); | ||
| 55 | |||
| 56 | fposition += step_size; | ||
| 57 | } | ||
| 58 | |||
| 59 | state.xn2 = input[input.size() - 2]; | ||
| 60 | state.xn1 = input[input.size() - 1]; | ||
| 61 | |||
| 62 | return output; | ||
| 63 | } | ||
| 64 | |||
| 65 | StereoBuffer16 None(State& state, const StereoBuffer16& input, float rate_multiplier) { | ||
| 66 | return StepOverSamples(state, input, rate_multiplier, [](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { | ||
| 67 | return x0; | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | |||
| 71 | StereoBuffer16 Linear(State& state, const StereoBuffer16& input, float rate_multiplier) { | ||
| 72 | // Note on accuracy: Some values that this produces are +/- 1 from the actual firmware. | ||
| 73 | return StepOverSamples(state, input, rate_multiplier, [](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { | ||
| 74 | // This is a saturated subtraction. (Verified by black-box fuzzing.) | ||
| 75 | s64 delta0 = MathUtil::Clamp<s64>(x1[0] - x0[0], -32768, 32767); | ||
| 76 | s64 delta1 = MathUtil::Clamp<s64>(x1[1] - x0[1], -32768, 32767); | ||
| 77 | |||
| 78 | return std::array<s16, 2> { | ||
| 79 | static_cast<s16>(x0[0] + fraction * delta0 / scale_factor), | ||
| 80 | static_cast<s16>(x0[1] + fraction * delta1 / scale_factor) | ||
| 81 | }; | ||
| 82 | }); | ||
| 83 | } | ||
| 84 | |||
| 85 | } // namespace AudioInterp | ||
diff --git a/src/audio_core/interpolate.h b/src/audio_core/interpolate.h new file mode 100644 index 000000000..a4c0a453d --- /dev/null +++ b/src/audio_core/interpolate.h | |||
| @@ -0,0 +1,41 @@ | |||
| 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 | |||
| 12 | namespace AudioInterp { | ||
| 13 | |||
| 14 | /// A variable length buffer of signed PCM16 stereo samples. | ||
| 15 | using StereoBuffer16 = std::vector<std::array<s16, 2>>; | ||
| 16 | |||
| 17 | struct State { | ||
| 18 | // Two historical samples. | ||
| 19 | std::array<s16, 2> xn1 = {}; ///< x[n-1] | ||
| 20 | std::array<s16, 2> xn2 = {}; ///< x[n-2] | ||
| 21 | }; | ||
| 22 | |||
| 23 | /** | ||
| 24 | * No interpolation. This is equivalent to a zero-order hold. There is a two-sample predelay. | ||
| 25 | * @param input Input buffer. | ||
| 26 | * @param rate_multiplier Stretch factor. Must be a positive non-zero value. | ||
| 27 | * rate_multiplier > 1.0 performs decimation and rate_multipler < 1.0 performs upsampling. | ||
| 28 | * @return The resampled audio buffer. | ||
| 29 | */ | ||
| 30 | StereoBuffer16 None(State& state, const StereoBuffer16& input, float rate_multiplier); | ||
| 31 | |||
| 32 | /** | ||
| 33 | * Linear interpolation. This is equivalent to a first-order hold. There is a two-sample predelay. | ||
| 34 | * @param input Input buffer. | ||
| 35 | * @param rate_multiplier Stretch factor. Must be a positive non-zero value. | ||
| 36 | * rate_multiplier > 1.0 performs decimation and rate_multipler < 1.0 performs upsampling. | ||
| 37 | * @return The resampled audio buffer. | ||
| 38 | */ | ||
| 39 | StereoBuffer16 Linear(State& state, const StereoBuffer16& input, float rate_multiplier); | ||
| 40 | |||
| 41 | } // namespace AudioInterp | ||