OpenShot Audio Library | OpenShotAudio  0.6.0
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>
32  : DelayLine (0)
33 {
34 }
35 
36 template <typename SampleType, typename InterpolationType>
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>
62 {
63  return delay;
64 }
65 
66 //==============================================================================
67 template <typename SampleType, typename InterpolationType>
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>
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>
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>
112 SampleType 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 //==============================================================================
134 
135 } // namespace juce::dsp
void prepare(const ProcessSpec &spec)
void setDelay(SampleType newDelayInSamples)
SampleType popSample(int channel, SampleType delayInSamples=-1, bool updateReadPointer=true)
void setMaximumDelayInSamples(int maxDelayInSamples)
SampleType getDelay() const
void pushSample(int channel, SampleType sample)