-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture-audio
executable file
·202 lines (176 loc) · 5.84 KB
/
capture-audio
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env fish
# capture-audio - Capture the audio output going to the speakers and record it to a file.
# requirements: jack_capture, convert-audio, c
# FIX: not done yet:
# - better output when starting/stopping
# settings:
# directory to save the recordings
set directory ~/misc/snd/jack-cap/
# default filename (not including the extension)
set filename "rec-$(date '+%Y-%m-%d-%H-%M')"
# whether to copy the filename to the clipboard by default
set copy 'y'
# the default format to record in
set format 'flac'
# code:
set -- script "$(path basename (status filename))"
argparse -n $script 'h/help' 'a/action=' 's/source=' 'c/copy=?' 'f/format=' -- $argv
if set -q _flag_h
echo "$script - Capture the audio output going to the speakers and record it to a file."
echo "Usage: $script [arguments] [action]"
echo
echo " -h/--help - Print help and exit."
echo " -a/--action - Specify the action to perform; can be one of: toggle (default), start, go, stop, status, return."
echo " -s/--source - Specify the audio source; can be one of: output (default), mic."
echo " -c/--copy - Whether to copy the filename of the output to the clipboard when stopping recording (default: $copy)."
echo " -f/--format - Specify the output file format (default: $format)."
exit
end
set jack_capture_formats w64 wav
if set -q _flag_f
if contains "$_flag_f" 'w64' 'wav' 'flac' 'opus'
set format "$_flag_f"
else
echo -- "$script: unknown format: \"$_flag_f\"."
exit 4
end
end
if set -q _flag_s
if not contains "$_flag_s" 'output' 'mic'
echo "$script: No such source known: $_flag_s"
exit 2
else
set source "$_flag_s"
end
end
if set -q _flag_c
if not set -q _flag_c
set copy 'y'
else
set copy "$_flag_c"
end
end
if string match -qir '^=?[y1]' "$copy"
set copy 'y'
else
set copy 'n'
end
set action 'toggle'
if set -q _flag_a
set action "$_flag_a"
else
if test (count $argv) -gt '0'
set action "$argv[1]"
end
end
set recording_filename
set result_filename
function parse-filename -d "Parse a recording filename into its component parts."
string match --regex -- '^(?<basename>.+)-(?<target_format>.+?)\\.(?<recording_format>[^.]+)$' $recording_filename | tail -3
end
function capture-status -d "If audio is currently being captured, echoes the pid, elapsed time, the filename being written to, and the result filename. Otherwise, echoes nothing and returns false."
if ps -C jack_capture -o pid,etime,args --no-headers | string match --quiet --regex '^\\s*(?<pid>\\S+)\\s+(?<etime>\\S+)\\s+.+\\s+(?<recording_filename>\\S+)$'
set components (parse-filename $recording_filename)
set result_filename "$components[1].$components[2]"
echo "$pid"
echo "$etime"
echo "$recording_filename"
echo "$result_filename"
true
else
false
end
end
function echo-status -d "Echo the status in a human-readable way."
if test (count $argv) -gt 0
echo "Status: Recording."
echo "PID: $cstatus[1]"
echo "Time: $cstatus[2]"
echo "Recording: $cstatus[3]"
echo "Result: $cstatus[4]"
else
echo "Status: Stopped."
end
end
function make-filename -d "Echo the full path to the recording."
set -l format $argv[1]
set -l target_format $argv[2]
if not test -z "$target_format"
set filename "$filename-$target_format"
end
echo -- "$directory$filename.$format"
end
set action_taken
function start-recording
if test "$source" = 'mic'
set -- mic_args '--port' 'system:capture_1' '--port' 'system:capture_2'
end
# nice -n -5 ionice -c 2 -n 0
# --format "$format"
# XXX jack_capture doesn't seem to handle writing flac sometimes (it seems to happen when i try to record SuperCollider, for example). so instead we record as w64 and then convert to the requested format afterwards.
if contains $format w64 wav
set arg_format $format
else
set arg_format w64
end
jack_capture --daemon $mic_args --format $arg_format (make-filename $arg_format $format) &
set action_taken 'started'
end
function stop-recording
if test (count $argv) -lt 1
if set cstatus (capture-status)
kill "$cstatus[1]"
set recording_filename $cstatus[3]
else
echo "script: No recording is currently being made."
exit 1
end
else
kill "$argv[1]"
end
set action_taken 'ended'
end
switch "$action"
case 'status'
if set cstatus (capture-status) # FIX: allow the output format to be specified
echo-status $cstatus
else
echo-status
end
exit
case 'toggle'
if set cstatus (capture-status)
stop-recording "$cstatus[1]"
else
start-recording
end
case 'start' 'record' 'rec'
start-recording
case 'stop' 'end'
stop-recording
case 'return'
if capture-status >/dev/null
exit 0
else
exit 1
end
case '*'
echo "$script: Unknown action: $action"
exit 3
end
if test "$action_taken" = 'ended'
set components (parse-filename $recording_filename)
set result_filename "$components[1].$components[2]"
echo "filename: $filename"
echo "recording_filename: $recording_filename"
echo "copy: $copy"
echo "jack_capture_formats: $jack_capture_formats"
echo "result_filename: $result_filename"
if not contains "$format" $jack_capture_formats
convert-audio --format="$format" --delete (path change-extension $jack_capture_formats[1] $recording_filename)
end
mv (path change-extension $format $recording_filename) $result_filename
if test 'y' = "$copy"
echo -n -- $result_filename | c -
end
end