[vlc-devel] [PATCH] Add new API to libvlc for persistent equalizer settings.

Rémi Denis-Courmont remi at remlab.net
Mon Aug 27 22:43:53 CEST 2012


   Hello,

On Monday 27 August 2012, Mark Lee wrote:
> +libvlc_equalizer_t *libvlc_audio_equalizer_new_from_preset( unsigned
> u_index )
> +{
> +    libvlc_equalizer_t *p_equalizer;
> +
> +    if ( u_index >= NB_PRESETS )
> +        return NULL;
> +
> +    p_equalizer = libvlc_audio_equalizer_new();

This will needlessly zero the structure. You could use malloc() directly.

> +    if ( !p_equalizer )
> +        return NULL;

We usually wrap failed memory allocations with unlikely() for branch 
optimization.

> +
> +    p_equalizer->f_preamp = eqz_preset_10b[ u_index ].f_preamp;
> +
> +    for ( unsigned i = 0; i < EQZ_BANDS_MAX; i++ )
> +        p_equalizer->f_amp[ i ] = eqz_preset_10b[ u_index ].f_amp[ i ];
> +
> +    return p_equalizer;
> +}
> +
> +/*************************************************************************
> **** + * libvlc_audio_equalizer_release : Release a previously created
> equalizer +
> **************************************************************************
> ***/ +void libvlc_audio_equalizer_release( libvlc_equalizer_t *p_equalizer
> ) +{
> +    free( p_equalizer );
> +}
> +
> +/*************************************************************************
> **** + * libvlc_audio_equalizer_set_preamp : Set the preamp value for an
> equalizer +
> **************************************************************************
> ***/ +int libvlc_audio_equalizer_set_preamp( libvlc_equalizer_t
> *p_equalizer, float f_preamp ) +{
> +    if ( !p_equalizer )
> +        return -1;
> +
> +    if ( f_preamp < -20.0f )
> +        f_preamp = -20.0f;
> +    else if ( f_preamp > 20.0f )
> +        f_preamp = 20.0f;
> +
> +    p_equalizer->f_preamp = f_preamp;
> +    return 0;
> +}
> +
> +/*************************************************************************
> **** + * libvlc_audio_equalizer_get_preamp : Get the preamp value for an
> equalizer +
> **************************************************************************
> ***/ +float libvlc_audio_equalizer_get_preamp( libvlc_equalizer_t
> *p_equalizer ) +{
> +    if ( !p_equalizer )
> +        return 0.f;
> +
> +    return p_equalizer->f_preamp;
> +}
> +
> +/*************************************************************************
> **** + * libvlc_audio_equalizer_set_amp_at_index : Set the amplification
> value for an equalizer band +
> **************************************************************************
> ***/ +int libvlc_audio_equalizer_set_amp_at_index( libvlc_equalizer_t
> *p_equalizer, float f_amp, unsigned u_band ) +{
> +    if ( !p_equalizer )
> +        return -1;
> +
> +    if ( u_band >= EQZ_BANDS_MAX )
> +        return -1;
> +
> +    if ( f_amp < -20.0f )
> +        f_amp = -20.0f;
> +    else if ( f_amp > 20.0f )
> +        f_amp = 20.0f;
> +
> +    p_equalizer->f_amp[ u_band ] = f_amp;
> +    return 0;
> +}
> +
> +/*************************************************************************
> **** + * libvlc_audio_equalizer_get_amp_at_index : Get the amplification
> value for an equalizer band +
> **************************************************************************
> ***/ +float libvlc_audio_equalizer_get_amp_at_index( libvlc_equalizer_t
> *p_equalizer, unsigned u_band ) +{
> +    if ( !p_equalizer )
> +        return 0.f;
> +
> +    if ( u_band >= EQZ_BANDS_MAX )
> +        return 0.f;
> +
> +    return p_equalizer->f_amp[ u_band ];
> +}
> diff --git a/lib/media_player.c b/lib/media_player.c
> index a41b8c7..36d395f 100644
> --- a/lib/media_player.c
> +++ b/lib/media_player.c
> @@ -34,11 +34,14 @@
>  #include <vlc_input.h>
>  #include <vlc_vout.h>
>  #include <vlc_keys.h>
> +#include <vlc_aout_intf.h>
> 
>  #include "libvlc_internal.h"
>  #include "media_internal.h" // libvlc_media_set_state()
>  #include "media_player_internal.h"
> 
> +#include "modules/audio_filter/equalizer_presets.h"
> +
>  /*
>   * mapping of libvlc_navigate_mode_t to vlc_action_t
>   */
> @@ -482,6 +485,9 @@ libvlc_media_player_new( libvlc_instance_t *instance )
>      var_Create (mp, "amem-rate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
>      var_Create (mp, "amem-channels", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
> 
> +    var_Create (mp, "equalizer-preamp", VLC_VAR_FLOAT);
> +    var_Create (mp, "equalizer-bands", VLC_VAR_STRING);
> +
>      mp->p_md = NULL;
>      mp->state = libvlc_NothingSpecial;
>      mp->p_libvlc_instance = instance;
> @@ -1409,3 +1415,33 @@ void libvlc_media_player_next_frame(
> libvlc_media_player_t *p_mi ) vlc_object_release( p_input_thread );
>      }
>  }
> +
> +int libvlc_media_player_set_equalizer( libvlc_media_player_t *p_mi,
> libvlc_equalizer_t *p_equalizer ) +{
> +    if ( !p_equalizer )
> +    {
> +        aout_EnableFilter( p_mi, "equalizer", false );
> +        return 0;
> +    }
> +
> +    aout_EnableFilter( p_mi, "equalizer", true );

For efficiency, the filter ought to be enabled _after_ it is configured, rather 
than before.

> +
> +    var_SetFloat( p_mi, "equalizer-preamp", p_equalizer->f_preamp );
> +
> +    char *psz_bands = NULL;
> +    for ( int i = 0; i < EQZ_BANDS_MAX; i++ )
> +    {
> +        char *psz;
> +        if ( asprintf( &psz, "%s %.07f", psz_bands ? psz_bands : "",
> p_equalizer->f_amp[i] ) == -1 ) +        {
> +            free( psz_bands );
> +            return -1;
> +        }
> +        free( psz_bands );
> +        psz_bands = psz;
> +    }
> +    var_SetString( p_mi, "equalizer-bands", psz_bands );
> +    free( psz_bands );
> +
> +    return 0;
> +}

-- 
Rémi Denis-Courmont
http://www.remlab.info



More information about the vlc-devel mailing list