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