Skip to content

Commit ada0211

Browse files
committed
moved allpass and tapped delay to cpp/h file
1 parent d0f559e commit ada0211

File tree

11 files changed

+362
-210
lines changed

11 files changed

+362
-210
lines changed

RSAlgorithmicVerb.jucer

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
<FILE id="XAbMe6" name="DattorroVerb.cpp" compile="1" resource="0"
2424
file="Source/DattorroVerb.cpp"/>
2525
<FILE id="l80Jn6" name="DattorroVerb.h" compile="0" resource="0" file="Source/DattorroVerb.h"/>
26-
<FILE id="mCFD5y" name="DelayLineWithSampleAccess.h" compile="0" resource="0"
27-
file="Source/DelayLineWithSampleAccess.h"/>
2826
<FILE id="tXGyRQ" name="EarlyReflections.cpp" compile="1" resource="0"
2927
file="Source/EarlyReflections.cpp"/>
3028
<FILE id="jwqlsc" name="EarlyReflections.h" compile="0" resource="0"
@@ -39,6 +37,9 @@
3937
<FILE id="I5XfjM" name="SpecialFX.cpp" compile="1" resource="0" file="Source/SpecialFX.cpp"/>
4038
<FILE id="m5cIHP" name="SpecialFX.h" compile="0" resource="0" file="Source/SpecialFX.h"/>
4139
<FILE id="b5KXx0" name="GuiStyles.h" compile="0" resource="0" file="Source/GuiStyles.h"/>
40+
<FILE id="MWM6ce" name="CustomDelays.cpp" compile="1" resource="0"
41+
file="Source/CustomDelays.cpp"/>
42+
<FILE id="g0bLEt" name="CustomDelays.h" compile="0" resource="0" file="Source/CustomDelays.h"/>
4243
<FILE id="GqamHE" name="LFO.cpp" compile="1" resource="0" file="Source/LFO.cpp"/>
4344
<FILE id="aA7GG2" name="LFO.h" compile="0" resource="0" file="Source/LFO.h"/>
4445
<FILE id="mYaXC8" name="ProcessorBase.h" compile="0" resource="0" file="Source/ProcessorBase.h"/>

Source/ConcertHallB.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
#include <JuceHeader.h>
1414

15-
#include "DelayLineWithSampleAccess.h"
15+
//#include "DelayLineWithSampleAccess.h"
16+
#include "CustomDelays.h"
1617
#include "LFO.h"
1718
#include "ProcessorBase.h"
1819
#include "Utilities.h"

Source/CustomDelays.cpp

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/*
2+
==============================================================================
3+
4+
Tapped delay line, Allpass classes
5+
6+
==============================================================================
7+
*/
8+
9+
#include "CustomDelays.h"
10+
11+
template <typename SampleType>
12+
DelayLineWithSampleAccess<SampleType>::DelayLineWithSampleAccess(int maximumDelayInSamples)
13+
{
14+
jassert (maximumDelayInSamples >= 0);
15+
16+
mTotalSize = maximumDelayInSamples + 1 > 4 ? maximumDelayInSamples + 1 : 4;
17+
mDelayBuffer.setSize(static_cast<int>(mDelayBuffer.getNumChannels()), mTotalSize, false, false, false);
18+
mDelayBuffer.clear();
19+
mNumSamples = mDelayBuffer.getNumSamples();
20+
21+
}
22+
23+
template <typename SampleType>
24+
DelayLineWithSampleAccess<SampleType>::~DelayLineWithSampleAccess() {}
25+
26+
template <typename SampleType>
27+
void DelayLineWithSampleAccess<SampleType>::pushSample(int channel, SampleType newValue)
28+
{
29+
mDelayBuffer.setSample(channel, mWritePosition[static_cast<size_t>(channel)], newValue);
30+
mWritePosition[static_cast<size_t>(channel)] = (mWritePosition[static_cast<size_t>(channel)] + 1) % mNumSamples;
31+
}
32+
33+
template <typename SampleType>
34+
SampleType DelayLineWithSampleAccess<SampleType>::popSample(int channel)
35+
{
36+
mReadPosition[static_cast<size_t>(channel)] = wrapInt((mWritePosition[static_cast<size_t>(channel)] - mDelayInSamples), mNumSamples);
37+
return mDelayBuffer.getSample(channel, mReadPosition[static_cast<size_t>(channel)]);
38+
}
39+
40+
template <typename SampleType>
41+
SampleType DelayLineWithSampleAccess<SampleType>::getSampleAtDelay(int channel, int delay) const
42+
{
43+
return mDelayBuffer.getSample(channel, wrapInt((mWritePosition[static_cast<size_t>(channel)] - delay), mNumSamples));
44+
}
45+
46+
template <typename SampleType>
47+
void DelayLineWithSampleAccess<SampleType>::setDelay(int newLength) { mDelayInSamples = newLength; }
48+
49+
template <typename SampleType>
50+
void DelayLineWithSampleAccess<SampleType>::setSize(const int numChannels, const int newSize)
51+
{
52+
mTotalSize = newSize;
53+
mDelayBuffer.setSize(numChannels, mTotalSize, false, false, true);
54+
55+
reset();
56+
}
57+
58+
template <typename SampleType>
59+
int DelayLineWithSampleAccess<SampleType>::getNumSamples() const { return mDelayBuffer.getNumSamples(); }
60+
61+
template <typename SampleType>
62+
void DelayLineWithSampleAccess<SampleType>::prepare(const juce::dsp::ProcessSpec& spec)
63+
{
64+
jassert(spec.numChannels > 0);
65+
66+
mDelayBuffer.setSize(static_cast<int>(spec.numChannels), mTotalSize, false, false, true);
67+
68+
mWritePosition.resize(spec.numChannels);
69+
mReadPosition.resize(spec.numChannels);
70+
71+
v.resize(spec.numChannels);
72+
mSampleRate = spec.sampleRate;
73+
74+
reset();
75+
}
76+
77+
template <typename SampleType>
78+
void DelayLineWithSampleAccess<SampleType>::reset()
79+
{
80+
for (auto vec : {&mWritePosition, &mReadPosition})
81+
std::fill (vec->begin(), vec->end(), 0);
82+
83+
std::fill (v.begin(), v.end(), static_cast<SampleType>(0));
84+
85+
mDelayBuffer.clear();
86+
}
87+
88+
template <typename SampleType>
89+
int DelayLineWithSampleAccess<SampleType>::wrapInt(int a, int b) const
90+
{
91+
int c = a % b;
92+
return (c < 0) ? c + b : c;
93+
}
94+
95+
//============================================================================
96+
97+
template <typename SampleType>
98+
Allpass<SampleType>::Allpass() = default;
99+
100+
template <typename SampleType>
101+
Allpass<SampleType>::~Allpass() = default;
102+
103+
template <typename SampleType>
104+
void Allpass<SampleType>::setMaximumDelayInSamples(int maxDelayInSamples)
105+
{
106+
mDelayLine.setMaximumDelayInSamples(maxDelayInSamples);
107+
}
108+
109+
template <typename SampleType>
110+
void Allpass<SampleType>::setDelay(SampleType newDelayInSamples)
111+
{
112+
mDelayLine.setDelay(newDelayInSamples);
113+
}
114+
115+
template <typename SampleType>
116+
void Allpass<SampleType>::prepare(const juce::dsp::ProcessSpec& spec)
117+
{
118+
jassert(spec.numChannels > 0);
119+
120+
mSampleRate = spec.sampleRate;
121+
122+
mDelayLine.prepare(spec);
123+
124+
drySample.resize(spec.numChannels);
125+
delayOutput.resize(spec.numChannels);
126+
feedforward.resize(spec.numChannels);
127+
feedback.resize(spec.numChannels);
128+
129+
std::fill(drySample.begin(), drySample.end(), 0.0);
130+
std::fill(delayOutput.begin(), delayOutput.end(), 0.0);
131+
std::fill(feedforward.begin(), feedforward.end(), 0.0);
132+
std::fill(feedback.begin(), feedback.end(), 0.0);
133+
134+
reset();
135+
}
136+
137+
template <typename SampleType>
138+
void Allpass<SampleType>::reset() { mDelayLine.reset(); }
139+
140+
template <typename SampleType>
141+
void Allpass<SampleType>::pushSample(int channel, SampleType sample)
142+
{
143+
mDelayLine.pushSample(channel, sample + feedback[channel]);
144+
drySample[channel] = sample;
145+
}
146+
147+
template <typename SampleType>
148+
SampleType Allpass<SampleType>::popSample(int channel, SampleType delayInSamples, bool updateReadPointer)
149+
{
150+
delayOutput[channel] = mDelayLine.popSample(channel, delayInSamples, updateReadPointer);
151+
152+
feedback[channel] = delayOutput[channel] * mGain;
153+
154+
feedforward[channel] = -drySample[channel] - delayOutput[channel] * mGain;
155+
156+
return delayOutput[channel] + feedforward[channel];
157+
}
158+
159+
template <typename SampleType>
160+
void Allpass<SampleType>::setGain(SampleType newGain) { mGain = std::clamp<SampleType>(newGain, 0.0, 1.0); }
161+
162+
//============================================================================
163+
164+
template class DelayLineWithSampleAccess<float>;
165+
template class DelayLineWithSampleAccess<double>;
166+
167+
template class Allpass<float>;
168+
template class Allpass<double>;

Source/CustomDelays.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
==============================================================================
3+
4+
Tapped delay line, Allpass classes
5+
Delay based on juce::dsp::DelayLine, but allows access to the underlying buffer at specified sample offsets for multiple-tap delays.
6+
7+
==============================================================================
8+
*/
9+
10+
#pragma once
11+
12+
#include <JuceHeader.h>
13+
14+
template <typename SampleType>
15+
class DelayLineWithSampleAccess
16+
{
17+
public:
18+
DelayLineWithSampleAccess(int maximumDelayInSamples);
19+
20+
~DelayLineWithSampleAccess();
21+
22+
void pushSample(int channel, SampleType newValue);
23+
24+
SampleType popSample(int channel);
25+
26+
SampleType getSampleAtDelay(int channel, int delay) const;
27+
28+
void setDelay(int newLength);
29+
30+
void setSize(const int numChannels, const int newSize);
31+
32+
int getNumSamples() const;
33+
34+
void prepare(const juce::dsp::ProcessSpec& spec);
35+
36+
void reset();
37+
38+
int wrapInt(int a, int b) const;
39+
private:
40+
juce::AudioBuffer<SampleType> mDelayBuffer;
41+
std::vector<SampleType> v;
42+
int mNumSamples = 0;
43+
std::vector<int> mWritePosition, mReadPosition;
44+
SampleType delay = 0.0, delayFrac = 0.0;
45+
int mDelayInSamples {0};
46+
int mTotalSize = 4;
47+
48+
double mSampleRate = 44100.0;
49+
};
50+
51+
//============================================================================
52+
53+
template <typename SampleType>
54+
class Allpass
55+
{
56+
public:
57+
Allpass();
58+
59+
~Allpass();
60+
61+
void setMaximumDelayInSamples(int maxDelayInSamples);
62+
63+
void setDelay(SampleType newDelayInSamples);
64+
65+
void prepare(const juce::dsp::ProcessSpec& spec);
66+
67+
void reset();
68+
69+
void pushSample(int channel, SampleType sample);
70+
71+
SampleType popSample(int channel, SampleType delayInSamples=-1, bool updateReadPointer=true);
72+
73+
void setGain(SampleType newGain);
74+
75+
private:
76+
juce::dsp::DelayLine<SampleType> mDelayLine { 44100 };
77+
78+
int mDelayInSamples { 4 };
79+
80+
SampleType mGain { 0.5 };
81+
82+
std::vector<SampleType> drySample { };
83+
std::vector<SampleType> delayOutput { };
84+
std::vector<SampleType> feedforward { };
85+
std::vector<SampleType> feedback { };
86+
87+
SampleType mSampleRate { 44100.0 };
88+
};

Source/DattorroVerb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
#include <JuceHeader.h>
1212

13-
#include "DelayLineWithSampleAccess.h"
13+
//#include "DelayLineWithSampleAccess.h"
14+
#include "CustomDelays.h"
1415
#include "LFO.h"
1516
#include "ProcessorBase.h"
1617
#include "Utilities.h"

0 commit comments

Comments
 (0)