14
band pass filter for the Eq. This is a modification of Kai Lassfolk's work, as
15
removed from the Sound Processing Kit:
17
Sound Processing Kit - A C++ Class Library for Audio Signal Processing
18
Copyright (C) 1995-1998 Kai Lassfolk
20
This library is free software; you can redistribute it and/or
21
modify it under the terms of the GNU Library General Public
22
License as published by the Free Software Foundation; either
23
version 2 of the License, or (at your option) any later version.
25
This library is distributed in the hope that it will be useful,
26
but WITHOUT ANY WARRANTY; without even the implied warranty of
27
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28
Library General Public License for more details.
30
You should have received a copy of the GNU Library General Public
31
License along with this library; if not, write to the Free
32
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35
#define SAMPLERATE 44100
41
void BandPassInit(struct BandPassInfo *ip, float center, float bw)
46
ip->C = 1.0 / tan(M_PI * bw / SAMPLERATE);
47
ip->D = 2 * cos(2 * M_PI * center / SAMPLERATE);
49
ip->a[0] = 1.0 / (1.0 + ip->C);
53
ip->b[0] = -ip->C * ip->D * ip->a[0];
54
ip->b[1] = (ip->C - 1.0) * ip->a[0];
56
ip->bufferX[0] = ip->bufferX[1] = 0.0;
57
ip->bufferY[0] = ip->bufferY[1] = 0.0;
59
void BandPassSSE(struct BandPassInfo *ip, float *inbuffer, float *outbuffer, unsigned long samples)
64
"jz .l5 \n" /* if (!samples) */
67
"movups 0x10(%%ecx), %%xmm2 \n" /* ip->a[0] */
68
"shufps $0x00, %%xmm2, %%xmm2 \n" /* ip->a[0] all over xmm3 */
69
"movups 0x14(%%ecx), %%xmm4 \n" /* xmm4 = {ip->a[1], ip->a[2], ip->b} */
70
"movups 0x24(%%ecx), %%xmm5 \n" /* xmm5 = {ip->bufferX, ip->bufferY} */
71
"xorl %%ecx, %%ecx \n" /* i = 0 */
72
"movl $1, %%edx \n" /* j = 1 */
76
"decl %%edx \n" /* --j */
77
"jnz .l4 \n" /* if (j) */
79
/* only load single values if less than four remain in inbuffer */
80
"testl $0xfffffffc, %0 \n"
82
"movss (%2, %%ecx, 4), %%xmm3\n"
86
/* {inbuffer[i], inbuffer[i+1], inbuffer[i+2], inbuffer[i+3]} * ip->a[0] */
87
"movups (%2, %%ecx, 4), %%xmm3\n"
88
"movl $3, %%edx \n" /* j = 3 */
90
"movaps %%xmm3, %%xmm6 \n"
91
"mulps %%xmm2, %%xmm3 \n"
94
/* {ip->a[1], ip->a[2], ip->b} * {ip->bufferX, ip->bufferY} */
95
"movaps %%xmm4, %%xmm0 \n"
96
"mulps %%xmm5, %%xmm0 \n"
97
"movaps %%xmm0, %%xmm1 \n"
98
/* xmm0 = {xmm0[0] + xmm0[1], <unused>, xmm0[2] + xmm0[3], <unused>} */
99
"shufps $0xb1, %%xmm0, %%xmm1 \n"
100
"addps %%xmm1, %%xmm0 \n"
101
/* xmm0[0] -= xmm0[2] */
102
"movhlps %%xmm0, %%xmm1 \n"
103
"subss %%xmm1, %%xmm0 \n"
104
"addss %%xmm3, %%xmm0 \n" /* xmm0[0] += inbuffer[i] * ip->a[0] */
105
"movss %%xmm0, (%3, %%ecx, 4)\n" /* outbuffer[i] = xmm0[0] */
107
/* xmm5 = {inbuffer[i], xmm5[0], outbuffer[i], xmm5[2]} */
108
"shufps $0x24, %%xmm5, %%xmm0 \n"
109
"shufps $0x81, %%xmm0, %%xmm5 \n"
110
"movss %%xmm6, %%xmm5 \n"
112
/* right-shift xmm3 (inbuffer * ip->a[0]) and xmm6 (inbuffer) */
113
"shufps $0x39, %%xmm3, %%xmm3 \n"
114
"shufps $0x39, %%xmm6, %%xmm6 \n"
116
"incl %%ecx \n" /* ++i */
121
"movups %%xmm5, 0x24(%%ecx) \n" /* {ip->bufferX, ip->bufferY} = xmm5 */
125
: "r" (samples), /* %0 */
127
"r" (inbuffer), /* %2 */
128
"r" (outbuffer) /* %3 */
133
void BandPass(struct BandPassInfo *ip, float *inbuffer, float *outbuffer, unsigned long samples)
136
for (i=0; i<samples; ++i)
138
outbuffer[i] = ip->a[0] * inbuffer[i] + ip->a[1] * ip->bufferX[0] + ip->a[2]
139
* ip->bufferX[1] - ip->b[0] * ip->bufferY[0] - ip->b[1]
142
ip->bufferX[1] = ip->bufferX[0];
143
ip->bufferX[0] = inbuffer[i];
144
ip->bufferY[1] = ip->bufferY[0];
145
ip->bufferY[0] = outbuffer[i];
150
/*============================================================================
152
fftmisc.c - Don Cross <dcross@intersrv.com>
154
http://www.intersrv.com/~dcross/fft.html
156
Helper routines for Fast Fourier Transform implementation.
157
Contains common code for fft_float() and fft_double().
166
1998 September 19 [Don Cross]
167
Improved the efficiency of IsPowerOfTwo().
168
Updated coding standards.
170
============================================================================*/
173
#define BITS_PER_WORD (sizeof(unsigned) * 8)
176
static int IsPowerOfTwo ( unsigned x )
181
if ( x & (x-1) ) /* Thanks to 'byang' for this cute trick! */
188
static unsigned NumberOfBitsNeeded ( unsigned PowerOfTwo )
192
if ( PowerOfTwo < 2 )
196
">>> Error in fftmisc.c: argument %d to NumberOfBitsNeeded is too small.\n",
204
if ( PowerOfTwo & (1 << i) )
211
static unsigned ReverseBits ( unsigned index, unsigned NumBits )
215
for ( i=rev=0; i < NumBits; i++ )
217
rev = (rev << 1) | (index & 1);
225
static double Index_to_frequency ( unsigned NumSamples, unsigned Index )
227
if ( Index >= NumSamples )
229
else if ( Index <= NumSamples/2 )
230
return (double)Index / (double)NumSamples;
232
return -(double)(NumSamples-Index) / (double)NumSamples;
238
/*============================================================================
240
fourierf.c - Don Cross <dcross@intersrv.com>
242
http://www.intersrv.com/~dcross/fft.html
244
Contains definitions for doing Fourier transforms
245
and inverse Fourier transforms.
247
This module performs operations on arrays of 'float'.
251
1998 September 19 [Don Cross]
252
Updated coding standards.
253
Improved efficiency of trig calculations.
255
============================================================================*/
263
#define CHECKPOINTER(p) CheckPointer(p,#p)
265
static void CheckPointer ( const void *p, const char *name )
269
fprintf ( stderr, "Error in fft_float(): %s == NULL\n", name );
277
int InverseTransform,
283
unsigned NumBits; /* Number of bits needed to store indices */
285
unsigned BlockSize, BlockEnd;
287
double angle_numerator = 2.0 * DDC_PI;
288
double tr, ti; /* temp real, temp imaginary */
290
if ( !IsPowerOfTwo(NumSamples) )
294
"Error in fft(): NumSamples=%u is not power of two\n",
300
if ( InverseTransform )
301
angle_numerator = -angle_numerator;
303
CHECKPOINTER ( RealIn );
304
CHECKPOINTER ( RealOut );
305
CHECKPOINTER ( ImagOut );
307
NumBits = NumberOfBitsNeeded ( NumSamples );
310
** Do simultaneous data copy and bit-reversal ordering into outputs...
313
for ( i=0; i < NumSamples; i++ )
315
j = ReverseBits ( i, NumBits );
316
RealOut[j] = RealIn[i];
317
ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[i];
321
** Do the FFT itself...
325
for ( BlockSize = 2; BlockSize <= NumSamples; BlockSize <<= 1 )
327
double delta_angle = angle_numerator / (double)BlockSize;
328
double sm2 = sin ( -2 * delta_angle );
329
double sm1 = sin ( -delta_angle );
330
double cm2 = cos ( -2 * delta_angle );
331
double cm1 = cos ( -delta_angle );
335
for ( i=0; i < NumSamples; i += BlockSize )
343
for ( j=i, n=0; n < BlockEnd; j++, n++ )
345
ar[0] = w*ar[1] - ar[2];
349
ai[0] = w*ai[1] - ai[2];
354
tr = ar[0]*RealOut[k] - ai[0]*ImagOut[k];
355
ti = ar[0]*ImagOut[k] + ai[0]*RealOut[k];
357
RealOut[k] = RealOut[j] - tr;
358
ImagOut[k] = ImagOut[j] - ti;
365
BlockEnd = BlockSize;
369
** Need to normalize if inverse transform...
372
if ( InverseTransform )
374
double denom = (double)NumSamples;
376
for ( i=0; i < NumSamples; i++ )