-
Notifications
You must be signed in to change notification settings - Fork 1
/
synth-reverb.cpp
197 lines (159 loc) · 5.9 KB
/
synth-reverb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
FM. BISON hybrid FM synthesis -- Reverb effect based on FreeVerb.
(C) njdewit technologies (visualizers.nl) & bipolaraudio.nl
MIT license applies, please see https://en.wikipedia.org/wiki/MIT_License or LICENSE in the project root!
*/
#include "synth-reverb.h"
namespace SFM
{
// Defaults are tuned for 44.1KHz, so we need to adjust (can be done linearly, since we're working in time (sample) domain)
SFM_INLINE static size_t ScaleNumSamples(unsigned sampleRate, size_t numSamples)
{
const float scale = sampleRate/44100.f;
return size_t(floorf(numSamples*scale));
}
/*
Defaults taken from a decent FreeVerb implementation
*/
// Added to R
constexpr unsigned kStereoSpread = 23;
// L
const size_t kCombSizes[kReverbNumCombs] = {
1116,
1188,
1277,
1356,
1422,
1491,
1557,
1617
};
// L
const size_t kAllPassSizes[kReverbNumAllPasses] = {
556,
441,
341,
225
};
// FIXME: test variations (https://christianfloisand.wordpress.com/tag/all-pass-filter/ mentions slightly modulating the first pass)
constexpr float kAllPassDefFeedback = 0.6f; // FIXME: this is a very interesting parameter to play with!
constexpr float kDefaultRoomSize = 0.8f;
constexpr float kDefaultWidth = 2.f;
// Pre-delay line length (in seconds)
constexpr float kReverbPreDelayLen = 0.5f; // 500MS
Reverb::Reverb(unsigned sampleRate, unsigned Nyquist) :
m_sampleRate(sampleRate), m_Nyquist(Nyquist)
, m_preEQ(sampleRate, false)
, m_preDelayLine(sampleRate, kReverbPreDelayLen)
, m_width(kDefaultWidth)
, m_roomSize(kDefaultRoomSize)
, m_preDelay(0.f)
, m_curWet(0.f, sampleRate, kDefParameterLatency, 0.f, 1.f)
, m_curWidth(kMinReverbWidth, sampleRate, kDefParameterLatency, kMinReverbWidth, kMaxReverbWidth)
, m_curRoomSize(0.f, sampleRate, kDefParameterLatency, 0.f, 1.f)
, m_curDampening(0.f, sampleRate, kDefParameterLatency, 0.f, 1.f)
, m_curPreDelay(0.f, sampleRate, kDefParameterLatency * 4.f /* Longer */, 0.f, 1.f)
, m_curBassdB(0.f, sampleRate, kDefParameterLatency, 0.f, 1.f)
, m_curTrebledB(0.f, sampleRate, kDefParameterLatency, 0.f, 1.f)
{
// Semi-fixed
static_assert(8 == kReverbNumCombs);
static_assert(4 == kReverbNumAllPasses);
// Adjusted stereo spread
const size_t stereoSpread = ScaleNumSamples(sampleRate, kStereoSpread);
// Allocate single sequential buffer
size_t totalBufSize = 0;
for (auto size : kCombSizes)
{
size = ScaleNumSamples(sampleRate, size);
totalBufSize += size + (size+stereoSpread);
}
for (auto size : kAllPassSizes)
{
size = ScaleNumSamples(sampleRate, size);
totalBufSize += size + (size+stereoSpread);
}
m_totalBufSize = totalBufSize*sizeof(float);
m_buffer = reinterpret_cast<float*>(mallocAligned(m_totalBufSize, 16));
// Set sizes and pointers
size_t offset = 0;
for (unsigned iComb = 0; iComb < kReverbNumCombs; ++iComb)
{
const size_t size = ScaleNumSamples(sampleRate, kCombSizes[iComb]);
float *pCur = m_buffer+offset;
m_combsL[iComb].SetSizeAndBuffer(size, pCur);
m_combsR[iComb].SetSizeAndBuffer(size+stereoSpread, pCur+size);
offset += size + (size+stereoSpread);
}
for (unsigned iAllPass = 0; iAllPass < kReverbNumAllPasses; ++iAllPass)
{
const size_t size = ScaleNumSamples(sampleRate, kAllPassSizes[iAllPass]);
float *pCur = m_buffer + offset;
m_allPassesL[iAllPass].SetSizeAndBuffer(size, pCur);
m_allPassesR[iAllPass].SetSizeAndBuffer(size+stereoSpread, pCur+size);
offset += size + (size+stereoSpread);
}
// Clear buffer
memset(m_buffer, 0, m_totalBufSize);
}
constexpr float kFixedGain = 0.015f; // Taken from ref. implementation
void Reverb::Apply(float *pLeft, float *pRight, unsigned numSamples, float wet, float bassTuningdB, float trebleTuningdB)
{
SFM_ASSERT(nullptr != pLeft && nullptr != pRight);
SFM_ASSERT_NORM(wet);
SFM_ASSERT(bassTuningdB >= kMiniEQMindB && bassTuningdB <= kMiniEQMaxdB);
SFM_ASSERT(trebleTuningdB >= kMiniEQMindB && trebleTuningdB <= kMiniEQMaxdB);
// Set parameter targets
m_curWet.SetTarget(wet);
m_curWidth.SetTarget(m_width);
m_curRoomSize.SetTarget(m_roomSize);
m_curDampening.SetTarget(m_dampening);
m_curPreDelay.SetTarget(m_preDelay);
m_preEQ.SetTargetdBs(bassTuningdB, trebleTuningdB);
for (unsigned iSample = 0; iSample < numSamples; ++iSample)
{
const float curWet = m_curWet.Sample() * kMaxReverbWet; // Doesn't sound like much if fully open, consider different mix below? (FIXME)
const float dry = 1.f-curWet;
// Stereo (width) effect
const float width = m_curWidth.Sample();
const float wet1 = curWet*(width*0.5f + 0.5f);
const float wet2 = curWet*((1.f-width)*0.5f);
// In & out
const float inL = *pLeft;
const float inR = *pRight;
float outL = 0.f;
float outR = 0.f;
// Mix to monaural & apply EQ
/* const */ float monaural = 0.5f*inR + 0.5f*inL;
monaural = m_preEQ.ApplyMono(monaural);
// Apply pre-delay
m_preDelayLine.Write(monaural);
monaural = m_preDelayLine.ReadNormalized(m_curPreDelay.Sample()) * kFixedGain;
// Accumulate comb filters in parallel
const float dampening = m_curDampening.Sample();
const float roomSize = m_curRoomSize.Sample();
for (unsigned iComb = 0; iComb < kReverbNumCombs; ++iComb)
{
auto &combL = m_combsL[iComb];
auto &combR = m_combsR[iComb];
combL.SetDampening(dampening);
combR.SetDampening(dampening);
outL += combL.Apply(monaural, roomSize);
outR += combR.Apply(monaural, roomSize);
}
// Apply remaining all pass filters in series
for (unsigned iAllPass = 0; iAllPass < kReverbNumAllPasses; ++iAllPass)
{
auto &passL = m_allPassesL[iAllPass];
auto &passR = m_allPassesR[iAllPass];
outL = passL.Apply(outL, kAllPassDefFeedback);
outR = passR.Apply(outR, kAllPassDefFeedback);
}
// Mix
const float left = outL*wet1 + outR*wet2 + inL*dry;
const float right = outR*wet1 + outL*wet2 + inR*dry;
*pLeft++ = left;
*pRight++ = right;
}
}
}