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_aout_intf.h>
38 #include <vlc_input.h>
39 #include <vlc_charset.h>
45 #include "../../audio_filter/equalizer_presets.h"
59 /*****************************************************************************
60 * Get the preamp level
61 *****************************************************************************/
62 static int vlclua_preamp_get( lua_State *L )
64 input_thread_t *p_input = vlclua_get_input_internal( L );
68 audio_output_t *p_aout = input_GetAout( p_input );
69 vlc_object_release( p_input );
74 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
75 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
78 vlc_object_release( p_aout );
83 lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
84 vlc_object_release( p_aout );
89 /*****************************************************************************
90 * Set the preamp level
91 *****************************************************************************/
92 static int vlclua_preamp_set( lua_State *L )
94 input_thread_t *p_input = vlclua_get_input_internal( L );
98 audio_output_t *p_aout = input_GetAout( p_input );
99 vlc_object_release( p_input );
103 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
104 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
107 vlc_object_release( p_aout );
112 var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
113 vlc_object_release( p_aout );
118 /*****************************************************************************
130 *****************************************************************************/
131 /*****************************************************************************
132 * Return EQ level for all bands as a Table
133 *****************************************************************************/
134 static int vlclua_equalizer_get( lua_State *L )
136 const unsigned bands = 10;
137 input_thread_t *p_input = vlclua_get_input_internal( L );
140 audio_output_t *p_aout = input_GetAout( p_input );
141 vlc_object_release( p_input );
145 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
146 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
149 vlc_object_release( p_aout );
154 char *psz_bands_origin, *psz_bands;
155 psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
158 vlc_object_release( p_aout );
163 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
164 locale_t oldloc = uselocale (loc);
166 for( unsigned i = 0; i < bands; i++ )
168 float level = strtof( psz_bands, &psz_bands );
170 if( asprintf( &str , "%f" , level ) == -1 )
175 lua_pushstring( L, str );
177 if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
182 lua_setfield( L , -2 , str );
186 free( psz_bands_origin );
187 if( loc != (locale_t)0 )
192 vlc_object_release( p_aout );
193 return error ? 0 : 1;
197 /*****************************************************************************
198 * Set the equalizer level for the specified band
199 *****************************************************************************/
200 static int vlclua_equalizer_set( lua_State *L )
202 int bandid = luaL_checknumber( L, 1 );
203 if( bandid < 0 || bandid > 9)
205 input_thread_t *p_input = vlclua_get_input_internal( L );
209 audio_output_t *p_aout = input_GetAout( p_input );
210 vlc_object_release( p_input );
214 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
215 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
218 vlc_object_release( p_aout );
223 float level = luaL_checknumber( L, 2 );
224 char *bands = var_GetString( p_aout, "equalizer-bands" );
226 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
227 locale_t oldloc = uselocale (loc);
231 float dummy = strtof( b, &b );
237 float dummy = strtof( b, &b );
241 if( asprintf( &newstr, "%s %.1f%s", bands, level, b ) != -1 )
243 var_SetString( p_aout, "equalizer-bands", newstr );
247 if( loc != (locale_t)0 )
253 vlc_object_release( p_aout );
257 /*****************************************************************************
258 * Set the preset specified by preset id
259 *****************************************************************************/
260 static int vlclua_equalizer_setpreset( lua_State *L )
262 int presetid = luaL_checknumber( L, 1 );
263 if( presetid >= NB_PRESETS || presetid < 0 )
265 input_thread_t *p_input = vlclua_get_input_internal( L );
268 audio_output_t *p_aout = input_GetAout( p_input );
269 vlc_object_release( p_input );
274 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
275 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
278 vlc_object_release( p_aout );
282 var_SetString( p_aout , "equalizer-preset" , preset_list[presetid] );
283 vlc_object_release( p_aout );
289 /****************************************************************************
290 * Enable/disable Equalizer
291 *****************************************************************************/
292 static int vlclua_equalizer_enable ( lua_State *L )
294 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
295 bool state = luaL_checkboolean ( L , 1 );
296 aout_EnableFilter( p_playlist, "equalizer", state );
299 /*****************************************************************************
301 *****************************************************************************/
302 static int vlclua_equalizer_get_presets( lua_State *L )
306 for( int i = 0 ; i < NB_PRESETS ; i++ )
308 lua_pushstring( L, preset_list_text[i] );
309 if( asprintf( &str , "preset id=\"%d\"",i ) == -1 )
311 lua_setfield( L , -2 , str );
316 static const luaL_Reg vlclua_equalizer_reg[] = {
317 { "preampget", vlclua_preamp_get },
318 { "preampset", vlclua_preamp_set },
319 { "equalizerget", vlclua_equalizer_get },
320 { "equalizerset", vlclua_equalizer_set },
321 { "enable", vlclua_equalizer_enable },
322 { "presets",vlclua_equalizer_get_presets },
323 { "setpreset", vlclua_equalizer_setpreset },
328 void luaopen_equalizer( lua_State *L )
331 luaL_register( L, NULL, vlclua_equalizer_reg );
332 lua_setfield( L, -2, "equalizer" );