Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
delay_lines.hpp
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/reverb/freeverb/tuning.h"
21#include <algorithm>
22#include <array>
23#include <cstdint>
24#include <span>
25
26namespace freeverb {
27
28// N delay lines (circular buffers) of different lengths, which always step along together, either one frame at a time
29// or a block at a time. A block is done one line at a time, straight through, so a line that wraps round during the
30// block runs on into the overhang after the end of its buffer: that first gets a copy of the start of the buffer, and
31// is copied back there afterwards. The lines are all longer than a block, so the start is up to date when it's copied.
32template <size_t N>
33struct DelayLines {
34 // The last maxblockframes words of the buffer are the overhang
35 void setBuffer(size_t i, std::span<int32_t> buffer) {
36 begin[i] = buffer.data();
37 end[i] = buffer.data() + buffer.size() - maxblockframes;
38 pos[i] = begin[i];
39 updateFramesToWrap();
40 }
41
42 void mute() {
43 for (size_t i = 0; i < N; i++) {
44 std::fill(begin[i], end[i], 0);
45 }
46 }
47
48 // Each line's sample from this frame, to be read and then replaced
49 [[gnu::always_inline]] int32_t& operator[](size_t i) { return *pos[i]; }
50
51 [[gnu::always_inline]] void advance() {
52 for (size_t i = 0; i < N; i++) {
53 pos[i]++;
54 }
55 if (--framesToWrap == 0) {
56 wrap();
57 }
58 }
59
60 // Before processing a block of numFrames (at most maxblockframes) straight through from pos
61 void startBlock(size_t numFrames) {
62 if (numFrames >= framesToWrap) {
63 for (size_t i = 0; i < N; i++) {
64 if (pos[i] + numFrames > end[i]) {
65 std::copy(begin[i], begin[i] + (pos[i] + numFrames - end[i]), end[i]);
66 }
67 }
68 }
69 }
70
71 // After processing the block, with pos moved on by numFrames
72 void endBlock(size_t numFrames) {
73 if (numFrames >= framesToWrap) {
74 wrap();
75 }
76 else {
77 framesToWrap -= numFrames;
78 }
79 }
80
81 void wrap() {
82 for (size_t i = 0; i < N; i++) {
83 if (pos[i] >= end[i]) {
84 std::copy(end[i], pos[i], begin[i]);
85 pos[i] = begin[i] + (pos[i] - end[i]);
86 }
87 }
88 updateFramesToWrap();
89 }
90
91 void updateFramesToWrap() {
92 framesToWrap = end[0] - pos[0];
93 for (size_t i = 1; i < N; i++) {
94 framesToWrap = std::min<size_t>(framesToWrap, end[i] - pos[i]);
95 }
96 }
97
98 std::array<int32_t*, N> pos;
99 std::array<int32_t*, N> begin;
100 std::array<int32_t*, N> end;
101 size_t framesToWrap; // until the first of the lines gets to its end, so always at least 1
102};
103
104} // namespace freeverb
Definition delay_lines.hpp:33