Last active
October 17, 2021 13:08
-
-
Save gatlin/436fe265ce05574c84a9e3f0d2158650 to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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} DEVICE [-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. | |
# | |
# DEVICE Format "C,D", for a card number C and a device number D. | |
# See the NOTES section for details. | |
# | |
# -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 an argument 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 run this script as | |
# | |
# $> audio start "1,0" | |
# | |
# | |
# (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="$2" | |
if [ -z "${DEVICE}" ]; | |
then | |
echo "Please select a device (formatted, eg '1,0' for card 1, dev 0)." | |
exit 1 | |
fi | |
case $1 in | |
start ) | |
if pgrep jackd | |
then | |
echo jackd is already running | |
else | |
# tell pulse to suspend for a bit | |
pacmd suspend true | |
pasuspender -- jackd -d alsa --device hw:$DEVICE \ | |
&>/tmp/jackd.out & | |
sleep .5 | |
# 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 [ "$3" = "-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 | |
pactl load-module module-suspend-on-idle | |
pasuspender -- 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