]> git.sesse.net Git - vlc/commitdiff
all: parametric equalizer by Antti Huovilainen (adapted for vlc by me)
authorSigmund Augdal Helberg <sigmunau@videolan.org>
Mon, 13 Feb 2006 22:44:24 +0000 (22:44 +0000)
committerSigmund Augdal Helberg <sigmunau@videolan.org>
Mon, 13 Feb 2006 22:44:24 +0000 (22:44 +0000)
configure.ac
modules/audio_filter/Modules.am
modules/audio_filter/param_eq.c [new file with mode: 0644]

index acb5e0b4c2ddbfa7e4eb0bc50fbc079a633030b6..121065c0597c8b771dd18052c82acaeb551fe5df 100644 (file)
@@ -523,7 +523,7 @@ AC_CHECK_LIB(m,cos,[
   VLC_ADD_LDFLAGS([adjust distort a52tofloat32 dtstofloat32 x264 goom],[-lm])
 ])
 AC_CHECK_LIB(m,pow,[
-  VLC_ADD_LDFLAGS([ffmpeg ffmpegaltivec stream_out_transrate i420_rgb faad twolame equalizer vlc freetype mpc],[-lm])
+  VLC_ADD_LDFLAGS([ffmpeg ffmpegaltivec stream_out_transrate i420_rgb faad twolame equalizer param_eq vlc freetype mpc],[-lm])
 ])
 AC_CHECK_LIB(m,sqrt,[
   VLC_ADD_LDFLAGS([headphone_channel_mixer normvol speex],[-lm])
@@ -1069,7 +1069,7 @@ dnl  VLC_ADD_PLUGINS([externrun])
   VLC_ADD_PLUGINS([i420_yuy2 i422_yuy2 i420_ymga])
   VLC_ADD_PLUGINS([aout_file linear_resampler bandlimited_resampler])
   VLC_ADD_PLUGINS([float32_mixer spdif_mixer simple_channel_mixer])
-  VLC_ADD_PLUGINS([dolby_surround_decoder headphone_channel_mixer normvol equalizer])
+  VLC_ADD_PLUGINS([dolby_surround_decoder headphone_channel_mixer normvol equalizer param_eq])
   VLC_ADD_PLUGINS([fixed32tofloat32 float32tos16 float32tos8 float32tou16 float32tou8 a52tospdif dtstospdif s16tofloat32 s16tofloat32swab s8tofloat32 u8tofloat32 audio_format])
 fi
 
index 052ca63b10d9a548fd0d9c4a91854bd059b47e6d..fa3dc4f6f04dfe91f87100374c53b3295111a7a3 100644 (file)
@@ -1,3 +1,4 @@
 SOURCES_equalizer = equalizer.c equalizer_presets.h
 SOURCES_normvol = normvol.c
 SOURCES_audio_format = format.c
+SOURCES_param_eq = param_eq.c
diff --git a/modules/audio_filter/param_eq.c b/modules/audio_filter/param_eq.c
new file mode 100644 (file)
index 0000000..de580e9
--- /dev/null
@@ -0,0 +1,344 @@
+/*****************************************************************************\r
+ * param_eq.c:\r
+ *****************************************************************************\r
+ * Copyright (C) 2006 the VideoLAN team\r
+ * $Id: equalizer.c 13905 2006-01-12 23:10:04Z dionoea $\r
+ *\r
+ * Authors: Antti Huovilainen\r
+ *          Sigmund A. Helberg <dnumgis@videolan.org>\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.\r
+ *****************************************************************************/\r
+\r
+/*****************************************************************************\r
+ * Preamble\r
+ *****************************************************************************/\r
+#include <stdlib.h>                                      /* malloc(), free() */\r
+#include <string.h>\r
+#include <math.h>\r
+\r
+#include <vlc/vlc.h>\r
+\r
+#include <vlc/aout.h>\r
+#include "aout_internal.h"\r
+\r
+/*****************************************************************************\r
+ * Module descriptor\r
+ *****************************************************************************/\r
+static int  Open ( vlc_object_t * );\r
+static void Close( vlc_object_t * );\r
+static void CalcPeakEQCoeffs( float, float, float, float, float * );\r
+static void CalcShelfEQCoeffs( float, float, float, int, float, float * );\r
+static void ProcessEQ( float *, float *, float *, int, int, float *, int );\r
+static void DoWork( aout_instance_t *, aout_filter_t *,\r
+                    aout_buffer_t *, aout_buffer_t * );\r
+\r
+vlc_module_begin();\r
+    set_description( _("Parametric Equalizer") );\r
+    set_shortname( N_("Parametric Equalizer" ) );\r
+    set_capability( "audio filter", 0 );\r
+    set_category( CAT_AUDIO );\r
+    set_subcategory( SUBCAT_AUDIO_AFILTER );\r
+\r
+    add_float( "param-eq-lowf", 100, NULL, N_("Low freq (Hz)"),NULL, VLC_FALSE );\r
+    add_float_with_range( "param-eq-lowgain", 0, -20.0, 20.0, NULL,\r
+                          N_("Low freq gain (Db)"), NULL,VLC_FALSE );\r
+    add_float( "param-eq-highf", 10000, NULL, N_("High freq (Hz)"),NULL, VLC_FALSE );\r
+    add_float_with_range( "param-eq-highgain", 0, -20.0, 20.0, NULL,\r
+                          N_("High freq gain (Db)"), NULL,VLC_FALSE );\r
+    add_float( "param-eq-f1", 300, NULL, N_("Freq 1 (Hz)"),NULL, VLC_FALSE );\r
+    add_float_with_range( "param-eq-gain1", 0, -20.0, 20.0, NULL,\r
+                          N_("Freq 1 gain (Db)"), NULL,VLC_FALSE );\r
+    add_float_with_range( "param-eq-q1", 3, 0.1, 100.0, NULL,\r
+                          N_("Freq 1 Q"), NULL,VLC_FALSE );\r
+    add_float( "param-eq-f2", 1000, NULL, N_("Freq 2 (Hz)"),NULL, VLC_FALSE );\r
+    add_float_with_range( "param-eq-gain2", 0, -20.0, 20.0, NULL,\r
+                          N_("Freq 2 gain (Db)"), NULL,VLC_FALSE );\r
+    add_float_with_range( "param-eq-q2", 3, 0.1, 100.0, NULL,\r
+                          N_("Freq 2 Q"), NULL,VLC_FALSE );\r
+    add_float( "param-eq-f3", 3000, NULL, N_("Freq 3 (Hz)"),NULL, VLC_FALSE );\r
+    add_float_with_range( "param-eq-gain3", 0, -20.0, 20.0, NULL,\r
+                          N_("Freq 3 gain (Db)"), NULL,VLC_FALSE );\r
+    add_float_with_range( "param-eq-q3", 3, 0.1, 100.0, NULL,\r
+                          N_("Freq 3 Q"), NULL,VLC_FALSE );\r
+\r
+    set_callbacks( Open, Close );\r
+vlc_module_end();\r
+\r
+/*****************************************************************************\r
+ * Local prototypes\r
+ *****************************************************************************/\r
+typedef struct aout_filter_sys_t\r
+{\r
+    /* Filter static config */\r
+    float   f_lowf, f_lowgain;\r
+    float   f_f1, f_Q1, f_gain1;\r
+    float   f_f2, f_Q2, f_gain2;\r
+    float   f_f3, f_Q3, f_gain3;\r
+    float   f_highf, f_highgain;\r
+    /* Filter computed coeffs */\r
+    float   coeffs[5*5];\r
+    /* State */\r
+    float  *p_state;\r
+       \r
+} aout_filter_sys_t;\r
+\r
+\r
+\r
+\r
+/*****************************************************************************\r
+ * Open:\r
+ *****************************************************************************/\r
+static int Open( vlc_object_t *p_this )\r
+{\r
+    aout_filter_t     *p_filter = (aout_filter_t *)p_this;\r
+    aout_filter_sys_t *p_sys;\r
+    vlc_bool_t         b_fit = VLC_TRUE;\r
+    int                i_samplerate;\r
+\r
+    if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) ||\r
+        p_filter->output.i_format != VLC_FOURCC('f','l','3','2') )\r
+    {\r
+        b_fit = VLC_FALSE;\r
+        p_filter->input.i_format = VLC_FOURCC('f','l','3','2');\r
+        p_filter->output.i_format = VLC_FOURCC('f','l','3','2');\r
+        msg_Warn( p_filter, "Bad input or output format" );\r
+    }\r
+    if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) )\r
+    {\r
+        b_fit = VLC_FALSE;\r
+        memcpy( &p_filter->output, &p_filter->input,\r
+                sizeof(audio_sample_format_t) );\r
+        msg_Warn( p_filter, "input and output formats are not similar" );\r
+    }\r
+\r
+    if ( ! b_fit )\r
+    {\r
+        return VLC_EGENERIC;\r
+    }\r
+\r
+    p_filter->pf_do_work = DoWork;\r
+    p_filter->b_in_place = VLC_TRUE;\r
+\r
+    /* Allocate structure */\r
+    p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) );\r
+\r
+    p_sys->f_lowf = config_GetFloat( p_this, "param-eq-lowf");\r
+    p_sys->f_lowgain = config_GetFloat( p_this, "param-eq-lowgain");\r
+    p_sys->f_highf = config_GetFloat( p_this, "param-eq-highf");\r
+    p_sys->f_highgain = config_GetFloat( p_this, "param-eq-highgain");\r
+    \r
+    p_sys->f_f1 = config_GetFloat( p_this, "param-eq-f1");\r
+    p_sys->f_Q1 = config_GetFloat( p_this, "param-eq-q1");\r
+    p_sys->f_gain1 = config_GetFloat( p_this, "param-eq-gain1");\r
+    \r
+    p_sys->f_f2 = config_GetFloat( p_this, "param-eq-f2");\r
+    p_sys->f_Q2 = config_GetFloat( p_this, "param-eq-q2");\r
+    p_sys->f_gain2 = config_GetFloat( p_this, "param-eq-gain2");\r
+\r
+    p_sys->f_f3 = config_GetFloat( p_this, "param-eq-f3");\r
+    p_sys->f_Q3 = config_GetFloat( p_this, "param-eq-q3");\r
+    p_sys->f_gain3 = config_GetFloat( p_this, "param-eq-gain3");\r
+  \r
+\r
+    i_samplerate = p_filter->input.i_rate;\r
+    CalcPeakEQCoeffs(p_sys->f_f1, p_sys->f_Q1, p_sys->f_gain1,\r
+                     i_samplerate, p_sys->coeffs+0*5);\r
+    CalcPeakEQCoeffs(p_sys->f_f2, p_sys->f_Q2, p_sys->f_gain2,\r
+                     i_samplerate, p_sys->coeffs+1*5);\r
+    CalcPeakEQCoeffs(p_sys->f_f3, p_sys->f_Q3, p_sys->f_gain3,\r
+                     i_samplerate, p_sys->coeffs+2*5);\r
+    CalcShelfEQCoeffs(p_sys->f_lowf, 1, p_sys->f_lowgain, 0,\r
+                      i_samplerate, p_sys->coeffs+3*5);\r
+    CalcShelfEQCoeffs(p_sys->f_highf, 1, p_sys->f_highgain, 0,\r
+                      i_samplerate, p_sys->coeffs+4*5);\r
+    p_sys->p_state = (float*)calloc( p_filter->input.i_channels*5*4,\r
+                                     sizeof(float) );\r
+\r
+    return VLC_SUCCESS;\r
+}\r
+\r
+static void Close( vlc_object_t *p_this )\r
+{\r
+    aout_filter_t *p_filter = (aout_filter_t *)p_this;\r
+    free( p_filter->p_sys->p_state );\r
+    free( p_filter->p_sys );\r
+}\r
+\r
+/*****************************************************************************\r
+ * DoWork: process samples buffer\r
+ *****************************************************************************\r
+ *\r
+ *****************************************************************************/\r
+static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter,\r
+                    aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf )\r
+{\r
+    p_out_buf->i_nb_samples = p_in_buf->i_nb_samples;\r
+    p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes;\r
+\r
+    ProcessEQ( (float*)p_in_buf->p_buffer, (float*)p_out_buf->p_buffer,\r
+               p_filter->p_sys->p_state, \r
+               p_filter->input.i_channels, p_in_buf->i_nb_samples,\r
+               p_filter->p_sys->coeffs, 5 );\r
+}\r
+\r
+/*\r
+ * Calculate direct form IIR coefficients for peaking EQ\r
+ * coeffs[0] = b0\r
+ * coeffs[1] = b1\r
+ * coeffs[2] = b2\r
+ * coeffs[3] = a1\r
+ * coeffs[4] = a2\r
+ *\r
+ * Equations taken from RBJ audio EQ cookbook \r
+ * (http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt)\r
+ */\r
+static void CalcPeakEQCoeffs( float f0, float Q, float gainDB, float Fs,\r
+                              float *coeffs )\r
+{\r
+    float A;\r
+    float w0;\r
+    float alpha;\r
+    float b0, b1, b2;\r
+    float a0, a1, a2;\r
+\r
+    // Provide sane limits to avoid overflow\r
+    if (Q < 0.1f) Q = 0.1f;   \r
+    if (Q > 100) Q = 100;\r
+    if (f0 > Fs/2*0.95f) f0 = Fs/2*0.95f;\r
+    if (gainDB < -40) gainDB = -40;\r
+    if (gainDB > 40) gainDB = 40;\r
+    \r
+    A = pow(10, gainDB/40);\r
+    w0 = 2*3.141593f*f0/Fs;\r
+    alpha = sin(w0)/(2*Q);\r
+    \r
+    b0 = 1 + alpha*A;\r
+    b1 = -2*cos(w0);\r
+    b2 = 1 - alpha*A;\r
+    a0 = 1 + alpha/A;\r
+    a1 = -2*cos(w0);\r
+    a2 = 1 - alpha/A;\r
+    \r
+    // Store values to coeffs and normalize by 1/a0\r
+    coeffs[0] = b0/a0;\r
+    coeffs[1] = b1/a0;\r
+    coeffs[2] = b2/a0;\r
+    coeffs[3] = a1/a0;\r
+    coeffs[4] = a2/a0;\r
+}\r
+\r
+/*\r
+ * Calculate direct form IIR coefficients for low/high shelf EQ\r
+ * coeffs[0] = b0\r
+ * coeffs[1] = b1\r
+ * coeffs[2] = b2\r
+ * coeffs[3] = a1\r
+ * coeffs[4] = a2\r
+ *\r
+ * Equations taken from RBJ audio EQ cookbook \r
+ * (http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt)\r
+ */\r
+static void CalcShelfEQCoeffs( float f0, float slope, float gainDB, int high,\r
+                               float Fs, float *coeffs )\r
+{\r
+    float A;\r
+    float w0;\r
+    float alpha;\r
+    float b0, b1, b2;\r
+    float a0, a1, a2;\r
+\r
+    // Provide sane limits to avoid overflow\r
+    if (f0 > Fs/2*0.95f) f0 = Fs/2*0.95f;\r
+    if (gainDB < -40) gainDB = -40;\r
+    if (gainDB > 40) gainDB = 40;\r
+\r
+    A = pow(10, gainDB/40);\r
+    w0 = 2*3.141593f*f0/Fs;\r
+    alpha = sin(w0)/2 * sqrt( (A + 1/A)*(1/slope - 1) + 2 );\r
+\r
+    if (high)\r
+    {\r
+        b0 =    A*( (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha );\r
+        b1 = -2*A*( (A-1) + (A+1)*cos(w0) );\r
+        b2 =    A*( (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha );\r
+        a0 =        (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha;\r
+        a1 =    2*( (A-1) - (A+1)*cos(w0) );\r
+        a2 =        (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha;\r
+    }\r
+    else\r
+    {\r
+        b0 =    A*( (A+1) - (A-1)*cos(w0) + 2*sqrt(A)*alpha );\r
+        b1 =  2*A*( (A-1) - (A+1)*cos(w0));\r
+        b2 =    A*( (A+1) - (A-1)*cos(w0) - 2*sqrt(A)*alpha );\r
+        a0 =        (A+1) + (A-1)*cos(w0) + 2*sqrt(A)*alpha;\r
+        a1 =   -2*( (A-1) + (A+1)*cos(w0));\r
+        a2 =        (A+1) + (A-1)*cos(w0) - 2*sqrt(A)*alpha;\r
+    }\r
+    // Store values to coeffs and normalize by 1/a0\r
+    coeffs[0] = b0/a0;\r
+    coeffs[1] = b1/a0;\r
+    coeffs[2] = b2/a0;\r
+    coeffs[3] = a1/a0;\r
+    coeffs[4] = a2/a0;\r
+}\r
+\r
+/*\r
+  src is assumed to be interleaved\r
+  dest is assumed to be interleaved\r
+  size of state is 4*channels*eqCount\r
+  samples is not premultiplied by channels\r
+  size of coeffs is 5*eqCount\r
+*/\r
+void ProcessEQ( float *src, float *dest, float *state, \r
+                int channels, int samples, float *coeffs, \r
+                int eqCount )\r
+{\r
+    int i, chn, eq;\r
+    float   b0, b1, b2, a1, a2;\r
+    float   x, y = 0;\r
+    float   *src1, *dest1;\r
+    float   *coeffs1, *state1;\r
+    src1 = src;\r
+    dest1 = dest;\r
+    for (i = 0; i < samples; i++)\r
+    {\r
+        state1 = state;\r
+        for (chn = 0; chn < channels; chn++)\r
+        {\r
+            coeffs1 = coeffs;\r
+            x = *src1++;\r
+            /* Direct form 1 IIRs */\r
+            for (eq = 0; eq < eqCount; eq++)\r
+            {\r
+                b0 = coeffs1[0];\r
+                b1 = coeffs1[1];\r
+                b2 = coeffs1[2];\r
+                a1 = coeffs1[3];\r
+                a2 = coeffs1[4];\r
+                coeffs1 += 5;\r
+                y = x*b0 + state1[0]*b1 + state1[1]*b2 - state1[2]*a1 - state1[3]*a2;\r
+                state1[1] = state1[0];\r
+                state1[0] = x;\r
+                state1[3] = state1[2];\r
+                state1[2] = y;\r
+                x = y;\r
+                state1 += 4;\r
+            }\r
+            *dest1++ = y;\r
+        }\r
+    }\r
+}\r
+\r