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