29class [[gnu::hot]] ImpulseResponseProcessor {
30 constexpr static size_t IR_SIZE = 26;
31 constexpr static size_t HISTORY_SIZE = (IR_SIZE - 1);
33 constexpr static std::array<int32_t, IR_SIZE> ir = {
34 -3203916, 8857848, 24813136, 41537808, 35217472, 15195632, -27538592, -61984128, 1944654848,
35 1813580928, 438462784, 101125088, 6042048, -22429488, -46218864, -56638560, -64785312, -52108528,
36 -37256992, -11863856, 1390352, 14663296, 12784464, 14254800, 5690912, 4490736,
40 ImpulseResponseProcessor() =
default;
45 void process(std::span<StereoSample> buffer) {
48 std::array<StereoSample, HISTORY_SIZE + CHUNK_SIZE> input;
49 copy(history_.data(), HISTORY_SIZE, input.data());
52 size_t chunkSize = std::min(buffer.size(), CHUNK_SIZE);
53 copy(buffer.data(), chunkSize, input.data() + HISTORY_SIZE);
54 convolve(input.data() + HISTORY_SIZE, buffer.first(chunkSize));
55 buffer = buffer.subspan(chunkSize);
58 copy(input.data() + chunkSize, HISTORY_SIZE, history_.data());
62 copy(input.data() + chunkSize, HISTORY_SIZE, input.data());
67 constexpr static size_t CHUNK_SIZE = 64;
72 for (; n >= 2; n -= 2, from += 2, to += 2) {
73 Argon<int32_t>::Load(&from->l).StoreTo(&to->l);
86 constexpr static size_t NUM_COEFFICIENT_VECTORS = (IR_SIZE + 3) / 4;
87 alignas(16)
constexpr static std::array<int32_t, NUM_COEFFICIENT_VECTORS * 4> irHalf = [] {
88 std::array<int32_t, NUM_COEFFICIENT_VECTORS * 4> half{};
89 for (
size_t k = 0; k < IR_SIZE; k++) {
94 using CoefficientVectors = std::array<Argon<int32_t>, NUM_COEFFICIENT_VECTORS>;
95 static_assert(std::ranges::all_of(ir, [](int32_t h) {
return h % 2 == 0; }));
98 [[gnu::always_inline]]
static void tap(
const int32_t* newest, CoefficientVectors& coefficients,
99 Argon<int32_t>& later, Argon<int32_t>& earlier) {
100 auto pair = Argon<int32_t>::Load(newest - 2 *
static_cast<int32_t
>(j));
101 if constexpr (j < IR_SIZE) {
102 later = later + pair.MultiplyRoundFixedQMax(coefficients[j / 4].GetLane(j % 4));
104 if constexpr (j >= 2) {
105 earlier = earlier + pair.MultiplyRoundFixedQMax(coefficients[(j - 2) / 4].GetLane((j - 2) % 4));
111 CoefficientVectors coefficients;
112 for (
size_t i = 0; i < coefficients.size(); i++) {
113 coefficients[i] = Argon<int32_t>::Load(&irHalf[4 * i]);
117 for (; n + 3 < output.size(); n += 4) {
120 const int32_t* newest = &x[n + 2].l;
121 Argon<int32_t> later{0};
122 Argon<int32_t> earlier{0};
123 [&]<
size_t... j>(std::index_sequence<j...>) {
124 (tap<j>(newest, coefficients, later, earlier), ...);
125 }(std::make_index_sequence<IR_SIZE + 2>{});
126 earlier.StoreTo(&output[n].l);
127 later.StoreTo(&output[n + 2].l);
134 for (; n < output.size(); n++) {
138 for (int32_t k = 0; k < IR_SIZE; k++) {
139 l = multiply_accumulate_32x32_rshift32_rounded(l, newest[-k].l, ir[k]);
140 r = multiply_accumulate_32x32_rshift32_rounded(r, newest[-k].r, ir[k]);
147 std::array<StereoSample, HISTORY_SIZE> history_{};
static void convolve(const StereoSample *x, std::span< StereoSample > output)
x points at the first new input, with HISTORY_SIZE older inputs before it
Definition impulse_response_processor.h:81
static size_t convolveNeon(const StereoSample *x, std::span< StereoSample > output)
Four outputs (two vectors, each two stereo frames) at a time; returns how many outputs it did.
Definition impulse_response_processor.h:110
static void convolveScalar(const StereoSample *x, std::span< StereoSample > output, size_t n=0)
One output at a time, from output n on: the leftovers after convolveNeon, or everything off ARM.
Definition impulse_response_processor.h:133