aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/mt32/freeverb.cpp
blob: 181b878596b1e73c20e7341f27c6e3042a9b606c (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Allpass filter implementation
//
// Written by Jezar at Dreampoint, June 2000
// http://www.dreampoint.co.uk
// This code is public domain

#include "freeverb.h"

allpass::allpass()
{
	bufidx = 0;
}

void allpass::setbuffer(float *buf, int size)
{
	buffer = buf;
	bufsize = size;
}

void allpass::mute()
{
	for (int i=0; i<bufsize; i++)
		buffer[i]=0;
}

void allpass::setfeedback(float val)
{
	feedback = val;
}

float allpass::getfeedback()
{
	return feedback;
}

void allpass::deletebuffer()
{
	delete[] buffer;
	buffer = 0;
}
// Comb filter implementation
//
// Written by Jezar at Dreampoint, June 2000
// http://www.dreampoint.co.uk
// This code is public domain

comb::comb()
{
	filterstore = 0;
	bufidx = 0;
}

void comb::setbuffer(float *buf, int size)
{
	buffer = buf;
	bufsize = size;
}

void comb::mute()
{
	for (int i=0; i<bufsize; i++)
		buffer[i]=0;
}

void comb::setdamp(float val)
{
	damp1 = val;
	damp2 = 1-val;
}

float comb::getdamp()
{
	return damp1;
}

void comb::setfeedback(float val)
{
	feedback = val;
}

float comb::getfeedback()
{
	return feedback;
}

void comb::deletebuffer()
{
	delete[] buffer;
	buffer = 0;
}
// Reverb model implementation
//
// Written by Jezar at Dreampoint, June 2000
// Modifications by Jerome Fisher, 2009, 2011
// http://www.dreampoint.co.uk
// This code is public domain

revmodel::revmodel(float scaletuning)
{
	int i;
	int bufsize;

	// Allocate buffers for the components
	for (i = 0; i < numcombs; i++) {
		bufsize = int(scaletuning * combtuning[i]);
		combL[i].setbuffer(new float[bufsize], bufsize);
		bufsize += int(scaletuning * stereospread);
		combR[i].setbuffer(new float[bufsize], bufsize);
	}
	for (i = 0; i < numallpasses; i++) {
		bufsize = int(scaletuning * allpasstuning[i]);
		allpassL[i].setbuffer(new float[bufsize], bufsize);
		allpassL[i].setfeedback(0.5f);
		bufsize += int(scaletuning * stereospread);
		allpassR[i].setbuffer(new float[bufsize], bufsize);
		allpassR[i].setfeedback(0.5f);
	}

	// Set default values
	dry = initialdry;
	wet = initialwet*scalewet;
	damp = initialdamp*scaledamp;
	roomsize = (initialroom*scaleroom) + offsetroom;
	width = initialwidth;
	mode = initialmode;
	update();

	// Buffer will be full of rubbish - so we MUST mute them
	mute();
}

revmodel::~revmodel()
{
	int i;

	for (i = 0; i < numcombs; i++) {
		combL[i].deletebuffer();
		combR[i].deletebuffer();
	}
	for (i = 0; i < numallpasses; i++) {
		allpassL[i].deletebuffer();
		allpassR[i].deletebuffer();
	}
}

void revmodel::mute()
{
	int i;

	if (getmode() >= freezemode)
		return;

	for (i=0;i<numcombs;i++)
	{
		combL[i].mute();
		combR[i].mute();
	}
	for (i=0;i<numallpasses;i++)
	{
		allpassL[i].mute();
		allpassR[i].mute();
	}

	// Init LPF history
	filtprev1 = 0;
	filtprev2 = 0;
}

void revmodel::process(const float *inputL, const float *inputR, float *outputL, float *outputR, long numsamples)
{
	float outL,outR,input;

	while (numsamples-- > 0)
	{
		int i;

		outL = outR = 0;
		input = (*inputL + *inputR) * gain;

		// Implementation of 2-stage IIR single-pole low-pass filter
		// found at the entrance of reverb processing on real devices
		filtprev1 += (input - filtprev1) * filtval;
		filtprev2 += (filtprev1 - filtprev2) * filtval;
		input = filtprev2;

		int s = -1;
		// Accumulate comb filters in parallel
		for (i=0; i<numcombs; i++)
		{
			outL += s * combL[i].process(input);
			outR += s * combR[i].process(input);
			s = -s;
		}

		// Feed through allpasses in series
		for (i=0; i<numallpasses; i++)
		{
			outL = allpassL[i].process(outL);
			outR = allpassR[i].process(outR);
		}

		// Calculate output REPLACING anything already there
		*outputL = outL*wet1 + outR*wet2;
		*outputR = outR*wet1 + outL*wet2;

		inputL++;
		inputR++;
		outputL++;
		outputR++;
	}
}

void revmodel::update()
{
// Recalculate internal values after parameter change

	int i;

	wet1 = wet*(width/2 + 0.5f);
	wet2 = wet*((1-width)/2);

	if (mode >= freezemode)
	{
		roomsize1 = 1;
		damp1 = 0;
		gain = muted;
	}
	else
	{
		roomsize1 = roomsize;
		damp1 = damp;
		gain = fixedgain;
	}

	for (i=0; i<numcombs; i++)
	{
		combL[i].setfeedback(roomsize1);
		combR[i].setfeedback(roomsize1);
	}

	for (i=0; i<numcombs; i++)
	{
		combL[i].setdamp(damp1);
		combR[i].setdamp(damp1);
	}
}

// The following get/set functions are not inlined, because
// speed is never an issue when calling them, and also
// because as you develop the reverb model, you may
// wish to take dynamic action when they are called.

void revmodel::setroomsize(float value)
{
	roomsize = (value*scaleroom) + offsetroom;
	update();
}

float revmodel::getroomsize()
{
	return (roomsize-offsetroom)/scaleroom;
}

void revmodel::setdamp(float value)
{
	damp = value*scaledamp;
	update();
}

float revmodel::getdamp()
{
	return damp/scaledamp;
}

void revmodel::setwet(float value)
{
	wet = value*scalewet;
	update();
}

float revmodel::getwet()
{
	return wet/scalewet;
}

void revmodel::setdry(float value)
{
	dry = value*scaledry;
}

float revmodel::getdry()
{
	return dry/scaledry;
}

void revmodel::setwidth(float value)
{
	width = value;
	update();
}

float revmodel::getwidth()
{
	return width;
}

void revmodel::setmode(float value)
{
	mode = value;
	update();
}

float revmodel::getmode()
{
	if (mode >= freezemode)
		return 1;
	else
		return 0;
}

void revmodel::setfiltval(float value)
{
	filtval = value;
}