comparison libaf/af_equalizer.c @ 8073:c0e556f9986b

Adding equalizer filter + some cosmetics
author anders
date Sun, 03 Nov 2002 09:51:02 +0000
parents
children e8832e66babd
comparison
equal deleted inserted replaced
8072:8e97c4629611 8073:c0e556f9986b
1 /*=============================================================================
2 //
3 // This software has been released under the terms of the GNU 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
20 #include <unistd.h>
21 #include <inttypes.h>
22 #include <math.h>
23
24 #include "../config.h"
25 #include "../mp_msg.h"
26 #include "../libao2/afmt.h"
27
28 #include "af.h"
29 #include "equalizer.h"
30
31 #define NCH 6 // Max number of channels
32 #define L 2 // Storage for filter taps
33 #define KM 10 // Max number of bands
34
35 #define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2)
36 gives 4dB suppression @ Fc*2 and Fc/2 */
37
38 // Center frequencies for band-pass filters
39 #define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000}
40
41 // Maximum and minimum gain for the bands
42 #define G_MAX +12.0
43 #define G_MIN -12.0
44
45 // Data for specific instances of this filter
46 typedef struct af_equalizer_s
47 {
48 float a[KM][L]; // A weights
49 float b[KM][L]; // B weights
50 float wq[NCH][KM][L]; // Circular buffer for W data
51 float g[NCH][KM]; // Gain factor for each channel and band
52 int K; // Number of used eq bands
53 int channels; // Number of channels
54 } af_equalizer_t;
55
56 // 2nd order Band-pass Filter design
57 static void bp2(float* a, float* b, float fc, float q){
58 double th= 2.0 * M_PI * fc;
59 double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0));
60
61 a[0] = (1.0 + C) * cos(th);
62 a[1] = -1 * C;
63
64 b[0] = (1.0 - C)/2.0;
65 b[1] = -1.0050;
66 }
67
68 // Initialization and runtime control
69 static int control(struct af_instance_s* af, int cmd, void* arg)
70 {
71 af_equalizer_t* s = (af_equalizer_t*)af->setup;
72
73 switch(cmd){
74 case AF_CONTROL_REINIT:{
75 int k =0;
76 float F[KM] = CF;
77
78 // Sanity check
79 if(!arg) return AF_ERROR;
80
81 af->data->rate = ((af_data_t*)arg)->rate;
82 af->data->nch = ((af_data_t*)arg)->nch;
83 af->data->format = AFMT_S16_LE;
84 af->data->bps = 2;
85
86 // Calculate number of active filters
87 s->K=KM;
88 while(F[s->K-1] > (float)af->data->rate/2.0)
89 s->K--;
90
91 // Generate filter taps
92 for(k=0;k<s->K;k++)
93 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q);
94
95 // Calculate how much this plugin adds to the overall time delay
96 af->delay += 2000.0/((float)af->data->rate);
97
98 // Only AFMT_S16_LE is supported
99 if(af->data->format != ((af_data_t*)arg)->format ||
100 af->data->bps != ((af_data_t*)arg)->bps)
101 return AF_FALSE;
102 return AF_OK;
103 }
104 case AF_CONTROL_COMMAND_LINE:{
105 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
106 int i,j;
107 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1],
108 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]);
109 for(i=0;i<NCH;i++){
110 for(j=0;j<KM;j++){
111 ((af_equalizer_t*)af->setup)->g[i][j] =
112 pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0;
113 }
114 }
115 return AF_OK;
116 }
117 case AF_CONTROL_EQUALIZER_SET_GAIN:{
118 float gain = ((equalizer_t*)arg)->gain;
119 int ch = ((equalizer_t*)arg)->channel;
120 int band = ((equalizer_t*)arg)->band;
121 if(ch > NCH || ch < 0 || band > KM || band < 0)
122 return AF_ERROR;
123
124 s->g[ch][band] = pow(10.0,clamp(gain,G_MIN,G_MAX)/20.0)-1.0;
125 return AF_OK;
126 }
127 case AF_CONTROL_EQUALIZER_GET_GAIN:{
128 int ch =((equalizer_t*)arg)->channel;
129 int band =((equalizer_t*)arg)->band;
130 if(ch > NCH || ch < 0 || band > KM || band < 0)
131 return AF_ERROR;
132
133 ((equalizer_t*)arg)->gain = log10(s->g[ch][band]+1.0) * 20.0;
134 return AF_OK;
135 }
136 }
137 return AF_UNKNOWN;
138 }
139
140 // Deallocate memory
141 static void uninit(struct af_instance_s* af)
142 {
143 if(af->data)
144 free(af->data);
145 if(af->setup)
146 free(af->setup);
147 }
148
149 // Filter data through filter
150 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
151 {
152 af_data_t* c = data; // Current working data
153 af_equalizer_t* s = (af_equalizer_t*)af->setup; // Setup
154 uint32_t ci = af->data->nch; // Index for channels
155 uint32_t nch = af->data->nch; // Number of channels
156
157 while(ci--){
158 float* g = s->g[ci]; // Gain factor
159 int16_t* in = ((int16_t*)c->audio)+ci;
160 int16_t* out = ((int16_t*)c->audio)+ci;
161 int16_t* end = in + c->len/2; // Block loop end
162
163 while(in < end){
164 register uint32_t k = 0; // Frequency band index
165 register float yt = (float)(*in); // Current input sample
166 in+=nch;
167
168 // Run the filters
169 for(;k<s->K;k++){
170 // Pointer to circular buffer wq
171 register float* wq = s->wq[ci][k];
172 // Calculate output from AR part of current filter
173 register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1];
174 // Calculate output form MA part of current filter
175 yt+=(w + wq[1]*s->b[k][1])*g[k];
176 // Update circular buffer
177 wq[1] = wq[0];
178 wq[0] = w;
179 }
180 // Calculate output
181 *out=(int16_t)(yt/(4.0*10.0));
182 out+=nch;
183 }
184 }
185 return c;
186 }
187
188 // Allocate memory and set function pointers
189 static int open(af_instance_t* af){
190 af->control=control;
191 af->uninit=uninit;
192 af->play=play;
193 af->mul.n=1;
194 af->mul.d=1;
195 af->data=calloc(1,sizeof(af_data_t));
196 af->setup=calloc(1,sizeof(af_equalizer_t));
197 if(af->data == NULL || af->setup == NULL)
198 return AF_ERROR;
199 return AF_OK;
200 }
201
202 // Description of this filter
203 af_info_t af_info_equalizer = {
204 "Equalizer audio filter",
205 "equalizer",
206 "Anders",
207 "",
208 AF_FLAGS_NOT_REENTRANT,
209 open
210 };
211
212
213
214
215
216
217