Display audio volume percentage on the Panel and Conky in MX-19 and 21 (Xfce)

Here is where you can post tips and tricks to share with other users of MX. Do not ask for help in this Forum.
Message
Author
Stuart_M
Posts: 705
Joined: Wed Aug 07, 2019 7:10 pm

Display audio volume percentage on the Panel and Conky in MX-19 and 21 (Xfce)

#1 Post by Stuart_M »

This is not a repeat of https://forum.mxlinux.org/viewtopic.php ... 41#p652141 dated September 2021 ("Another free conky…") that shows how pamixer was used to display the volume as a percentage in conky.

In this post I show two ways to display the audio volume as a percentage in the Panel and Conky using amixer and pamixer. The primary differences are:
  • amixer:
    Uses an existing MX Linux package.

    To get the volume percentage output in the Panel or Conky a file must be created and then a path to the file is used to run the command. The command consists of amixer and grep and thus will not work if that command is put in the "Generic Monitor" or Conky.

    When the volume is muted, "muted" is NOT displayed (the existing volume percentage will remain).
  • pamixer:
    Not an existing application so it must be installed (Stable Repository).

    To get the volume percentage output in the Panel or Conky only the (absolute) path to pamixer is used. No need to create a file to run the command.

    When the volume is muted, "muted" is displayed in place of the percentage.


Using amixer to display the volume percentage in the Panel
1. Create a file for the amixer command. I am using /home/<username>/.volumepercent in this example so adjust as desired.

2. Make that file executable such as:
  • right-click the file > Properties > Permissions tab > enable "Allow this file to run as a program"
    -or-
  • In a Terminal run chmod -c u+x .volumepercent
3. Put the text from the below code window into the newly created file. You must replace <n> with the soundcard number being used (see below for examples on how to do that):

Code: Select all

#!/bin/sh
amixer -c<n> -M | grep -oP -m 1 '\[\K[^]]*%'
Edit-2 begin
If the percentage symbol (%) is not wanted to be displayed then use the below instead:

Code: Select all

#!/bin/sh
amixer -c<n> -M | grep -oP -m 1 '\[\K[^]]*%' | tr -d '%'
Note that the sound card number as identified in -c<n> of the above command is represented by <n> which will be a number. The greater-than (>) and less-than (<) symbols are NOT part of the command but are used to only separate the number from the rest of the command. For example, -c1 or -c2 would be used for sound cards 1 or 2 respectively (the under-score only denotes the portion of the command).
Edit-2 end


Here are four ways to get the soundcard number to use:
  • alsamixer > F6 (Select sound card) > view available soundcards
  • Shotgun it by trying all possible soundcard numbers until the correct percentage is displayed (there should only be a couple/few options).
To test the above code, in a Terminal run the absolute path to the file that was created, e.g. /home/<username>/.volumepercent. The output will (should) just be the audio volume percentage.

4. Add "Generic Monitor" to the Panel and fill out the fields as follows:
  • Command: Use the absolute path to the created file which in this example is /home/<username>/.volumepercent
  • Label: As desired (it is disabled in the below images)
  • Period (s): As desired (refresh period in seconds)
  • Font: As desired (FreeSans 12 is used in the below images)
That's it. If you want the volume percentage to be displayed in another color then continue with step 5.

5. Go to /home/<username>/.config/gtk-3.0/gtk.css and enter the following text in the gtk.css file ("#00cccc" is used in the images here - change as desired). Remember to replace <n> for the plugin ID (see below):

Code: Select all

#genmon-<n> {color: #00cccc}
To find <n> (plugin ID) use any of the following methods:
  • Panel Properties > Items tab > hover the mouse pointer over the plugin and look for the number after the hyphen.
  • Look for "genmon" in /home/<username>/.config/xfce4/panel/
  • Code: Select all

    xfconf-query -c xfce4-panel -lv | grep genmon | awk '{print $1}' | awk -F'-' '{print $2}'
6. Restart the panel for the change to take effect:

Code: Select all

xfce4-panel -r
The below image shows the Panel volume percentage using amixer (with the Label field disabled) in MX-19 and MX-21. Note that amixer will not display any change when the audio is muted as shown in the third Panel in the image.
MX-19_21_amixer.png


Using amixer to display the volume percentage in Conky
7. Use steps 1-3 above to create a file to run the command.

8. Insert the line in the below code window into Conky. The number after "execi" is the interval in seconds that the code will run. Edit as desired, as well as the text that will be displayed ("Volume: "):

Code: Select all

Volume: ${execi 2 /home/<username>/.volumepercent }
A couple Conky codes that may be useful:
  • Change the font and font size to what follows the code (Verdana size 12 is used as an example):

    Code: Select all

    ${font Verdana:size=12}
  • Aligns the output of the code that follows it to the right margin:

    Code: Select all

    ${alignr}


Using pamixer to display the volume percentage in the Panel
9. Install pamixer from the Stable Respository.

10. Use step 4 above EXCEPT the command field is changed as follows:

Code: Select all

/usr/bin/pamixer --get-volume-human
11. Use steps 5-6 to change the volume percentage color in the Panel if desired.

The below image shows MX-21 with pamixer (with the Label field disabled). Note that when the audio is muted, "mute" is displayed as shown in the lower Panel image. Also pamixer is often off by 1% throughout the output range in comparison to the VolumeIcon volume level. (The Linear Scale is enabled in VolumeIcon > Preferences > Channels tab.)

Pamixer will not display 0% when the output volume is set to 0% using either the linear or logarithmic scales. This is why you see 11% displayed in the below image when the audio level is set to 10% (amixer displays the output level identical to the VolumeIcon volume level throughout the range). A minor point but a point nevertheless.
MX-21_pamixer.png


Using pamixer to display the volume percentage in Conky
12. The code to add in Conky is:

Code: Select all

Volume: ${execi 2 /usr/bin/pamixer --get-volume-human }
Edit-1: Added two Conky code suggestions to step 8.
Edit-2 (7 February 2023): As noted between red two Edit-2 lines above.
You do not have the required permissions to view the files attached to this post.

Return to “Tips & Tricks by users (not for help)”