Mercurial > audlegacy
annotate src/audacious/af_equalizer.c @ 4633:3d5862f6c6c4
audtool needs AUDLDFLAGS too.
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 12 Jun 2008 13:42:09 +0900 |
parents | eb5f10afe3a0 |
children |
rev | line source |
---|---|
4300 | 1 /*============================================================================= |
2 // | |
3 // This software has been released under the terms of the GNU General Public | |
4 // license. See http://www.gnu.org/copyleft/gpl.html for details. | |
5 // | |
6 // Copyright 2001 Anders Johansson ajh@atri.curtin.edu.au | |
7 // | |
8 //============================================================================= | |
9 */ | |
10 | |
11 /* Equalizer filter, implementation of a 10 band time domain graphic | |
12 equalizer using IIR filters. The IIR filters are implemented using a | |
13 Direct Form II approach, but has been modified (b1 == 0 always) to | |
14 save computation. | |
15 */ | |
16 | |
17 #include <stdio.h> | |
18 #include <stdlib.h> | |
19 #include <string.h> | |
20 | |
21 #include <inttypes.h> | |
22 #include <math.h> | |
23 | |
24 #include "af_compat.h" | |
25 | |
26 #define L 2 // Storage for filter taps | |
27 #define KM 10 // Max number of bands | |
28 | |
29 #define Q 1.2247449 | |
30 /* Q value for band-pass filters 1.2247=(3/2)^(1/2) | |
31 gives 4dB suppression @ Fc*2 and Fc/2 */ | |
32 | |
33 /* Center frequencies for band-pass filters | |
34 The different frequency bands are: | |
35 nr. center frequency | |
36 0 31.25 Hz | |
37 1 62.50 Hz | |
38 2 125.0 Hz | |
39 3 250.0 Hz | |
40 4 500.0 Hz | |
41 5 1.000 kHz | |
42 6 2.000 kHz | |
43 7 4.000 kHz | |
44 8 8.000 kHz | |
45 9 16.00 kHz | |
46 */ | |
4304
eb5f10afe3a0
swith to original xmms band frequencies
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4300
diff
changeset
|
47 /*#define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000}*/ |
eb5f10afe3a0
swith to original xmms band frequencies
Eugene Zagidullin <e.asphyx@gmail.com>
parents:
4300
diff
changeset
|
48 #define CF {60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000} |
4300 | 49 |
50 // Maximum and minimum gain for the bands | |
51 #define G_MAX +12.0 | |
52 #define G_MIN -12.0 | |
53 | |
54 // Data for specific instances of this filter | |
55 typedef struct af_equalizer_s | |
56 { | |
57 float a[KM][L]; // A weights | |
58 float b[KM][L]; // B weights | |
59 float wq[AF_NCH][KM][L]; // Circular buffer for W data | |
60 float g[AF_NCH][KM]; // Gain factor for each channel and band | |
61 int K; // Number of used eq bands | |
62 int channels; // Number of channels | |
63 float gain_factor; // applied at output to avoid clipping | |
64 } af_equalizer_t; | |
65 | |
66 static int af_test_output(struct af_instance_s* af, af_data_t* out) | |
67 { | |
68 if((af->data->format != out->format) || | |
69 (af->data->bps != out->bps) || | |
70 (af->data->rate != out->rate) || | |
71 (af->data->nch != out->nch)){ | |
72 memcpy(out,af->data,sizeof(af_data_t)); | |
73 return AF_FALSE; | |
74 } | |
75 return AF_OK; | |
76 } | |
77 | |
78 // 2nd order Band-pass Filter design | |
79 static void bp2(float* a, float* b, float fc, float q){ | |
80 double th= 2.0 * M_PI * fc; | |
81 double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0)); | |
82 | |
83 a[0] = (1.0 + C) * cos(th); | |
84 a[1] = -1 * C; | |
85 | |
86 b[0] = (1.0 - C)/2.0; | |
87 b[1] = -1.0050; | |
88 } | |
89 | |
90 // Initialization and runtime control | |
91 static int control(struct af_instance_s* af, int cmd, void* arg) | |
92 { | |
93 af_equalizer_t* s = (af_equalizer_t*)af->setup; | |
94 | |
95 switch(cmd){ | |
96 case AF_CONTROL_REINIT:{ | |
97 int k =0, i =0; | |
98 float F[KM] = CF; | |
99 | |
100 s->gain_factor=0.0; | |
101 | |
102 // Sanity check | |
103 if(!arg) return AF_ERROR; | |
104 | |
105 af->data->rate = ((af_data_t*)arg)->rate; | |
106 af->data->nch = ((af_data_t*)arg)->nch; | |
107 af->data->format = AF_FORMAT_FLOAT_NE; | |
108 af->data->bps = 4; | |
109 | |
110 // Calculate number of active filters | |
111 s->K=KM; | |
112 while(F[s->K-1] > (float)af->data->rate/2.2) | |
113 s->K--; | |
114 | |
115 if(s->K != KM) | |
116 af_msg(AF_MSG_INFO,"[equalizer] Limiting the number of filters to" | |
117 " %i due to low sample rate.\n",s->K); | |
118 | |
119 // Generate filter taps | |
120 for(k=0;k<s->K;k++) | |
121 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q); | |
122 | |
123 // Calculate how much this plugin adds to the overall time delay | |
124 af->delay = 2 * af->data->nch * af->data->bps; | |
125 | |
126 // Calculate gain factor to prevent clipping at output | |
127 for(k=0;k<AF_NCH;k++) | |
128 { | |
129 for(i=0;i<KM;i++) | |
130 { | |
131 if(s->gain_factor < s->g[k][i]) s->gain_factor=s->g[k][i]; | |
132 } | |
133 } | |
134 | |
135 s->gain_factor=log10(s->gain_factor + 1.0) * 20.0; | |
136 | |
137 if(s->gain_factor > 0.0) | |
138 { | |
139 s->gain_factor=0.1+(s->gain_factor/12.0); | |
140 }else{ | |
141 s->gain_factor=1; | |
142 } | |
143 | |
144 return af_test_output(af,arg); | |
145 } | |
146 case AF_CONTROL_COMMAND_LINE:{ | |
147 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; | |
148 int i,j; | |
149 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1], | |
150 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]); | |
151 for(i=0;i<AF_NCH;i++){ | |
152 for(j=0;j<KM;j++){ | |
153 ((af_equalizer_t*)af->setup)->g[i][j] = | |
154 pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0; | |
155 } | |
156 } | |
157 return AF_OK; | |
158 } | |
159 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{ | |
160 float* gain = ((af_control_ext_t*)arg)->arg; | |
161 int ch = ((af_control_ext_t*)arg)->ch; | |
162 int k; | |
163 if(ch >= AF_NCH || ch < 0) | |
164 return AF_ERROR; | |
165 | |
166 for(k = 0 ; k<KM ; k++) | |
167 s->g[ch][k] = pow(10.0,clamp(gain[k],G_MIN,G_MAX)/20.0)-1.0; | |
168 | |
169 return AF_OK; | |
170 } | |
171 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{ | |
172 float* gain = ((af_control_ext_t*)arg)->arg; | |
173 int ch = ((af_control_ext_t*)arg)->ch; | |
174 int k; | |
175 if(ch >= AF_NCH || ch < 0) | |
176 return AF_ERROR; | |
177 | |
178 for(k = 0 ; k<KM ; k++) | |
179 gain[k] = log10(s->g[ch][k]+1.0) * 20.0; | |
180 | |
181 return AF_OK; | |
182 } | |
183 } | |
184 return AF_UNKNOWN; | |
185 } | |
186 | |
187 // Deallocate memory | |
188 static void uninit(struct af_instance_s* af) | |
189 { | |
190 if(af->data) | |
191 free(af->data); | |
192 if(af->setup) | |
193 free(af->setup); | |
194 } | |
195 | |
196 // Filter data through filter | |
197 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
198 { | |
199 af_data_t* c = data; // Current working data | |
200 af_equalizer_t* s = (af_equalizer_t*)af->setup; // Setup | |
201 uint32_t ci = af->data->nch; // Index for channels | |
202 uint32_t nch = af->data->nch; // Number of channels | |
203 | |
204 while(ci--){ | |
205 float* g = s->g[ci]; // Gain factor | |
206 float* in = ((float*)c->audio)+ci; | |
207 float* out = ((float*)c->audio)+ci; | |
208 float* end = in + c->len/4; // Block loop end | |
209 | |
210 while(in < end){ | |
211 register int k = 0; // Frequency band index | |
212 register float yt = *in; // Current input sample | |
213 in+=nch; | |
214 | |
215 // Run the filters | |
216 for(;k<s->K;k++){ | |
217 // Pointer to circular buffer wq | |
218 register float* wq = s->wq[ci][k]; | |
219 // Calculate output from AR part of current filter | |
220 register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1]; | |
221 // Calculate output form MA part of current filter | |
222 yt+=(w + wq[1]*s->b[k][1])*g[k]; | |
223 // Update circular buffer | |
224 wq[1] = wq[0]; | |
225 wq[0] = w; | |
226 } | |
227 // Calculate output | |
228 *out=yt*s->gain_factor; | |
229 out+=nch; | |
230 } | |
231 } | |
232 return c; | |
233 } | |
234 | |
235 // Allocate memory and set function pointers | |
236 int equalizer_open(af_instance_t* af){ | |
237 af->control=control; | |
238 af->uninit=uninit; | |
239 af->play=play; | |
240 af->mul=1; | |
241 af->data=calloc(1,sizeof(af_data_t)); | |
242 af->setup=calloc(1,sizeof(af_equalizer_t)); | |
243 if(af->data == NULL || af->setup == NULL) | |
244 return AF_ERROR; | |
245 return AF_OK; | |
246 } | |
247 | |
248 // Description of this filter | |
249 /*af_info_t af_info_equalizer = { | |
250 "Equalizer audio filter", | |
251 "equalizer", | |
252 "Anders", | |
253 "", | |
254 AF_FLAGS_NOT_REENTRANT, | |
255 af_open | |
256 };*/ | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | |
263 |