Volume control and display

From awesome
Jump to: navigation, search

This tutorial will get you a small inconspicious square which fades in and out. When muted a red M will display:

Volume.png

    volume_widget = widget({ type = "textbox", name = "tb_volume",
                             align = "right" })

    function update_volume(widget)
        local fd = io.popen("amixer sget Master")
        local status = fd:read("*all")
        fd:close()
        
        local volume = tonumber(string.match(status, "(%d?%d?%d)%%")) / 100
        -- volume = string.format("% 3d", volume)

        status = string.match(status, "%[(o[^%]]*)%]")

        -- starting colour
        local sr, sg, sb = 0x3F, 0x3F, 0x3F
        -- ending colour
        local er, eg, eb = 0xDC, 0xDC, 0xCC

        local ir = volume * (er - sr) + sr
        local ig = volume * (eg - sg) + sg
        local ib = volume * (eb - sb) + sb
        interpol_colour = string.format("%.2x%.2x%.2x", ir, ig, ib)
        if string.find(status, "on", 1, true) then
            volume = " <span background='#" .. interpol_colour .. "'>   </span>"
        else
            volume = " <span color='red' background='#" .. interpol_colour .. "'> M </span>"
        end
        widget.text = volume
     end

    update_volume(volume_widget)
    awful.hooks.timer.register(1, function () update_volume(volume_widget) end)

Save that file in ~/.config/awesome/volume.lua

In rc.lua at the top add:

 require("volume")

Add the widget to your taskbar:


   mywibox[s].widgets = {
       --  .... 
       volume_widget,
       -- ... ^ don't forget to add the widget to your taskbar!
   }

Finally add the keyboard keys for controlling/muting your volume:


   awful.key({ }, "XF86AudioRaiseVolume", function ()
       awful.util.spawn("amixer set Master 9%+") end),
   awful.key({ }, "XF86AudioLowerVolume", function ()
       awful.util.spawn("amixer set Master 9%-") end),
   awful.key({ }, "XF86AudioMute", function ()
       awful.util.spawn("amixer sset Master toggle") end),

Should work now.


If you are annoyed by the stopwatch/busy mouse cursor, give the second parameter a false value to disable the startup-notification.[1]

   awful.key({ }, "XF86AudioRaiseVolume", function ()
       awful.util.spawn("amixer set Master 9%+", false) end),
   awful.key({ }, "XF86AudioLowerVolume", function ()
       awful.util.spawn("amixer set Master 9%-", false) end),
   awful.key({ }, "XF86AudioMute", function ()
       awful.util.spawn("amixer set Master toggle", false) end),


If you have pulse installed (Ubuntu users) change this line:

       awful.util.spawn("amixer set Master toggle", false) end),

Too:

       awful.util.spawn("amixer -q -D default sset Master toggle", false) end),


For awesome 3.5.x

volume.lua

local wibox = require("wibox")
local awful = require("awful")

volume_widget = wibox.widget.textbox()
volume_widget:set_align("right")

function update_volume(widget)
   local fd = io.popen("amixer sget Master")
   local status = fd:read("*all")
   fd:close()

   local volume = tonumber(string.match(status, "(%d?%d?%d)%%")) / 100
   -- volume = string.format("% 3d", volume)

   status = string.match(status, "%[(o[^%]]*)%]")

   -- starting colour
   local sr, sg, sb = 0x3F, 0x3F, 0x3F
   -- ending colour
   local er, eg, eb = 0xDC, 0xDC, 0xCC

   local ir = volume * (er - sr) + sr
   local ig = volume * (eg - sg) + sg
   local ib = volume * (eb - sb) + sb
   interpol_colour = string.format("%.2x%.2x%.2x", ir, ig, ib)
   if string.find(status, "on", 1, true) then
       volume = " <span background='#" .. interpol_colour .. "'>   </span>"
   else
       volume = " <span color='red' background='#" .. interpol_colour .. "'> M </span>"
   end
   widget:set_markup(volume)
end

update_volume(volume_widget)

mytimer = timer({ timeout = 1 })
mytimer:connect_signal("timeout", function () update_volume(volume_widget) end)
mytimer:start()

rc.lua

At the top add

require("volume")

Before "right_layout:add(mytextclock)" paste

right_layout:add(volume_widget)


For a numeric volume display:


Normalvolume.png

Mute.png


There are a few things to change to the code:

local wibox = require("wibox")
local awful = require("awful")
 
volume_widget = wibox.widget.textbox()
volume_widget:set_align("right")
 
function update_volume(widget)
   local fd = io.popen("amixer sget Master")
   local status = fd:read("*all")
   fd:close()
 
   -- local volume = tonumber(string.match(status, "(%d?%d?%d)%%")) / 100
   local volume = string.match(status, "(%d?%d?%d)%%")
   volume = string.format("% 3d", volume)
 
   status = string.match(status, "%[(o[^%]]*)%]")

   if string.find(status, "on", 1, true) then
       -- For the volume numbers
       volume = volume .. "%"
   else
       -- For the mute button
       volume = volume .. "M"
       
   end
   widget:set_markup(volume)
end
 
update_volume(volume_widget)
 
mytimer = timer({ timeout = 0.2 })
mytimer:connect_signal("timeout", function () update_volume(volume_widget) end)
mytimer:start()
https://github.com/esn89/volumetextwidget