#!/bin/bash #{{{ printhelp printhelp () { printf "rl: launch REAPER with a specified PIPEWIRE latency (to a point).\n" printf " this sets the PIPEWIRE_LATENCY variable for REAPER; as such, it\n" printf " does not necessarily set the period size and sample rate, but\n" printf " rather calculates based on the ratio and what values are " printf "available,\n" printf " -h) printf this help message\n" printf " -f) force using the provided rate even if PIPEWIRE_LATENCY is " printf "already set.\n" printf " -w) use Wayland menu (bemenu)\n" printf " -x) use X11 menu (dmenu)\n" printf " -a ARG) provide ARG as an argument for REAPER\n" printf " -d ARG) provide ARG as an argument for dmenu/bemenu\n" printf " -p ARG) sets the period size to ARG\n" printf " -s ARG) sets the sample rate to ARG\n" exit } #}}} #{{{ message message () { if [[ -t 0 ]]; then printf "$@\n" else notify-send "rl" "$@" fi } #}}} #{{{ getopts unset DARG RARG USE_SERVER while getopts "hfwxa:d:p:s:" o; do case "${o}" in f) FORCE="YES" ;; x) USE_SERVER="X11" ;; w) USE_SERVER="WAY" ;; a) RARG="$OPTARG $RARG" ;; d) DARG="$OPTARG $DARG";; p) PERIOD_SIZE="$OPTARG" ;; s) SAMPLE_RATE="$OPTARG" ;; *) printhelp ;; esac done #}}} #{{{ Check display server if -w or -x not provided if [ -z "$USE_SERVER" ]; then if [ -n "$WAYLAND_DISPLAY" ]; then DMENU="bemenu" elif [ -n "$DISPLAY" ]; then DMENU="dmenu" else printf "Can't tell if Wayland or X; what gives?\n" exit 1 fi else case "$USE_SERVER" in X11) DMENU="dmenu" ;; WAY) DMENU="bemenu" ;; *) printf "Can't tell if Wayland or X; what gives?\n" ;; esac unset USE_SERVER fi #}}} #{{{ Check if pipewire exists and is running if type pipewire >/dev/null 2>&1; then if ! pgrep -x pipewire >/dev/null 2>&1; then printf "pipewire is not running; this is not going to work!\n" exit 1 fi else if ! pgrep -x pipewire >/dev/null 2>&1; then printf "Cannot find pipewire in path, and it is not running; is it " printf "installed?\n" exit 1 fi fi #}}} #{{{ main #{{{ if PIPEWIRE_LATENCY is already set if [ -n "$PIPEWIRE_LATENCY" ]; then [ -z "$FORCE" ] && \ FORCE="$(echo "YES NO" | \ $DMENU -p "Override PIPEWIRE_LATENCY VALUE of ${PIPEWIRE_LATENCY}?" $DARG)" case "$FORCE" in YES|yes) unset FORCE ;; NO|no) message "Using existing PIPEWIRE_LATENCY=${PIPEWIRE_LATENCY}..." unset DMENU DARG FORCE reaper $RARG unset RARG ;; *) unset DMENU DARG RARG FORCE message "Did not understand value." exit 1 ;; esac fi #}}} [ -z "$PERIOD_SIZE" ] && \ PERIOD_SIZE="$(echo "16 32 64 128 256 512 1024" | $DMENU -p "Period size: " $DARG)" if [ -z "$PERIOD_SIZE" ]; then message "PERIOD_SIZE not set; will not continue." exit 1 fi [ -z "$SAMPLE_RATE" ] && \ SAMPLE_RATE="$(echo "44100 48000 88200 96000 176400 192000" | $DMENU -p "Sample rate: " $DARG)" if [ -z "$SAMPLE_RATE" ]; then message "SAMPLE_RATE not set; will not continue." exit 1 fi PIPEWIRE_LATENCY="${PERIOD_SIZE}/${SAMPLE_RATE}" reaper $RARG unset PERIOD_SIZE SAMPLE_RATE DMENU DARG RARG #}}}