LMMS
Loading...
Searching...
No Matches
juce_DelayLine.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28namespace dsp
29{
30
31//==============================================================================
37{
45 struct None {};
46
55 struct Linear {};
56
65 struct Lagrange3rd {};
66
76 struct Thiran {};
77}
78
79//==============================================================================
93template <typename SampleType, typename InterpolationType = DelayLineInterpolationTypes::Linear>
95{
96public:
97 //==============================================================================
99 DelayLine();
100
102 explicit DelayLine (int maximumDelayInSamples);
103
104 //==============================================================================
106 void setDelay (SampleType newDelayInSamples);
107
109 SampleType getDelay() const;
110
111 //==============================================================================
113 void prepare (const ProcessSpec& spec);
114
121 void setMaximumDelayInSamples (int maxDelayInSamples);
122
129
131 void reset();
132
133 //==============================================================================
142 void pushSample (int channel, SampleType sample);
143
161 SampleType popSample (int channel, SampleType delayInSamples = -1, bool updateReadPointer = true);
162
163 //==============================================================================
171 template <typename ProcessContext>
172 void process (const ProcessContext& context) noexcept
173 {
174 const auto& inputBlock = context.getInputBlock();
175 auto& outputBlock = context.getOutputBlock();
176 const auto numChannels = outputBlock.getNumChannels();
177 const auto numSamples = outputBlock.getNumSamples();
178
179 jassert (inputBlock.getNumChannels() == numChannels);
180 jassert (inputBlock.getNumChannels() == writePos.size());
181 jassert (inputBlock.getNumSamples() == numSamples);
182
183 if (context.isBypassed)
184 {
185 outputBlock.copyFrom (inputBlock);
186 return;
187 }
188
189 for (size_t channel = 0; channel < numChannels; ++channel)
190 {
191 auto* inputSamples = inputBlock.getChannelPointer (channel);
192 auto* outputSamples = outputBlock.getChannelPointer (channel);
193
194 for (size_t i = 0; i < numSamples; ++i)
195 {
196 pushSample ((int) channel, inputSamples[i]);
197 outputSamples[i] = popSample ((int) channel);
198 }
199 }
200 }
201
202private:
203 //==============================================================================
204 template <typename T = InterpolationType>
205 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::None>::value, SampleType>::type
206 interpolateSample (int channel) const
207 {
208 auto index = (readPos[(size_t) channel] + delayInt) % totalSize;
209 return bufferData.getSample (channel, index);
210 }
211
212 template <typename T = InterpolationType>
213 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Linear>::value, SampleType>::type
214 interpolateSample (int channel) const
215 {
216 auto index1 = readPos[(size_t) channel] + delayInt;
217 auto index2 = index1 + 1;
218
219 if (index2 >= totalSize)
220 {
221 index1 %= totalSize;
222 index2 %= totalSize;
223 }
224
225 auto value1 = bufferData.getSample (channel, index1);
226 auto value2 = bufferData.getSample (channel, index2);
227
228 return value1 + delayFrac * (value2 - value1);
229 }
230
231 template <typename T = InterpolationType>
232 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Lagrange3rd>::value, SampleType>::type
233 interpolateSample (int channel) const
234 {
235 auto index1 = readPos[(size_t) channel] + delayInt;
236 auto index2 = index1 + 1;
237 auto index3 = index2 + 1;
238 auto index4 = index3 + 1;
239
240 if (index4 >= totalSize)
241 {
242 index1 %= totalSize;
243 index2 %= totalSize;
244 index3 %= totalSize;
245 index4 %= totalSize;
246 }
247
248 auto* samples = bufferData.getReadPointer (channel);
249
250 auto value1 = samples[index1];
251 auto value2 = samples[index2];
252 auto value3 = samples[index3];
253 auto value4 = samples[index4];
254
255 auto d1 = delayFrac - 1.f;
256 auto d2 = delayFrac - 2.f;
257 auto d3 = delayFrac - 3.f;
258
259 auto c1 = -d1 * d2 * d3 / 6.f;
260 auto c2 = d2 * d3 * 0.5f;
261 auto c3 = -d1 * d3 * 0.5f;
262 auto c4 = d1 * d2 / 6.f;
263
264 return value1 * c1 + delayFrac * (value2 * c2 + value3 * c3 + value4 * c4);
265 }
266
267 template <typename T = InterpolationType>
268 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Thiran>::value, SampleType>::type
269 interpolateSample (int channel)
270 {
271 auto index1 = readPos[(size_t) channel] + delayInt;
272 auto index2 = index1 + 1;
273
274 if (index2 >= totalSize)
275 {
276 index1 %= totalSize;
277 index2 %= totalSize;
278 }
279
280 auto value1 = bufferData.getSample (channel, index1);
281 auto value2 = bufferData.getSample (channel, index2);
282
283 auto output = delayFrac == 0 ? value1 : value2 + alpha * (value1 - v[(size_t) channel]);
284 v[(size_t) channel] = output;
285
286 return output;
287 }
288
289 //==============================================================================
290 template <typename T = InterpolationType>
291 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::None>::value, void>::type
295
296 template <typename T = InterpolationType>
297 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Linear>::value, void>::type
301
302 template <typename T = InterpolationType>
303 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Lagrange3rd>::value, void>::type
305 {
306 if (delayInt >= 1)
307 {
308 delayFrac++;
309 delayInt--;
310 }
311 }
312
313 template <typename T = InterpolationType>
314 typename std::enable_if <std::is_same <T, DelayLineInterpolationTypes::Thiran>::value, void>::type
316 {
317 if (delayFrac < (SampleType) 0.618 && delayInt >= 1)
318 {
319 delayFrac++;
320 delayInt--;
321 }
322
323 alpha = (1 - delayFrac) / (1 + delayFrac);
324 }
325
326 //==============================================================================
328
329 //==============================================================================
331 std::vector<SampleType> v;
332 std::vector<int> writePos, readPos;
333 SampleType delay = 0.0, delayFrac = 0.0;
334 int delayInt = 0, totalSize = 4;
335 SampleType alpha = 0.0;
336};
337
338} // namespace dsp
339} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
CAdPlugDatabase::CRecord::RecordType type
Definition adplugdb.cpp:93
Definition juce_AudioSampleBuffer.h:34
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::None >::value, void >::type updateInternalVariables()
Definition juce_DelayLine.h:292
int getMaximumDelayInSamples() const noexcept
Definition juce_DelayLine.h:128
void pushSample(int channel, SampleType sample)
Definition juce_DelayLine.cpp:107
std::vector< SampleType > v
Definition juce_DelayLine.h:331
SampleType alpha
Definition juce_DelayLine.h:335
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Lagrange3rd >::value, void >::type updateInternalVariables()
Definition juce_DelayLine.h:304
AudioBuffer< SampleType > bufferData
Definition juce_DelayLine.h:330
DelayLine()
Definition juce_DelayLine.cpp:33
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Linear >::value, SampleType >::type interpolateSample(int channel) const
Definition juce_DelayLine.h:214
SampleType delayFrac
Definition juce_DelayLine.h:333
void setDelay(SampleType newDelayInSamples)
Definition juce_DelayLine.cpp:50
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Linear >::value, void >::type updateInternalVariables()
Definition juce_DelayLine.h:298
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Thiran >::value, SampleType >::type interpolateSample(int channel)
Definition juce_DelayLine.h:269
std::vector< int > readPos
Definition juce_DelayLine.h:332
std::vector< int > writePos
Definition juce_DelayLine.h:332
double sampleRate
Definition juce_DelayLine.h:327
void setMaximumDelayInSamples(int maxDelayInSamples)
Definition juce_DelayLine.cpp:86
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::None >::value, SampleType >::type interpolateSample(int channel) const
Definition juce_DelayLine.h:206
int delayInt
Definition juce_DelayLine.h:334
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Lagrange3rd >::value, SampleType >::type interpolateSample(int channel) const
Definition juce_DelayLine.h:233
void process(const ProcessContext &context) noexcept
Definition juce_DelayLine.h:172
int totalSize
Definition juce_DelayLine.h:334
SampleType getDelay() const
Definition juce_DelayLine.cpp:63
void prepare(const ProcessSpec &spec)
Definition juce_DelayLine.cpp:70
SampleType delay
Definition juce_DelayLine.h:333
SampleType popSample(int channel, SampleType delayInSamples=-1, bool updateReadPointer=true)
Definition juce_DelayLine.cpp:114
std::enable_if< std::is_same< T, DelayLineInterpolationTypes::Thiran >::value, void >::type updateInternalVariables()
Definition juce_DelayLine.h:315
register unsigned i
Definition inflate.c:1575
static void c2(register WDL_FFT_COMPLEX *a)
Definition fft.c:270
static void c4(register WDL_FFT_COMPLEX *a)
Definition fft.c:283
#define jassert(expression)
Definition juce_DelayLine.h:37
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
Definition juce_DelayLine.h:45
Definition juce_ProcessContext.h:38
signed int sample
Definition tap_dynamics_m.c:41
#define const
Definition zconf.h:137