Mercurial > audlegacy
annotate src/audlegacy/af_equalizer.c @ 4849:123b35cd71ab
Heal the equalizer's clipping prevention of insanity
author | John Lindgren <john.lindgren@tds.net> |
---|---|
date | Sun, 12 Apr 2009 23:52:38 -0400 |
parents | 7bf7f83a217e |
children |
rev | line source |
---|---|
4300 | 1 /*============================================================================= |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
2 // |
4300 | 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 | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
27 #define KM 10 // Max number of bands |
4300 | 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 | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
34 The different frequency bands are: |
4300 | 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 | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
52 #define G_MIN -12.0 |
4300 | 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 } af_equalizer_t; | |
64 | |
65 static int af_test_output(struct af_instance_s* af, af_data_t* out) | |
66 { | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
67 if((af->data->format != out->format) || |
4300 | 68 (af->data->bps != out->bps) || |
69 (af->data->rate != out->rate) || | |
70 (af->data->nch != out->nch)){ | |
71 memcpy(out,af->data,sizeof(af_data_t)); | |
72 return AF_FALSE; | |
73 } | |
74 return AF_OK; | |
75 } | |
76 | |
77 // 2nd order Band-pass Filter design | |
78 static void bp2(float* a, float* b, float fc, float q){ | |
79 double th= 2.0 * M_PI * fc; | |
80 double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0)); | |
81 | |
82 a[0] = (1.0 + C) * cos(th); | |
83 a[1] = -1 * C; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
84 |
4300 | 85 b[0] = (1.0 - C)/2.0; |
86 b[1] = -1.0050; | |
87 } | |
88 | |
89 // Initialization and runtime control | |
90 static int control(struct af_instance_s* af, int cmd, void* arg) | |
91 { | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
92 af_equalizer_t* s = (af_equalizer_t*)af->setup; |
4300 | 93 |
94 switch(cmd){ | |
95 case AF_CONTROL_REINIT:{ | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
96 int k = 0; |
4300 | 97 float F[KM] = CF; |
98 | |
99 // Sanity check | |
100 if(!arg) return AF_ERROR; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
101 |
4300 | 102 af->data->rate = ((af_data_t*)arg)->rate; |
103 af->data->nch = ((af_data_t*)arg)->nch; | |
104 af->data->format = AF_FORMAT_FLOAT_NE; | |
105 af->data->bps = 4; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
106 |
4300 | 107 // Calculate number of active filters |
108 s->K=KM; | |
109 while(F[s->K-1] > (float)af->data->rate/2.2) | |
110 s->K--; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
111 |
4300 | 112 if(s->K != KM) |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
113 af_msg(AF_MSG_INFO,"[equalizer] Limiting the number of filters to" |
4300 | 114 " %i due to low sample rate.\n",s->K); |
115 | |
116 // Generate filter taps | |
117 for(k=0;k<s->K;k++) | |
118 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q); | |
119 | |
120 // Calculate how much this plugin adds to the overall time delay | |
121 af->delay = 2 * af->data->nch * af->data->bps; | |
122 | |
123 return af_test_output(af,arg); | |
124 } | |
125 case AF_CONTROL_COMMAND_LINE:{ | |
126 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; | |
127 int i,j; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
128 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1], |
4300 | 129 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]); |
130 for(i=0;i<AF_NCH;i++){ | |
131 for(j=0;j<KM;j++){ | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
132 ((af_equalizer_t*)af->setup)->g[i][j] = |
4300 | 133 pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0; |
134 } | |
135 } | |
136 return AF_OK; | |
137 } | |
138 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{ | |
139 float* gain = ((af_control_ext_t*)arg)->arg; | |
140 int ch = ((af_control_ext_t*)arg)->ch; | |
141 int k; | |
142 if(ch >= AF_NCH || ch < 0) | |
143 return AF_ERROR; | |
144 | |
145 for(k = 0 ; k<KM ; k++) | |
146 s->g[ch][k] = pow(10.0,clamp(gain[k],G_MIN,G_MAX)/20.0)-1.0; | |
147 | |
148 return AF_OK; | |
149 } | |
150 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{ | |
151 float* gain = ((af_control_ext_t*)arg)->arg; | |
152 int ch = ((af_control_ext_t*)arg)->ch; | |
153 int k; | |
154 if(ch >= AF_NCH || ch < 0) | |
155 return AF_ERROR; | |
156 | |
157 for(k = 0 ; k<KM ; k++) | |
158 gain[k] = log10(s->g[ch][k]+1.0) * 20.0; | |
159 | |
160 return AF_OK; | |
161 } | |
162 } | |
163 return AF_UNKNOWN; | |
164 } | |
165 | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
166 // Deallocate memory |
4300 | 167 static void uninit(struct af_instance_s* af) |
168 { | |
169 if(af->data) | |
170 free(af->data); | |
171 if(af->setup) | |
172 free(af->setup); | |
173 } | |
174 | |
175 // Filter data through filter | |
176 static af_data_t* play(struct af_instance_s* af, af_data_t* data) | |
177 { | |
178 af_data_t* c = data; // Current working data | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
179 af_equalizer_t* s = (af_equalizer_t*)af->setup; // Setup |
4300 | 180 uint32_t ci = af->data->nch; // Index for channels |
181 uint32_t nch = af->data->nch; // Number of channels | |
182 | |
183 while(ci--){ | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
184 float* g = s->g[ci]; // Gain factor |
4300 | 185 float* in = ((float*)c->audio)+ci; |
186 float* out = ((float*)c->audio)+ci; | |
187 float* end = in + c->len/4; // Block loop end | |
188 | |
189 while(in < end){ | |
190 register int k = 0; // Frequency band index | |
191 register float yt = *in; // Current input sample | |
192 in+=nch; | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
193 |
4300 | 194 // Run the filters |
195 for(;k<s->K;k++){ | |
196 // Pointer to circular buffer wq | |
197 register float* wq = s->wq[ci][k]; | |
198 // Calculate output from AR part of current filter | |
199 register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1]; | |
200 // Calculate output form MA part of current filter | |
201 yt+=(w + wq[1]*s->b[k][1])*g[k]; | |
202 // Update circular buffer | |
203 wq[1] = wq[0]; | |
204 wq[0] = w; | |
205 } | |
4849
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
206 // Calculate output |
123b35cd71ab
Heal the equalizer's clipping prevention of insanity
John Lindgren <john.lindgren@tds.net>
parents:
4811
diff
changeset
|
207 * out = yt; |
4300 | 208 out+=nch; |
209 } | |
210 } | |
211 return c; | |
212 } | |
213 | |
214 // Allocate memory and set function pointers | |
215 int equalizer_open(af_instance_t* af){ | |
216 af->control=control; | |
217 af->uninit=uninit; | |
218 af->play=play; | |
219 af->mul=1; | |
220 af->data=calloc(1,sizeof(af_data_t)); | |
221 af->setup=calloc(1,sizeof(af_equalizer_t)); | |
222 if(af->data == NULL || af->setup == NULL) | |
223 return AF_ERROR; | |
224 return AF_OK; | |
225 } | |
226 | |
227 // Description of this filter | |
228 /*af_info_t af_info_equalizer = { | |
229 "Equalizer audio filter", | |
230 "equalizer", | |
231 "Anders", | |
232 "", | |
233 AF_FLAGS_NOT_REENTRANT, | |
234 af_open | |
235 };*/ | |
236 | |
237 | |
238 | |
239 | |
240 | |
241 | |
242 |