X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Faudio_filter%2Fequalizer.c;h=40bbcc16b30e5115cb3b0f3acb5cd5b11b992d5d;hb=7ab733f2c39d5f2c56d3f52724d5b2274cede1ec;hp=df8168af858e3dd7fcafd5c80ee8420bae6ed931;hpb=99fab9089e9e1709d9c3a4bc5ced0c137ac59134;p=vlc diff --git a/modules/audio_filter/equalizer.c b/modules/audio_filter/equalizer.c index df8168af85..40bbcc16b3 100644 --- a/modules/audio_filter/equalizer.c +++ b/modules/audio_filter/equalizer.c @@ -24,13 +24,15 @@ /***************************************************************************** * Preamble *****************************************************************************/ -#include #ifdef HAVE_CONFIG_H # include "config.h" #endif -#include +#include + +#include +#include #include "vlc_aout.h" @@ -67,30 +69,30 @@ static void Close( vlc_object_t * ); #define PREAMP_TEXT N_("Global gain" ) #define PREAMP_LONGTEXT N_("Set the global gain in dB (-20 ... 20)." ) -vlc_module_begin(); - set_description( _("Equalizer with 10 bands") ); - set_shortname( _("Equalizer" ) ); - set_capability( "audio filter", 0 ); - set_category( CAT_AUDIO ); - set_subcategory( SUBCAT_AUDIO_AFILTER ); +vlc_module_begin () + set_description( N_("Equalizer with 10 bands") ) + set_shortname( N_("Equalizer" ) ) + set_capability( "audio filter", 0 ) + set_category( CAT_AUDIO ) + set_subcategory( SUBCAT_AUDIO_AFILTER ) add_string( "equalizer-preset", "flat", NULL, PRESET_TEXT, - PRESET_LONGTEXT, VLC_FALSE ); - change_string_list( preset_list, preset_list_text, 0 ); + PRESET_LONGTEXT, false ) + change_string_list( preset_list, preset_list_text, 0 ) add_string( "equalizer-bands", NULL, NULL, BANDS_TEXT, - BANDS_LONGTEXT, VLC_TRUE ); + BANDS_LONGTEXT, true ) add_bool( "equalizer-2pass", 0, NULL, TWOPASS_TEXT, - TWOPASS_LONGTEXT, VLC_TRUE ); + TWOPASS_LONGTEXT, true ) add_float( "equalizer-preamp", 12.0, NULL, PREAMP_TEXT, - PREAMP_LONGTEXT, VLC_TRUE ); - set_callbacks( Open, Close ); - add_shortcut( "equalizer" ); -vlc_module_end(); + PREAMP_LONGTEXT, true ) + set_callbacks( Open, Close ) + add_shortcut( "equalizer" ) +vlc_module_end () /***************************************************************************** * Local prototypes *****************************************************************************/ -typedef struct aout_filter_sys_t +struct aout_filter_sys_t { /* Filter static config */ int i_band; @@ -100,12 +102,12 @@ typedef struct aout_filter_sys_t float f_newpreamp; char *psz_newbands; - vlc_bool_t b_first; + bool b_first; /* Filter dyn config */ float *f_amp; /* Per band amp */ float f_gamp; /* Global preamp */ - vlc_bool_t b_2eqz; + bool b_2eqz; /* Filter state */ float x[32][2]; @@ -115,14 +117,14 @@ typedef struct aout_filter_sys_t float x2[32][2]; float y2[32][128][2]; -} aout_filter_sys_t; +}; static void DoWork( aout_instance_t *, aout_filter_t *, aout_buffer_t *, aout_buffer_t * ); #define EQZ_IN_FACTOR (0.25) static int EqzInit( aout_filter_t *, int ); -static void EqzFilter( aout_instance_t *,aout_filter_t *, float *, float *, +static void EqzFilter( aout_filter_t *, float *, float *, int, int ); static void EqzClean( aout_filter_t * ); @@ -142,19 +144,19 @@ static int Open( vlc_object_t *p_this ) { aout_filter_t *p_filter = (aout_filter_t *)p_this; aout_filter_sys_t *p_sys; - vlc_bool_t b_fit = VLC_TRUE; + bool b_fit = true; if( p_filter->input.i_format != VLC_FOURCC('f','l','3','2' ) || p_filter->output.i_format != VLC_FOURCC('f','l','3','2') ) { - b_fit = VLC_FALSE; + b_fit = false; p_filter->input.i_format = VLC_FOURCC('f','l','3','2'); p_filter->output.i_format = VLC_FOURCC('f','l','3','2'); msg_Warn( p_filter, "bad input or output format" ); } if ( !AOUT_FMTS_SIMILAR( &p_filter->input, &p_filter->output ) ) { - b_fit = VLC_FALSE; + b_fit = false; memcpy( &p_filter->output, &p_filter->input, sizeof(audio_sample_format_t) ); msg_Warn( p_filter, "input and output formats are not similar" ); @@ -166,13 +168,18 @@ static int Open( vlc_object_t *p_this ) } p_filter->pf_do_work = DoWork; - p_filter->b_in_place = VLC_TRUE; + p_filter->b_in_place = true; /* Allocate structure */ p_sys = p_filter->p_sys = malloc( sizeof( aout_filter_sys_t ) ); + if( !p_sys ) + return VLC_ENOMEM; - if( EqzInit( p_filter, p_filter->input.i_rate ) ) + if( EqzInit( p_filter, p_filter->input.i_rate ) != VLC_SUCCESS ) + { + free( p_sys ); return VLC_EGENERIC; + } return VLC_SUCCESS; } @@ -197,10 +204,11 @@ static void Close( vlc_object_t *p_this ) static void DoWork( aout_instance_t * p_aout, aout_filter_t * p_filter, aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf ) { + VLC_UNUSED(p_aout); p_out_buf->i_nb_samples = p_in_buf->i_nb_samples; p_out_buf->i_nb_bytes = p_in_buf->i_nb_bytes; - EqzFilter( p_aout, p_filter, (float*)p_out_buf->p_buffer, + EqzFilter( p_filter, (float*)p_out_buf->p_buffer, (float*)p_in_buf->p_buffer, p_in_buf->i_nb_samples, aout_FormatNbChannels( &p_filter->input ) ); } @@ -302,6 +310,14 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) p_sys->f_alpha = malloc( p_sys->i_band * sizeof(float) ); p_sys->f_beta = malloc( p_sys->i_band * sizeof(float) ); p_sys->f_gamma = malloc( p_sys->i_band * sizeof(float) ); + if( !p_sys->f_alpha || !p_sys->f_beta || !p_sys->f_gamma ) + { + free( p_sys->f_alpha ); + free( p_sys->f_beta ); + free( p_sys->f_gamma ); + return VLC_ENOMEM; + } + for( i = 0; i < p_sys->i_band; i++ ) { p_sys->f_alpha[i] = p_cfg->band[i].f_alpha; @@ -310,9 +326,16 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) } /* Filter dyn config */ - p_sys->b_2eqz = VLC_FALSE; + p_sys->b_2eqz = false; p_sys->f_gamp = 1.0; - p_sys->f_amp = malloc( p_sys->i_band * sizeof(float) ); + p_sys->f_amp = malloc( p_sys->i_band * sizeof(float) ); + if( !p_sys->f_amp ) + { + free( p_sys->f_alpha ); + free( p_sys->f_beta ); + free( p_sys->f_gamma ); + return VLC_ENOMEM; + } for( i = 0; i < p_sys->i_band; i++ ) { p_sys->f_amp[i] = 0.0; @@ -335,23 +358,25 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) } } - var_CreateGetString( p_aout,"equalizer-bands" ); - var_CreateGetString( p_aout, "equalizer-preset" ); + var_Create( p_aout, "equalizer-bands", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); + var_Create( p_aout, "equalizer-preset", VLC_VAR_STRING | VLC_VAR_DOINHERIT ); p_sys->b_2eqz = var_CreateGetBool( p_aout, "equalizer-2pass" ); - var_CreateGetFloat( p_aout, "equalizer-preamp" ); + var_Create( p_aout, "equalizer-preamp", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT ); /* Get initial values */ var_Get( p_aout, "equalizer-preset", &val1 ); var_Get( p_aout, "equalizer-bands", &val2 ); var_Get( p_aout, "equalizer-preamp", &val3 ); - p_sys->b_first = VLC_TRUE; + p_sys->b_first = true; PresetCallback( VLC_OBJECT( p_aout ), NULL, val1, val1, p_sys ); BandsCallback( VLC_OBJECT( p_aout ), NULL, val2, val2, p_sys ); PreampCallback( VLC_OBJECT( p_aout ), NULL, val3, val3, p_sys ); - p_sys->b_first = VLC_FALSE; + p_sys->b_first = false; + + free( val1.psz_string ); /* Register preset bands (for intf) if : */ /* We have no bands info --> the preset info must be given to the intf */ @@ -359,7 +384,12 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) if (p_sys->psz_newbands == NULL) { msg_Err(p_filter, "No preset selected"); - return (VLC_EGENERIC); + free( val2.psz_string ); + free( p_sys->f_amp ); + free( p_sys->f_alpha ); + free( p_sys->f_beta ); + free( p_sys->f_gamma ); + return VLC_EGENERIC; } if( ( *(val2.psz_string) && strstr( p_sys->psz_newbands, val2.psz_string ) ) || !*val2.psz_string ) @@ -368,6 +398,7 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) if( p_sys->f_newpreamp == p_sys->f_gamp ) var_SetFloat( p_aout, "equalizer-preamp", p_sys->f_newpreamp ); } + free( val2.psz_string ); /* Add our own callbacks */ var_AddCallback( p_aout, "equalizer-preset", PresetCallback, p_sys ); @@ -385,8 +416,7 @@ static int EqzInit( aout_filter_t *p_filter, int i_rate ) return VLC_SUCCESS; } -static void EqzFilter( aout_instance_t *p_aout, - aout_filter_t *p_filter, float *out, float *in, +static void EqzFilter( aout_filter_t *p_filter, float *out, float *in, int i_samples, int i_channels ) { aout_filter_sys_t *p_sys = p_filter->p_sys; @@ -463,68 +493,71 @@ static void EqzClean( aout_filter_t *p_filter ) free( p_sys->f_gamma ); free( p_sys->f_amp ); + free( p_sys->psz_newbands ); } static int PresetCallback( vlc_object_t *p_this, char const *psz_cmd, vlc_value_t oldval, vlc_value_t newval, void *p_data ) { + VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data; aout_instance_t *p_aout = (aout_instance_t *)p_this; char *psz_preset = newval.psz_string; - char psz_newbands[120]; - memset( psz_newbands, 0, 120 ); + if( !*psz_preset || p_sys->i_band != 10 ) + return VLC_SUCCESS; - if( *psz_preset && p_sys->i_band == 10 ) + for( unsigned i = 0; eqz_preset_10b[i] != NULL; i++ ) { - int i; - /* */ - for( i = 0; eqz_preset_10b[i] != NULL; i++ ) + if( !strcasecmp( eqz_preset_10b[i]->psz_name, psz_preset ) ) { - if( !strcasecmp( eqz_preset_10b[i]->psz_name, psz_preset ) ) + char *psz_newbands = NULL; + + p_sys->f_gamp *= pow( 10, eqz_preset_10b[i]->f_preamp / 20.0 ); + for( int j = 0; j < p_sys->i_band; j++ ) { - int j; - p_sys->f_gamp *= pow( 10, eqz_preset_10b[i]->f_preamp / 20.0 ); - for( j = 0; j < p_sys->i_band; j++ ) - { - lldiv_t div; - p_sys->f_amp[j] = EqzConvertdB( - eqz_preset_10b[i]->f_amp[j] ); - div = lldiv( eqz_preset_10b[i]->f_amp[j] * 10000000, - 10000000 ); - sprintf( psz_newbands, "%s "I64Fd".%07u", psz_newbands, - (int64_t)div.quot, (unsigned int) div.rem ); - } - if( p_sys->b_first == VLC_FALSE ) + lldiv_t d; + char *psz; + + p_sys->f_amp[j] = EqzConvertdB( eqz_preset_10b[i]->f_amp[j] ); + d = lldiv( eqz_preset_10b[i]->f_amp[j] * 10000000, 10000000 ); + if( asprintf( &psz, "%s %lld.%07llu", + psz_newbands ? psz_newbands : "", + d.quot, d.rem ) == -1 ) { - var_SetString( p_aout, "equalizer-bands", psz_newbands ); - var_SetFloat( p_aout, "equalizer-preamp", - eqz_preset_10b[i]->f_preamp ); + free( psz_newbands ); + return VLC_ENOMEM; } - else - { - p_sys->psz_newbands = strdup( psz_newbands ); - p_sys->f_newpreamp = eqz_preset_10b[i]->f_preamp; - } - break; + psz_newbands = psz; } - } - if( eqz_preset_10b[i] == NULL ) - { - msg_Err( p_aout, "equalizer preset '%s' not found", psz_preset ); - msg_Dbg( p_aout, "full list:" ); - for( i = 0; eqz_preset_10b[i] != NULL; i++ ) - msg_Dbg( p_aout, " - '%s'", eqz_preset_10b[i]->psz_name ); + if( p_sys->b_first == false ) + { + var_SetString( p_aout, "equalizer-bands", psz_newbands ); + var_SetFloat( p_aout, "equalizer-preamp", + eqz_preset_10b[i]->f_preamp ); + free( psz_newbands ); + } + else + { + p_sys->psz_newbands = psz_newbands; + p_sys->f_newpreamp = eqz_preset_10b[i]->f_preamp; + } + return VLC_SUCCESS; } } + msg_Err( p_aout, "equalizer preset '%s' not found", psz_preset ); + msg_Info( p_aout, "full list:" ); + for( unsigned i = 0; eqz_preset_10b[i] != NULL; i++ ) + msg_Info( p_aout, " - '%s'", eqz_preset_10b[i]->psz_name ); return VLC_SUCCESS; } static int PreampCallback( vlc_object_t *p_this, char const *psz_cmd, vlc_value_t oldval, vlc_value_t newval, void *p_data ) { + VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data; if( newval.f_float < -20.0 ) @@ -539,31 +572,36 @@ static int PreampCallback( vlc_object_t *p_this, char const *psz_cmd, static int BandsCallback( vlc_object_t *p_this, char const *psz_cmd, vlc_value_t oldval, vlc_value_t newval, void *p_data ) { + VLC_UNUSED(p_this); VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); aout_filter_sys_t *p_sys = (aout_filter_sys_t *)p_data; char *psz_bands = newval.psz_string; + char *psz_next; + char *p = psz_bands; + int i; /* Same thing for bands */ - if( *psz_bands ) + for( i = 0; i < p_sys->i_band; i++ ) { - char *p = psz_bands, *p_next; - int i; + float f; - for( i = 0; i < p_sys->i_band; i++ ) - { - /* Read dB -20/20 */ + if( *psz_bands == '\0' ) + break; + + /* Read dB -20/20 */ #ifdef HAVE_STRTOF - float f = strtof( p, &p_next ); + f = strtof( p, &psz_next ); #else - float f = (float) strtod( p, &p_next ); + f = (float)strtod( p, &psz_next ); #endif - if( !p_next || p_next == p ) break; /* strtof() failed */ + if( psz_next == p ) + break; /* no conversion */ - p_sys->f_amp[i] = EqzConvertdB( f ); + p_sys->f_amp[i] = EqzConvertdB( f ); - if( !*p ) break; /* end of line */ - p=p_next+1; - } + if( *psz_next == '\0' ) + break; /* end of line */ + p = &psz_next[1]; } - return VLC_SUCCESS; } +