What I did on my vacation from reality

Home About RTL8187B Laptop Cam Quotes Login

February 1, 2009

STFU, Rhythmbox!

1:12 pm — Main

Ubuntu’s default music player for Gnome is Rhythmbox, in case you weren’t aware. It’s a decent enough player, though I’m told it fails at large collections, but I’m not here to plug it or rag on it.

One of its niftier features is software mixing. In other words, it can adjust the volume of the playing audio without adjusting the actual hardware volume levels for the soundcard.

This is cool for two reasons. First, there’s a CLI to get and set the player volume. It’s not very script-friendly, but it’s workable. Second, you can play something else at full volume while playing an mp3 at reduced volume.

For example, if your email client can execute a command when mail comes in, you can turn down the volume on the mp3, play a notification wav at full volume, and then turn the volume back up on the mp3. Mine happens to read a brief summary through the eSpeak speech synthesizer.

This program will turn the volume down on Rhythmbox, leaving the hardware volumes untouched and storing the volume for use by the second script.

#!/bin/sh
#
# rhythmbox-stfu.sh
#
#   Turn down the volume on Rhythmbox, storing the previous
#   volume in $TMPDIR/rhythmbox-saved-volume.
#
 
export XAUTHORITY=$HOME/.Xauthority DISPLAY=:0
vol=`rhythmbox-client --no-start --print-volume | cut -d' ' -f4 | sed 's/\.$//' 2>/dev/null`
 
if test "x$vol" = "x";
then
	exit 0 # Rhythmbox isn't running
fi
 
echo $vol > ${TMPDIR:-/tmp}/rhythmbox-saved-volume
rhythmbox-client --set-volume 0.1
 
sleep 1

After running that, you can feed whatever else you want through the sound system at full volume, with the mp3 playing at about 10% volume, after which you can run this script to restore it:

#!/bin/sh
 
export XAUTHORITY=$HOME/.Xauthority DISPLAY=:0
rhythmbox-client \
	--no-start \
	--set-volume `cat ${TMPDIR:=/tmp}/rhythmbox-saved-volume`
 
rm -f $TMPDIR/rhythmbox-saved-volume

Pretty easy, huh? Now we just hook it into a wrapper for a speech synthesizer:

#!/bin/sh
 
if test "x$1" != "x";
then
  echo "$@" | sh $0
  exit $?
fi
 
tmpfile=`tempfile -p speech`
 
rhythmbox-stfu
 
while read text;
do
  echo "$text" | espeak -w $tmpfile
  aplay -q $tmpfile
  rm -f $tmpfile
done
 
rhythmbox-ok

And there you go. Volume goes down while the computer speaks, comes back up when it’s done.




RSS feed | Trackback URI

1 Comment»

ralfpaqui —June 4, 2009 @ 2:10 am

thus trade browsers serious 1990 study

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped=""> in your comment.