]> git.sesse.net Git - vlc/blob - modules/lua/libs/equalizer.c
demux: asf: remove dead code (cid #1251046)
[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 "input.h"
41 #include "../libs.h"
42 #include "../vlc.h"
43 #include "../../audio_filter/equalizer_presets.h"
44
45 #if !defined _WIN32
46 # include <locale.h>
47 #else
48 # include <windows.h>
49 #endif
50
51 #ifdef __APPLE__
52 #   include <string.h>
53 #   include <xlocale.h>
54 #endif
55
56
57 /*****************************************************************************
58 * Get the preamp level
59 *****************************************************************************/
60 static int vlclua_preamp_get( lua_State *L )
61 {
62     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
63     audio_output_t *p_aout = playlist_GetAout( p_playlist );
64     if( p_aout == NULL )
65         return 0;
66
67     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
68     if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
69     {
70         free( psz_af );
71         vlc_object_release( p_aout );
72         return 0;
73     }
74     free( psz_af );
75
76     lua_pushnumber( L, var_GetFloat( p_aout, "equalizer-preamp") );
77     vlc_object_release( p_aout );
78     return 1;
79 }
80
81
82 /*****************************************************************************
83 * Set the preamp level
84 *****************************************************************************/
85 static int vlclua_preamp_set( lua_State *L )
86 {
87     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
88     audio_output_t *p_aout = playlist_GetAout( p_playlist );
89     if( p_aout == NULL )
90         return 0;
91
92     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
93     if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
94     {
95         free( psz_af );
96         vlc_object_release( p_aout );
97         return 0;
98     }
99     free( psz_af );
100
101     var_SetFloat( p_aout, "equalizer-preamp", luaL_checknumber( L, 1 ) );
102     vlc_object_release( p_aout );
103     return 1;
104 }
105
106
107 /*****************************************************************************
108 Bands:
109 Band 0:  60 Hz
110 Band 1: 170 Hz
111 Band 2: 310 Hz
112 Band 3: 600 Hz
113 Band 4:  1 kHz
114 Band 5:  3 kHz
115 Band 6:  6 kHz
116 Band 7: 12 kHz
117 Band 8: 14 kHz
118 Band 9: 16 kHz
119 *****************************************************************************/
120 /*****************************************************************************
121 * Return EQ level for all bands as a Table
122 *****************************************************************************/
123 static int vlclua_equalizer_get( lua_State *L )
124 {
125     const unsigned bands = 10;
126
127     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
128     audio_output_t *p_aout = playlist_GetAout( p_playlist );
129     if( p_aout == NULL )
130         return 0;
131
132     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
133     if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
134     {
135         free( psz_af );
136         vlc_object_release( p_aout );
137         return 0;
138     }
139     free( psz_af );
140
141     char *psz_bands_origin, *psz_bands;
142     psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
143     if( !psz_bands )
144     {
145         vlc_object_release( p_aout );
146         return 0;
147     }
148
149     bool error = false;
150     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
151     locale_t oldloc = uselocale (loc);
152     lua_newtable( L );
153     for( unsigned i = 0; i < bands; i++ )
154     {
155         float level = strtof( psz_bands, &psz_bands );
156         char *str;
157         if( asprintf( &str , "%f" , level ) == -1 )
158         {
159             error = true;
160             break;
161         }
162         lua_pushstring( L, str );
163         free(str);
164         if( asprintf( &str , "band id=\"%u\"", i ) == -1 )
165         {
166             error = true;
167             break;
168         }
169         lua_setfield( L , -2 , str );
170         free( str );
171     }
172
173     free( psz_bands_origin );
174     if( loc != (locale_t)0 )
175     {
176         uselocale (oldloc);
177         freelocale (loc);
178     }
179     vlc_object_release( p_aout );
180     return error ? 0 : 1;
181 }
182
183
184 /*****************************************************************************
185 * Set the equalizer level for the specified band
186 *****************************************************************************/
187 static int vlclua_equalizer_set( lua_State *L )
188 {
189     int bandid = luaL_checknumber( L, 1 );
190     if( bandid < 0 || bandid > 9)
191         return 0;
192
193     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
194     audio_output_t *p_aout = playlist_GetAout( p_playlist );
195     if( p_aout == NULL )
196         return 0;
197
198     char *psz_af = var_GetNonEmptyString( p_aout, "audio-filter" );
199     if( !psz_af || strstr ( psz_af, "equalizer" ) == NULL )
200     {
201         free( psz_af );
202         vlc_object_release( p_aout );
203         return 0;
204     }
205     free( psz_af );
206
207     float level = luaL_checknumber( L, 2 );
208     char *bands = var_GetString( p_aout, "equalizer-bands" );
209
210     locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
211     locale_t oldloc = uselocale (loc);
212     char *b = bands;
213     while( bandid > 0 )
214     {
215         float dummy = strtof( b, &b );
216         (void)dummy;
217         bandid--;
218     }
219     if( *b != '\0' )
220         *b++ = '\0';
221     float dummy = strtof( b, &b );
222     (void)dummy;
223
224     char *newstr;
225     if( asprintf( &newstr, "%s %.1f%s", bands, level, b ) != -1 )
226     {
227         var_SetString( p_aout, "equalizer-bands", newstr );
228         free( newstr );
229     }
230
231     if( loc != (locale_t)0 )
232     {
233         uselocale (oldloc);
234         freelocale (loc);
235     }
236     free( bands );
237     vlc_object_release( p_aout );
238     return 0;
239 }
240
241 /*****************************************************************************
242 * Set the preset specified by preset id
243 *****************************************************************************/
244 static int vlclua_equalizer_setpreset( lua_State *L )
245 {
246     int presetid = luaL_checknumber( L, 1 );
247     if( presetid >= NB_PRESETS || presetid < 0 )
248         return 0;
249
250     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
251     audio_output_t *p_aout = playlist_GetAout( p_playlist );
252     if( p_aout == NULL )
253         return 0;
254
255     int ret = 0;
256     char *psz_af = var_InheritString( p_aout, "audio-filter" );
257     if( psz_af != NULL && strstr ( psz_af, "equalizer" ) != NULL )
258     {
259         var_SetString( p_aout , "equalizer-preset" , preset_list[presetid] );
260         ret = 1;
261     }
262     free( psz_af );
263     vlc_object_release( p_aout );
264     return ret;
265 }
266  
267 /****************************************************************************
268 * Enable/disable Equalizer
269 *****************************************************************************/
270 static int vlclua_equalizer_enable ( lua_State *L )
271 {
272     playlist_t *p_playlist = vlclua_get_playlist_internal( L );
273     bool state = luaL_checkboolean ( L , 1 );
274     playlist_EnableAudioFilter( p_playlist, "equalizer", state );
275     return 0;
276 }
277 /*****************************************************************************
278 * Get preset names
279 *****************************************************************************/
280 static int vlclua_equalizer_get_presets( lua_State *L )
281 {
282     lua_newtable( L );
283     char *str;
284     for( int i = 0 ; i < NB_PRESETS ; i++ )
285     {
286         lua_pushstring( L, preset_list_text[i] );
287         if( asprintf( &str , "preset id=\"%d\"",i ) == -1 )
288             return 0;
289         lua_setfield( L , -2 , str );
290         free(str);
291     }
292     return 1;
293 }
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 },
302     { NULL, NULL }
303 };
304
305
306 void luaopen_equalizer( lua_State *L )
307 {
308     lua_newtable( L );
309     luaL_register( L, NULL, vlclua_equalizer_reg );
310     lua_setfield( L, -2, "equalizer" );
311 }