Skip to content

Instantly share code, notes, and snippets.

@MaartenBaert
Last active February 22, 2016 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MaartenBaert/77bd611694cd6d00e007 to your computer and use it in GitHub Desktop.
Save MaartenBaert/77bd611694cd6d00e007 to your computer and use it in GitHub Desktop.
A script that automates the process of configuring JACK, PulseAudio and ALSA for recording with SSR. It's used to record game audio, microphone audio and Skype/TeamSpeak audio simultaneously. This script only works when combined with the right config files. Also, this is completely unsupported, don't expect it to 'just work'.
#!/bin/bash
# IMPORTANT: READ THIS BEFORE RUNNING THIS SCRIPT!
# This script only works when the system is configured correctly. Do these things first:
#
# Step 1:
# - If you have an ~/.asoundrc file, rename it to ~/.asoundrc-default. If you don't have one, create an empty file named ~/.asoundrc-default.
# - Create ~/.asoundrc-jack with the contents found here: https://gist.github.com/MaartenBaert/3630a1863969ba344704
# The script will create a symlink from .asoundrc to .asoundrc-jack when started, and from .asoundrc to .asoundrc-default when stopped.
#
# Step 2:
# - Open /etc/pulse/default.pa and comment out the following lines:
# load-module module-udev-detect
# load-module module-detect
# - Next, add the following lines to the end:
# load-module module-alsa-sink device=sysdefault:0
# load-module module-alsa-source device=sysdefault:1
# Now PulseAudio will cooperate better with ALSA, but you will lose all autodetection - you have to configure devices manually.
#
# This script assumes that you use a USB microphone which is not your system sound card (i.e. you must use alsa_in). If this is not the case, don't use this script, it just adds unnecessary complexity.
set -e
usage() {
echo "Usage: gast [OPTIONS]" >& 2
echo "" >& 2
echo "Options:" >& 2
echo " --help Show this help message." >& 2
echo " --micloop Enables the nice micloop." >& 2
echo " --echo Enables echo micloop." >& 2
}
C0=$( echo -e "\e[0m" )
C1=$( echo -e "\e[1;31m" )
C2=$( echo -e "\e[1;32m" )
OPT_MICLOOP=0
OPT_ECHO=0
while [ $# -gt 0 ]
do
if [ x"$1" = x"--" ]
then
shift
break
elif [ x"$1" = x"--help" ]
then
usage
exit
elif [ x"$1" = x"--micloop" ]
then
OPT_MICLOOP=1
shift
elif [ x"$1" = x"--echo" ]
then
OPT_ECHO=1
shift
elif [ x"${1:0:1}" = x"-" ]
then
echo "gast: Unknown option '$1'!" >& 2
usage
exit 1
else
break
fi
done
echo "${C1}---- Switching asoundrc to JACK ...${C0}"
rm -f ~/.asoundrc
ln -s .asoundrc-jack ~/.asoundrc
echo "${C1}---- Starting JACK ...${C0}"
jackd -T -ndefault -dalsa -dhw:0 -r44100 -i2 -o2 -p2048 -n2 &
PID_JACK=$!
sleep 2
echo "${C1}---- Starting PulseAudio JACK sink ...${C0}"
pactl load-module module-jack-sink
pactl set-default-source alsa_input.sysdefault_1
pactl set-default-sink jack_out
sleep 1
if [ $OPT_MICLOOP = 1 ]
then
echo "${C1}---- Starting micloop ...${C0}"
chrt 5 alsa_in -j mic -d sysdefault:1 -p 2048 -n 6 &
PID_MICLOOP=$!
echo "${C1}---- Starting micloop autoconnect ...${C0}"
python -c '
import subprocess
import time
while True:
ports = subprocess.Popen(["jack_lsp", "-c"], stdout=subprocess.PIPE).stdout.read()
if "mic:capture_1" in ports and "SimpleScreenRecorder:in_1" in ports and not "mic:capture_1\n SimpleScreenRecorder:in_1\n SimpleScreenRecorder:in_2" in ports:
subprocess.call(["jack_connect", "mic:capture_1", "SimpleScreenRecorder:in_1"])
subprocess.call(["jack_connect", "mic:capture_1", "SimpleScreenRecorder:in_2"])
time.sleep(1)
' &
PID_MICLOOP_AUTOCONNECT=$!
fi
if [ $OPT_ECHO = 1 ]
then
echo "${C1}---- Starting echo ...${C0}"
micloop-pulse &
#alsaloop -C pulse -P pulse -t 100000 &
#alsa_in -j mic -d pulse -p 2048 -n 3 &
#alsa_in -j mic -d hw:Microphone,0 -p 2048 -n 3 &
PID_ECHO=$!
fi
sleep 1
simplescreenrecorder &
echo "${C2}---- Ready! Press Enter to stop.${C0}"
read
if [ $OPT_MICLOOP = 1 ]
then
echo "${C1}---- Stopping micloop autoconnect ...${C0}"
kill $PID_MICLOOP_AUTOCONNECT
echo "${C1}---- Stopping micloop ...${C0}"
kill $PID_MICLOOP
fi
if [ $OPT_ECHO = 1 ]
then
echo "${C1}---- Stopping echo ...${C0}"
kill $PID_ECHO
pkill alsaloop
fi
sleep 1
echo "${C1}---- Stopping PulseAudio JACK sink ...${C0}"
pactl set-default-source alsa_input.sysdefault_11
pactl set-default-sink alsa_output.sysdefault_0
pactl unload-module module-jack-sink
echo "${C1}---- Stopping JACK ...${C0}"
kill $PID_JACK
sleep 1
echo "${C1}---- Switching asoundrc to default ...${C0}"
rm -f ~/.asoundrc
ln -s .asoundrc-default ~/.asoundrc
sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment