You are not logged in.

#1 2010-01-19 07:08:54

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

bash-mixer: easy to use, amixer-based, with libnotify support

Hi:
I'm tired of gmixer. It consumes more than needed for just a mixer and the most of the time I don't even use it, so, I written my own tiny bash helper for volume control.

I setted up some keybindings in my Openbox for doing that:
- Ctrl-Shift-Alt-Up: Incresase volume (bash-mixer up)
- Ctrl-Shift-Alt-Down: Decrease volume (bash-mixer down)
- Ctrl-Shift-Alt-V: Show volume level using libnotify (bash-mixer osd)
- Ctrl-Shift-Alt-M: Toggle Mute/Unmute, keeping the volume state before muting and restoring it at unmuting (bash-mixer togglemute)

As simple as that big_smile

Configure $MASTER_CONTROL, $OTHER_CONTROLS array, and $STEP as needed, but it should work out of the box.


#!/bin/sh

# Bash amixer-based mixer for ALSA
#
# Author: Pável Varela Rodríguez <neonskull@gmail.com>
#
# Use it by setting some keybinding or calling it directly from CLI
#
# Configure as needed:
#     MASTER_CONTROL
#     STEP
#
# Interface: All defined function names
#

# User's Settings
MASTER_CONTROL="PCM"
STEP=3


# Script's Settings
STATE_FILE="/tmp/volume.state"


getMasterVolume ()
{
    amixer get $MASTER_CONTROL|tail -n1|sed -r 's/.*\[(.*)%\].*/\1/'
}


# $1: New Volume Level in %
setMasterVolume () { amixer set $MASTER_CONTROL $1"%">/dev/null; }


volumeUp ()
{
    VOL=$(getMasterVolume)
    [[ $VOL -ge 100 ]] && exit 0
    setMasterVolume $((VOL + $STEP))
}


volumeDown ()
{
    VOL=$(getMasterVolume)
    [[ $VOL -le 0 ]] && exit 0
    setMasterVolume $((VOL - $STEP))
}


mute () { echo $(getMasterVolume) > $STATE_FILE;setMasterVolume 0; }
unmute () { setMasterVolume $(cat $STATE_FILE);rm -f $STATE_FILE; }


# Main Interface
up () { volumeUp; }
down () { volumeDown; }
togglemute () { [[ -f "${STATE_FILE}" ]] && unmute || mute; }
get () { getMasterVolume; }
osd ()
{
    BODY="Current Volume Level: "$(getMasterVolume)"%"
    notify-send -t 3000 -i "info" "Volume" "${BODY}"
}


# This do the interface magic (thanks Bash ;-))
$@

So, I won't need never ever a mixer app running all the time and wasting resources.

Hope you find this useful.

Last edited by NeOnsKuLL (2010-01-20 02:48:32)


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

#2 2010-01-19 19:25:45

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

Re: bash-mixer: easy to use, amixer-based, with libnotify support

Updated script:
- Added Toogle Mute/Unmute
- Cleaned the code a bit

Enjoy it


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

#3 2010-01-19 22:26:12

Andrwe
Member
From: Leipzig/Germany
Registered: 2009-06-17
Posts: 322
Website

Re: bash-mixer: easy to use, amixer-based, with libnotify support

Thanks that is a very nice script.
I've used mpc to change volume untill now but this doesn't work if mpd isn't active.
But your script solved this problem for me now, again thaks.

Offline

#4 2010-01-19 22:58:54

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

Re: bash-mixer: easy to use, amixer-based, with libnotify support

Thanks to you for your feedback. I'm really glad it helps at least one people, besides me, off course. If you have any other feedback, don't hesitage to contact me.

See you, and as I always say: enjoy it


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

#5 2010-01-20 01:04:58

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

Re: bash-mixer: easy to use, amixer-based, with libnotify support

Updated script:
- Removed $OTHER_CONTROLS since they are not really needed.
- Because of the previous modification, some functions are not needed anymore, so, I removed they too.
- Changed the pipeline used to get current volume. The new one is shortier than previous and with less steps.

Because of those changes, now it executes in aproximately 60% of the time (about 16 ms), so, it's a big preformance improve, not to mention now the code is really clean, and there is less code.

Enjoy it


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

#6 2010-01-20 05:10:12

Kitty
Member
From: The Burning Desert
Registered: 2008-01-11
Posts: 88

Re: bash-mixer: easy to use, amixer-based, with libnotify support

I made the following change to make it work w/o needing libnotify, because I don't use a systray in my tiling wm. I also added a function 'fix' to set the volume to a fixed number without have to do 'up/down' repeatedly. Again because I don't have it hotkeyed.

osd ()
{
    BODY="Current Volume Level: "$(getMasterVolume)"%"
#    notify-send -t 3000 -i "info" "Volume" "${BODY}"
    echo $BODY
}
fix () { setMasterVolume $1; }

call fix with '$ bash-mixer fix 50'

It's a little more command line friendly now. smile


/etc/rc.d/ is where daemons reside. Beware.

Offline

#7 2010-01-20 05:35:13

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

Re: bash-mixer: easy to use, amixer-based, with libnotify support

@Kitty
Man, I don't know what are you talking about, but, why don't you just call '$ bash-mixer get' instead of comment the notify line? Just asking.

About "fix", man, couldn't you just call '$ bash-mixer setMasterVolume 50'? OK, I know, is more typing.

That's the beauty of open source. You don't like it the way it is? Doesn't matters, you can always change it to your needs ;-D

Thanks for your comment.

See you around.

Last edited by NeOnsKuLL (2010-01-20 05:38:15)


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

#8 2010-01-20 07:05:45

Kitty
Member
From: The Burning Desert
Registered: 2008-01-11
Posts: 88

Re: bash-mixer: easy to use, amixer-based, with libnotify support

Heh, I did the echo hack before you added the get function. smile It's a nice script, glad you shared.


/etc/rc.d/ is where daemons reside. Beware.

Offline

#9 2010-01-20 07:28:16

NeOnsKuLL
Member
From: Havana, Cuba
Registered: 2005-03-29
Posts: 117

Re: bash-mixer: easy to use, amixer-based, with libnotify support

No my friend, the get function is there from the begining, I have removed some unuseful functions since I removed $OTHER_CONTROLS, I've not added any new function.

Doesn't matters, you put your own two cents, that's the spirit.

Thank you

Last edited by NeOnsKuLL (2010-01-20 07:29:41)


Intel Core 2 Duo E8400 3.0 GHz | 2x1GB 667MHz | 250+750GB Seageate SATAII | Samsung 19" TFT 1440x900
Openbox + obmenugen + PyTyle | bmpanel2 | oblogout | conky | pyBgSetter (with Esetroot as backend)
Projects: obmenugen, pyBgSetter

Offline

Board footer

Powered by FluxBB