1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2011 VideoLAN and VLC authors
7 * Authors: Akash Mehrotra < mehrotra <dot> akash <at> gmail <dot> com >
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_charset.h>
43 #include "../../audio_filter/equalizer_presets.h"
57 /*****************************************************************************
58 * Get the preamp level
59 *****************************************************************************/
60 static int vlclua_preamp_get( lua_State *L )
62 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
63 audio_output_t *p_aout = playlist_GetAout( p_playlist );
67 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
68 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
71 vlc_object_release( p_aout );
76 lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
77 vlc_object_release( p_aout );
82 /*****************************************************************************
83 * Set the preamp level
84 *****************************************************************************/
85 static int vlclua_preamp_set( lua_State *L )
87 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
88 audio_output_t *p_aout = playlist_GetAout( p_playlist );
92 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
93 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
96 vlc_object_release( p_aout );
101 var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
102 vlc_object_release( p_aout );
107 /*****************************************************************************
119 *****************************************************************************/
120 /*****************************************************************************
121 * Return EQ level for all bands as a Table
122 *****************************************************************************/
123 static int vlclua_equalizer_get( lua_State *L )
125 const unsigned bands = 10;
127 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
128 audio_output_t *p_aout = playlist_GetAout( p_playlist );
132 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
133 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
136 vlc_object_release( p_aout );
141 char *psz_bands_origin, *psz_bands;
142 psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
145 vlc_object_release( p_aout );
150 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
151 locale_t oldloc = uselocale (loc);
153 for( unsigned i = 0; i < bands; i++ )
155 float level = strtof( psz_bands, &psz_bands );
157 if( asprintf( &str , "%f" , level ) == -1 )
162 lua_pushstring( L, str );
164 if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
169 lua_setfield( L , -2 , str );
173 free( psz_bands_origin );
174 if( loc != (locale_t)0 )
179 vlc_object_release( p_aout );
180 return error ? 0 : 1;
184 /*****************************************************************************
185 * Set the equalizer level for the specified band
186 *****************************************************************************/
187 static int vlclua_equalizer_set( lua_State *L )
189 int bandid = luaL_checknumber( L, 1 );
190 if( bandid < 0 || bandid > 9)
193 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
194 audio_output_t *p_aout = playlist_GetAout( p_playlist );
198 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
199 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
202 vlc_object_release( p_aout );
207 float level = luaL_checknumber( L, 2 );
208 char *bands = var_GetString( p_aout, "equalizer-bands" );
210 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
211 locale_t oldloc = uselocale (loc);
215 float dummy = strtof( b, &b );
221 float dummy = strtof( b, &b );
225 if( asprintf( &newstr, "%s %.1f%s", bands, level, b ) != -1 )
227 var_SetString( p_aout, "equalizer-bands", newstr );
231 if( loc != (locale_t)0 )
237 vlc_object_release( p_aout );
241 /*****************************************************************************
242 * Set the preset specified by preset id
243 *****************************************************************************/
244 static int vlclua_equalizer_setpreset( lua_State *L )
246 int presetid = luaL_checknumber( L, 1 );
247 if( presetid >= NB_PRESETS || presetid < 0 )
250 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
251 audio_output_t *p_aout = playlist_GetAout( p_playlist );
256 char *psz_af = var_InheritString( p_aout, "audio-filter" );
257 if( psz_af != NULL && strstr ( psz_af, "equalizer" ) != NULL )
259 var_SetString( p_aout , "equalizer-preset" , preset_list[presetid] );
263 vlc_object_release( p_aout );
267 /****************************************************************************
268 * Enable/disable Equalizer
269 *****************************************************************************/
270 static int vlclua_equalizer_enable ( lua_State *L )
272 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
273 bool state = luaL_checkboolean ( L , 1 );
274 playlist_EnableAudioFilter( p_playlist, "equalizer", state );
277 /*****************************************************************************
279 *****************************************************************************/
280 static int vlclua_equalizer_get_presets( lua_State *L )
284 for( int i = 0 ; i < NB_PRESETS ; i++ )
286 lua_pushstring( L, preset_list_text[i] );
287 if( asprintf( &str , "preset id=\"%d\"",i ) == -1 )
289 lua_setfield( L , -2 , str );
294 static const luaL_Reg vlclua_equalizer_reg[] = {
295 { "preampget", vlclua_preamp_get },
296 { "preampset", vlclua_preamp_set },
297 { "equalizerget", vlclua_equalizer_get },
298 { "equalizerset", vlclua_equalizer_set },
299 { "enable", vlclua_equalizer_enable },
300 { "presets",vlclua_equalizer_get_presets },
301 { "setpreset", vlclua_equalizer_setpreset },
306 void luaopen_equalizer( lua_State *L )
309 luaL_register( L, NULL, vlclua_equalizer_reg );
310 lua_setfield( L, -2, "equalizer" );