#!/bin/sh # # Changes the JACK sound device on the fly with minimal damage to running # programs. Useful for those who frequently unplug their USB sound cards, e.g. # if you need to move your laptop to the kitchen and continue watching the # movie with the built-in audio. # # The damage mostly lies in PulseAudio, which usually segfaults if jackd dies, # and with PulseAudio dies Skype, hangs mplayer and other bad things happen. # This script properly unloads PulseAudio modules then loads them when JACK is # ready to be restarted; if you don't use PulseAudio, JACK is just started. # # For this to work with PulseAudio you will need the pactl-unload-by-name # script, which is available at: # . # # @author hex@umonkey.net (Justin Forest) # @license Public Domain export GREP_OPTIONS= AVAILABLE_CARDS="$(aplay -L | grep CARD= | cut -d: -f2 | cut -d, -f1 | cut -d= -f2 | sort -u | xargs echo)" if [ -n "$1" ]; then CARDNAME="$1" if [ -z "$(aplay -L | grep CARD=$1, )" ]; then echo "No such card: $1, available options: $AVAILABLE_CARDS." exit 1 fi elif [ -n "$(aplay -L | grep 'CARD=USB')" ]; then CARDNAME="USB" else CARDNAME="Intel" fi CONFIG="/usr/bin/jackd -dalsa -dhw:$CARDNAME -r44100 -p1024 -n2 -s -H -M" CONFIG_NAME=$HOME/.jackdrc if [ "$CONFIG" = "$(cat $CONFIG_NAME)" ]; then echo "Current card: $CARDNAME, available options: $AVAILABLE_CARDS." exit 0 fi HAVE_JACK="$(pgrep -u $USER jackd)" HAVE_PULSE="$(pgrep -u $USER pulseaudio)" if [ -n "$HAVE_PULSE" ]; then if [ -z "$(which pactl-unload-by-name)" ]; then echo "Please install pactl-unload-by-name (copy to your \$PATH) from here:" echo "http://umonkey-tools.googlecode.com/hg/bin/pactl-unload-by-name" exit 1 fi echo "Unloading PulseAudio modules." pactl-unload-by-name "module-jack-*" fi echo "Switching to $CARDNAME." echo -n "$CONFIG" > $CONFIG_NAME if [ -n "$HAVE_JACK" ]; then echo "Stopping JACK." pkill -9 -u $USER jackd sleep 1 fi if [ -n "$HAVE_PULSE" ]; then echo "Loading PulseAudio JACK sink." pactl load-module module-jack-sink >/dev/null echo "Loading PulseAudio JACK source." pactl load-module module-jack-source >/dev/null else echo "Starting the JACK daemon." . $CONFIG_NAME fi if [ -n "$(which jack.plumbing)" ]; then echo "Restarting jack.plumbing" pkill -u $USER jack.plumbing jack.plumbing >/dev/null 2>&1 & fi