Skip to content

Instantly share code, notes, and snippets.

@doyousketch2
Last active July 21, 2020 04:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doyousketch2/e276b4591d64cd04cf1d090508203e5d to your computer and use it in GitHub Desktop.
Save doyousketch2/e276b4591d64cd04cf1d090508203e5d to your computer and use it in GitHub Desktop.
Python Volume control for Linux

View vol-up.py & vol-down.py scripts, or Download ZIP

OpenSource PythonVersions Git.io License

Normally, when you scroll up-and-down on the volume icon,
the volume jumps 5% every time. 😝

I've got a 600 watt πŸ“» Yamaha 6.1 surround system, with a 300 watt sub. πŸ”ˆ
a 5% increase in volume from my πŸ’» PC is a huge increase in volume from the speakers πŸ“£ ‼️

I'd imagine you can change the global scroll-wheel settings, 🐭
but then it would take forever 😰 to scroll up-and-down webpages and text πŸ“‹

So instead, I created my own Python volume script πŸ˜„

vol-up is essentially the same as vol-down,
with a slight difference: that it only goes πŸ”Ό Up by 2%
whereas, it goes ⏬ Down by 6%

This was so that you get finer control over setting the volume you want,
without overshooting your desired goal. 🎯

Plus, giving you the ability to quickly πŸ”‡ dim the volume when neccissary.

In XFCE, Settings > Keyboard > Application shortcuts:
I set these to the volume-up and down keys.

I also discarded the regular ↕️ mixer icon on the taskbar.

Replaced it with two launchers:
One with an arrow icon πŸ”½ pointing down, and one πŸ”Ό up.

#!/usr/bin/python
# -*- coding: utf-8 -*-
## ============================================
## vol-down.py
## Eli Innis @Doyousketch2
## < Doyousketch2 @ yahoo.com >
## 15 Dec, 2016
##
## GNU GPLv3 www.gnu.org/licenses/gpl-3.0.html
## ============================================
## libs --------------------------------------
from Tkinter import Tk, Label
from subprocess import Popen, PIPE
## ============================================
## vars --------------------------------------
fontsize = 16 ## set font size
fontname = 'Balker' ## use whatever system font we have available
amount = '6%-' ## set volume change here
window = Tk() ## access a tk window
width = window .winfo_screenwidth() ## get screenwidth
height = window .winfo_screenheight() ## get screenheight
## ============================================
## functs ------------------------------------
## run 'amixer' as a subprocess, then pipe the output
card = Popen(['amixer', 'sset', 'Master', amount], stdout = PIPE)
out = card .communicate()[0] ## we only need the first element returned
vol = str(out) .split('[')[1] .split(']')[0]
## discard everything before open-bracket, discard everything after closing-bracket
if width > 1440: ## if dual-screen, don't place volume on second screen.
window .geometry('%dx%d+%d+%d' % (fontsize * 4, fontsize * 1.5, 1000, height - 90))
else: ## (widget h x widget w + x pos + y pos) ...no spaces tho.
window .geometry('%dx%d+%d+%d' % (fontsize * 4, fontsize * 1.5, width - width / 5, height - 90))
window .overrideredirect(1) ## hide title
window .after(1000, lambda: window .destroy() ) ## close it after a second
## create widget on main window, with volume % as the text, specified font & size
widget = Label(window, text = vol, font = (fontname, fontsize))
widget .pack() ## pack the widget into the required area, so everything lines up
## ============================================
## main --------------------------------------
window .mainloop() ## display widget on window
#!/usr/bin/python
# -*- coding: utf-8 -*-
## ============================================
## vol-up.py
## Eli Innis @Doyousketch2
## < Doyousketch2 @ yahoo.com >
## 15 Dec, 2016
##
## GNU GPLv3 www.gnu.org/licenses/gpl-3.0.html
## ============================================
## libs --------------------------------------
from Tkinter import Tk, Label
from subprocess import Popen, PIPE
## ============================================
## vars --------------------------------------
fontsize = 16 ## set font size
fontname = 'Balker' ## use whatever system font we have available
amount = '2%+' ## set volume change here
window = Tk() ## access a tk window
width = window .winfo_screenwidth() ## get screenwidth
height = window .winfo_screenheight() ## get screenheight
## ============================================
## functs ------------------------------------
## run 'amixer' as a subprocess, then pipe the output
card = Popen(['amixer', 'sset', 'Master', amount], stdout = PIPE)
out = card .communicate()[0] ## we only need the first element returned
vol = str(out) .split('[')[1] .split(']')[0]
## discard everything before open-bracket, discard everything after closing-bracket
if width > 1440: ## if dual-screen, don't place volume on second screen.
window .geometry('%dx%d+%d+%d' % (fontsize * 4, fontsize * 1.5, 1000, height - 90))
else: ## (widget h x widget w + x pos + y pos) ...no spaces tho.
window .geometry('%dx%d+%d+%d' % (fontsize * 4, fontsize * 1.5, width - width / 5, height - 90))
window .overrideredirect(1) ## hide title
window .after(1000, lambda: window .destroy() ) ## close it after a second
## create widget on main window, with volume % as the text, specified font & size
widget = Label(window, text = vol, font = (fontname, fontsize))
widget .pack() ## pack the widget into the required area, so everything lines up
## ============================================
## main --------------------------------------
window .mainloop() ## display widget on window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment