reverb.c
10.1 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*====================================================================
* reverb.c
*
* Copyright 1993, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics,
* Inc.; the contents of this file may not be disclosed to third
* parties, copied or duplicated in any form, in whole or in part,
* without the prior written permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to
* restrictions as set forth in subdivision (c)(1)(ii) of the Rights
* in Technical Data and Computer Software clause at DFARS
* 252.227-7013, and/or in similar or successor clauses in the FAR,
* DOD or NASA FAR Supplement. Unpublished - rights reserved under the
* Copyright Laws of the United States.
*====================================================================*/
#include <libaudio.h>
#include "synthInternals.h"
#include <em.h>
#include <os.h>
#include <stdio.h>
/*
* all-pass macro
*/
#define ALL_PASS(in, out, coef) \
aSetBuffer(ptr++, 0, 0, 0, outCount<<1); \
aMix(ptr++, 0, coef, out, in); \
aMix(ptr++, 0, (unsigned short)(-(coef)), in, out);
void init_lpfilter(ALLowPass *lp)
{
int i;
float a = lp->fc;
lp->first = 1;
for (i=0; i<8; i++)
lp->fcvec.fccoef[i] = 0;
lp->fcvec.fccoef[i++] = (short)(a * 16384);
lp->fgain = (short)((1.0 - a) * 16384);
for (; i<16; i++){
a *= lp->fc;
lp->fcvec.fccoef[i] = (short)(a * 16384);
}
}
Acmd *_loadBuffer(ALFx *r, int sect, int buff, int count, Acmd *p);
Acmd *_saveBuffer(ALFx *r, int sect, int buff, int count, Acmd *p);
Acmd *_filterBuffer(ALFx *r, int sect, int buff, int count, Acmd *p);
/***********************************************************************
* Reverb filter public interfaces
***********************************************************************/
void alReverbNew(ALFx *r, ALFxId id, short *fxmem, int size, float rate)
{
int i,j, total_length = 0;
#define ms *(((int)rate/1000)&~0x7)
r->id = id;
switch ( id ) {
case AL_FX_SMALLROOM:
alFilterNew((ALFilter *) r, alSmallRoomPull, alReverbParam, AL_FX);
r->section_count = 3;
r->delay[0].length = 22 ms; /* length of delay */
r->delay[0].coef = 9830; /* coefficient for delay */
r->delay[1].length = 35 ms;
r->delay[1].coef = 3276;
r->delay[2].length = 66 ms;
r->delay[2].coef = 10000;
r->delay[0].base = fxmem; /* base address of delay */
r->delay[0].tap = r->delay[0].base; /* current position of tap */
r->delay[0].lpfilt.fc = 0; /* coef for 2 pole low pass */
r->delay[1].base = r->delay[0].base + r->delay[0].length;
r->delay[1].tap = r->delay[1].base;
r->delay[1].lpfilt.fc = 0;
r->delay[2].base = r->delay[1].base + r->delay[1].length;
r->delay[2].tap = r->delay[2].base;
r->delay[2].lpfilt.fc = .25;
total_length += r->delay[0].length;
total_length += r->delay[1].length;
total_length += r->delay[2].length;
break;
case AL_FX_BIGROOM:
alFilterNew((ALFilter *) r, alSmallRoomPull, alReverbParam, AL_FX);
r->section_count = 3;
r->delay[0].length = 42 ms;
r->delay[0].coef = 9830;
r->delay[1].length = 55 ms;
r->delay[1].coef = 9830;
r->delay[2].length = 86 ms;
r->delay[2].coef = 12000;
r->delay[0].base = fxmem;
r->delay[0].tap = r->delay[0].base;
r->delay[0].lpfilt.fc = 0;
r->delay[1].base = r->delay[0].base + r->delay[0].length;
r->delay[1].tap = r->delay[1].base;
r->delay[1].lpfilt.fc = 0;
r->delay[2].base = r->delay[1].base + r->delay[1].length;
r->delay[2].tap = r->delay[2].base;
r->delay[2].lpfilt.fc = .25;
total_length += r->delay[0].length;
total_length += r->delay[1].length;
total_length += r->delay[2].length;
break;
case AL_FX_ECHO:
alFilterNew((ALFilter *) r, alEchoPull, alReverbParam, AL_FX);
r->section_count = 1;
r->delay[0].length = 180 ms;
r->delay[0].coef = 0x6000;
r->delay[0].base = fxmem;
r->delay[0].tap = r->delay[0].base;
r->delay[0].lpfilt.fc = 0;
total_length += r->delay[0].length;
break;
default:
break;
}
if (total_length > size) {
/*
* total delay too long! zero coefficients to protect ears!
* FIXME: need a debug error ststement here
*/
for (i=0; i<r->section_count; i++)
r->delay[i].coef = 0;
return;
}
/*
* clear delay memory
*/
for (i=0; i < r->section_count; i++){
for (j=0; j < r->delay[i].length; j++){
r->delay[i].base[j] = 0;
}
init_lpfilter(&r->delay[i].lpfilt);
}
r->motion = AL_PLAYING; /* FIXME: is this what we want? */
}
Acmd *alSmallRoomPull(void *filter, short *outp, int outCount, int flags,
Acmd *p)
{
Acmd *ptr = p;
ALFx *r = (ALFx *)filter;
ALFilter *source;
int i, after_end, before_end;
short *prev_ptr, *end_ptr;
/*
* pull channels going into this effect first
*/
if (source = r->filter.source) {
ptr = (*source->handler)(source, outp, outCount, flags, p);
} else {
/* error */
}
/*
* two all-passes in series inside a comb filter with a low-pass
*/
if (r->motion == AL_PLAYING) {
/*
* sum inputs attenuated by 3db
*/
aSetBuffer(ptr++, 0, 0, 0, outCount<<1);
aMix(ptr++, 0, 0xda83, AL_AUX_L_OUT, AL_AUX_L_OUT);
aMix(ptr++, 0, 0x5a82, AL_AUX_R_OUT, AL_AUX_L_OUT);
ptr = _loadBuffer(r, 2, AL_AUX_R_OUT, outCount, ptr);
ptr = _filterBuffer(r, 2, AL_AUX_R_OUT, outCount, ptr);
aMix(ptr++, 0, r->delay[2].coef, AL_AUX_R_OUT, AL_AUX_L_OUT);
ptr = _loadBuffer(r, 0, AL_AUX_R_OUT, outCount, ptr);
ALL_PASS(AL_AUX_L_OUT, AL_AUX_R_OUT, r->delay[0].coef);
ptr = _saveBuffer(r, 0, AL_AUX_L_OUT, outCount, ptr);
ptr = _loadBuffer(r, 1, AL_AUX_L_OUT, outCount, ptr);
ALL_PASS(AL_AUX_R_OUT, AL_AUX_L_OUT, r->delay[1].coef);
ptr = _saveBuffer(r, 1, AL_AUX_R_OUT, outCount, ptr);
ptr = _saveBuffer(r, 2, AL_AUX_L_OUT, outCount, ptr);
aDMEMMove(ptr++, AL_AUX_L_OUT, AL_AUX_R_OUT, outCount<<1);
}
return ptr;
}
Acmd *alEchoPull(void *filter, short *outp, int outCount, int flags,
Acmd *p)
{
Acmd *ptr = p;
ALFx *r = (ALFx *)filter;
ALFilter *source;
int i, after_end, before_end;
short *curr_ptr;
short *prev_ptr, *end_ptr;
/*
* pull channels going into this effect first
*/
if (source = r->filter.source) {
ptr = (*source->handler)(source, outp, outCount, flags, p);
} else {
/* error */
}
if (r->motion == AL_PLAYING) {
/*
* sum inputs attenuated by 3db
*/
aSetBuffer(ptr++, 0, AL_AUX_R_OUT, AL_AUX_L_OUT, outCount<<1);
aMix(ptr++, 0, 0xda83, AL_AUX_L_OUT, AL_AUX_L_OUT);
aMix(ptr++, 0, 0x5a82, AL_AUX_R_OUT, AL_AUX_L_OUT);
ptr = _loadBuffer(r, 0, AL_AUX_R_OUT, outCount, ptr);
aMix(ptr++, 0, r->delay[0].coef, AL_AUX_R_OUT, AL_AUX_L_OUT);
ptr = _saveBuffer(r, 0, AL_AUX_L_OUT, outCount, ptr);
aDMEMMove(ptr++, AL_AUX_R_OUT, AL_AUX_L_OUT, outCount<<1);
}
return ptr;
}
int alReverbParam(void *filter, int paramID, void *param)
{
ALFilter *f = (ALFilter *) filter;
ALFx *r = (ALFx *) filter;
int pp = (int) param;
switch (paramID) {
case (AL_FILTER_SET_SOURCE):
f->source = (ALFilter *) param;
break;
case (AL_FILTER_SET_DECAY):
switch (r->id) {
case AL_FX_SMALLROOM:
case AL_FX_BIGROOM:
r->delay[2].coef = (short)param;
break;
case AL_FX_ECHO:
r->delay[0].coef = (short)param;
break;
default:
break;
}
break;
case (AL_FILTER_SET_FC):
switch (r->id) {
case AL_FX_SMALLROOM:
case AL_FX_BIGROOM:
r->delay[2].lpfilt.fc = (short)param;
break;
case AL_FX_ECHO:
r->delay[0].lpfilt.fc = (short)param;
break;
default:
break;
}
break;
case (AL_FILTER_START):
r->motion = AL_PLAYING;
break;
case (AL_FILTER_RESET):
r->motion = AL_STOPPED;
if (f->source)
(*f->source->setParam)(f->source, AL_FILTER_RESET, param);
break;
default:
/* ??? */
break;
}
return 0;
}
Acmd *_loadBuffer(ALFx *r, int sect, int buff, int count, Acmd *p)
{
Acmd *ptr = p;
int after_end, before_end;
short *curr_ptr, *end_ptr;
curr_ptr = r->delay[sect].tap + count;
end_ptr = r->delay[sect].base + r->delay[sect].length;
if (curr_ptr > end_ptr) {
after_end = curr_ptr - end_ptr;
before_end = count - after_end;
aSetBuffer(ptr++, 0, buff, 0, before_end<<1);
aLoadBuffer(ptr++, osVirtualToPhysical(r->delay[sect].tap));
aSetBuffer(ptr++, 0, buff+(before_end<<1), 0, after_end<<1);
aLoadBuffer(ptr++, osVirtualToPhysical(r->delay[sect].base));
} else {
aSetBuffer(ptr++, 0, buff, 0, count<<1);
aLoadBuffer(ptr++, osVirtualToPhysical(r->delay[sect].tap));
}
return ptr;
}
Acmd *_saveBuffer(ALFx *r, int sect, int buff, int count, Acmd *p)
{
Acmd *ptr = p;
int after_end, before_end;
short *curr_ptr, *end_ptr;
curr_ptr = r->delay[sect].tap + count;
end_ptr = r->delay[sect].base + r->delay[sect].length;
if (curr_ptr > end_ptr) {
after_end = curr_ptr - end_ptr;
before_end = count - after_end;
aSetBuffer(ptr++, 0, 0, buff, before_end<<1);
aSaveBuffer(ptr++, osVirtualToPhysical(r->delay[sect].tap));
aSetBuffer(ptr++, 0, 0, buff+(before_end<<1), after_end<<1);
aSaveBuffer(ptr++, osVirtualToPhysical(r->delay[sect].base));
r->delay[sect].tap = r->delay[sect].base + after_end;
} else {
aSetBuffer(ptr++, 0, 0, buff, count<<1);
aSaveBuffer(ptr++, osVirtualToPhysical(r->delay[sect].tap));
r->delay[sect].tap = curr_ptr;
}
return ptr;
}
Acmd *_filterBuffer(ALFx *r, int sect, int buff, int count, Acmd *p)
{
Acmd *ptr = p;
aSetBuffer(ptr++, 0, buff, buff, count<<1);
aLoadADPCM(ptr++, sizeof(POLEF_STATE),
osVirtualToPhysical(&r->delay[sect].lpfilt.fcvec.fccoef));
if (r->delay[sect].lpfilt.first){
r->delay[sect].lpfilt.first = 0;
aPoleFilter(ptr++, A_INIT, r->delay[sect].lpfilt.fgain,
osVirtualToPhysical(&r->delay[sect].lpfilt.fstate.astate));
} else {
aPoleFilter(ptr++, 0, r->delay[sect].lpfilt.fgain,
osVirtualToPhysical(&r->delay[sect].lpfilt.fstate.astate));
}
return ptr;
}