2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
31 //==============================================================================
32 template <typename SampleType
, typename InterpolationType
>
33 DelayLine
<SampleType
, InterpolationType
>::DelayLine()
38 template <typename SampleType
, typename InterpolationType
>
39 DelayLine
<SampleType
, InterpolationType
>::DelayLine (int maximumDelayInSamples
)
41 jassert (maximumDelayInSamples
>= 0);
45 setMaximumDelayInSamples (maximumDelayInSamples
);
48 //==============================================================================
49 template <typename SampleType
, typename InterpolationType
>
50 void DelayLine
<SampleType
, InterpolationType
>::setDelay (SampleType newDelayInSamples
)
52 auto upperLimit
= (SampleType
) getMaximumDelayInSamples();
53 jassert (isPositiveAndNotGreaterThan (newDelayInSamples
, upperLimit
));
55 delay
= jlimit ((SampleType
) 0, upperLimit
, newDelayInSamples
);
56 delayInt
= static_cast<int> (std::floor (delay
));
57 delayFrac
= delay
- (SampleType
) delayInt
;
59 updateInternalVariables();
62 template <typename SampleType
, typename InterpolationType
>
63 SampleType DelayLine
<SampleType
, InterpolationType
>::getDelay() const
68 //==============================================================================
69 template <typename SampleType
, typename InterpolationType
>
70 void DelayLine
<SampleType
, InterpolationType
>::prepare (const ProcessSpec
& spec
)
72 jassert (spec
.numChannels
> 0);
74 bufferData
.setSize ((int) spec
.numChannels
, totalSize
, false, false, true);
76 writePos
.resize (spec
.numChannels
);
77 readPos
.resize (spec
.numChannels
);
79 v
.resize (spec
.numChannels
);
80 sampleRate
= spec
.sampleRate
;
85 template <typename SampleType
, typename InterpolationType
>
86 void DelayLine
<SampleType
, InterpolationType
>::setMaximumDelayInSamples (int maxDelayInSamples
)
88 jassert (maxDelayInSamples
>= 0);
89 totalSize
= jmax (4, maxDelayInSamples
+ 1);
90 bufferData
.setSize ((int) bufferData
.getNumChannels(), totalSize
, false, false, true);
94 template <typename SampleType
, typename InterpolationType
>
95 void DelayLine
<SampleType
, InterpolationType
>::reset()
97 for (auto vec
: { &writePos
, &readPos
})
98 std::fill (vec
->begin(), vec
->end(), 0);
100 std::fill (v
.begin(), v
.end(), static_cast<SampleType
> (0));
105 //==============================================================================
106 template <typename SampleType
, typename InterpolationType
>
107 void DelayLine
<SampleType
, InterpolationType
>::pushSample (int channel
, SampleType sample
)
109 bufferData
.setSample (channel
, writePos
[(size_t) channel
], sample
);
110 writePos
[(size_t) channel
] = (writePos
[(size_t) channel
] + totalSize
- 1) % totalSize
;
113 template <typename SampleType
, typename InterpolationType
>
114 SampleType DelayLine
<SampleType
, InterpolationType
>::popSample (int channel
, SampleType delayInSamples
, bool updateReadPointer
)
116 if (delayInSamples
>= 0)
117 setDelay(delayInSamples
);
119 auto result
= interpolateSample (channel
);
121 if (updateReadPointer
)
122 readPos
[(size_t) channel
] = (readPos
[(size_t) channel
] + totalSize
- 1) % totalSize
;
127 //==============================================================================
128 template class DelayLine
<float, DelayLineInterpolationTypes::None
>;
129 template class DelayLine
<double, DelayLineInterpolationTypes::None
>;
130 template class DelayLine
<float, DelayLineInterpolationTypes::Linear
>;
131 template class DelayLine
<double, DelayLineInterpolationTypes::Linear
>;
132 template class DelayLine
<float, DelayLineInterpolationTypes::Lagrange3rd
>;
133 template class DelayLine
<double, DelayLineInterpolationTypes::Lagrange3rd
>;
134 template class DelayLine
<float, DelayLineInterpolationTypes::Thiran
>;
135 template class DelayLine
<double, DelayLineInterpolationTypes::Thiran
>;