[vlc-devel] [PATCH 5/8] lua: Update equalizer support

Ronald Wright logiconcepts819 at gmail.com
Tue Dec 1 14:57:23 CET 2015


---
 modules/lua/libs/equalizer.c | 243 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 180 insertions(+), 63 deletions(-)

diff --git a/modules/lua/libs/equalizer.c b/modules/lua/libs/equalizer.c
index 512bae1..d3099b8 100644
--- a/modules/lua/libs/equalizer.c
+++ b/modules/lua/libs/equalizer.c
@@ -36,23 +36,13 @@
 #include <vlc_aout.h>
 #include <vlc_input.h>
 #include <vlc_charset.h>
+#include <vlc_eqz_util.h>
 
 #include "input.h"
 #include "../libs.h"
 #include "../vlc.h"
 #include "../../audio_filter/equalizer_presets.h"
 
-#if !defined _WIN32
-# include <locale.h>
-#else
-# include <windows.h>
-#endif
-
-#ifdef __APPLE__
-#   include <string.h>
-#   include <xlocale.h>
-#endif
-
 
 /*****************************************************************************
 * Get the preamp level
@@ -106,6 +96,8 @@ static int vlclua_preamp_set( lua_State *L )
 
 /*****************************************************************************
 Bands:
+
+10-band VLC equalizer:
 Band 0:  60 Hz
 Band 1: 170 Hz
 Band 2: 310 Hz
@@ -116,14 +108,74 @@ Band 6:  6 kHz
 Band 7: 12 kHz
 Band 8: 14 kHz
 Band 9: 16 kHz
+
+10-band ISO equalizer:
+Band 0:  31.5 Hz
+Band 1:  63   Hz
+Band 2: 125   Hz
+Band 3: 250   Hz
+Band 4: 500   Hz
+Band 5:   1  kHz
+Band 6:   2  kHz
+Band 7:   4  kHz
+Band 8:   8  kHz
+Band 9:  16  kHz
+
+15-band ISO equalizer:
+Band  0:  25  Hz
+Band  1:  40  Hz
+Band  2:  63  Hz
+Band  3: 100  Hz
+Band  4: 160  Hz
+Band  5: 250  Hz
+Band  6: 400  Hz
+Band  7: 630  Hz
+Band  8:   1 kHz
+Band  9: 1.6 kHz
+Band 10: 2.5 kHz
+Band 11:   4 kHz
+Band 12: 6.3 kHz
+Band 13:  10 kHz
+Band 14:  16 kHz
+
+31-band ISO equalizer:
+Band  0:  20     Hz
+Band  1:  25     Hz
+Band  2:  31.5   Hz
+Band  3:  40     Hz
+Band  4:  50     Hz
+Band  5:  63     Hz
+Band  6:  80     Hz
+Band  7: 100     Hz
+Band  8: 125     Hz
+Band  9: 160     Hz
+Band 10: 200     Hz
+Band 11: 250     Hz
+Band 12: 315     Hz
+Band 13: 400     Hz
+Band 14: 500     Hz
+Band 15: 630     Hz
+Band 16: 800     Hz
+Band 17:   1    kHz
+Band 18:   1.25 kHz
+Band 19:   1.6  kHz
+Band 20:   2    kHz
+Band 21:   2.5  kHz
+Band 22:   3.15 kHz
+Band 23:   4    kHz
+Band 24:   5    kHz
+Band 25:   6.3  kHz
+Band 26:   8    kHz
+Band 27:  10    kHz
+Band 28:  12.5  kHz
+Band 29:  16    kHz
+Band 30:  20    kHz
 *****************************************************************************/
 /*****************************************************************************
 * Return EQ level for all bands as a Table
 *****************************************************************************/
 static int vlclua_equalizer_get( lua_State *L )
 {
-    const unsigned bands = 10;
-
     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
     audio_output_t *p_aout = playlist_GetAout( p_playlist );
     if( p_aout == NULL )
@@ -138,44 +190,58 @@ static int vlclua_equalizer_get( lua_State *L )
     }
     free( psz_af );
 
-    char *psz_bands_origin, *psz_bands;
-    psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
+    char *psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
     if( !psz_bands )
     {
         vlc_object_release( p_aout );
         return 0;
     }
 
-    bool error = false;
-    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
-    locale_t oldloc = uselocale (loc);
-    lua_newtable( L );
-    for( unsigned i = 0; i < bands; i++ )
+    char *psz_eqz_type = var_InheritString( p_aout, "equalizer-type" );
+    if( !psz_eqz_type )
     {
-        float level = strtof( psz_bands, &psz_bands );
-        char *str;
-        if( asprintf( &str , "%f" , level ) == -1 )
-        {
-            error = true;
-            break;
-        }
-        lua_pushstring( L, str );
-        free(str);
-        if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
-        {
-            error = true;
-            break;
-        }
-        lua_setfield( L , -2 , str );
-        free( str );
+        free( psz_bands );
+        vlc_object_release( p_aout );
+        return 0;
     }
 
-    free( psz_bands_origin );
-    if( loc != (locale_t)0 )
+    int i_type = EqzGetTypeNumber( psz_eqz_type );
+    if( i_type == EQZ_UNKNOWN_TYPE )
+        i_type = EQZ_VLC10_TYPE;
+
+    vlc_band_parser_t *p_ctx;
+    bool error = vlc_band_parser_init( VLC_OBJECT( p_aout ), &p_ctx,
+        psz_bands, i_type, NULL ) != VLC_SUCCESS;
+    if( !error )
     {
-        uselocale (oldloc);
-        freelocale (loc);
+        int i_bands = EqzGetNumBandsByType( i_type );
+        const float *pf_levels = vlc_get_amp_array( p_ctx );
+
+        lua_newtable( L );
+
+        for( int i = 0; i < i_bands; i++ )
+        {
+            float level = pf_levels[i];
+            char *str;
+            if( asprintf( &str , "%f" , level ) == -1 )
+            {
+                error = true;
+                break;
+            }
+            lua_pushstring( L, str );
+            free(str);
+            if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
+            {
+                error = true;
+                break;
+            }
+            lua_setfield( L , -2 , str );
+            free( str );
+        }
+        vlc_band_parser_destroy( p_ctx );
     }
+    free( psz_bands );
+    free( psz_eqz_type );
     vlc_object_release( p_aout );
     return error ? 0 : 1;
 }
@@ -187,7 +253,7 @@ static int vlclua_equalizer_get( lua_State *L )
 static int vlclua_equalizer_set( lua_State *L )
 {
     int bandid = luaL_checknumber( L, 1 );
-    if( bandid < 0 || bandid > 9)
+    if( bandid < 0 )
         return 0;
 
     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
@@ -204,41 +270,91 @@ static int vlclua_equalizer_set( lua_State *L )
     }
     free( psz_af );
 
+    char *psz_eqz_type = var_InheritString( p_aout, "equalizer-type" );
+    int i_type = EqzGetTypeNumber( psz_eqz_type );
+    if( i_type == EQZ_UNKNOWN_TYPE )
+        i_type = EQZ_VLC10_TYPE;
+    int i_bands = EqzGetNumBandsByType( i_type );
+    if( bandid >= i_bands )
+    {
+        vlc_object_release( p_aout );
+        return 0;
+    }
+
     float level = luaL_checknumber( L, 2 );
     char *bands = var_GetString( p_aout, "equalizer-bands" );
 
-    locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
-    locale_t oldloc = uselocale (loc);
-    char *b = bands;
-    while( bandid > 0 )
+    vlc_band_parser_t *p_ctx;
+    if( vlc_band_parser_init( VLC_OBJECT( p_aout ), &p_ctx,
+        bands, i_type, NULL ) == VLC_SUCCESS )
     {
-        float dummy = strtof( b, &b );
-        (void)dummy;
-        bandid--;
-    }
-    if( *b != '\0' )
-        *b++ = '\0';
-    float dummy = strtof( b, &b );
-    (void)dummy;
+        const float * pf_levels = vlc_get_amp_array( p_ctx );
+        char *newstr = NULL;
 
-    char *newstr;
-    if( asprintf( &newstr, "%s %.1f%s", bands, level, b ) != -1 )
-    {
-        var_SetString( p_aout, "equalizer-bands", newstr );
-        free( newstr );
-    }
+        for( int i = 0; i < i_bands; i++ )
+        {
+            float f_level = i == bandid ? level : pf_levels[i];
+            char *psz;
+            if( asprintf( &psz, "%s %.1f", i ? newstr : "", f_level ) == -1 )
+                psz = NULL;
+
+            free( newstr );
+            newstr = psz;
+            if( unlikely( psz == NULL ) )
+                break;
+        }
+        vlc_band_parser_destroy( p_ctx );
 
-    if( loc != (locale_t)0 )
-    {
-        uselocale (oldloc);
-        freelocale (loc);
+        /* For 10 bands, we must help VLC differentiate between VLC bands and
+         * ISO bands */
+        if( likely( newstr ) && i_bands == EQZ_ISO10_BANDS_MAX )
+        {
+            char id = EqzGetIdentifier( i_type );
+            char * psz;
+            if( asprintf( &psz, "%s %c", newstr, id ) == -1 )
+                psz = NULL;
+
+            free( newstr );
+            newstr = psz;
+        }
+
+        if( likely( newstr ) )
+        {
+            var_SetString( p_aout, "equalizer-bands", newstr );
+            free( newstr );
+        }
     }
     free( bands );
+    free( psz_eqz_type );
     vlc_object_release( p_aout );
     return 0;
 }
 
 /*****************************************************************************
+* Get whether VLC frequencies are active
+*****************************************************************************/
+static int vlclua_usevlcfreqs_get( lua_State *L )
+{
+    playlist_t *p_playlist = vlclua_get_playlist_internal( L );
+    audio_output_t *p_aout = playlist_GetAout( p_playlist );
+    if( p_aout == NULL )
+        return 0;
+
+    char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
+    if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
+    {
+        free( psz_af );
+        vlc_object_release( p_aout );
+        return 0;
+    }
+    free( psz_af );
+
+    lua_pushboolean( L, var_InheritBool( p_aout, "equalizer-vlcfreqs") );
+    vlc_object_release( p_aout );
+    return 1;
+}
+
+/*****************************************************************************
 * Set the preset specified by preset id
 *****************************************************************************/
 static int vlclua_equalizer_setpreset( lua_State *L )
@@ -296,6 +412,7 @@ static const luaL_Reg vlclua_equalizer_reg[] = {
     { "preampset", vlclua_preamp_set },
     { "equalizerget", vlclua_equalizer_get },
     { "equalizerset", vlclua_equalizer_set },
+    { "usingvlcfreqsget", vlclua_usevlcfreqs_get },
     { "enable", vlclua_equalizer_enable },
     { "presets",vlclua_equalizer_get_presets },
     { "setpreset", vlclua_equalizer_setpreset },
-- 
1.9.1



More information about the vlc-devel mailing list