aboutsummaryrefslogtreecommitdiff
path: root/Src/Winamp/eq10dsp.cpp
blob: 5f8f013739a9db09aaa2cb9d153b88f5c3d6c988 (plain) (blame)
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
/*****************************************

   EQ10 library version 1.0
   Copyright (C)2002 4Front Technologies
   Written by George Yohng

   http://www.opensound.com
   
   Proprietary software.
 
 *****************************************/
#include "main.h"
#include "eq10dsp.h"

//#include <stdio.h>
//#include <string.h>
#include <math.h>
#include "WinampAttributes.h"

#define DENORMAL_FIX // comment this for no denormal fixes

char _eq10_copyright[]=
"EQ10 Library version 1.0\n"
"Copyright (C)2002 4Front Technologies  http://www.opensound.com\n"
"Copyright (C)2001-2002 by George Yohng http://www.yohng.com\n\0"
"EQ10 ENGINE";

static double eq10_freq[EQ10_NOFBANDS]={ 70, 180, 320, 600, 1000, 3000, 6000, 12000, 14000, 16000 }; // winamp style frequency table;
static double eq10_freq_iso[EQ10_NOFBANDS]={31,62,125,250,500,1000,2000,4000,8000,16000}; // ISO frequency table

#ifdef EQ10_DQ
static double eq10_q[EQ10_NOFBANDS]=EQ10_DQ;
#endif


static void eq10_bsetup2(int u,double rate,eq10band_t *band,double freq,double Q)
{
    double angle;
    double a0,/*a1,a2,*/b0,b1,b2,alpha;

    if (rate<4000.0)     rate=4000.0;
    if (rate>384000.0)   rate=384000.0;
    if (freq<20.0)       freq=20.0;
    if (freq>=(rate*0.499)) {band->ua0=band->da0=0;return;}

    angle = 2.0*3.1415926535897932384626433832795*freq/rate;
    alpha = sin(angle)/(2.0*Q);

    b0 = 1.0/(1.0+alpha); 
    a0 = b0*alpha;  
    b1 = b0*2*cos(angle); 
    b2 = b0*(alpha-1);

    if (u>0) 
    {
        band->ua0=a0;
        band->ub1=b1;
        band->ub2=b2;
    }
		else
    {
        band->da0=a0;
        band->db1=b1;
        band->db2=b2;
    }
}

static void eq10_bsetup(double rate,eq10band_t *band,double freq,double Q)
{
    memset(band,0,sizeof(*band));
    eq10_bsetup2(-1,rate,band,freq,Q*0.5);
    eq10_bsetup2(1,rate,band,freq,Q*2.0);
#ifdef EQ10_DETECTOR_CODE
    /* release of detector */
    band->detectdecay=pow(0.001,1.0/(rate*EQ10_DETECTOR_RELEASE)); 
#endif
}

void eq10_setup(eq10_t *eq, int eqs, double rate)
{
    int t,k;

    for(k=0;k<eqs;k++,eq++)
    {
        for(t=0;t<EQ10_NOFBANDS;t++)
#ifndef EQ10_DQ
					eq10_bsetup(rate,&eq->band[t],(config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?eq10_freq[t]:eq10_freq_iso[t],EQ10_Q);
#else
            eq10_bsetup(rate,&eq->band[t],(config_eq_frequencies==EQ_FREQUENCIES_WINAMP)?eq10_freq[t]:eq10_freq_iso[t],eq10_q[t]);
#endif

        eq->detect=0;
        /* release of trimmer */
        eq->detectdecay=pow(0.001,1.0/(rate*EQ10_TRIM_RELEASE)); 
    }
}

void eq10_processf(eq10_t *eq,float *buf,float *outbuf,int sz,int idx,int step)
{
    int t,k;
    float *in,*out;
	if (!eq) return;
	
    buf+=idx;
    outbuf+=idx;
            
    in=buf;

    for(k=0;k<EQ10_NOFBANDS;k++)
    {
        double a0,b1,b2;
        double x1 = eq->band[k].x1;
        double x2 = eq->band[k].x2;
        double y1 = eq->band[k].y1;
        double y2 = eq->band[k].y2;
        double gain = eq->band[k].gain;

#ifdef EQ10_DETECTOR_CODE
        double detect = eq->band[k].detect;
        double detectdecay = eq->band[k].detectdecay;
#endif

        
        out = outbuf;

        if (gain>0.0)
        {
            a0 = eq->band[k].ua0*gain;
            b1 = eq->band[k].ub1;
            b2 = eq->band[k].ub2;
        }
				else
        {
            a0 = eq->band[k].da0*gain;
            b1 = eq->band[k].db1;
            b2 = eq->band[k].db2;
        }

        if (a0==0.0) continue;

        for(t=0;t<sz;t++,in+=step,out+=step)
        {
            double y0 = (in[0]-x2)*a0 + y1*b1 + y2*b2 

#ifdef DENORMAL_FIX            
            + 1e-30;
#else
            ;
#endif

#ifdef EQ10_DETECTOR_CODE
            if (fabs(y0)>detect) detect=fabs(y0);
            detect*=detectdecay;

#ifdef DENORMAL_FIX
            detect+=1e-30;
#endif

#endif
            x2=x1; x1=in[0]; y2=y1; y1=y0;

            out[0] = (float)(y0 + in[0]);
        }

        in=outbuf;

        eq->band[k].x1=x1;
        eq->band[k].x2=x2;
        eq->band[k].y1=y1;
        eq->band[k].y2=y2;

#ifdef EQ10_DETECTOR_CODE
        eq->band[k].detect=detect;
#endif
    }

		if (config_eq_limiter)
    {
        double detect=eq->detect;
        double  detectdecay=eq->detectdecay;
        out=outbuf;
        for(t=0;t<sz;t++,in+=step,out+=step)
        {
            /* *0.99 - reserve */
            if (fabs(in[0])>detect) detect=fabs(in[0]);


            if (detect>EQ10_TRIM_CODE) 
                out[0]=in[0]*(float)(EQ10_TRIM_CODE/detect);
            else
                out[0]=in[0];

            detect*=detectdecay;
#ifdef DENORMAL_FIX
            detect+=1e-30;
#endif
        }
        eq->detect=detect;
    }
		else if ((in==buf)&&(buf!=outbuf))
    { 
        out=outbuf;
        for(t=0;t<sz;t++,in+=step,out+=step) out[0]=in[0];
    }

}

double eq10_db2gain(double gain_dB)
{
    return pow(10.0,gain_dB/20.0)-1.0;
}

double eq10_gain2db(double gain)
{
    return 20.0*log10(gain+1.0);
}


void eq10_setgain(eq10_t *eq,int eqs,int bandnr,double gain_dB)
{
    double realgain;
    int k;

		if (!eq)
		return;

			realgain=eq10_db2gain(gain_dB);

    for(k=0;k<eqs;k++,eq++)
        eq->band[bandnr].gain=realgain;
}

double eq10_getgain(eq10_t *eq,int bandnr)
{
    return eq10_gain2db(eq->band[bandnr].gain);
}

double eq10_detect(eq10_t *eq,int bandnr)
{
#ifdef EQ10_DETECTOR_CODE
    return eq10_gain2db(eq->band[bandnr].detect);
#else
    return 0;
#endif    
}


#ifdef TESTCASE


eq10_t eq;

float buf1[4096] = {0};
float buf[4096] = {0};

int main()
{
    int t,k;
    eq10_setup(&eq,1,44100);

    for(t=0;t<4096;t++)
    {
        buf1[t]=warand()*(1.0/16384);
    }

    for(t=0;t<EQ10_NOFBANDS;t++) eq.band[t].gain=-0.874107;

    for(t=0;t<10000;t++)
    {
        eq10_processf(&eq,buf1,buf,4096,0,1); 
    }

    return 0;
}

#endif