comparison libaf/af_equalizer.c @ 8607:d6f40a06867b

Changes includes: - Improved runtime control system - 3 New filter panning, compressor/limiter and a noise gate - The compressor/limiter and the noise gate are not yet finished - The panning filter does combined mixing and channel routing and can be used to down-mix from stereo to mono (for example) - Improvements to volume and channel - volume now has a very good soft clipping using sin() - channel can handle generic routing of audio data - Conversion of all filters to handle floating point data - Cleanup of message printing - Fix for the sig 11 bug reported by Denes
author anders
date Sat, 28 Dec 2002 13:59:53 +0000
parents e8832e66babd
children 14090f7300a8
comparison
equal deleted inserted replaced
8606:d80edba39db9 8607:d6f40a06867b
20 #include <unistd.h> 20 #include <unistd.h>
21 #include <inttypes.h> 21 #include <inttypes.h>
22 #include <math.h> 22 #include <math.h>
23 23
24 #include "af.h" 24 #include "af.h"
25 #include "equalizer.h" 25
26
27 #define NCH AF_NCH // Number of channels
28 #define L 2 // Storage for filter taps 26 #define L 2 // Storage for filter taps
29 #define KM 10 // Max number of bands 27 #define KM 10 // Max number of bands
30 28
31 #define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2) 29 #define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2)
32 gives 4dB suppression @ Fc*2 and Fc/2 */ 30 gives 4dB suppression @ Fc*2 and Fc/2 */
33 31
34 // Center frequencies for band-pass filters 32 /* Center frequencies for band-pass filters
33 The different frequency bands are:
34 nr. center frequency
35 0 31.25 Hz
36 1 62.50 Hz
37 2 125.0 Hz
38 3 250.0 Hz
39 4 500.0 Hz
40 5 1.000 kHz
41 6 2.000 kHz
42 7 4.000 kHz
43 8 8.000 kHz
44 9 16.00 kHz
45 */
35 #define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000} 46 #define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000}
36 47
37 // Maximum and minimum gain for the bands 48 // Maximum and minimum gain for the bands
38 #define G_MAX +12.0 49 #define G_MAX +12.0
39 #define G_MIN -12.0 50 #define G_MIN -12.0
40 51
41 // Data for specific instances of this filter 52 // Data for specific instances of this filter
42 typedef struct af_equalizer_s 53 typedef struct af_equalizer_s
43 { 54 {
44 float a[KM][L]; // A weights 55 float a[KM][L]; // A weights
45 float b[KM][L]; // B weights 56 float b[KM][L]; // B weights
46 float wq[NCH][KM][L]; // Circular buffer for W data 57 float wq[AF_NCH][KM][L]; // Circular buffer for W data
47 float g[NCH][KM]; // Gain factor for each channel and band 58 float g[AF_NCH][KM]; // Gain factor for each channel and band
48 int K; // Number of used eq bands 59 int K; // Number of used eq bands
49 int channels; // Number of channels 60 int channels; // Number of channels
50 } af_equalizer_t; 61 } af_equalizer_t;
51 62
52 // 2nd order Band-pass Filter design 63 // 2nd order Band-pass Filter design
53 static void bp2(float* a, float* b, float fc, float q){ 64 static void bp2(float* a, float* b, float fc, float q){
54 double th= 2.0 * M_PI * fc; 65 double th= 2.0 * M_PI * fc;
74 // Sanity check 85 // Sanity check
75 if(!arg) return AF_ERROR; 86 if(!arg) return AF_ERROR;
76 87
77 af->data->rate = ((af_data_t*)arg)->rate; 88 af->data->rate = ((af_data_t*)arg)->rate;
78 af->data->nch = ((af_data_t*)arg)->nch; 89 af->data->nch = ((af_data_t*)arg)->nch;
79 af->data->format = AF_FORMAT_NE | AF_FORMAT_SI; 90 af->data->format = AF_FORMAT_NE | AF_FORMAT_F;
80 af->data->bps = 2; 91 af->data->bps = 4;
81 92
82 // Calculate number of active filters 93 // Calculate number of active filters
83 s->K=KM; 94 s->K=KM;
84 while(F[s->K-1] > (float)af->data->rate/2.2) 95 while(F[s->K-1] > (float)af->data->rate/2.2)
85 s->K--; 96 s->K--;
86 97
87 if(s->K != KM) 98 if(s->K != KM)
88 af_msg(AF_MSG_INFO,"Limiting the number of filters to %i due to low sample rate.\n",s->K); 99 af_msg(AF_MSG_INFO,"[equalizer] Limiting the number of filters to"
100 " %i due to low sample rate.\n",s->K);
89 101
90 // Generate filter taps 102 // Generate filter taps
91 for(k=0;k<s->K;k++) 103 for(k=0;k<s->K;k++)
92 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q); 104 bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q);
93 105
94 // Calculate how much this plugin adds to the overall time delay 106 // Calculate how much this plugin adds to the overall time delay
95 af->delay += 2000.0/((float)af->data->rate); 107 af->delay += 2000.0/((float)af->data->rate);
96 108
97 // Only signed 16 bit little endian is supported 109 return af_test_output(af,arg);
98 if(af->data->format != ((af_data_t*)arg)->format ||
99 af->data->bps != ((af_data_t*)arg)->bps)
100 return AF_FALSE;
101 return AF_OK;
102 } 110 }
103 case AF_CONTROL_COMMAND_LINE:{ 111 case AF_CONTROL_COMMAND_LINE:{
104 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; 112 float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
105 int i,j; 113 int i,j;
106 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1], 114 sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1],
107 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]); 115 &g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]);
108 for(i=0;i<NCH;i++){ 116 for(i=0;i<AF_NCH;i++){
109 for(j=0;j<KM;j++){ 117 for(j=0;j<KM;j++){
110 ((af_equalizer_t*)af->setup)->g[i][j] = 118 ((af_equalizer_t*)af->setup)->g[i][j] =
111 pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0; 119 pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0;
112 } 120 }
113 } 121 }
114 return AF_OK; 122 return AF_OK;
115 } 123 }
116 case AF_CONTROL_EQUALIZER_SET_GAIN:{ 124 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{
117 float gain = ((equalizer_t*)arg)->gain; 125 float* gain = ((af_control_ext_t*)arg)->arg;
118 int ch = ((equalizer_t*)arg)->channel; 126 int ch = ((af_control_ext_t*)arg)->ch;
119 int band = ((equalizer_t*)arg)->band; 127 int k;
120 if(ch > NCH || ch < 0 || band > KM || band < 0) 128 if(ch > AF_NCH || ch < 0)
121 return AF_ERROR; 129 return AF_ERROR;
122 130
123 s->g[ch][band] = pow(10.0,clamp(gain,G_MIN,G_MAX)/20.0)-1.0; 131 for(k = 0 ; k<KM ; k++)
132 s->g[ch][k] = pow(10.0,clamp(gain[k],G_MIN,G_MAX)/20.0)-1.0;
133
124 return AF_OK; 134 return AF_OK;
125 } 135 }
126 case AF_CONTROL_EQUALIZER_GET_GAIN:{ 136 case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{
127 int ch =((equalizer_t*)arg)->channel; 137 float* gain = ((af_control_ext_t*)arg)->arg;
128 int band =((equalizer_t*)arg)->band; 138 int ch = ((af_control_ext_t*)arg)->ch;
129 if(ch > NCH || ch < 0 || band > KM || band < 0) 139 int k;
140 if(ch > AF_NCH || ch < 0)
130 return AF_ERROR; 141 return AF_ERROR;
131 142
132 ((equalizer_t*)arg)->gain = log10(s->g[ch][band]+1.0) * 20.0; 143 for(k = 0 ; k<KM ; k++)
144 gain[k] = log10(s->g[ch][k]+1.0) * 20.0;
145
133 return AF_OK; 146 return AF_OK;
134 } 147 }
135 } 148 }
136 return AF_UNKNOWN; 149 return AF_UNKNOWN;
137 } 150 }
153 uint32_t ci = af->data->nch; // Index for channels 166 uint32_t ci = af->data->nch; // Index for channels
154 uint32_t nch = af->data->nch; // Number of channels 167 uint32_t nch = af->data->nch; // Number of channels
155 168
156 while(ci--){ 169 while(ci--){
157 float* g = s->g[ci]; // Gain factor 170 float* g = s->g[ci]; // Gain factor
158 int16_t* in = ((int16_t*)c->audio)+ci; 171 float* in = ((float*)c->audio)+ci;
159 int16_t* out = ((int16_t*)c->audio)+ci; 172 float* out = ((float*)c->audio)+ci;
160 int16_t* end = in + c->len/2; // Block loop end 173 float* end = in + c->len/4; // Block loop end
161 174
162 while(in < end){ 175 while(in < end){
163 register uint32_t k = 0; // Frequency band index 176 register uint32_t k = 0; // Frequency band index
164 register float yt = (float)(*in); // Current input sample 177 register float yt = *in; // Current input sample
165 in+=nch; 178 in+=nch;
166 179
167 // Run the filters 180 // Run the filters
168 for(;k<s->K;k++){ 181 for(;k<s->K;k++){
169 // Pointer to circular buffer wq 182 // Pointer to circular buffer wq
175 // Update circular buffer 188 // Update circular buffer
176 wq[1] = wq[0]; 189 wq[1] = wq[0];
177 wq[0] = w; 190 wq[0] = w;
178 } 191 }
179 // Calculate output 192 // Calculate output
180 *out=(int16_t)(yt/(4.0*10.0)); 193 *out=yt/(4.0*10.0);
181 out+=nch; 194 out+=nch;
182 } 195 }
183 } 196 }
184 return c; 197 return c;
185 } 198 }