aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/mt32/AReverbModel.cpp
blob: 151f6c2c81cf4b32160a771af9065ae4d7d70968 (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
/* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
 * Copyright (C) 2011 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 2.1 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mt32emu.h"
#include "AReverbModel.h"

namespace MT32Emu {

// Default reverb settings for modes 0-2

static const unsigned int NUM_ALLPASSES = 6;
static const unsigned int NUM_DELAYS = 5;

static const Bit32u MODE_0_ALLPASSES[] = {729, 78, 394, 994, 1250, 1889};
static const Bit32u MODE_0_DELAYS[] = {846, 4, 1819, 778, 346};
static const float MODE_0_TIMES[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.9f};
static const float MODE_0_LEVELS[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 1.01575f};

static const Bit32u MODE_1_ALLPASSES[] = {176, 809, 1324, 1258};
static const Bit32u MODE_1_DELAYS[] = {2262, 124, 974, 2516, 356};
static const float MODE_1_TIMES[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.95f};
static const float MODE_1_LEVELS[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 1.01575f};

static const Bit32u MODE_2_ALLPASSES[] = {78, 729, 994, 389};
static const Bit32u MODE_2_DELAYS[] = {846, 4, 1819, 778, 346};
static const float MODE_2_TIMES[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f};
static const float MODE_2_LEVELS[] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f};

const AReverbSettings AReverbModel::REVERB_MODE_0_SETTINGS = {MODE_0_ALLPASSES, MODE_0_DELAYS, MODE_0_TIMES, MODE_0_LEVELS, 0.687770909f, 0.5f, 0.5f};
const AReverbSettings AReverbModel::REVERB_MODE_1_SETTINGS = {MODE_1_ALLPASSES, MODE_1_DELAYS, MODE_1_TIMES, MODE_1_LEVELS, 0.712025098f, 0.375f, 0.625f};
const AReverbSettings AReverbModel::REVERB_MODE_2_SETTINGS = {MODE_2_ALLPASSES, MODE_2_DELAYS, MODE_2_TIMES, MODE_2_LEVELS, 0.939522749f, 0.0f, 0.0f};

RingBuffer::RingBuffer(Bit32u newsize) {
	index = 0;
	size = newsize;
	buffer = new float[size];
}

RingBuffer::~RingBuffer() {
	delete[] buffer;
	buffer = NULL;
	size = 0;
}

float RingBuffer::next() {
	index++;
	if (index >= size) {
		index = 0;
	}
	return buffer[index];
}

bool RingBuffer::isEmpty() {
	if (buffer == NULL) return true;

	float *buf = buffer;
	float total = 0;
	for (Bit32u i = 0; i < size; i++) {
		total += (*buf < 0 ? -*buf : *buf);
		buf++;
	}
	return ((total / size) < .0002 ? true : false);
}

void RingBuffer::mute() {
	float *buf = buffer;
	for (Bit32u i = 0; i < size; i++) {
		*buf++ = 0;
	}
}

AllpassFilter::AllpassFilter(Bit32u useSize) : RingBuffer(useSize) {
}

Delay::Delay(Bit32u useSize) : RingBuffer(useSize) {
}

float AllpassFilter::process(float in) {
	// This model corresponds to the allpass filter implementation in the real CM-32L device
	// found from sample analysis

	float out;

	out = next();

	// store input - feedback / 2
	buffer[index] = in - 0.5f * out;

	// return buffer output + feedforward / 2
	return out + 0.5f * buffer[index];
}

float Delay::process(float in) {
	// Implements a very simple delay

	float out;

	out = next();

	// store input
	buffer[index] = in;

	// return buffer output
	return out;
}

AReverbModel::AReverbModel(const AReverbSettings *useSettings) : allpasses(NULL), delays(NULL), currentSettings(useSettings) {
}

AReverbModel::~AReverbModel() {
	close();
}

void AReverbModel::open(unsigned int /*sampleRate*/) {
	// FIXME: filter sizes must be multiplied by sample rate to 32000Hz ratio
	// IIR filter values depend on sample rate as well
	allpasses = new AllpassFilter*[NUM_ALLPASSES];
	for (Bit32u i = 0; i < NUM_ALLPASSES; i++) {
		allpasses[i] = new AllpassFilter(currentSettings->allpassSizes[i]);
	}
	delays = new Delay*[NUM_DELAYS];
	for (Bit32u i = 0; i < NUM_DELAYS; i++) {
		delays[i] = new Delay(currentSettings->delaySizes[i]);
	}
	mute();
}

void AReverbModel::close() {
	if (allpasses != NULL) {
		for (Bit32u i = 0; i < NUM_ALLPASSES; i++) {
			if (allpasses[i] != NULL) {
				delete allpasses[i];
				allpasses[i] = NULL;
			}
		}
		delete[] allpasses;
		allpasses = NULL;
	}
	if (delays != NULL) {
		for (Bit32u i = 0; i < NUM_DELAYS; i++) {
			if (delays[i] != NULL) {
				delete delays[i];
				delays[i] = NULL;
			}
		}
		delete[] delays;
		delays = NULL;
	}
}

void AReverbModel::mute() {
	for (Bit32u i = 0; i < NUM_ALLPASSES; i++) {
		allpasses[i]->mute();
	}
	for (Bit32u i = 0; i < NUM_DELAYS; i++) {
		delays[i]->mute();
	}
	filterhist1 = 0;
	filterhist2 = 0;
	combhist = 0;
}

void AReverbModel::setParameters(Bit8u time, Bit8u level) {
// FIXME: wetLevel definitely needs ramping when changed
// Although, most games don't set reverb level during MIDI playback
	decayTime = currentSettings->decayTimes[time];
	wetLevel = currentSettings->wetLevels[level];
}

bool AReverbModel::isActive() const {
	bool bActive = false;
	for (Bit32u i = 0; i < NUM_ALLPASSES; i++) {
		bActive |= !allpasses[i]->isEmpty();
	}
	for (Bit32u i = 0; i < NUM_DELAYS; i++) {
		bActive |= !delays[i]->isEmpty();
	}
	return bActive;
}

void AReverbModel::process(const float *inLeft, const float *inRight, float *outLeft, float *outRight, unsigned long numSamples) {
// Three series allpass filters followed by a delay, fourth allpass filter and another delay
	float dry, link, outL1, outL2, outR1, outR2;

	for (unsigned long i = 0; i < numSamples; i++) {
		dry = *inLeft + *inRight;

		// Implementation of 2-stage IIR single-pole low-pass filter
		// found at the entrance of reverb processing on real devices
		filterhist1 += (dry - filterhist1) * currentSettings->filtVal;
		filterhist2 += (filterhist1 - filterhist2) * currentSettings->filtVal;

		link = allpasses[0]->process(-filterhist2);
		link = allpasses[1]->process(link);

		// this implements a comb filter cross-linked with the fourth allpass filter
		link += combhist * decayTime;
		link = allpasses[2]->process(link);
		link = delays[0]->process(link);
		outL1 = link;
		link = allpasses[3]->process(link);
		link = delays[1]->process(link);
		outR1 = link;
		link = allpasses[4]->process(link);
		link = delays[2]->process(link);
		outL2 = link;
		link = allpasses[5]->process(link);
		link = delays[3]->process(link);
		outR2 = link;
		link = delays[4]->process(link);

		// comb filter end point
		combhist = combhist * currentSettings->damp1 + link * currentSettings->damp2;

		*outLeft = (outL1 + outL2) * wetLevel;
		*outRight = (outR1 + outR2) * wetLevel;

		inLeft++;
		inRight++;
		outLeft++;
		outRight++;
	}
}

}