summaryrefslogtreecommitdiff
path: root/rl
blob: 3275d11b3d1ec155eaaa789cad48e759d94ea497 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/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
#}}}