Create a gist now

Instantly share code, notes, and snippets.

@gatlin /audio
Last active Sep 14, 2016

What would you like to do?
Shell script to make jackd and pulseaudio (and optionally fluidsynth) coexist with minimal interruption to my youtubing. Also it Works For Me™ on my laptop, will keep working on it
#!/bin/bash
###
# Script to launch jackd and reroute PulseAudio through it. And optionally
# start fluidsynth.
#
# READ THE NOTES PLEASE
###
# Usage (assuming this script is named `audio`, is in your PATH, and is
# executable):
#
# audio {start|stop} [-f]
#
# start Starts up jack and configures PulseAudio to route sound through
# it.
#
# stop Terminates jack and restores PulseAudio's original behavior. If
# fluidsynth is running, it is terminated as well.
#
# -f (Optional) Start fluidsynth along with jack.
###
# Notes
#
# 1. You'll need a package that on most distros is called `pulseaudio-jack`. It
# contains PulseAudio / jackd interop modules. Just install it and this script
# does everything else.
#
# 2. There's a variable defined below called DEVICE. You need to make sure it
# contains the identifier of the sound card and sound device you want to use.
#
# To figure that out it's super duper simple. If you have `aplay` run this:
#
# aplay -l
#
# Find the card you want, eg
#
# card 1: PCH [HDA Intel PCH], device 0: ALC3232 Analog [ALC3232 Analog]
# Subdevices: 1/1
# Subdevice #0: subdevice #0
#
# This entry says card 1 and device 0, so I set DEVICE to "1,0". Change it to
# whatever you need it to be.
#
# (If you don't have `aplay` install the `alsa-utils` package for your distro.)
#
# 3. You may need to refresh any browser tabs or restart any applications using
# the sound card when you run `audio start`.
###
# Credit for the initial script goes Ted Felix
# (see http://tedfelix.com/linux/linux-midi.html)
#
# Additional credit goes to this Arch Linux Wiki page:
# https://wiki.archlinux.org/index.php/PulseAudio/Examples
DEVICE="1,0"
case $1 in
start )
if pgrep jackd
then
echo jackd is already running
else
# tell pulse to suspend for a bit
pacmd suspend true
jackd -d alsa --device hw:$DEVICE --rate 44100 --period 128 \
&>/tmp/jackd.out &
# tell pulse to load the jack modules
pactl load-module module-jack-sink channels=2
pactl load-module module-jack-source channels=2
pacmd set-default-sink jack_out
pacmd set-default-source jack_in
if pgrep jackd
then
echo jackd is running
else
echo There was a problem starting jackd.
exit 1
fi
fi
# If fluidsynth is installed then start it as well
if [ "$2" = "-f" ] && hash fluidsynth 2>/dev/null; then
# Start fluidsynth
fluidsynth --server --no-shell --audio-driver=jack \
--connect-jack-outputs --reverb=0 --chorus=0 --gain=0.8 \
/usr/share/soundfonts/FluidR3_GM2-2.sf2 \
&>/tmp/fluidsynth.out &
sleep 1
if pgrep fluidsynth
then
echo fluidsynth is running
else
echo There was a problem starting fluidsynth.
fi
fi
;;
stop )
# fluidsynth may not be installed
if pgrep fluidsynth
then
killall fluidsynth
fi
if pgrep jackd
then
# unload the pulse jack modules
SINKID=$(pactl list | grep -B 1 "Name: module-jack-sink" | \
grep Module | sed 's/[^0-9]//g')
SOURCEID=$(pactl list | grep -B 1 "Name: module-jack-source" | \
grep Module | sed 's/[^0-9]//g')
pactl unload-module $SINKID
pactl unload-module $SOURCEID
sleep 5
killall jackd
fi
# unsuspend pulse
pacmd suspend false
echo Audio servers stopped.
;;
* )
echo Please specify start or stop...
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment