]> git.madduck.net Git - code/pulserecorder.git/blobdiff - pulserecorder

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Pipe recording experiments
[code/pulserecorder.git] / pulserecorder
index a9f4cdca1c3be5484ebcabf41765bcfb02ee0e2f..5dad254431cae55d6a3cbb5ec14b99993ce64420 100755 (executable)
@@ -15,7 +15,7 @@
 #
 # The third form waits for a new sink-input to appear, and will record that.
 #
 #
 # The third form waits for a new sink-input to appear, and will record that.
 #
-# Copyright © 2020 martin f. krafft <madduck@madduck.net>
+# Copyright © 2020–2021 martin f. krafft <madduck@madduck.net>
 # Released under the teams of the Artistic Licence 2.0
 #
 set -eu
 # Released under the teams of the Artistic Licence 2.0
 #
 set -eu
@@ -44,6 +44,10 @@ export TMPDIR
 TMPDIR=$(mktemp -dp "$TMPDIR" parec.XXXXXXXXXX)
 cleanup_hook rm -r $TMPDIR
 
 TMPDIR=$(mktemp -dp "$TMPDIR" parec.XXXXXXXXXX)
 cleanup_hook rm -r $TMPDIR
 
+if command -v systemd-inhibit >/dev/null; then
+  systemd-inhibit --who=pulserecorder --why=recording sleep 99d 2>/dev/null &
+  cleanup_hook kill $!
+fi
 
 state= outfile= index= clobber=0
 for arg in "$@"; do
 
 state= outfile= index= clobber=0
 for arg in "$@"; do
@@ -68,7 +72,7 @@ pa_wait_for_event() {
   type="${1:-*}" event="${2:-*}" id="${3:-*}"
   pa_subscribe | (
     read pid
   type="${1:-*}" event="${2:-*}" id="${3:-*}"
   pa_subscribe | (
     read pid
-    #echo >&2 pa_subscribe started with PID $pid
+    #echo >&2 pa_subscribe started with PID $pid, waiting for $type/$event/$id
     while read lead xevent on xtype xid; do
       xid=${xid#\#}
       #echo >&2 "pa_subscribe: $xtype/$xevent/$xid ($type/$event/$id)"
     while read lead xevent on xtype xid; do
       xid=${xid#\#}
       #echo >&2 "pa_subscribe: $xtype/$xevent/$xid ($type/$event/$id)"
@@ -87,6 +91,28 @@ pa_get_next_index() {
   }
 }
 
   }
 }
 
+uuidgen() {
+  hascmd() { command -v "$@" >/dev/null;}
+  if   hascmd uuid; then uuid
+  elif hascmd uuidgen; then uuidgen
+  elif hascmd python3; then
+    python3 -c 'import uuid; print(uuid.uuid1())'
+  elif hascmd python; then
+    python -c 'import uuid; print(uuid.uuid1())'
+  else
+    dd if=/dev/urandom bs=16 count=1 status=none | base64
+  fi
+}
+
+if [ -z "$outfile" ]; then
+  uuid=$(uuidgen)
+  outfile="${uuid}.ogg"
+fi
+
+if [ -f "$outfile" ] && [ $clobber -eq 0 ]; then
+  echo >&2 "E: file exists, and -f not given: $outfile"
+  exit 1
+fi
 case "$index" in
 
   (last)
 case "$index" in
 
   (last)
@@ -113,29 +139,7 @@ if [ -z "$index" ]; then
   exit 1
 fi
 
   exit 1
 fi
 
-uuidgen() {
-  hascmd() { command -v "$@" >/dev/null;}
-  if   hascmd uuid; then uuid
-  elif hascmd uuidgen; then uuidgen
-  elif hascmd python3; then
-    python3 -c 'import uuid; print(uuid.uuid1())'
-  elif hascmd python; then
-    python -c 'import uuid; print(uuid.uuid1())'
-  else
-    dd if=/dev/urandom bs=16 count=1 status=none | base64
-  fi
-}
-
-uuid=$(uuidgen)
-
-[ -n "$outfile" ] || outfile="${uuid}.ogg"
-
-if [ -f "$outfile" ] && [ $clobber -eq 0 ]; then
-  echo >&2 "E: file exists, and -f not given: $outfile"
-  exit 1
-fi
-
-devname="record-to-file-${uuid}"
+devname="record-to-file-$(echo -n ${outfile%.*} | tr -c '[:alnum:]' '_')"
 
 echo >&2 "Recording source $index to $outfile …"
 
 
 echo >&2 "Recording source $index to $outfile …"
 
@@ -148,13 +152,23 @@ load_module() {
 }
 
 move_source_to_sink() {
 }
 
 move_source_to_sink() {
-  local c;
+  local c d;
   c=$(pactl list short sink-inputs | sed -rne "s,^${1}[[:space:]]+([[:digit:]]+).+,\1,p")
   c=$(pactl list short sink-inputs | sed -rne "s,^${1}[[:space:]]+([[:digit:]]+).+,\1,p")
+  d="$(pactl list short sinks | sed -rne "s,^${c}[[:space:]]+([^[:space:]]+).+,\1,p")"
+  case "$d" in
+    (record-to-file-*)
+      # Never restore to a record-to-file destination, or it could botch
+      # another recording
+      c="@DEFAULT_SINK@"
+      ;;
+    (*) :;;
+  esac
+  echo >&2 "Moving input $1 to sink $2 (restore to $c) …"
   cleanup_hook pactl move-sink-input $1 $c
   pactl move-sink-input $1 $2
 }
 
   cleanup_hook pactl move-sink-input $1 $c
   pactl move-sink-input $1 $2
 }
 
-if false; then
+if [ -n "${PAREC_PIPE:-}" ]; then
   # This would be great, but it does not work. For instance, trying this on
   # audible meant that audible would play at maximum speed (3h played in 3
   # minutes), but the result would be full of skips, making me think that
   # This would be great, but it does not work. For instance, trying this on
   # audible meant that audible would play at maximum speed (3h played in 3
   # minutes), but the result would be full of skips, making me think that
@@ -163,8 +177,11 @@ if false; then
 
   load_module module-pipe-sink "$devname" "$devname" file="$TMPDIR/outfifo"
   move_source_to_sink $index "$devname"
 
   load_module module-pipe-sink "$devname" "$devname" file="$TMPDIR/outfifo"
   move_source_to_sink $index "$devname"
-  oggenc --raw -q5 -o "$outfile" "$TMPDIR/outfifo" || :
-  #pv -pterb "$TMPDIR/outfifo" > $TMPDIR/outfile.wav || :
+  #oggenc --raw -q5 -o "$outfile" "$TMPDIR/outfifo" || :
+  pv -pterb "$TMPDIR/outfifo" > $TMPDIR/outfile.wav &
+  pid=$!
+  pa_wait_for_event sink-input remove $index >/dev/null
+  kill $pid
 
 else
   # More traditional approach, which just takes 1:1 time.
 
 else
   # More traditional approach, which just takes 1:1 time.