Skip to content

Instantly share code, notes, and snippets.

@cscorley
Created March 16, 2012 23:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscorley/2053614 to your computer and use it in GitHub Desktop.
Save cscorley/2053614 to your computer and use it in GitHub Desktop.
Play audio from line-in in Pulseaudio; unload when finished.
#!/bin/bash
set -e
function get_loopback(){
# Detect the module number module-loopback is on
# If it isn't loaded, then load it.
loopback=`pactl list short modules | grep module-loopback | awk '{print $1}'`
if [ "$loopback" == "" ]; then
loopback=`pactl load-module module-loopback`
fi
echo $loopback
}
function get_active_port(){
# Find the active port on the source
port=`pactl list sources | grep "Active Port:" | awk '{print $3}'`
echo $port
}
function get_running_source(){
# Find the source which is running
src=`pactl list short sources | grep "RUNNING" | awk '{print $1}'`
echo $src
}
function main_loop(){
echo "Setting source $2 to use port $4"
pactl set-source-port $2 $4
echo "Press 'q' to quit playing from $4"
while read -n 1 user; do
if [ "$user" == "q" ]; then
echo ""
echo "Unloading module $1, Restoring source $2 back to port $3"
# should detect the previously used input and store it
pactl set-source-port $2 $3
pactl unload-module $1
exit
fi
done
}
module=$(get_loopback)
running=$(get_running_source)
prev_port=$(get_active_port)
main_loop $module $running $prev_port analog-input-linein
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment