1 /*****************************************************************************
2  * volume.c
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id: 81a6156c2aeadc70466ad3e3cc2334c4bf47177c $
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35 
36 #include <math.h>
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_meta.h>
40 #include <vlc_playlist.h>
41 
42 #include "../vlc.h"
43 #include "../libs.h"
44 
45 /*****************************************************************************
46  * Volume related
47  *****************************************************************************/
vlclua_volume_set(lua_State * L)48 static int vlclua_volume_set( lua_State *L )
49 {
50     playlist_t *p_this = vlclua_get_playlist_internal( L );
51     int i_volume = luaL_checkint( L, 1 );
52     if( i_volume < 0 )
53         i_volume = 0;
54     int i_ret = playlist_VolumeSet( p_this, i_volume/(float)AOUT_VOLUME_DEFAULT );
55     return vlclua_push_ret( L, i_ret );
56 }
57 
vlclua_volume_get(lua_State * L)58 static int vlclua_volume_get( lua_State *L )
59 {
60     playlist_t *p_this = vlclua_get_playlist_internal( L );
61     long i_volume = lroundf(playlist_VolumeGet( p_this ) * AOUT_VOLUME_DEFAULT);
62     lua_pushnumber( L, i_volume );
63     return 1;
64 }
65 
vlclua_volume_up(lua_State * L)66 static int vlclua_volume_up( lua_State *L )
67 {
68     playlist_t *p_this = vlclua_get_playlist_internal( L );
69     float volume;
70 
71     playlist_VolumeUp( p_this, (int)luaL_optinteger( L, 1, 1 ), &volume );
72     lua_pushnumber( L, lroundf(volume * AOUT_VOLUME_DEFAULT) );
73     return 1;
74 }
75 
vlclua_volume_down(lua_State * L)76 static int vlclua_volume_down( lua_State *L )
77 {
78     playlist_t *p_this = vlclua_get_playlist_internal( L );
79     float volume;
80 
81     playlist_VolumeDown( p_this, (int)luaL_optinteger( L, 1, 1 ), &volume );
82     lua_pushnumber( L, lroundf(volume * AOUT_VOLUME_DEFAULT) );
83     return 1;
84 }
85 
86 /*****************************************************************************
87  *
88  *****************************************************************************/
89 static const luaL_Reg vlclua_volume_reg[] = {
90     { "get", vlclua_volume_get },
91     { "set", vlclua_volume_set },
92     { "up", vlclua_volume_up },
93     { "down", vlclua_volume_down },
94     { NULL, NULL }
95 };
96 
luaopen_volume(lua_State * L)97 void luaopen_volume( lua_State *L )
98 {
99     lua_newtable( L );
100     luaL_register( L, NULL, vlclua_volume_reg );
101     lua_setfield( L, -2, "volume" );
102 }
103