Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
impulse_response_processor.h
1/*
2 * Copyright © 2015-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "dsp/stereo_sample.h"
21#include "util/fixedpoint.h"
22#include <algorithm>
23#include <argon.hpp>
24#include <array>
25#include <cstdint>
26#include <span>
27#include <utility>
28
29class [[gnu::hot]] ImpulseResponseProcessor {
30 constexpr static size_t IR_SIZE = 26;
31 constexpr static size_t HISTORY_SIZE = (IR_SIZE - 1);
32
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,
37 };
38
39public:
40 ImpulseResponseProcessor() = default;
41
45 void process(std::span<StereoSample> buffer) {
46 // Render windows are often shorter than the IR, so rather than special-casing the outputs that reach back
47 // into the last call's inputs, lay the history and a chunk of input out contiguously and convolve that
48 std::array<StereoSample, HISTORY_SIZE + CHUNK_SIZE> input;
49 copy(history_.data(), HISTORY_SIZE, input.data());
50
51 while (true) {
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);
56 if (buffer.empty()) {
57 // The end of the last chunk's input is the history for next time
58 copy(input.data() + chunkSize, HISTORY_SIZE, history_.data());
59 return;
60 }
61 // The end of this chunk's input is the history for the next
62 copy(input.data() + chunkSize, HISTORY_SIZE, input.data());
63 }
64 }
65
66private:
67 constexpr static size_t CHUNK_SIZE = 64;
68
71 static void copy(const StereoSample* from, size_t n, StereoSample* to) {
72 for (; n >= 2; n -= 2, from += 2, to += 2) {
73 Argon<int32_t>::Load(&from->l).StoreTo(&to->l);
74 }
75 if (n != 0) {
76 *to = *from;
77 }
78 }
79
81 static void convolve(const StereoSample* x, std::span<StereoSample> output) {
82 convolveScalar(x, output, convolveNeon(x, output));
83 }
84 // Every coefficient is even, so vqrdmulh by half of one - round(2 * x * (h / 2) / 2^32) - is exactly smmulr by
85 // it: round(x * h / 2^32). It can't saturate, as no half coefficient is INT32_MIN
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++) {
90 half[k] = ir[k] / 2;
91 }
92 return half;
93 }();
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; }));
96
97 template <size_t j>
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));
103 }
104 if constexpr (j >= 2) {
105 earlier = earlier + pair.MultiplyRoundFixedQMax(coefficients[(j - 2) / 4].GetLane((j - 2) % 4));
106 }
107 }
108
110 static size_t convolveNeon(const StereoSample* x, std::span<StereoSample> output) {
111 CoefficientVectors coefficients;
112 for (size_t i = 0; i < coefficients.size(); i++) {
113 coefficients[i] = Argon<int32_t>::Load(&irHalf[4 * i]);
114 }
115
116 size_t n = 0;
117 for (; n + 3 < output.size(); n += 4) {
118 // Outputs n + 2 and n + 3 take input pair j with coefficient j; outputs n and n + 1 take the same pair
119 // with coefficient j - 2
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);
128 }
129 return n;
130 }
131
133 static void convolveScalar(const StereoSample* x, std::span<StereoSample> output, size_t n = 0) {
134 for (; n < output.size(); n++) {
135 const StereoSample* newest = &x[n];
136 int32_t l = 0;
137 int32_t r = 0;
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]);
141 }
142 output[n] = {l, r};
143 }
144 }
145
146 // The last HISTORY_SIZE inputs, oldest first
147 std::array<StereoSample, HISTORY_SIZE> history_{};
148};
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 void copy(const StereoSample *from, size_t n, StereoSample *to)
Definition impulse_response_processor.h:71
void process(std::span< StereoSample > buffer)
Definition impulse_response_processor.h:45
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
Definition stereo_sample.h:25