2016-04-24 20:11:47 +00:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-26 11:23:25 +00:00
|
|
|
#include <algorithm>
|
2016-04-24 20:11:47 +00:00
|
|
|
#include "audio_core/interpolate.h"
|
|
|
|
#include "common/assert.h"
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
namespace AudioCore {
|
2016-04-24 20:11:47 +00:00
|
|
|
namespace AudioInterp {
|
|
|
|
|
|
|
|
// Calculations are done in fixed point with 24 fractional bits.
|
|
|
|
// (This is not verified. This was chosen for minimal error.)
|
|
|
|
constexpr u64 scale_factor = 1 << 24;
|
|
|
|
constexpr u64 scale_mask = scale_factor - 1;
|
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
/// Here we step over the input in steps of rate, until we consume all of the input.
|
2016-04-24 20:11:47 +00:00
|
|
|
/// Three adjacent samples are passed to fn each step.
|
|
|
|
template <typename Function>
|
2017-12-20 18:44:32 +00:00
|
|
|
static void StepOverSamples(State& state, StereoBuffer16& input, float rate, StereoFrame16& output,
|
|
|
|
size_t& outputi, Function fn) {
|
2017-08-03 11:22:51 +00:00
|
|
|
ASSERT(rate > 0);
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
if (input.empty())
|
|
|
|
return;
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
input.insert(input.begin(), {state.xn2, state.xn1});
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
const u64 step_size = static_cast<u64>(rate * scale_factor);
|
|
|
|
u64 fposition = state.fposition;
|
|
|
|
size_t inputi = 0;
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
while (outputi < output.size()) {
|
|
|
|
inputi = static_cast<size_t>(fposition / scale_factor);
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
if (inputi + 2 >= input.size()) {
|
|
|
|
inputi = input.size() - 2;
|
|
|
|
break;
|
|
|
|
}
|
2016-04-24 20:11:47 +00:00
|
|
|
|
|
|
|
u64 fraction = fposition & scale_mask;
|
2017-08-03 11:22:51 +00:00
|
|
|
output[outputi++] = fn(fraction, input[inputi], input[inputi + 1], input[inputi + 2]);
|
2016-04-24 20:11:47 +00:00
|
|
|
|
|
|
|
fposition += step_size;
|
|
|
|
}
|
|
|
|
|
2017-08-03 11:22:51 +00:00
|
|
|
state.xn2 = input[inputi];
|
|
|
|
state.xn1 = input[inputi + 1];
|
|
|
|
state.fposition = fposition - inputi * scale_factor;
|
2016-04-24 20:11:47 +00:00
|
|
|
|
2017-09-25 18:06:42 +00:00
|
|
|
input.erase(input.begin(), std::next(input.begin(), inputi + 2));
|
2016-04-24 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
void None(State& state, StereoBuffer16& input, float rate, StereoFrame16& output, size_t& outputi) {
|
2017-08-03 11:22:51 +00:00
|
|
|
StepOverSamples(
|
|
|
|
state, input, rate, output, outputi,
|
2016-09-18 00:38:01 +00:00
|
|
|
[](u64 fraction, const auto& x0, const auto& x1, const auto& x2) { return x0; });
|
2016-04-24 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 18:44:32 +00:00
|
|
|
void Linear(State& state, StereoBuffer16& input, float rate, StereoFrame16& output,
|
2017-08-03 11:22:51 +00:00
|
|
|
size_t& outputi) {
|
2016-04-24 20:11:47 +00:00
|
|
|
// Note on accuracy: Some values that this produces are +/- 1 from the actual firmware.
|
2017-08-03 11:22:51 +00:00
|
|
|
StepOverSamples(state, input, rate, output, outputi,
|
|
|
|
[](u64 fraction, const auto& x0, const auto& x1, const auto& x2) {
|
|
|
|
// This is a saturated subtraction. (Verified by black-box fuzzing.)
|
2018-07-24 17:08:17 +00:00
|
|
|
s64 delta0 = std::clamp<s64>(x1[0] - x0[0], -32768, 32767);
|
|
|
|
s64 delta1 = std::clamp<s64>(x1[1] - x0[1], -32768, 32767);
|
2017-08-03 11:22:51 +00:00
|
|
|
|
|
|
|
return std::array<s16, 2>{
|
|
|
|
static_cast<s16>(x0[0] + fraction * delta0 / scale_factor),
|
|
|
|
static_cast<s16>(x0[1] + fraction * delta1 / scale_factor),
|
|
|
|
};
|
|
|
|
});
|
2016-04-24 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AudioInterp
|
2017-12-20 18:44:32 +00:00
|
|
|
} // namespace AudioCore
|