]> git.sesse.net Git - vlc/blob - modules/lua/libs/equalizer.c
lua_equalizer: fix memory leaks
[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         free( psz_af );
74         vlc_object_release( p_aout );
75         return 0;
76     }
77     free( psz_af );
78
79     lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
80     vlc_object_release( p_aout );
81     return 1;
82 }
83
84
85 /*****************************************************************************
86 * Set the preamp level
87 *****************************************************************************/
88 static int vlclua_preamp_set( lua_State *L )
89 {
90     input_thread_t *p_input = vlclua_get_input_internal( L );
91     if( !p_input )
92         return 0;
93
94     aout_instance_t *p_aout = input_GetAout( p_input );
95     vlc_object_release( p_input );
96     if( !p_aout )
97         return 0;
98
99     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
100     if( strstr ( psz_af, "equalizer" ) == NULL )
101     {
102         free( psz_af );
103         vlc_object_release( p_aout );
104         return 0;
105     }
106     free( psz_af );
107
108     var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
109     vlc_object_release( p_aout );
110     return 1;
111 }
112
113
114 /*****************************************************************************
115 Bands:
116 Band 0:  60 Hz
117 Band 1: 170 Hz
118 Band 2: 310 Hz
119 Band 3: 600 Hz
120 Band 4:  1 kHz
121 Band 5:  3 kHz
122 Band 6:  6 kHz
123 Band 7: 12 kHz
124 Band 8: 14 kHz
125 Band 9: 16 kHz
126 *****************************************************************************/
127 /*****************************************************************************
128 * Get the equalizer level for the specified band
129 *****************************************************************************/
130 static int vlclua_equalizer_get( lua_State *L )
131 {
132     input_thread_t *p_input = vlclua_get_input_internal( L );
133     if( !p_input )
134         return 0;
135
136     aout_instance_t *p_aout = input_GetAout( p_input );
137     vlc_object_release( p_input );
138     if( !p_aout )
139         return 0;
140
141     float level = 0 ;
142     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
143     if( strstr ( psz_af, "equalizer" ) == NULL )
144     {
145         free( psz_af );
146         vlc_object_release( p_aout );
147         return 0;
148     }
149     free( psz_af );
150
151     int bandid = luaL_checknumber( L, 1 );
152     char *psz_bands_origin, *psz_bands;
153     psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
154     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
155     locale_t oldloc = uselocale (loc);
156     while( bandid >= 0 )
157     {
158         level = strtof( psz_bands, &psz_bands);
159         bandid--;
160     }
161     free( psz_bands_origin );
162     if (loc != (locale_t)0)
163     {
164         uselocale (oldloc);
165         freelocale (loc);
166     }
167
168     vlc_object_release( p_aout );
169     if( bandid == -1 )
170     {
171         lua_pushnumber( L, level );
172         return 1;
173     }
174     else
175         return 0;
176 }
177
178
179 /*****************************************************************************
180 * Set the equalizer level for the specified band
181 *****************************************************************************/
182 static int vlclua_equalizer_set( lua_State *L )
183 {
184     input_thread_t *p_input = vlclua_get_input_internal( L );
185     if( !p_input )
186         return 0;
187
188     int i_pos = 0 , j = 0;
189     aout_instance_t *p_aout = input_GetAout( p_input );
190     vlc_object_release( p_input );
191     if( !p_aout )
192         return 0;
193
194     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
195     if( strstr ( psz_af, "equalizer" ) == NULL )
196     {
197         free( psz_af );
198         vlc_object_release( p_aout );
199         return 0;
200     }
201     free( psz_af );
202
203     int bandid = luaL_checknumber( L, 1 );
204     float level = luaL_checknumber( L, 2 );
205     char *bands = var_GetString( p_aout, "equalizer-bands" );
206     char newstr[7];
207     while( j != bandid )
208     {
209         i_pos++;
210         if( bands[i_pos] == '.' )
211         {
212             i_pos++;
213             j++;
214         }
215     }
216     if( bandid != 0 )
217         i_pos++;
218     snprintf( newstr, sizeof ( newstr ) , "%6.1f", level);
219     for( int i = 0 ; i < 6 ; i++ )
220         bands[i_pos+i] = newstr[i];
221     var_SetString( p_aout, "equalizer-bands", bands );
222
223     vlc_object_release( p_aout );
224     return 1;
225 }
226
227
228 static const luaL_Reg vlclua_equalizer_reg[] = {
229     { "preampget", vlclua_preamp_get },
230     { "preampset", vlclua_preamp_set },
231     { "equalizerget", vlclua_equalizer_get },
232     { "equalizerset", vlclua_equalizer_set },
233     { NULL, NULL }
234 };
235
236
237 void luaopen_equalizer( lua_State *L )
238 {
239     lua_newtable( L );
240     luaL_register( L, NULL, vlclua_equalizer_reg );
241     lua_setfield( L, -2, "equalizer" );
242 }