Increase support for audio controls and add config for soundcard and parameter choice

This commit is contained in:
Alice Gaudon 2020-03-28 19:48:04 +01:00
parent 6198fa4148
commit f56403f0bf
2 changed files with 15 additions and 6 deletions

View File

@ -135,6 +135,15 @@ config.keys.custom_keys = {
}; };
--
-- Audio
--
config.audio = {
card = '-D pulse',
device = 'Master',
};
-- --
-- Widgets -- Widgets
-- --

View File

@ -35,15 +35,15 @@ end
-- Volume control -- Volume control
function getVolume() function getVolume()
if isMuted() then return 0 end if isMuted() then return 0 end
local handle = io.popen("amixer -D pulse sget Master | awk -F\"[][]\" '/%/ { print $2 }' | head -n 1", "r") local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' -M | awk -F\"[][]\" '/%/ { print $2 }' | head -n 1", "r")
local volume = string.gsub(handle:read("*a"), "[\n%%]+", "") / 100 local volume = string.gsub(handle:read("*a"), "[\n%%]+", "") / 100
handle:close() handle:close()
return volume return volume
end end
function isMuted() function isMuted()
local handle = io.popen("amixer -D pulse sget Master | awk -F\"[][]\" '/[onf]{2,3}/ { print $4 }' | head -n -1", "r") local handle = io.popen("amixer " .. config.audio.card .. " sget '" .. config.audio.device .. "' | egrep 'Playback.*?\\[o' | egrep -o '\\[o.+\\]'", "r")
local muted = string.gsub(handle:read("*a"), "[\n]+", "") == "off" local muted = string.gsub(handle:read("*a"), "[\n]+", "") == "[off]"
handle:close() handle:close()
return muted return muted
end end
@ -178,16 +178,16 @@ local displayVolume = function()
timer:start() timer:start()
end end
local raiseVolume = function() local raiseVolume = function()
os.execute("amixer -D pulse set Master 5%+") os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%+ -M")
displayVolume() displayVolume()
end end
local lowerVolume = function() local lowerVolume = function()
os.execute("amixer -D pulse set Master 5%-") os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' 5%- -M")
displayVolume() displayVolume()
end end
local toggleMute = function() local toggleMute = function()
local muted = isMuted() local muted = isMuted()
os.execute("amixer -D pulse set Master " .. (muted and 'on' or 'off')) os.execute("amixer " .. config.audio.card .. " set '" .. config.audio.device .. "' " .. (muted and 'on' or 'off'))
displayVolume() displayVolume()
end end
-- }}} -- }}}