Add pulseaudio_record_from_everything.sh
[experiments/scripts.git] / pulseaudio_record_from_everything.sh
1 #!/bin/bash
2 #
3 # pulseaudio_record_from_anything - record from all sources with pulseaudio
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software. It comes without any warranty, to
8 # the extent permitted by applicable law. You can redistribute it
9 # and/or modify it under the terms of the Do What The Fuck You Want
10 # To Public License, Version 2, as published by Sam Hocevar. See
11 # http://sam.zoy.org/wtfpl/COPYING for more details.
12
13 set -e
14
15 command -v /usr/bin/parec > /dev/null || { echo "parec missing, install pulseaudio-utils" 1>&2; exit 1; }
16 command -v /usr/bin/oggenc > /dev/null || { echo "oggenc missing, install vorbis-tools" 1>&2; exit 1; }
17
18 if pactl list short sinks | grep -q module-null-sink;
19 then
20   echo "The null sink is already loaded, bailing out." 1>&2
21   exit 1
22 fi
23
24 # Load the null sink.
25 # It'll act as a "kitchen sink"... I'll show myself out.
26 PA_IDS=$(pactl load-module module-null-sink)
27 trap 'exit 1' INT
28 trap 'trap INT; for pa_id in $PA_IDS; do pactl unload-module "$pa_id"; done' EXIT
29
30 # Record from everything by redirecting any source to the null sink.
31 SOURCES=$(pactl list short sources | cut -f 2 | grep -v '^null\.monitor$')
32 for source in $SOURCES;
33 do
34   MODULE_ID=$(pactl load-module module-loopback latency_msec=1 source="$source" sink=null)
35   PA_IDS="$MODULE_ID $PA_IDS"
36 done
37
38 # Listen to what you are recording.
39 # NOTE: beware of Larsen effect when using microphones.
40 #pactl load-module module-loopback source=null.monitor sink=@DEFAULT_SINK@
41
42 # Un-mute the sink.
43 pactl set-sink-mute null 0
44
45 # Record everything.
46 parec -d null.monitor | oggenc -b 192 -o "recording_output_$(date +"%F_%T").ogg" --raw -