~ubuntu-branches/ubuntu/trusty/aeolus/trusty

« back to all changes in this revision

Viewing changes to reverb.cc

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2007-05-14 22:18:54 UTC
  • Revision ID: james.westby@ubuntu.com-20070514221854-274rj6fqs5tegu7q
Tags: upstream-0.6.6+2
ImportĀ upstreamĀ versionĀ 0.6.6+2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2003 Fons Adriaensen <fons.adriaensen@skynet.be>
 
3
    
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
*/
 
18
 
 
19
 
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <math.h>
 
23
#include "reverb.h"
 
24
 
 
25
 
 
26
void Delelm::init (int size, float fb)
 
27
{
 
28
    _size = size;
 
29
    _line = new float [size];
 
30
    memset (_line, 0, size * sizeof (float));
 
31
    _i = 0;
 
32
    _fb = fb;
 
33
    _slo = 0;
 
34
    _shi = 0;
 
35
}
 
36
 
 
37
 
 
38
void Delelm::fini (void)
 
39
{
 
40
    delete[] _line;
 
41
}
 
42
 
 
43
 
 
44
void Delelm::set_t60mf (float tmf)
 
45
{
 
46
    _gmf = powf (0.001f, _size / tmf);
 
47
 
48
 
 
49
 
 
50
void Delelm::set_t60lo (float tlo, float wlo)
 
51
{
 
52
    _glo = powf (0.001f, _size / tlo) / _gmf - 1.0f;
 
53
    _wlo = wlo;    
 
54
}
 
55
 
 
56
 
 
57
void Delelm::set_t60hi (float thi, float chi)
 
58
{
 
59
    float g, t;
 
60
 
 
61
    g = powf (0.001f, _size / thi) / _gmf;
 
62
    t = (1 - g * g) / (2 * g * g * chi);
 
63
    _whi = (sqrt (1 + 4 * t) - 1) / (2 * t); 
 
64
 
65
 
 
66
 
 
67
float Delelm::process (float x)
 
68
{
 
69
    float t;
 
70
 
 
71
    t = _line [_i] * _gmf;
 
72
    _slo += _wlo * (t - _slo);
 
73
    t += _glo * _slo;
 
74
    _shi += _whi * (t - _shi);
 
75
    t = x - _fb * _shi + 1e-10;
 
76
    _line [_i] = t;
 
77
    if (++_i == _size) _i = 0;
 
78
    return _shi + _fb * t;
 
79
}
 
80
 
 
81
 
 
82
void Delelm::print (void)
 
83
{
 
84
    printf ("%5d %6.3lf   %5.3lf %5.3lf   %6.4lf %6.4lf\n",
 
85
            _size, _fb, _glo, _gmf, _wlo, _whi); 
 
86
}
 
87
 
 
88
 
 
89
int Reverb::_sizes [16] = 
 
90
 
91
     839,  6732 -  839,
 
92
    1181,  7339 - 1181,
 
93
    1229,  8009 - 1229,
 
94
    2477,  8731 - 2477,
 
95
    2731,  9521 - 2731,
 
96
    1361, 10381 - 1361,
 
97
    3203, 11321 - 3203,
 
98
    1949, 12347 - 1949
 
99
};
 
100
 
 
101
 
 
102
float Reverb::_feedb [16] = 
 
103
{  
 
104
   -0.6f,  0.1f,
 
105
    0.6f,  0.1f,
 
106
    0.6f,  0.1f,
 
107
   -0.6f,  0.1f,
 
108
    0.6f,  0.1f,
 
109
   -0.6f,  0.1f,
 
110
   -0.6f,  0.1f,
 
111
    0.6f,  0.1f
 
112
};
 
113
 
 
114
 
 
115
void Reverb::init (float rate)
 
116
{
 
117
    int m;
 
118
 
 
119
    _rate = rate;
 
120
    _size = (int)(0.15f * rate);
 
121
    _line = new float [_size];
 
122
    memset (_line, 0, _size * sizeof (float));
 
123
    _i = 0;
 
124
    m = (rate < 64e3) ? 1 : 2;    
 
125
    for (int i = 0; i < 16; i++) _delm [i].init (m * _sizes [i], _feedb [i]);
 
126
    _x0 = _x1 = _x2 = _x3 = _x4 = _x5 = _x6 = _x7 = _z = 0;
 
127
    set_delay (0.05);
 
128
    set_t60mf (4.0f);
 
129
    set_t60lo (5.0f, 250.0f);
 
130
    set_t60hi (2.0f, 4e3f);
 
131
}
 
132
 
 
133
 
 
134
void Reverb::fini (void)
 
135
{
 
136
    delete[] _line;
 
137
    for (int i = 0; i < 16; i++) _delm [i].fini ();
 
138
}
 
139
 
 
140
 
 
141
void Reverb::set_delay (float del)
 
142
{
 
143
    if (del < 0.01f) del = 0.01f;
 
144
    _idel = (int)(_rate * del);
 
145
    if (_idel > _size) _idel = _size;
 
146
}
 
147
 
 
148
 
 
149
void Reverb::set_t60mf (float tmf)
 
150
{
 
151
    float t;
 
152
 
 
153
    _tmf = tmf;
 
154
    t = tmf * _rate;
 
155
    for (int i = 0; i < 16; i++) _delm [i].set_t60mf (t);
 
156
    _gain = 1.0f / sqrtf (tmf);
 
157
}
 
158
 
 
159
 
 
160
void Reverb::set_t60lo (float tlo, float flo)
 
161
{
 
162
    float t, w;
 
163
 
 
164
    _tlo = tlo;
 
165
    _flo = flo;
 
166
    t = tlo * _rate;
 
167
    w = 2 * M_PI * flo / _rate;
 
168
    for (int i = 0; i < 16; i++) _delm [i].set_t60lo (t, w);
 
169
}
 
170
 
 
171
 
 
172
void Reverb::set_t60hi (float thi, float fhi)
 
173
{
 
174
    float t, c;
 
175
 
 
176
    _thi = thi;
 
177
    _fhi = fhi;
 
178
    t = thi * _rate;
 
179
    c = 1 - cosf (2 * M_PI * fhi / _rate);
 
180
    for (int i = 0; i < 16; i++) _delm [i].set_t60hi (t, c);
 
181
}
 
182
 
 
183
 
 
184
void Reverb::print (void)
 
185
{
 
186
    for (int i = 0; i < 16; i++) _delm [i].print ();
 
187
}
 
188
 
 
189
 
 
190
void Reverb::process (int n, float gain, float *R, float *W, float *X, float *Y, float *Z)
 
191
{       
 
192
    int   i, j;
 
193
    float t, g, x;
 
194
 
 
195
    g = sqrtf (0.125f);
 
196
    gain *= _gain;
 
197
 
 
198
    i = _i;
 
199
    while (n--)   
 
200
    {
 
201
        j = i - _idel;
 
202
        if (j < 0) j += _size;
 
203
        x = _line [j];
 
204
        _z += 0.6f * (*R++ - _z) + 1e-10f;
 
205
        _line [i] = _z;
 
206
        if (++i == _size) i = 0;
 
207
 
 
208
        _x0 = _delm  [0].process (g * _x0 + x); 
 
209
        _x1 = _delm  [2].process (g * _x1 + x); 
 
210
        _x2 = _delm  [4].process (g * _x2 + x); 
 
211
        _x3 = _delm  [6].process (g * _x3 + x); 
 
212
        _x4 = _delm  [8].process (g * _x4 + x); 
 
213
        _x5 = _delm [10].process (g * _x5 + x); 
 
214
        _x6 = _delm [12].process (g * _x6 + x); 
 
215
        _x7 = _delm [14].process (g * _x7 + x); 
 
216
 
 
217
        t = _x0 - _x1; _x0 += _x1;  _x1 = t;
 
218
        t = _x2 - _x3; _x2 += _x3;  _x3 = t;
 
219
        t = _x4 - _x5; _x4 += _x5;  _x5 = t;
 
220
        t = _x6 - _x7; _x6 += _x7;  _x7 = t;
 
221
 
 
222
        t = _x0 - _x2; _x0 += _x2;  _x2 = t;
 
223
        t = _x1 - _x3; _x1 += _x3;  _x3 = t;
 
224
        t = _x4 - _x6; _x4 += _x6;  _x6 = t;
 
225
        t = _x5 - _x7; _x5 += _x7;  _x7 = t;
 
226
 
 
227
        t = _x0 - _x4; _x0 += _x4;  _x4 = t;
 
228
        t = _x1 - _x5; _x1 += _x5;  _x5 = t;
 
229
        t = _x2 - _x6; _x2 += _x6;  _x6 = t;
 
230
        t = _x3 - _x7; _x3 += _x7;  _x7 = t;
 
231
 
 
232
        *W++ += 1.25f * gain * _x0; 
 
233
        *X++ += gain * (_x1 - 0.05f * _x2);
 
234
        *Y++ += gain * _x2;
 
235
        *Z++ += gain * _x4;
 
236
 
 
237
        _x0 = _delm  [1].process (_x0);
 
238
        _x1 = _delm  [3].process (_x1); 
 
239
        _x2 = _delm  [5].process (_x2); 
 
240
        _x3 = _delm  [7].process (_x3); 
 
241
        _x4 = _delm  [9].process (_x4); 
 
242
        _x5 = _delm [11].process (_x5); 
 
243
        _x6 = _delm [13].process (_x6); 
 
244
        _x7 = _delm [15].process (_x7); 
 
245
    }
 
246
    _i = i;
 
247
}
 
248