VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_dsp / processors / juce_DelayLine.cpp
blob3ea3edbc4d9e96107d6080a1bd75766e2f1efbcc
1 /*
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
8 licensing.
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
21 DISCLAIMED.
23 ==============================================================================
26 namespace juce
28 namespace dsp
31 //==============================================================================
32 template <typename SampleType, typename InterpolationType>
33 DelayLine<SampleType, InterpolationType>::DelayLine()
34 : DelayLine (0)
38 template <typename SampleType, typename InterpolationType>
39 DelayLine<SampleType, InterpolationType>::DelayLine (int maximumDelayInSamples)
41 jassert (maximumDelayInSamples >= 0);
43 sampleRate = 44100.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
65 return delay;
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;
82 reset();
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);
91 reset();
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));
102 bufferData.clear();
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;
124 return result;
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>;
137 } // namespace dsp
138 } // namespace juce