Every PulseAudio “Sink” has a “Source” named “monitor”. This lets you attach to a given Sink and chain more stuff to it, for example, recording the audio that is playing through PulseAudio at any given moment. This is very handy for creating, for example, PubQuiz-style clips of songs, movies, etc.
Here is a script to find the monitor for the most recently added Sink, record from it, and shove it through “sox” to get a WAV instead of raw sound data (requires recent sox, Pulse, etc):
#!/bin/bash WAV="$1" if [ -z "$WAV" ]; then echo "Usage: $0 OUTPUT.WAV" >&2 exit 1 fi rm -f "$WAV" # Get sink monitor: MONITOR=$(pactl list | egrep -A2 '^(\*\*\* )?Source #' | \ grep 'Name: .*\.monitor$' | awk '{print $NF}' | tail -n1) echo "set-source-mute ${MONITOR} false" | pacmd >/dev/null # Record it raw, and convert to a wav echo "Recording to $WAV ..." echo "Close this window to stop" parec -d "$MONITOR" | sox -t raw -r 44k -sLb 16 -c 2 - "$WAV"
© 2009 – 2011, Kees Cook. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 License.