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>
41 #include <lua.h> /* Low level lua C API */
42 #include <lauxlib.h> /* Higher level C API */
48 #include "../../audio_filter/equalizer_presets.h"
62 /*****************************************************************************
63 * Get the preamp level
64 *****************************************************************************/
65 static int vlclua_preamp_get( lua_State *L )
67 input_thread_t *p_input = vlclua_get_input_internal( L );
71 audio_output_t *p_aout = input_GetAout( p_input );
72 vlc_object_release( p_input );
77 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
78 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
81 vlc_object_release( p_aout );
86 lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
87 vlc_object_release( p_aout );
92 /*****************************************************************************
93 * Set the preamp level
94 *****************************************************************************/
95 static int vlclua_preamp_set( lua_State *L )
97 input_thread_t *p_input = vlclua_get_input_internal( L );
101 audio_output_t *p_aout = input_GetAout( p_input );
102 vlc_object_release( p_input );
106 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
107 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
110 vlc_object_release( p_aout );
115 var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
116 vlc_object_release( p_aout );
121 /*****************************************************************************
133 *****************************************************************************/
134 /*****************************************************************************
135 * Return EQ level for all bands as a Table
136 *****************************************************************************/
137 static int vlclua_equalizer_get( lua_State *L )
140 input_thread_t *p_input = vlclua_get_input_internal( L );
143 audio_output_t *p_aout = input_GetAout( p_input );
144 vlc_object_release( p_input );
149 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
150 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
153 vlc_object_release( p_aout );
158 char *psz_bands_origin, *psz_bands;
159 psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
162 vlc_object_release( p_aout );
165 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
166 locale_t oldloc = uselocale (loc);
172 level = strtof( psz_bands, &psz_bands);
174 if( asprintf( &str , "%f" , level ) == -1 )
176 lua_pushstring( L, str );
178 if( asprintf( &str , "band_%d", i++ ) == -1 )
180 lua_setfield( L , -2 , str );
183 free( psz_bands_origin );
184 if( loc != (locale_t)0 )
189 vlc_object_release( p_aout );
194 /*****************************************************************************
195 * Set the equalizer level for the specified band
196 *****************************************************************************/
197 static int vlclua_equalizer_set( lua_State *L )
199 int bandid = luaL_checknumber( L, 1 );
200 if( bandid < 0 || bandid > 9)
202 input_thread_t *p_input = vlclua_get_input_internal( L );
206 int i_pos = 0 , j = 0;
207 audio_output_t *p_aout = input_GetAout( p_input );
208 vlc_object_release( p_input );
212 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
213 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
216 vlc_object_release( p_aout );
221 float level = luaL_checknumber( L, 2 );
222 char *bands = var_GetString( p_aout, "equalizer-bands" );
227 if( bands[i_pos] == '.' )
235 snprintf( newstr, sizeof ( newstr ) , "%6.1f", level);
236 for( int i = 0 ; i < 6 ; i++ )
237 bands[i_pos+i] = newstr[i];
238 var_SetString( p_aout, "equalizer-bands", bands );
240 vlc_object_release( p_aout );
244 /*****************************************************************************
245 * Set the preset specified by preset id
246 *****************************************************************************/
247 static int vlclua_equalizer_setpreset( lua_State *L )
249 int presetid = luaL_checknumber( L, 1 );
250 if( presetid >= NB_PRESETS || presetid < 0 )
252 input_thread_t *p_input = vlclua_get_input_internal( L );
255 audio_output_t *p_aout = input_GetAout( p_input );
256 vlc_object_release( p_input );
261 char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
262 if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
264 vlc_object_release( p_aout );
268 if( asprintf( &newstr , "%6.1f" , eqz_preset_10b[presetid].f_amp[0] ) == -1 )
270 for ( int i = 1 ; i < 10 ; i++ )
272 if( asprintf( &newstr, "%s%6.1f",newstr ,eqz_preset_10b[presetid].f_amp[i]) == -1 )
275 var_SetString( p_aout, "equalizer-bands",newstr );
276 var_SetFloat( p_aout, "equalizer-preamp", eqz_preset_10b[presetid].f_preamp );
277 var_SetString( p_aout , "equalizer-preset" , preset_list[presetid] );
278 vlc_object_release( p_aout );
285 /****************************************************************************
286 * Enable/disable Equalizer
287 *****************************************************************************/
288 static int vlclua_equalizer_enable ( lua_State *L )
290 playlist_t *p_playlist = vlclua_get_playlist_internal( L );
291 bool state = luaL_checkboolean ( L , 1 );
292 aout_EnableFilter( p_playlist, "equalizer", state );
295 /*****************************************************************************
297 *****************************************************************************/
298 static int vlclua_equalizer_get_presets( lua_State *L )
302 for( int i = 0 ; i < NB_PRESETS ; i++ )
304 lua_pushstring( L, preset_list_text[i] );
305 if( asprintf( &str , "id_%d",i ) == -1 )
307 lua_setfield( L , -2 , str );
312 static const luaL_Reg vlclua_equalizer_reg[] = {
313 { "preampget", vlclua_preamp_get },
314 { "preampset", vlclua_preamp_set },
315 { "equalizerget", vlclua_equalizer_get },
316 { "equalizerset", vlclua_equalizer_set },
317 { "enable", vlclua_equalizer_enable },
318 { "presets",vlclua_equalizer_get_presets },
319 { "setpreset", vlclua_equalizer_setpreset },
324 void luaopen_equalizer( lua_State *L )
327 luaL_register( L, NULL, vlclua_equalizer_reg );
328 lua_setfield( L, -2, "equalizer" );