]> git.sesse.net Git - vlc/blob - modules/lua/libs/equalizer.c
lua: small cleanup (no functional changes)
[vlc] / modules / lua / libs / equalizer.c
1 /*****************************************************************************
2  * equalizer.c
3  *****************************************************************************
4  * Copyright (C) 2011 VideoLAN and VLC authors
5  * $Id$
6  *
7  * Authors: Akash Mehrotra < mehrotra <dot> akash <at> gmail <dot> com >
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_aout.h>
37 #include <vlc_input.h>
38 #include <vlc_charset.h>
39
40 #include <lua.h>        /* Low level lua C API */
41 #include <lauxlib.h>    /* Higher level C API */
42
43 #include "input.h"
44 #include "../libs.h"
45
46 #if !defined WIN32
47 # include <locale.h>
48 #else
49 # include <windows.h>
50 #endif
51
52 #ifdef __APPLE__
53 #   include <string.h>
54 #   include <xlocale.h>
55 #endif
56
57
58 /*****************************************************************************
59 * Get the preamp level
60 *****************************************************************************/
61 static int vlclua_preamp_get( lua_State *L )
62 {
63     input_thread_t *p_input = vlclua_get_input_internal( L );
64     if( !p_input )
65         return 0;
66
67     aout_instance_t *p_aout = input_GetAout( p_input );
68     vlc_object_release( p_input );
69
70     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
71     if( strstr ( psz_af, "equalizer" ) == NULL )
72     {
73         vlc_object_release( p_aout );
74         return 0;
75     }
76
77     lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
78     vlc_object_release( p_aout );
79     return 1;
80 }
81
82
83 /*****************************************************************************
84 * Set the preamp level
85 *****************************************************************************/
86 static int vlclua_preamp_set( lua_State *L )
87 {
88     input_thread_t *p_input = vlclua_get_input_internal( L );
89     if( !p_input )
90         return 0;
91
92     aout_instance_t *p_aout = input_GetAout( p_input );
93     vlc_object_release( p_input );
94     if( !p_aout )
95         return 0;
96
97     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
98     if( strstr ( psz_af, "equalizer" ) == NULL )
99     {
100         vlc_object_release( p_aout );
101         return 0;
102     }
103
104     var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
105     vlc_object_release( p_aout );
106     return 1;
107 }
108
109
110 /*****************************************************************************
111 Bands:
112 Band 0:  60 Hz
113 Band 1: 170 Hz
114 Band 2: 310 Hz
115 Band 3: 600 Hz
116 Band 4:  1 kHz
117 Band 5:  3 kHz
118 Band 6:  6 kHz
119 Band 7: 12 kHz
120 Band 8: 14 kHz
121 Band 9: 16 kHz
122 *****************************************************************************/
123 /*****************************************************************************
124 * Get the equalizer level for the specified band
125 *****************************************************************************/
126 static int vlclua_equalizer_get( lua_State *L )
127 {
128     input_thread_t *p_input = vlclua_get_input_internal( L );
129     if( !p_input )
130         return 0;
131
132     aout_instance_t *p_aout = input_GetAout( p_input );
133     vlc_object_release( p_input );
134     if( !p_aout )
135         return 0;
136
137     float level = 0 ;
138     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
139     if( strstr ( psz_af, "equalizer" ) == NULL )
140     {
141         vlc_object_release( p_aout );
142         return 0;
143     }
144
145     int bandid = luaL_checknumber( L, 1 );
146     char *bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
147     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
148     locale_t oldloc = uselocale (loc);
149     while( bandid >= 0 )
150     {
151         level = strtof( bands, &bands);
152         bandid--;
153     }
154     if (loc != (locale_t)0)
155     {
156         uselocale (oldloc);
157         freelocale (loc);
158     }
159
160
161     vlc_object_release( p_aout );
162     if( bandid == -1 )
163     {
164         lua_pushnumber( L, level );
165         return 1;
166     }
167     else
168         return 0;
169 }
170
171
172 /*****************************************************************************
173 * Set the equalizer level for the specified band
174 *****************************************************************************/
175 static int vlclua_equalizer_set( lua_State *L )
176 {
177     input_thread_t *p_input = vlclua_get_input_internal( L );
178     if( !p_input )
179         return 0;
180
181     int i_pos = 0 , j = 0;
182     aout_instance_t *p_aout = input_GetAout( p_input );
183     vlc_object_release( p_input );
184     if( !p_aout )
185         return 0;
186
187     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
188     if( strstr ( psz_af, "equalizer" ) == NULL )
189     {
190         vlc_object_release( p_aout );
191         return 0;
192     }
193
194     int bandid = luaL_checknumber( L, 1 );
195     float level = luaL_checknumber( L, 2 );
196     char *bands = var_GetString( p_aout, "equalizer-bands" );
197     char newstr[7];
198     while( j != bandid )
199     {
200         i_pos++;
201         if( bands[i_pos] == '.' )
202         {
203             i_pos++;
204             j++;
205         }
206     }
207     if( bandid != 0 )
208         i_pos++;
209     snprintf( newstr, sizeof ( newstr ) , "%6.1f", level);
210     for( int i = 0 ; i < 6 ; i++ )
211         bands[i_pos+i] = newstr[i];
212     var_SetString( p_aout, "equalizer-bands", bands );
213
214     vlc_object_release( p_aout );
215     return 1;
216 }
217
218
219 static const luaL_Reg vlclua_equalizer_reg[] = {
220     { "preampget", vlclua_preamp_get },
221     { "preampset", vlclua_preamp_set },
222     { "equalizerget", vlclua_equalizer_get },
223     { "equalizerset", vlclua_equalizer_set },
224     { NULL, NULL }
225 };
226
227
228 void luaopen_equalizer( lua_State *L )
229 {
230     lua_newtable( L );
231     luaL_register( L, NULL, vlclua_equalizer_reg );
232     lua_setfield( L, -2, "equalizer" );
233 }