VCV Rack API v2
Loading...
Searching...
No Matches
minblep.hpp
Go to the documentation of this file.
1#pragma once
2#include <dsp/common.hpp>
3
4
5namespace rack {
6namespace dsp {
7
8
18void minBlepImpulse(int Z, int O, float* output);
19
20
23template <int Z, int O>
24struct MinBlep {
29 float impulseReordered[O][1 + 2 * Z + 4] = {};
30
32 float impulse[2 * Z][O];
33 dsp::minBlepImpulse(Z, O, &impulse[0][0]);
34
35 // Fill impulseReordered with transposed and pre-subtracted data
36 // Storage index = z + 1 (to allow z = -1 access at index 0)
37 for (int o = 0; o < O; o++) {
38 // z = -1: before impulse starts
39 impulseReordered[o][0] = -1.f;
40 // z = 0 to 2*Z-1: actual impulse data
41 for (int z = 0; z < 2 * Z; z++) {
42 impulseReordered[o][1 + z] = impulse[z][o] - 1.f;
43 }
44 // z = 2*Z to 2*Z+3: after impulse ends (for SIMD padding)
45 for (int z = 2 * Z; z < 2 * Z + 4; z++) {
46 impulseReordered[o][1 + z] = 0.f;
47 }
48 }
49 }
50
52 const float* getImpulse(int o, int z) const {
53 z += o / O;
54 o = o % O;
55 if (o < 0) {
56 z -= 1;
57 o += O;
58 }
59 // assert(0 <= o && o < O);
60 // assert(-1 <= z && z < 2 * Z + 4);
61 return &impulseReordered[o][z + 1];
62 }
63
64 template <typename T = float>
65 struct State {
66 // Double buffer of length 2 * Z
67 T buffer[4 * Z] = {};
68 int32_t bufferIndex = 0;
69 };
70
76 template <typename T>
77 void insertDiscontinuity(State<T>& s, float p, T x) const {
78 if (!(0 < p && p <= 1))
79 return;
80
81 // Calculate impulse array index and fractional part
82 float subsample = (1 - p) * O;
83 int o = (int) subsample;
84 float t = subsample - o;
85
86 // For each zero crossing, cubic interpolate impulse response
87 for (int z = 0; z < 2 * Z; z += 4) {
93
94 // Write all 4 samples to contiguous buffer
95 for (int zi = 0; zi < 4; zi++) {
96 s.buffer[s.bufferIndex + z + zi] += y[zi] * x;
97 }
98 }
99 }
100
102 template <typename T>
103 T process(State<T>& s) const {
104 T v = s.buffer[s.bufferIndex];
105 s.buffer[s.bufferIndex] = T(0);
106 s.bufferIndex++;
107 if (s.bufferIndex >= 2 * Z) {
108 // Move second half of buffer to beginning
109 std::memcpy(s.buffer, s.buffer + 2 * Z, 2 * Z * sizeof(T));
110 std::memset(s.buffer + 2 * Z, 0, 2 * Z * sizeof(T));
111 s.bufferIndex = 0;
112 }
113 return v;
114 }
115};
116
117
119template <int Z, int O, typename T = float>
123
128 void insertDiscontinuity(float p, T x) {
129 minBlep.insertDiscontinuity(state, p + 1.f, x);
130 }
131
133 return minBlep.process(state);
134 }
135};
136
137
138} // namespace dsp
139} // namespace rack
Digital signal processing routines and classes.
Definition approx.hpp:6
T catmullRomInterpolate(T y0, T y1, T y2, T y3, T t)
Catmull-Rom cubic interpolation t is the fractional position between y1 and y2.
Definition common.hpp:94
void minBlepImpulse(int Z, int O, float *output)
Computes the minimum-phase bandlimited step (MinBLEP) Z: number of zero-crossings on each side of the...
Vector< float, 4 > float_4
Definition Vector.hpp:340
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Deprecated.
Definition minblep.hpp:120
T process()
Definition minblep.hpp:132
MinBlep< Z, O > minBlep
Definition minblep.hpp:121
MinBlep< Z, O >::template State< T > state
Definition minblep.hpp:122
void insertDiscontinuity(float p, T x)
In this class, p uses the range (-1, 0] to represent a subsample position.
Definition minblep.hpp:128
Definition minblep.hpp:65
int32_t bufferIndex
Definition minblep.hpp:68
T buffer[4 *Z]
Definition minblep.hpp:67
Holds a precomputed minBLEP impulse response, reordered to efficiently insert into an audio buffer.
Definition minblep.hpp:24
const float * getImpulse(int o, int z) const
Get pointer to impulse data, handling o wrapping and z offset.
Definition minblep.hpp:52
T process(State< T > &s) const
Should be called every frame after inserting any discontinuities.
Definition minblep.hpp:103
float impulseReordered[O][1+2 *Z+4]
Reordered impulse response for cubic interpolation, minus 1.0.
Definition minblep.hpp:29
void insertDiscontinuity(State< T > &s, float p, T x) const
Places a discontinuity with magnitude x at 0 < p <= 1 relative to the current frame.
Definition minblep.hpp:77
MinBlep()
Definition minblep.hpp:31
static Vector load(const float *x)
Reads an array of 4 values.
Definition Vector.hpp:74