You are not logged in.
I'm setting up openbox to use my media keys, and want to show an OSD with the current volume(%) when I press volume up or down.
I have come this far:
amixer get Master |grep % |awk '{print $4}'|sed 's/.//'
this gives me
51%]
How do I get rid of the last bracket?
Is there an easier way to get this value? (probably. I don't often do something like this )
Through google I found how to remove the first char, and I also found how to remove the last char using sed. However I couldn't find anything that combines the two.
Any good ideas?
Last edited by madeye (2010-01-26 20:54:58)
MadEye | Registered Linux user #167944 since 2000-02-28 | Homepage
Offline
amixer get Master |grep % |awk '{print $5}'|sed -e 's/\[//' -e 's/\]//'
Probably not good form, but it works
Note that I have to change $4 to $5 for it to work on my machine
Cheers
edit
a slightly nicer version..
amixer get Master |grep % |awk '{print $5}'|sed 's/[^0-9\%]//g'
or if you dont want the %
amixer get Master |grep % |awk '{print $5}'|sed 's/[^0-9]//g'
Last edited by Grazz256 (2010-01-26 20:25:04)
Offline
I think you can do this even a little bit more easy:
amixer get Master | awk '$0~/%/{print $4}' | tr -d '[]'
or if you don't want the "%":
amixer get Master | awk '$0~/%/{print $4}' | tr -d '[]%'
You might have to replace $4 by $5
[pidgin-knotify] Native KDE notifications for pidgin - [series60-remote] Manage your Symbian S60 phone
Offline
Here's mine. I use gawk, but I think awk will work, too.
amixer get Master | gawk '/Mono.+/ {gsub("\\[","", $4);gsub("\\]", "", $4);printf("%s",$4)}'
It should be slightly more efficient as there's fewer processes spawned, but that's theory I haven't tested.
I'm matching "Mono..." you may need to match differently, and $4 is my volume setting.
Also, I think it's possible to use just the one gsub, but I couldn't get that to work.
As an aside, another one I've used was this, which kept the [] as I used that to tell the difference between volume and other percentages in the status bar:
amixer get Master | awk '/Mono.+/ {print $6=="[off]"?$6:$4}'
This prints out [off] or the volume percentage, depending on if the volume is muted or not.
As usual, YMMV.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
And then I learned something new again
Actually I didn't know about the tr command.
Thanks for all the solutions.
EDIT:
In case anyone is interested in my volume script, here it is:
#!/bin/sh
case $1 in
up)
/usr/bin/amixer -q -c 0 sset 'Master' 5%+
;;
down)
/usr/bin/amixer -q -c 0 sset 'Master' 5%-
;;
*)
/usr/bin/amixer -q -c 0 sset 'Master' toggle
;;
esac
CURVOL=`amixer get Master | awk '/Mono.+/ {print $6=="[off]"?$6:$4}' | tr -d '[]'`
notify-send "Volume: $CURVOL"
I have added the following to my ~/.config/openbox/lxde-rc.xml
<!-- volume control -->
<keybind key="XF86AudioRaiseVolume">
<action name="Execute">
<command>amix.sh up</command>
</action>
</keybind>
<keybind key="XF86AudioLowerVolume">
<action name="Execute">
<command>amix.sh down</command>
</action>
</keybind>
<keybind key="XF86AudioMute">
<action name="Execute">
<command>amix.sh</command>
</action>
</keybind>
Last edited by madeye (2010-01-26 21:18:44)
MadEye | Registered Linux user #167944 since 2000-02-28 | Homepage
Offline
You're using notify-send, if you want something a bit more cooler, look at this piece of python code I wrote a while back, it might inspire you
def notify():
value = get_master_volume()
if 66 < value <= 100:
icon = "notification-audio-volume-high"
if 33 < value <= 66:
icon = "notification-audio-volume-medium"
if 0 < value <= 33:
icon = "notification-audio-volume-low"
if value == 0:
icon = "notification-audio-volume-off"
if is_master_mute() == True:
icon = "notification-audio-volume-muted"
os.system("notify-send ' ' -i %s -h int:value:%s -h string:synchronous:volume" % (icon, value))
The notify-send line looks a little messy, but I'm sure you can figure it out.
notify-send ' ' -i $icon -h int:value:$volume -h string:synchronous:volume
Last edited by robrene (2010-01-26 22:57:18)
Offline
Can be done in plain C :
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(int argc,char *argv[])
{
int pipe_fds[2],pid_amixer;
if (argc<2) return 0;
pipe(pipe_fds);
if ((pid_amixer=fork())==0)
{
close(pipe_fds[0]);
dup2(pipe_fds[1], 1);
argv[0]="amixer";
execvp(argv[0], argv);
}
else
{
char buffer[1024];
char *tok;
const char *target="Front Left:";
int volume;
char buf_is_on[6];
int is_on;
close(pipe_fds[1]);
read(pipe_fds[0], buffer, 1024);
close(pipe_fds[0]);
if ((tok=strstr(buffer,target)))
{
sscanf(tok, "%*s %*s %*s %*s %*c%i%*s %*s %5s", &volume,buf_is_on);
is_on=!strcmp("[on]",buf_is_on);
printf("Volume = %d, is on = %d",volume,is_on);
//instead of printf, you can exec notify-osd, etc...
}
}
return 0;
}
Compile with :
gcc amixer-wrapper.c -o amixer-wrapper
Launch amixer-wrapper with usual amixer parameters, for instances:
amixer-wrapper get Master
amixer-wrapper set Master 3%+
amixer-wrapper set Master toggle
Offline
Can be done in plain C
Of course it can be done in C. It can be done in anything. But do we want it to? Above shell script is just fine for a Linux user, a one-liner is simple and elegant, and those that did not employ anything other than awk especially so. Since you did write C maybe we will see other examples, maybe in Ruby?
Last edited by anrxc (2010-02-04 01:44:57)
You need to install an RTFM interface.
Offline