Automatically Unmute USB Headset

I wrote a post a while ago about configuring my USB headset from the command line.  Naturally, the next step was to automate the process so it would unmute and raise the mic volume to the maximum without any intervention on my part.  Fortunately for me, almost all the work was done for me.

I was fortunate enough to stumble upon some IBM DeveloperWorks documentation on a fairly recent kernel addition, i-notify.  I-notify is very, very cool.  It allows for someone to monitor file system events.  The problem is that I thought I would need a lot more code (in C, of all languages) to do something useful, say, monitor when the kernel loads a driver module for USB audio devices.  Sounds relevant, no?  Boy, was I wrong.

Somehow, I had foolishly believed that this code could lie around for years without someone making a toolset in Linux.  Further research brought me to the inotify-tools website, which mentioned how it is conveniently packaged as a RPM file for me in the Fedora Extras repository.  So, I installed it.

sudo yum -y install inotify-tools

With a toolset already coded for me, this changed from a programming problem to a simple scripting problem.  I was having some trouble, but that was because I have never scripted bash before.  I stumbled upon this post on LinuxQuestions.org, and it guided me in the right direction.  So, I drafted this little script here, and saved it under the clever name unmute.

unmute

#!/bin/bash
# Use the inotify-tools toolset to run command to unmute
# mic and raise volume if it detects the proper driver.

kmod=/lib/modules/`uname -r`/kernel/sound/usb/snd-usb-audio.ko

inotifywait -q –monitor –format %e -e open $kmod | while read event; \
do sleep 5 && amixer -q -c 2 sset Mic 200% cap unmute; \
done

The fun doesn’t stop there.  Now, we need to make sure that the script is active on startup.  So, make the script executable and move it to a directory in your $PATH environment variable.

chmod +x unmute

mv unmute /usr/bin

And last but not least, we must add the script to the rc.local (located at /etc/rc.local) file to ensure that it loads when the computer boots up.  Make sure you add the line in bold with the necessary admin privileges.  Keep in mind your rc.local file might look different.

/etc/rc.local

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.

unmute &

touch /var/lock/subsys/local

And voila!  Now your computer, if it has Fedora 9 with necessary libraries and it uses snd-usb-audio kernel module for your headset as it should, will automatically unmute the USB headset and set the volume to max.  Let me know if you have problems.


About this entry