2009-01-05 12:41:40 +00:00
|
|
|
/*
|
|
|
|
* Equalizer filter, implementation of a 10 band time domain graphic
|
|
|
|
* equalizer using IIR filters. The IIR filters are implemented using a
|
|
|
|
* Direct Form II approach, but has been modified (b1 == 0 always) to
|
|
|
|
* save computation.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001 Anders Johansson ajh@atri.curtin.edu.au
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "af.h"
|
|
|
|
|
2002-11-12 12:33:56 +00:00
|
|
|
#define L 2 // Storage for filter taps
|
2009-07-06 23:26:13 +00:00
|
|
|
#define KM 10 // Max number of bands
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
#define Q 1.2247449 /* Q value for band-pass filters 1.2247=(3/2)^(1/2)
|
|
|
|
gives 4dB suppression @ Fc*2 and Fc/2 */
|
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
/* Center frequencies for band-pass filters
|
2009-07-06 23:26:13 +00:00
|
|
|
The different frequency bands are:
|
2002-12-28 13:59:53 +00:00
|
|
|
nr. center frequency
|
|
|
|
0 31.25 Hz
|
|
|
|
1 62.50 Hz
|
|
|
|
2 125.0 Hz
|
|
|
|
3 250.0 Hz
|
|
|
|
4 500.0 Hz
|
|
|
|
5 1.000 kHz
|
|
|
|
6 2.000 kHz
|
|
|
|
7 4.000 kHz
|
|
|
|
8 8.000 kHz
|
|
|
|
9 16.00 kHz
|
|
|
|
*/
|
2002-11-03 09:51:02 +00:00
|
|
|
#define CF {31.25,62.5,125,250,500,1000,2000,4000,8000,16000}
|
|
|
|
|
|
|
|
// Maximum and minimum gain for the bands
|
|
|
|
#define G_MAX +12.0
|
2009-07-06 23:26:13 +00:00
|
|
|
#define G_MIN -12.0
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
// Data for specific instances of this filter
|
|
|
|
typedef struct af_equalizer_s
|
|
|
|
{
|
2002-12-28 13:59:53 +00:00
|
|
|
float a[KM][L]; // A weights
|
|
|
|
float b[KM][L]; // B weights
|
|
|
|
float wq[AF_NCH][KM][L]; // Circular buffer for W data
|
|
|
|
float g[AF_NCH][KM]; // Gain factor for each channel and band
|
|
|
|
int K; // Number of used eq bands
|
|
|
|
int channels; // Number of channels
|
2006-03-11 21:16:59 +00:00
|
|
|
float gain_factor; // applied at output to avoid clipping
|
2002-11-03 09:51:02 +00:00
|
|
|
} af_equalizer_t;
|
|
|
|
|
|
|
|
// 2nd order Band-pass Filter design
|
|
|
|
static void bp2(float* a, float* b, float fc, float q){
|
|
|
|
double th= 2.0 * M_PI * fc;
|
|
|
|
double C = (1.0 - tan(th*q/2.0))/(1.0 + tan(th*q/2.0));
|
|
|
|
|
|
|
|
a[0] = (1.0 + C) * cos(th);
|
|
|
|
a[1] = -1 * C;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
b[0] = (1.0 - C)/2.0;
|
|
|
|
b[1] = -1.0050;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialization and runtime control
|
|
|
|
static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
|
|
{
|
2009-07-06 23:26:13 +00:00
|
|
|
af_equalizer_t* s = (af_equalizer_t*)af->setup;
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
switch(cmd){
|
|
|
|
case AF_CONTROL_REINIT:{
|
2006-03-11 21:16:59 +00:00
|
|
|
int k =0, i =0;
|
2002-11-03 09:51:02 +00:00
|
|
|
float F[KM] = CF;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-03-11 21:16:59 +00:00
|
|
|
s->gain_factor=0.0;
|
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
// Sanity check
|
|
|
|
if(!arg) return AF_ERROR;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
af->data->rate = ((af_data_t*)arg)->rate;
|
|
|
|
af->data->nch = ((af_data_t*)arg)->nch;
|
2004-12-27 17:30:15 +00:00
|
|
|
af->data->format = AF_FORMAT_FLOAT_NE;
|
2002-12-28 13:59:53 +00:00
|
|
|
af->data->bps = 4;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
// Calculate number of active filters
|
|
|
|
s->K=KM;
|
2002-11-12 12:33:56 +00:00
|
|
|
while(F[s->K-1] > (float)af->data->rate/2.2)
|
2002-11-03 09:51:02 +00:00
|
|
|
s->K--;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-11-12 12:33:56 +00:00
|
|
|
if(s->K != KM)
|
2009-07-06 23:26:13 +00:00
|
|
|
mp_msg(MSGT_AFILTER, MSGL_INFO, "[equalizer] Limiting the number of filters to"
|
2002-12-28 13:59:53 +00:00
|
|
|
" %i due to low sample rate.\n",s->K);
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
// Generate filter taps
|
|
|
|
for(k=0;k<s->K;k++)
|
|
|
|
bp2(s->a[k],s->b[k],F[k]/((float)af->data->rate),Q);
|
|
|
|
|
|
|
|
// Calculate how much this plugin adds to the overall time delay
|
2007-11-01 06:52:50 +00:00
|
|
|
af->delay = 2 * af->data->nch * af->data->bps;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-03-11 21:16:59 +00:00
|
|
|
// Calculate gain factor to prevent clipping at output
|
|
|
|
for(k=0;k<AF_NCH;k++)
|
|
|
|
{
|
|
|
|
for(i=0;i<KM;i++)
|
|
|
|
{
|
|
|
|
if(s->gain_factor < s->g[k][i]) s->gain_factor=s->g[k][i];
|
|
|
|
}
|
|
|
|
}
|
2002-11-03 09:51:02 +00:00
|
|
|
|
2006-03-11 21:16:59 +00:00
|
|
|
s->gain_factor=log10(s->gain_factor + 1.0) * 20.0;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2006-03-11 21:16:59 +00:00
|
|
|
if(s->gain_factor > 0.0)
|
|
|
|
{
|
|
|
|
s->gain_factor=0.1+(s->gain_factor/12.0);
|
|
|
|
}else{
|
|
|
|
s->gain_factor=1;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-12-28 13:59:53 +00:00
|
|
|
return af_test_output(af,arg);
|
2002-11-03 09:51:02 +00:00
|
|
|
}
|
|
|
|
case AF_CONTROL_COMMAND_LINE:{
|
|
|
|
float g[10]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
|
|
|
|
int i,j;
|
2009-07-06 23:26:13 +00:00
|
|
|
sscanf((char*)arg,"%f:%f:%f:%f:%f:%f:%f:%f:%f:%f", &g[0], &g[1],
|
2002-11-03 09:51:02 +00:00
|
|
|
&g[2], &g[3], &g[4], &g[5], &g[6], &g[7], &g[8] ,&g[9]);
|
2002-12-28 13:59:53 +00:00
|
|
|
for(i=0;i<AF_NCH;i++){
|
2002-11-03 09:51:02 +00:00
|
|
|
for(j=0;j<KM;j++){
|
2009-07-06 23:26:13 +00:00
|
|
|
((af_equalizer_t*)af->setup)->g[i][j] =
|
2002-11-03 09:51:02 +00:00
|
|
|
pow(10.0,clamp(g[j],G_MIN,G_MAX)/20.0)-1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AF_OK;
|
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_SET:{
|
|
|
|
float* gain = ((af_control_ext_t*)arg)->arg;
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
int k;
|
2006-03-08 13:00:51 +00:00
|
|
|
if(ch >= AF_NCH || ch < 0)
|
2002-11-03 09:51:02 +00:00
|
|
|
return AF_ERROR;
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
for(k = 0 ; k<KM ; k++)
|
|
|
|
s->g[ch][k] = pow(10.0,clamp(gain[k],G_MIN,G_MAX)/20.0)-1.0;
|
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
2002-12-28 13:59:53 +00:00
|
|
|
case AF_CONTROL_EQUALIZER_GAIN | AF_CONTROL_GET:{
|
|
|
|
float* gain = ((af_control_ext_t*)arg)->arg;
|
|
|
|
int ch = ((af_control_ext_t*)arg)->ch;
|
|
|
|
int k;
|
2006-03-08 13:00:51 +00:00
|
|
|
if(ch >= AF_NCH || ch < 0)
|
2002-11-03 09:51:02 +00:00
|
|
|
return AF_ERROR;
|
2002-12-28 13:59:53 +00:00
|
|
|
|
|
|
|
for(k = 0 ; k<KM ; k++)
|
|
|
|
gain[k] = log10(s->g[ch][k]+1.0) * 20.0;
|
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
// Deallocate memory
|
2002-11-03 09:51:02 +00:00
|
|
|
static void uninit(struct af_instance_s* af)
|
|
|
|
{
|
|
|
|
if(af->data)
|
|
|
|
free(af->data);
|
|
|
|
if(af->setup)
|
|
|
|
free(af->setup);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
|
|
|
static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
|
|
{
|
|
|
|
af_data_t* c = data; // Current working data
|
2009-07-06 23:26:13 +00:00
|
|
|
af_equalizer_t* s = (af_equalizer_t*)af->setup; // Setup
|
2002-11-03 09:51:02 +00:00
|
|
|
uint32_t ci = af->data->nch; // Index for channels
|
|
|
|
uint32_t nch = af->data->nch; // Number of channels
|
|
|
|
|
|
|
|
while(ci--){
|
2009-07-06 23:26:13 +00:00
|
|
|
float* g = s->g[ci]; // Gain factor
|
2002-12-28 13:59:53 +00:00
|
|
|
float* in = ((float*)c->audio)+ci;
|
|
|
|
float* out = ((float*)c->audio)+ci;
|
|
|
|
float* end = in + c->len/4; // Block loop end
|
2002-11-03 09:51:02 +00:00
|
|
|
|
|
|
|
while(in < end){
|
2004-12-27 17:30:15 +00:00
|
|
|
register int k = 0; // Frequency band index
|
2002-12-28 13:59:53 +00:00
|
|
|
register float yt = *in; // Current input sample
|
2002-11-03 09:51:02 +00:00
|
|
|
in+=nch;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-11-03 09:51:02 +00:00
|
|
|
// Run the filters
|
|
|
|
for(;k<s->K;k++){
|
|
|
|
// Pointer to circular buffer wq
|
|
|
|
register float* wq = s->wq[ci][k];
|
|
|
|
// Calculate output from AR part of current filter
|
|
|
|
register float w=yt*s->b[k][0] + wq[0]*s->a[k][0] + wq[1]*s->a[k][1];
|
|
|
|
// Calculate output form MA part of current filter
|
|
|
|
yt+=(w + wq[1]*s->b[k][1])*g[k];
|
|
|
|
// Update circular buffer
|
|
|
|
wq[1] = wq[0];
|
|
|
|
wq[0] = w;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
// Calculate output
|
2006-03-11 21:16:59 +00:00
|
|
|
*out=yt*s->gain_factor;
|
2002-11-03 09:51:02 +00:00
|
|
|
out+=nch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory and set function pointers
|
2007-03-20 09:46:00 +00:00
|
|
|
static int af_open(af_instance_t* af){
|
2002-11-03 09:51:02 +00:00
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
2007-11-01 06:52:01 +00:00
|
|
|
af->mul=1;
|
2002-11-03 09:51:02 +00:00
|
|
|
af->data=calloc(1,sizeof(af_data_t));
|
|
|
|
af->setup=calloc(1,sizeof(af_equalizer_t));
|
|
|
|
if(af->data == NULL || af->setup == NULL)
|
|
|
|
return AF_ERROR;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description of this filter
|
|
|
|
af_info_t af_info_equalizer = {
|
|
|
|
"Equalizer audio filter",
|
|
|
|
"equalizer",
|
|
|
|
"Anders",
|
|
|
|
"",
|
|
|
|
AF_FLAGS_NOT_REENTRANT,
|
2007-03-20 09:46:00 +00:00
|
|
|
af_open
|
2002-11-03 09:51:02 +00:00
|
|
|
};
|