OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_DelayLine.cpp
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
26 namespace juce::dsp
27{
28
29//==============================================================================
30 template <typename SampleType, typename InterpolationType>
31 DelayLine<SampleType, InterpolationType>::DelayLine()
32 : DelayLine (0)
33{
34}
35
36 template <typename SampleType, typename InterpolationType>
37 DelayLine<SampleType, InterpolationType>::DelayLine (int maximumDelayInSamples)
38{
39 jassert (maximumDelayInSamples >= 0);
40
41 sampleRate = 44100.0;
42
43 setMaximumDelayInSamples (maximumDelayInSamples);
44}
45
46//==============================================================================
47 template <typename SampleType, typename InterpolationType>
48 void DelayLine<SampleType, InterpolationType>::setDelay (SampleType newDelayInSamples)
49{
50 auto upperLimit = (SampleType) getMaximumDelayInSamples();
51 jassert (isPositiveAndNotGreaterThan (newDelayInSamples, upperLimit));
52
53 delay = jlimit ((SampleType) 0, upperLimit, newDelayInSamples);
54 delayInt = static_cast<int> (std::floor (delay));
55 delayFrac = delay - (SampleType) delayInt;
56
57 updateInternalVariables();
58}
59
60 template <typename SampleType, typename InterpolationType>
61SampleType DelayLine<SampleType, InterpolationType>::getDelay() const
62{
63 return delay;
64}
65
66//==============================================================================
67 template <typename SampleType, typename InterpolationType>
68 void DelayLine<SampleType, InterpolationType>::prepare (const ProcessSpec& spec)
69{
70 jassert (spec.numChannels > 0);
71
72 bufferData.setSize ((int) spec.numChannels, totalSize, false, false, true);
73
74 writePos.resize (spec.numChannels);
75 readPos.resize (spec.numChannels);
76
77 v.resize (spec.numChannels);
78 sampleRate = spec.sampleRate;
79
80 reset();
81}
82
83 template <typename SampleType, typename InterpolationType>
84 void DelayLine<SampleType, InterpolationType>::setMaximumDelayInSamples (int maxDelayInSamples)
85{
86 jassert (maxDelayInSamples >= 0);
87 totalSize = jmax (4, maxDelayInSamples + 2);
88 bufferData.setSize ((int) bufferData.getNumChannels(), totalSize, false, false, true);
89 reset();
90}
91
92 template <typename SampleType, typename InterpolationType>
93 void DelayLine<SampleType, InterpolationType>::reset()
94{
95 for (auto vec : { &writePos, &readPos })
96 std::fill (vec->begin(), vec->end(), 0);
97
98 std::fill (v.begin(), v.end(), static_cast<SampleType> (0));
99
100 bufferData.clear();
101}
102
103//==============================================================================
104 template <typename SampleType, typename InterpolationType>
105 void DelayLine<SampleType, InterpolationType>::pushSample (int channel, SampleType sample)
106{
107 bufferData.setSample (channel, writePos[(size_t) channel], sample);
108 writePos[(size_t) channel] = (writePos[(size_t) channel] + totalSize - 1) % totalSize;
109}
110
111 template <typename SampleType, typename InterpolationType>
112SampleType DelayLine<SampleType, InterpolationType>::popSample (int channel, SampleType delayInSamples, bool updateReadPointer)
113{
114 if (delayInSamples >= 0)
115 setDelay (delayInSamples);
116
117 auto result = interpolateSample (channel);
118
119 if (updateReadPointer)
120 readPos[(size_t) channel] = (readPos[(size_t) channel] + totalSize - 1) % totalSize;
121
122 return result;
123}
124
125//==============================================================================
126 template class DelayLine<float, DelayLineInterpolationTypes::None>;
127 template class DelayLine<double, DelayLineInterpolationTypes::None>;
128 template class DelayLine<float, DelayLineInterpolationTypes::Linear>;
129 template class DelayLine<double, DelayLineInterpolationTypes::Linear>;
130 template class DelayLine<float, DelayLineInterpolationTypes::Lagrange3rd>;
131 template class DelayLine<double, DelayLineInterpolationTypes::Lagrange3rd>;
132 template class DelayLine<float, DelayLineInterpolationTypes::Thiran>;
133 template class DelayLine<double, DelayLineInterpolationTypes::Thiran>;
134
135} // namespace juce::dsp
juce::dsp::DelayLine
Definition juce_DelayLine.h:93
juce::dsp::DelayLine::prepare
void prepare(const ProcessSpec &spec)
Definition juce_DelayLine.cpp:68
juce::dsp::DelayLine::setDelay
void setDelay(SampleType newDelayInSamples)
Definition juce_DelayLine.cpp:48
juce::dsp::DelayLine::popSample
SampleType popSample(int channel, SampleType delayInSamples=-1, bool updateReadPointer=true)
Definition juce_DelayLine.cpp:112
juce::dsp::DelayLine::DelayLine
DelayLine()
Definition juce_DelayLine.cpp:31
juce::dsp::DelayLine::setMaximumDelayInSamples
void setMaximumDelayInSamples(int maxDelayInSamples)
Definition juce_DelayLine.cpp:84
juce::dsp::DelayLine::getDelay
SampleType getDelay() const
Definition juce_DelayLine.cpp:61
juce::dsp::DelayLine::reset
void reset()
Definition juce_DelayLine.cpp:93
juce::dsp::DelayLine::pushSample
void pushSample(int channel, SampleType sample)
Definition juce_DelayLine.cpp:105
juce::dsp::ProcessSpec
Definition juce_ProcessContext.h:36
juce::dsp::ProcessSpec::numChannels
uint32 numChannels
Definition juce_ProcessContext.h:44
juce::dsp::ProcessSpec::sampleRate
double sampleRate
Definition juce_ProcessContext.h:38