comparison src/modplug/snd_dsp.cxx @ 136:6b5a52635b3b trunk

[svn] - like with so many other things, modplug is now maintained by us.
author nenolod
date Sun, 29 Oct 2006 01:04:52 -0700
parents
children 032053ca08ab 3673c7ec4ea2
comparison
equal deleted inserted replaced
135:33d24bd94ccc 136:6b5a52635b3b
1 /*
2 * This source code is public domain.
3 *
4 * Authors: Olivier Lapicque <olivierl@jps.net>
5 */
6
7 #include "stdafx.h"
8 #include "sndfile.h"
9
10 #ifdef MODPLUG_FASTSOUNDLIB
11 #define MODPLUG_NO_REVERB
12 #endif
13
14
15 // Delayed Surround Filters
16 #ifndef MODPLUG_FASTSOUNDLIB
17 #define nDolbyHiFltAttn 6
18 #define nDolbyHiFltMask 3
19 #define DOLBYATTNROUNDUP 31
20 #else
21 #define nDolbyHiFltAttn 3
22 #define nDolbyHiFltMask 3
23 #define DOLBYATTNROUNDUP 3
24 #endif
25
26 // Bass Expansion
27 #define XBASS_DELAY 14 // 2.5 ms
28
29 // Buffer Sizes
30 #define XBASSBUFFERSIZE 64 // 2 ms at 50KHz
31 #define FILTERBUFFERSIZE 64 // 1.25 ms
32 #define SURROUNDBUFFERSIZE ((MAX_SAMPLE_RATE * 50) / 1000)
33 #define REVERBBUFFERSIZE ((MAX_SAMPLE_RATE * 200) / 1000)
34 #define REVERBBUFFERSIZE2 ((REVERBBUFFERSIZE*13) / 17)
35 #define REVERBBUFFERSIZE3 ((REVERBBUFFERSIZE*7) / 13)
36 #define REVERBBUFFERSIZE4 ((REVERBBUFFERSIZE*7) / 19)
37
38
39 // DSP Effects: PUBLIC members
40 UINT CSoundFile::m_nXBassDepth = 6;
41 UINT CSoundFile::m_nXBassRange = XBASS_DELAY;
42 UINT CSoundFile::m_nReverbDepth = 1;
43 UINT CSoundFile::m_nReverbDelay = 100;
44 UINT CSoundFile::m_nProLogicDepth = 12;
45 UINT CSoundFile::m_nProLogicDelay = 20;
46
47 ////////////////////////////////////////////////////////////////////
48 // DSP Effects internal state
49
50 // Bass Expansion: low-pass filter
51 static LONG nXBassSum = 0;
52 static LONG nXBassBufferPos = 0;
53 static LONG nXBassDlyPos = 0;
54 static LONG nXBassMask = 0;
55
56 // Noise Reduction: simple low-pass filter
57 static LONG nLeftNR = 0;
58 static LONG nRightNR = 0;
59
60 // Surround Encoding: 1 delay line + low-pass filter + high-pass filter
61 static LONG nSurroundSize = 0;
62 static LONG nSurroundPos = 0;
63 static LONG nDolbyDepth = 0;
64 static LONG nDolbyLoDlyPos = 0;
65 static LONG nDolbyLoFltPos = 0;
66 static LONG nDolbyLoFltSum = 0;
67 static LONG nDolbyHiFltPos = 0;
68 static LONG nDolbyHiFltSum = 0;
69
70 // Reverb: 4 delay lines + high-pass filter + low-pass filter
71 #ifndef MODPLUG_NO_REVERB
72 static LONG nReverbSize = 0;
73 static LONG nReverbBufferPos = 0;
74 static LONG nReverbSize2 = 0;
75 static LONG nReverbBufferPos2 = 0;
76 static LONG nReverbSize3 = 0;
77 static LONG nReverbBufferPos3 = 0;
78 static LONG nReverbSize4 = 0;
79 static LONG nReverbBufferPos4 = 0;
80 static LONG nReverbLoFltSum = 0;
81 static LONG nReverbLoFltPos = 0;
82 static LONG nReverbLoDlyPos = 0;
83 static LONG nFilterAttn = 0;
84 static LONG gRvbLowPass[8];
85 static LONG gRvbLPPos = 0;
86 static LONG gRvbLPSum = 0;
87 static LONG ReverbLoFilterBuffer[XBASSBUFFERSIZE];
88 static LONG ReverbLoFilterDelay[XBASSBUFFERSIZE];
89 static LONG ReverbBuffer[REVERBBUFFERSIZE];
90 static LONG ReverbBuffer2[REVERBBUFFERSIZE2];
91 static LONG ReverbBuffer3[REVERBBUFFERSIZE3];
92 static LONG ReverbBuffer4[REVERBBUFFERSIZE4];
93 #endif
94 static LONG XBassBuffer[XBASSBUFFERSIZE];
95 static LONG XBassDelay[XBASSBUFFERSIZE];
96 static LONG DolbyLoFilterBuffer[XBASSBUFFERSIZE];
97 static LONG DolbyLoFilterDelay[XBASSBUFFERSIZE];
98 static LONG DolbyHiFilterBuffer[FILTERBUFFERSIZE];
99 static LONG SurroundBuffer[SURROUNDBUFFERSIZE];
100
101 // Access the main temporary mix buffer directly: avoids an extra pointer
102 extern int MixSoundBuffer[MIXBUFFERSIZE*2];
103 //cextern int MixReverbBuffer[MIXBUFFERSIZE*2];
104 extern int MixReverbBuffer[MIXBUFFERSIZE*2];
105
106 static UINT GetMaskFromSize(UINT len)
107 //-----------------------------------
108 {
109 UINT n = 2;
110 while (n <= len) n <<= 1;
111 return ((n >> 1) - 1);
112 }
113
114
115 void CSoundFile::InitializeDSP(BOOL bReset)
116 //-----------------------------------------
117 {
118 if (!m_nReverbDelay) m_nReverbDelay = 100;
119 if (!m_nXBassRange) m_nXBassRange = XBASS_DELAY;
120 if (!m_nProLogicDelay) m_nProLogicDelay = 20;
121 if (m_nXBassDepth > 8) m_nXBassDepth = 8;
122 if (m_nXBassDepth < 2) m_nXBassDepth = 2;
123 if (bReset)
124 {
125 // Noise Reduction
126 nLeftNR = nRightNR = 0;
127 }
128 // Pro-Logic Surround
129 nSurroundPos = nSurroundSize = 0;
130 nDolbyLoFltPos = nDolbyLoFltSum = nDolbyLoDlyPos = 0;
131 nDolbyHiFltPos = nDolbyHiFltSum = 0;
132 if (gdwSoundSetup & SNDMIX_SURROUND)
133 {
134 memset(DolbyLoFilterBuffer, 0, sizeof(DolbyLoFilterBuffer));
135 memset(DolbyHiFilterBuffer, 0, sizeof(DolbyHiFilterBuffer));
136 memset(DolbyLoFilterDelay, 0, sizeof(DolbyLoFilterDelay));
137 memset(SurroundBuffer, 0, sizeof(SurroundBuffer));
138 nSurroundSize = (gdwMixingFreq * m_nProLogicDelay) / 1000;
139 if (nSurroundSize > SURROUNDBUFFERSIZE) nSurroundSize = SURROUNDBUFFERSIZE;
140 if (m_nProLogicDepth < 8) nDolbyDepth = (32 >> m_nProLogicDepth) + 32;
141 else nDolbyDepth = (m_nProLogicDepth < 16) ? (8 + (m_nProLogicDepth - 8) * 7) : 64;
142 nDolbyDepth >>= 2;
143 }
144 // Reverb Setup
145 #ifndef MODPLUG_NO_REVERB
146 if (gdwSoundSetup & SNDMIX_REVERB)
147 {
148 UINT nrs = (gdwMixingFreq * m_nReverbDelay) / 1000;
149 UINT nfa = m_nReverbDepth+1;
150 if (nrs > REVERBBUFFERSIZE) nrs = REVERBBUFFERSIZE;
151 if ((bReset) || (nrs != (UINT)nReverbSize) || (nfa != (UINT)nFilterAttn))
152 {
153 nFilterAttn = nfa;
154 nReverbSize = nrs;
155 nReverbBufferPos = nReverbBufferPos2 = nReverbBufferPos3 = nReverbBufferPos4 = 0;
156 nReverbLoFltSum = nReverbLoFltPos = nReverbLoDlyPos = 0;
157 gRvbLPSum = gRvbLPPos = 0;
158 nReverbSize2 = (nReverbSize * 13) / 17;
159 if (nReverbSize2 > REVERBBUFFERSIZE2) nReverbSize2 = REVERBBUFFERSIZE2;
160 nReverbSize3 = (nReverbSize * 7) / 13;
161 if (nReverbSize3 > REVERBBUFFERSIZE3) nReverbSize3 = REVERBBUFFERSIZE3;
162 nReverbSize4 = (nReverbSize * 7) / 19;
163 if (nReverbSize4 > REVERBBUFFERSIZE4) nReverbSize4 = REVERBBUFFERSIZE4;
164 memset(ReverbLoFilterBuffer, 0, sizeof(ReverbLoFilterBuffer));
165 memset(ReverbLoFilterDelay, 0, sizeof(ReverbLoFilterDelay));
166 memset(ReverbBuffer, 0, sizeof(ReverbBuffer));
167 memset(ReverbBuffer2, 0, sizeof(ReverbBuffer2));
168 memset(ReverbBuffer3, 0, sizeof(ReverbBuffer3));
169 memset(ReverbBuffer4, 0, sizeof(ReverbBuffer4));
170 memset(gRvbLowPass, 0, sizeof(gRvbLowPass));
171 }
172 } else nReverbSize = 0;
173 #endif
174 BOOL bResetBass = FALSE;
175 // Bass Expansion Reset
176 if (gdwSoundSetup & SNDMIX_MEGABASS)
177 {
178 UINT nXBassSamples = (gdwMixingFreq * m_nXBassRange) / 10000;
179 if (nXBassSamples > XBASSBUFFERSIZE) nXBassSamples = XBASSBUFFERSIZE;
180 UINT mask = GetMaskFromSize(nXBassSamples);
181 if ((bReset) || (mask != (UINT)nXBassMask))
182 {
183 nXBassMask = mask;
184 bResetBass = TRUE;
185 }
186 } else
187 {
188 nXBassMask = 0;
189 bResetBass = TRUE;
190 }
191 if (bResetBass)
192 {
193 nXBassSum = nXBassBufferPos = nXBassDlyPos = 0;
194 memset(XBassBuffer, 0, sizeof(XBassBuffer));
195 memset(XBassDelay, 0, sizeof(XBassDelay));
196 }
197 }
198
199
200 void CSoundFile::ProcessStereoDSP(int count)
201 //------------------------------------------
202 {
203 #ifndef MODPLUG_NO_REVERB
204 // Reverb
205 if (gdwSoundSetup & SNDMIX_REVERB)
206 {
207 int *pr = MixSoundBuffer, *pin = MixReverbBuffer, rvbcount = count;
208 do
209 {
210 int echo = ReverbBuffer[nReverbBufferPos] + ReverbBuffer2[nReverbBufferPos2]
211 + ReverbBuffer3[nReverbBufferPos3] + ReverbBuffer4[nReverbBufferPos4]; // echo = reverb signal
212 // Delay line and remove Low Frequencies // v = original signal
213 int echodly = ReverbLoFilterDelay[nReverbLoDlyPos]; // echodly = delayed signal
214 ReverbLoFilterDelay[nReverbLoDlyPos] = echo >> 1;
215 nReverbLoDlyPos++;
216 nReverbLoDlyPos &= 0x1F;
217 int n = nReverbLoFltPos;
218 nReverbLoFltSum -= ReverbLoFilterBuffer[n];
219 int tmp = echo / 128;
220 ReverbLoFilterBuffer[n] = tmp;
221 nReverbLoFltSum += tmp;
222 echodly -= nReverbLoFltSum;
223 nReverbLoFltPos = (n + 1) & 0x3F;
224 // Reverb
225 int v = (pin[0]+pin[1]) >> nFilterAttn;
226 pr[0] += pin[0] + echodly;
227 pr[1] += pin[1] + echodly;
228 v += echodly >> 2;
229 ReverbBuffer3[nReverbBufferPos3] = v;
230 ReverbBuffer4[nReverbBufferPos4] = v;
231 v += echodly >> 4;
232 v >>= 1;
233 gRvbLPSum -= gRvbLowPass[gRvbLPPos];
234 gRvbLPSum += v;
235 gRvbLowPass[gRvbLPPos] = v;
236 gRvbLPPos++;
237 gRvbLPPos &= 7;
238 int vlp = gRvbLPSum >> 2;
239 ReverbBuffer[nReverbBufferPos] = vlp;
240 ReverbBuffer2[nReverbBufferPos2] = vlp;
241 if (++nReverbBufferPos >= nReverbSize) nReverbBufferPos = 0;
242 if (++nReverbBufferPos2 >= nReverbSize2) nReverbBufferPos2 = 0;
243 if (++nReverbBufferPos3 >= nReverbSize3) nReverbBufferPos3 = 0;
244 if (++nReverbBufferPos4 >= nReverbSize4) nReverbBufferPos4 = 0;
245 pr += 2;
246 pin += 2;
247 } while (--rvbcount);
248 }
249 #endif
250 // Dolby Pro-Logic Surround
251 if (gdwSoundSetup & SNDMIX_SURROUND)
252 {
253 int *pr = MixSoundBuffer, n = nDolbyLoFltPos;
254 for (int r=count; r; r--)
255 {
256 int v = (pr[0]+pr[1]+DOLBYATTNROUNDUP) >> (nDolbyHiFltAttn+1);
257 #ifndef MODPLUG_FASTSOUNDLIB
258 v *= (int)nDolbyDepth;
259 #endif
260 // Low-Pass Filter
261 nDolbyHiFltSum -= DolbyHiFilterBuffer[nDolbyHiFltPos];
262 DolbyHiFilterBuffer[nDolbyHiFltPos] = v;
263 nDolbyHiFltSum += v;
264 v = nDolbyHiFltSum;
265 nDolbyHiFltPos++;
266 nDolbyHiFltPos &= nDolbyHiFltMask;
267 // Surround
268 int secho = SurroundBuffer[nSurroundPos];
269 SurroundBuffer[nSurroundPos] = v;
270 // Delay line and remove low frequencies
271 v = DolbyLoFilterDelay[nDolbyLoDlyPos]; // v = delayed signal
272 DolbyLoFilterDelay[nDolbyLoDlyPos] = secho; // secho = signal
273 nDolbyLoDlyPos++;
274 nDolbyLoDlyPos &= 0x1F;
275 nDolbyLoFltSum -= DolbyLoFilterBuffer[n];
276 int tmp = secho / 64;
277 DolbyLoFilterBuffer[n] = tmp;
278 nDolbyLoFltSum += tmp;
279 v -= nDolbyLoFltSum;
280 n++;
281 n &= 0x3F;
282 // Add echo
283 pr[0] += v;
284 pr[1] -= v;
285 if (++nSurroundPos >= nSurroundSize) nSurroundPos = 0;
286 pr += 2;
287 }
288 nDolbyLoFltPos = n;
289 }
290 // Bass Expansion
291 if (gdwSoundSetup & SNDMIX_MEGABASS)
292 {
293 int *px = MixSoundBuffer;
294 int xba = m_nXBassDepth+1, xbamask = (1 << xba) - 1;
295 int n = nXBassBufferPos;
296 for (int x=count; x; x--)
297 {
298 nXBassSum -= XBassBuffer[n];
299 int tmp0 = px[0] + px[1];
300 int tmp = (tmp0 + ((tmp0 >> 31) & xbamask)) >> xba;
301 XBassBuffer[n] = tmp;
302 nXBassSum += tmp;
303 int v = XBassDelay[nXBassDlyPos];
304 XBassDelay[nXBassDlyPos] = px[0];
305 px[0] = v + nXBassSum;
306 v = XBassDelay[nXBassDlyPos+1];
307 XBassDelay[nXBassDlyPos+1] = px[1];
308 px[1] = v + nXBassSum;
309 nXBassDlyPos = (nXBassDlyPos + 2) & nXBassMask;
310 px += 2;
311 n++;
312 n &= nXBassMask;
313 }
314 nXBassBufferPos = n;
315 }
316 // Noise Reduction
317 if (gdwSoundSetup & SNDMIX_NOISEREDUCTION)
318 {
319 int n1 = nLeftNR, n2 = nRightNR;
320 int *pnr = MixSoundBuffer;
321 for (int nr=count; nr; nr--)
322 {
323 int vnr = pnr[0] >> 1;
324 pnr[0] = vnr + n1;
325 n1 = vnr;
326 vnr = pnr[1] >> 1;
327 pnr[1] = vnr + n2;
328 n2 = vnr;
329 pnr += 2;
330 }
331 nLeftNR = n1;
332 nRightNR = n2;
333 }
334 }
335
336
337 void CSoundFile::ProcessMonoDSP(int count)
338 //----------------------------------------
339 {
340 #ifndef MODPLUG_NO_REVERB
341 // Reverb
342 if (gdwSoundSetup & SNDMIX_REVERB)
343 {
344 int *pr = MixSoundBuffer, rvbcount = count, *pin = MixReverbBuffer;
345 do
346 {
347 int echo = ReverbBuffer[nReverbBufferPos] + ReverbBuffer2[nReverbBufferPos2]
348 + ReverbBuffer3[nReverbBufferPos3] + ReverbBuffer4[nReverbBufferPos4]; // echo = reverb signal
349 // Delay line and remove Low Frequencies // v = original signal
350 int echodly = ReverbLoFilterDelay[nReverbLoDlyPos]; // echodly = delayed signal
351 ReverbLoFilterDelay[nReverbLoDlyPos] = echo >> 1;
352 nReverbLoDlyPos++;
353 nReverbLoDlyPos &= 0x1F;
354 int n = nReverbLoFltPos;
355 nReverbLoFltSum -= ReverbLoFilterBuffer[n];
356 int tmp = echo / 128;
357 ReverbLoFilterBuffer[n] = tmp;
358 nReverbLoFltSum += tmp;
359 echodly -= nReverbLoFltSum;
360 nReverbLoFltPos = (n + 1) & 0x3F;
361 // Reverb
362 int v = pin[0] >> (nFilterAttn-1);
363 *pr++ += pin[0] + echodly;
364 pin++;
365 v += echodly >> 2;
366 ReverbBuffer3[nReverbBufferPos3] = v;
367 ReverbBuffer4[nReverbBufferPos4] = v;
368 v += echodly >> 4;
369 v >>= 1;
370 gRvbLPSum -= gRvbLowPass[gRvbLPPos];
371 gRvbLPSum += v;
372 gRvbLowPass[gRvbLPPos] = v;
373 gRvbLPPos++;
374 gRvbLPPos &= 7;
375 int vlp = gRvbLPSum >> 2;
376 ReverbBuffer[nReverbBufferPos] = vlp;
377 ReverbBuffer2[nReverbBufferPos2] = vlp;
378 if (++nReverbBufferPos >= nReverbSize) nReverbBufferPos = 0;
379 if (++nReverbBufferPos2 >= nReverbSize2) nReverbBufferPos2 = 0;
380 if (++nReverbBufferPos3 >= nReverbSize3) nReverbBufferPos3 = 0;
381 if (++nReverbBufferPos4 >= nReverbSize4) nReverbBufferPos4 = 0;
382 } while (--rvbcount);
383 }
384 #endif
385 // Bass Expansion
386 if (gdwSoundSetup & SNDMIX_MEGABASS)
387 {
388 int *px = MixSoundBuffer;
389 int xba = m_nXBassDepth, xbamask = (1 << xba)-1;
390 int n = nXBassBufferPos;
391 for (int x=count; x; x--)
392 {
393 nXBassSum -= XBassBuffer[n];
394 int tmp0 = *px;
395 int tmp = (tmp0 + ((tmp0 >> 31) & xbamask)) >> xba;
396 XBassBuffer[n] = tmp;
397 nXBassSum += tmp;
398 int v = XBassDelay[nXBassDlyPos];
399 XBassDelay[nXBassDlyPos] = *px;
400 *px++ = v + nXBassSum;
401 nXBassDlyPos = (nXBassDlyPos + 2) & nXBassMask;
402 n++;
403 n &= nXBassMask;
404 }
405 nXBassBufferPos = n;
406 }
407 // Noise Reduction
408 if (gdwSoundSetup & SNDMIX_NOISEREDUCTION)
409 {
410 int n = nLeftNR;
411 int *pnr = MixSoundBuffer;
412 for (int nr=count; nr; pnr++, nr--)
413 {
414 int vnr = *pnr >> 1;
415 *pnr = vnr + n;
416 n = vnr;
417 }
418 nLeftNR = n;
419 }
420 }
421
422
423 /////////////////////////////////////////////////////////////////
424 // Clean DSP Effects interface
425
426 // [Reverb level 0(quiet)-100(loud)], [delay in ms, usually 40-200ms]
427 BOOL CSoundFile::SetReverbParameters(UINT nDepth, UINT nDelay)
428 //------------------------------------------------------------
429 {
430 if (nDepth > 100) nDepth = 100;
431 UINT gain = nDepth / 20;
432 if (gain > 4) gain = 4;
433 m_nReverbDepth = 4 - gain;
434 if (nDelay < 40) nDelay = 40;
435 if (nDelay > 250) nDelay = 250;
436 m_nReverbDelay = nDelay;
437 return TRUE;
438 }
439
440
441 // [XBass level 0(quiet)-100(loud)], [cutoff in Hz 20-100]
442 BOOL CSoundFile::SetXBassParameters(UINT nDepth, UINT nRange)
443 //-----------------------------------------------------------
444 {
445 if (nDepth > 100) nDepth = 100;
446 UINT gain = nDepth / 20;
447 if (gain > 4) gain = 4;
448 m_nXBassDepth = 8 - gain; // filter attenuation 1/256 .. 1/16
449 UINT range = nRange / 5;
450 if (range > 5) range -= 5; else range = 0;
451 if (nRange > 16) nRange = 16;
452 m_nXBassRange = 21 - range; // filter average on 0.5-1.6ms
453 return TRUE;
454 }
455
456
457 // [Surround level 0(quiet)-100(heavy)] [delay in ms, usually 5-50ms]
458 BOOL CSoundFile::SetSurroundParameters(UINT nDepth, UINT nDelay)
459 //--------------------------------------------------------------
460 {
461 UINT gain = (nDepth * 16) / 100;
462 if (gain > 16) gain = 16;
463 if (gain < 1) gain = 1;
464 m_nProLogicDepth = gain;
465 if (nDelay < 4) nDelay = 4;
466 if (nDelay > 50) nDelay = 50;
467 m_nProLogicDelay = nDelay;
468 return TRUE;
469 }
470
471 BOOL CSoundFile::SetWaveConfigEx(BOOL bSurround,BOOL bNoOverSampling,BOOL bReverb,BOOL hqido,BOOL bMegaBass,BOOL bNR,BOOL bEQ)
472 //----------------------------------------------------------------------------------------------------------------------------
473 {
474 DWORD d = gdwSoundSetup & ~(SNDMIX_SURROUND | SNDMIX_NORESAMPLING | SNDMIX_REVERB | SNDMIX_HQRESAMPLER | SNDMIX_MEGABASS | SNDMIX_NOISEREDUCTION | SNDMIX_EQ);
475 if (bSurround) d |= SNDMIX_SURROUND;
476 if (bNoOverSampling) d |= SNDMIX_NORESAMPLING;
477 if (bReverb) d |= SNDMIX_REVERB;
478 if (hqido) d |= SNDMIX_HQRESAMPLER;
479 if (bMegaBass) d |= SNDMIX_MEGABASS;
480 if (bNR) d |= SNDMIX_NOISEREDUCTION;
481 if (bEQ) d |= SNDMIX_EQ;
482 gdwSoundSetup = d;
483 InitPlayer(FALSE);
484 return TRUE;
485 }