Commit 80d51438 authored by Akash Mehrotra's avatar Akash Mehrotra Committed by Jean-Baptiste Kempf

LUA: Enable/Disable EQ, fix EQ input validation, and presets getter

move equalizer.xml to httprequests.lua
Use a table to return equalizer band values
Signed-off-by: default avatarJean-Baptiste Kempf <jb@videolan.org>
parent 8568f791
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_aout.h> #include <vlc_aout.h>
#include <vlc_aout_intf.h>
#include <vlc_input.h> #include <vlc_input.h>
#include <vlc_charset.h> #include <vlc_charset.h>
...@@ -42,6 +43,9 @@ ...@@ -42,6 +43,9 @@
#include "input.h" #include "input.h"
#include "../libs.h" #include "../libs.h"
#include "../vlc.h"
#include "playlist.h"
#include "../../audio_filter/equalizer_presets.h"
#if !defined WIN32 #if !defined WIN32
# include <locale.h> # include <locale.h>
...@@ -128,14 +132,14 @@ Band 8: 14 kHz ...@@ -128,14 +132,14 @@ Band 8: 14 kHz
Band 9: 16 kHz Band 9: 16 kHz
*****************************************************************************/ *****************************************************************************/
/***************************************************************************** /*****************************************************************************
* Get the equalizer level for the specified band * Return EQ level for all bands as a Table
*****************************************************************************/ *****************************************************************************/
static int vlclua_equalizer_get( lua_State *L ) static int vlclua_equalizer_get( lua_State *L )
{ {
int bands = 9;
input_thread_t *p_input = vlclua_get_input_internal( L ); input_thread_t *p_input = vlclua_get_input_internal( L );
if( !p_input ) if( !p_input )
return 0; return 0;
audio_output_t *p_aout = input_GetAout( p_input ); audio_output_t *p_aout = input_GetAout( p_input );
vlc_object_release( p_input ); vlc_object_release( p_input );
if( !p_aout ) if( !p_aout )
...@@ -151,15 +155,28 @@ static int vlclua_equalizer_get( lua_State *L ) ...@@ -151,15 +155,28 @@ static int vlclua_equalizer_get( lua_State *L )
} }
free( psz_af ); free( psz_af );
int bandid = luaL_checknumber( L, 1 );
char *psz_bands_origin, *psz_bands; char *psz_bands_origin, *psz_bands;
psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" ); psz_bands_origin = psz_bands = var_GetNonEmptyString( p_aout, "equalizer-bands" );
if( !psz_bands )
{
vlc_object_release( p_aout );
return 0;
}
locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL); locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
locale_t oldloc = uselocale (loc); locale_t oldloc = uselocale (loc);
while( bandid >= 0 ) int i = 0;
char *str;
lua_newtable( L );
while( bands >= 0 )
{ {
level = strtof( psz_bands, &psz_bands); level = strtof( psz_bands, &psz_bands);
bandid--; bands--;
asprintf( &str , "%f" , level );
lua_pushstring( L, str );
free(str);
asprintf( &str , "band_%d", i++ );
lua_setfield( L , -2 , str );
free(str);
} }
free( psz_bands_origin ); free( psz_bands_origin );
if (loc != (locale_t)0) if (loc != (locale_t)0)
...@@ -167,15 +184,8 @@ static int vlclua_equalizer_get( lua_State *L ) ...@@ -167,15 +184,8 @@ static int vlclua_equalizer_get( lua_State *L )
uselocale (oldloc); uselocale (oldloc);
freelocale (loc); freelocale (loc);
} }
vlc_object_release( p_aout ); vlc_object_release( p_aout );
if( bandid == -1 ) return 1;
{
lua_pushnumber( L, level );
return 1;
}
else
return 0;
} }
...@@ -184,6 +194,9 @@ static int vlclua_equalizer_get( lua_State *L ) ...@@ -184,6 +194,9 @@ static int vlclua_equalizer_get( lua_State *L )
*****************************************************************************/ *****************************************************************************/
static int vlclua_equalizer_set( lua_State *L ) static int vlclua_equalizer_set( lua_State *L )
{ {
int bandid = luaL_checknumber( L, 1 );
if ( bandid < 0 || bandid > 9)
return 0;
input_thread_t *p_input = vlclua_get_input_internal( L ); input_thread_t *p_input = vlclua_get_input_internal( L );
if( !p_input ) if( !p_input )
return 0; return 0;
...@@ -203,7 +216,6 @@ static int vlclua_equalizer_set( lua_State *L ) ...@@ -203,7 +216,6 @@ static int vlclua_equalizer_set( lua_State *L )
} }
free( psz_af ); free( psz_af );
int bandid = luaL_checknumber( L, 1 );
float level = luaL_checknumber( L, 2 ); float level = luaL_checknumber( L, 2 );
char *bands = var_GetString( p_aout, "equalizer-bands" ); char *bands = var_GetString( p_aout, "equalizer-bands" );
char newstr[7]; char newstr[7];
...@@ -227,12 +239,39 @@ static int vlclua_equalizer_set( lua_State *L ) ...@@ -227,12 +239,39 @@ static int vlclua_equalizer_set( lua_State *L )
return 1; return 1;
} }
/****************************************************************************
* Enable/disable Equalizer
*****************************************************************************/
static int vlclua_equalizer_enable ( lua_State *L )
{
playlist_t *p_playlist = vlclua_get_playlist_internal( L );
bool state = luaL_checkboolean ( L , 1 );
aout_EnableFilter( p_playlist, "equalizer", state );
return 0;
}
/*****************************************************************************
* Get preset names
*****************************************************************************/
static int vlclua_equalizer_get_presets( lua_State *L )
{
lua_newtable( L );
char *str;
for( int i = 0 ; i < NB_PRESETS ; i++ )
{
lua_pushstring( L, preset_list_text[i] );
asprintf( &str , "id_%d",i );
lua_setfield( L , -2 , str );
free(str);
}
return 1;
}
static const luaL_Reg vlclua_equalizer_reg[] = { static const luaL_Reg vlclua_equalizer_reg[] = {
{ "preampget", vlclua_preamp_get }, { "preampget", vlclua_preamp_get },
{ "preampset", vlclua_preamp_set }, { "preampset", vlclua_preamp_set },
{ "equalizerget", vlclua_equalizer_get }, { "equalizerget", vlclua_equalizer_get },
{ "equalizerset", vlclua_equalizer_set }, { "equalizerset", vlclua_equalizer_set },
{ "enable", vlclua_equalizer_enable },
{"presets",vlclua_equalizer_get_presets },
{ NULL, NULL } { NULL, NULL }
}; };
......
...@@ -333,7 +333,6 @@ DIST_http_lua = \ ...@@ -333,7 +333,6 @@ DIST_http_lua = \
lua/http/requests/playlist.xml \ lua/http/requests/playlist.xml \
lua/http/requests/README.txt \ lua/http/requests/README.txt \
lua/http/requests/playlist_jstree.xml \ lua/http/requests/playlist_jstree.xml \
lua/http/requests/equalizer.xml \
lua/http/requests/browse.xml \ lua/http/requests/browse.xml \
lua/http/requests/vlm_cmd.xml \ lua/http/requests/vlm_cmd.xml \
lua/http/requests/status.xml \ lua/http/requests/status.xml \
......
...@@ -63,7 +63,7 @@ status.xml or status.json ...@@ -63,7 +63,7 @@ status.xml or status.json
> set aspect ratio. Must be one of the following values. Any other value will reset aspect ratio to default > set aspect ratio. Must be one of the following values. Any other value will reset aspect ratio to default
?command=aspectratio&val=<newratio> ?command=aspectratio&val=<newratio>
Valid aspect ratio values: 1:1 , 4:3 , 5:4 , 16:9 , 16:10 , 221:100 , 235:100 , 239:100 Valid aspect ratio values: 1:1 , 4:3 , 5:4 , 16:9 , 16:10 , 221:100 , 235:100 , 239:100
> sort playlist using sort mode <val> and order <id>: > sort playlist using sort mode <val> and order <id>:
?command=pl_sort&id=<id>&val=<val> ?command=pl_sort&id=<id>&val=<val>
...@@ -112,6 +112,23 @@ status.xml or status.json ...@@ -112,6 +112,23 @@ status.xml or status.json
+1H:2M -> seek 1 hour and 2 minutes forward +1H:2M -> seek 1 hour and 2 minutes forward
-10% -> seek 10% back -10% -> seek 10% back
>command=preamp&val=<val in dB>
sets the preamp value, must be >=-20 and <=20
>command=equalizer&band=<band>&val=<gain in dB, must be >=-20 and <=20)
set the gain for a specific band
>command=enableeq&val=<0 or 1>
0 -- disables the equalizer
1 -- enables the equalizer
<Displays the equalizer band gains.
Band 0: 60 Hz, 1: 170 Hz, 2: 310 Hz, 3: 600 Hz, 4: 1 kHz,
5: 3 kHz, 6: 6 kHz, 7: 12 kHz , 8: 14 kHz , 9: 16 kHz
<Display the list of presets available for the equalizer
=======
playlist.xml: playlist.xml:
============= =============
< get the full playlist tree < get the full playlist tree
...@@ -131,13 +148,3 @@ vlm_cmd.xml: ...@@ -131,13 +148,3 @@ vlm_cmd.xml:
?command=<cmd> ?command=<cmd>
> get the error message from <cmd> > get the error message from <cmd>
equalizer.xml:
=============
>command=preamp&val=<val in dB>
sets the preamp value, must be >=-20 and <=20
>command=equalizer&band=<band>&val=<gain in dB, must be >=-20 and <=20)
<Displays the equalizer band gains.
Band 0: 60 Hz, 1: 170 Hz, 2: 310 Hz, 3: 600 Hz, 4: 1 kHz,
5: 3 kHz, 6: 6 kHz, 7: 12 kHz , 8: 14 kHz , 9: 16 kHz
<?xml version="1.0" encoding="utf-8" standalone="yes" ?<?vlcprint'>'?>
<?vlc --[[
vim:syntax=lua
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
< equalizer.xml: VLC media player web interface
< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - >
< Copyright (C) 2011 the VideoLAN team
< $Id$
<
< Authors: Akash Mehrotra < mehrotra <dot> akash <at> gmail <dot> com >
<
< This program is free software; you can redistribute it and/or modify
< it under the terms of the GNU General Public License as published by
< the Free Software Foundation; either version 2 of the License, or
< (at your option) any later version.
<
< This program is distributed in the hope that it will be useful,
< but WITHOUT ANY WARRANTY; without even the implied warranty of
< MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
< GNU General Public License for more details.
<
< You should have received a copy of the GNU General Public License
< along with this program; if not, write to the Free Software
< Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
< - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
]]?>
<?vlc
local command = _GET['command']
local val = _GET['val']
local band = _GET['band']
function round(what, precision)
if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
end
if command == "preamp" then vlc.equalizer.preampset(val)
elseif command == "equalizer" then vlc.equalizer.equalizerset(band,val)
end
freq = { 60 , 170 , 310 , 600 , 1000 , 3000 , 6000 , 12000 , 14000 , 16000 }
?>
<root>
<preamp><?vlc print(round(vlc.equalizer.preampget(),2)) ?></preamp>
<equalizer>
<?vlc for i = 0,9
do print("<band id='"..i.."' freqency = '"..freq[i+1].."'>"..round(vlc.equalizer.equalizerget(i),1).."</band>") end ?>
</equalizer>
</root>
...@@ -59,20 +59,24 @@ function strsplit(text, delimiter) ...@@ -59,20 +59,24 @@ function strsplit(text, delimiter)
return list return list
end end
function round(what, precision)
if what then return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision) else return "" end
end
--main function to process commands sent with the request --main function to process commands sent with the request
processcommands = function () processcommands = function ()
local input = _GET['input'] local input = _GET['input']
local command = _GET['command'] local command = _GET['command']
local id = tonumber(_GET['id'] or -1) local id = tonumber(_GET['id'] or -1)
local val = _GET['val'] local val = _GET['val']
local options = _GET['option'] local options = _GET['option']
local band = _GET['band']
if type(options) ~= "table" then -- Deal with the 0 or 1 option case if type(options) ~= "table" then -- Deal with the 0 or 1 option case
options = { options } options = { options }
end end
if command == "in_play" then if command == "in_play" then
--[[ --[[
vlc.msg.err( "<options>" ) vlc.msg.err( "<options>" )
...@@ -158,8 +162,14 @@ processcommands = function () ...@@ -158,8 +162,14 @@ processcommands = function ()
if vlc.object.vout() then if vlc.object.vout() then
vlc.var.set(vlc.object.vout(),"aspect-ratio",val) vlc.var.set(vlc.object.vout(),"aspect-ratio",val)
end end
elseif command == "preamp" then
vlc.equalizer.preampset(val)
elseif command == "equalizer" then
vlc.equalizer.equalizerset(band,val)
elseif command == "enableeq" then
if val == '0' then vlc.equalizer.enable(false) else vlc.equalizer.enable(true) end
end end
local input = nil local input = nil
local command = nil local command = nil
local id = nil local id = nil
...@@ -183,18 +193,18 @@ local printJsonKeyValue = function (k,v,indent) ...@@ -183,18 +193,18 @@ local printJsonKeyValue = function (k,v,indent)
if (k) then if (k) then
print("\""..k.."\":") print("\""..k.."\":")
end end
if (type(v)=="number") then if (type(v)=="number") then
print(xmlString(v)) print(xmlString(v))
elseif (type(v)=="table") then elseif (type(v)=="table") then
if (v._array==NULL) then if (v._array==NULL) then
print("{\n") print("{\n")
printTableAsJson(v,indent+2) printTableAsJson(v,indent+2)
print("\n}") print("\n}")
else else
print("[") print("[")
printArrayAsJson(v._array,indent+2) printArrayAsJson(v._array,indent+2)
print("\n]") print("\n]")
end end
else else
print("\""..xmlString(v).."\"") print("\""..xmlString(v).."\"")
...@@ -206,7 +216,7 @@ printArrayAsJson = function(array,indent) ...@@ -206,7 +216,7 @@ printArrayAsJson = function(array,indent)
first=true first=true
for i,v in ipairs(array) do for i,v in ipairs(array) do
if not first then print(",") end if not first then print(",") end
printJsonKeyValue(NULL,v,indent) printJsonKeyValue(NULL,v,indent)
first=false first=false
end end
end end
...@@ -226,13 +236,13 @@ local printXmlKeyValue = function (k,v,indent) ...@@ -226,13 +236,13 @@ local printXmlKeyValue = function (k,v,indent)
if (k) then if (k) then
print("<"..k..">") print("<"..k..">")
end end
if (type(v)=="table") then if (type(v)=="table") then
printTableAsXml(v,indent+2) printTableAsXml(v,indent+2)
else else
print(xmlString(v)) print(xmlString(v))
end end
if (k) then if (k) then
print("</"..k..">") print("</"..k..">")
end end
...@@ -260,7 +270,7 @@ end ...@@ -260,7 +270,7 @@ end
getplaylist = function () getplaylist = function ()
local p local p
if _GET["search"] then if _GET["search"] then
if _GET["search"] ~= "" then if _GET["search"] ~= "" then
_G.search_key = _GET["search"] _G.search_key = _GET["search"]
...@@ -272,54 +282,54 @@ getplaylist = function () ...@@ -272,54 +282,54 @@ getplaylist = function ()
else else
p = vlc.playlist.get() p = vlc.playlist.get()
end end
--logTable(p) --Uncomment to debug --logTable(p) --Uncomment to debug
return p return p
end end
parseplaylist = function (item) parseplaylist = function (item)
if item.flags.disabled then return end if item.flags.disabled then return end
if (item.children) then if (item.children) then
local result={} local result={}
local name = vlc.strings.convert_xml_special_chars(item.name or "") local name = vlc.strings.convert_xml_special_chars(item.name or "")
result["type"]="node" result["type"]="node"
result.id=tostring(item.id) result.id=tostring(item.id)
result.name=tostring(name) result.name=tostring(name)
result.ro=item.flags.ro and "ro" or "rw" result.ro=item.flags.ro and "ro" or "rw"
--store children in an array --store children in an array
--we use _array as a proxy for arrays --we use _array as a proxy for arrays
result.children={} result.children={}
result.children._array={} result.children._array={}
for _, child in ipairs(item.children) do for _, child in ipairs(item.children) do
local nextChild=parseplaylist(child) local nextChild=parseplaylist(child)
table.insert(result.children._array,nextChild) table.insert(result.children._array,nextChild)
end end
return result return result
else else
local result={} local result={}
local name, path = vlc.strings.convert_xml_special_chars(item.name or "", item.path or "") local name, path = vlc.strings.convert_xml_special_chars(item.name or "", item.path or "")
local current_item = vlc.input.item() local current_item = vlc.input.item()
-- Is the item the one currently played -- Is the item the one currently played
if(current_item ~= nil) then if(current_item ~= nil) then
if(vlc.input.item().uri(current_item) == path) then if(vlc.input.item().uri(current_item) == path) then
result.current = "current" result.current = "current"
end end
end end
result["type"]="leaf" result["type"]="leaf"
result.id=tostring(item.id) result.id=tostring(item.id)
result.uri=tostring(path) result.uri=tostring(path)
result.name=name result.name=name
result.ro=item.flags.ro and "ro" or "rw" result.ro=item.flags.ro and "ro" or "rw"
result.duration=math.floor(item.duration) result.duration=math.floor(item.duration)
return result return result
end end
...@@ -328,7 +338,7 @@ end ...@@ -328,7 +338,7 @@ end
playlisttable = function () playlisttable = function ()
local basePlaylist=getplaylist() local basePlaylist=getplaylist()
return parseplaylist(basePlaylist) return parseplaylist(basePlaylist)
end end
...@@ -343,20 +353,20 @@ local vout = vlc.object.vout() ...@@ -343,20 +353,20 @@ local vout = vlc.object.vout()
local aout = vlc.object.aout() local aout = vlc.object.aout()
local s ={} local s ={}
--update api version when new data/commands added --update api version when new data/commands added
s.apiversion=1 s.apiversion=1
s.version=vlc.misc.version() s.version=vlc.misc.version()
s.volume=vlc.volume.get() s.volume=vlc.volume.get()
if input then if input then
s.length=math.floor(vlc.var.get(input,"length")) s.length=math.floor(vlc.var.get(input,"length"))
s.time=math.floor(vlc.var.get(input,"time")) s.time=math.floor(vlc.var.get(input,"time"))
s.position=vlc.var.get(input,"position") s.position=vlc.var.get(input,"position")
s.audiodelay=vlc.var.get(input,"audio-delay") s.audiodelay=vlc.var.get(input,"audio-delay")
s.rate=vlc.var.get(input,"rate") s.rate=vlc.var.get(input,"rate")
s.subtitledelay=vlc.var.get(input,"spu-delay") s.subtitledelay=vlc.var.get(input,"spu-delay")
else else
s.length=0 s.length=0
s.time=0 s.time=0
s.position=0 s.position=0
...@@ -391,12 +401,19 @@ local aout = vlc.object.aout() ...@@ -391,12 +401,19 @@ local aout = vlc.object.aout()
s.random=vlc.var.get(playlist,"random") s.random=vlc.var.get(playlist,"random")
s.loop=vlc.var.get(playlist,"loop") s.loop=vlc.var.get(playlist,"loop")
s["repeat"]=vlc.var.get(playlist,"repeat") s["repeat"]=vlc.var.get(playlist,"repeat")
s.equalizer={}
s.equalizer.preamp=round(vlc.equalizer.preampget(),2)
s.equalizer.bands=vlc.equalizer.equalizerget()
if s.equalizer.bands ~= null then
for k,i in pairs(s.equalizer.bands) do s.equalizer.bands[k]=round(i,2) end
s.equalizer.presets=vlc.equalizer.presets()
end
if (includecategories and item) then if (includecategories and item) then
s.information={} s.information={}
s.information.category={} s.information.category={}
s.information.category.meta=item:metas() s.information.category.meta=item:metas()
local info = item:info() local info = item:info()
for k, v in pairs(info) do for k, v in pairs(info) do
local streamTable={} local streamTable={}
...@@ -404,20 +421,21 @@ local aout = vlc.object.aout() ...@@ -404,20 +421,21 @@ local aout = vlc.object.aout()
local tag = string.gsub(k2," ","_") local tag = string.gsub(k2," ","_")
streamTable[xmlString(tag)]=xmlString(v2) streamTable[xmlString(tag)]=xmlString(v2)
end end
s.information.category[xmlString(k)]=streamTable s.information.category[xmlString(k)]=streamTable
end end
s.stats={} s.stats={}
local statsdata = item:stats() local statsdata = item:stats()
for k,v in pairs(statsdata) do for k,v in pairs(statsdata) do
local tag = string.gsub(k,"_","") local tag = string.gsub(k,"_","")
s.stats[tag]=xmlString(v) s.stats[tag]=xmlString(v)
end end
end end
return s return s
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment