-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-split.sh
executable file
·557 lines (522 loc) · 18.9 KB
/
edit-split.sh
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/bin/bash
parse_time () {
# 0:09:24.300
output="$( echo "$1" | tr '.:[[:blank:]]' '\n' | sed -r 's/^0*([^0]+)$/\1/' | tr '\n' '\t' )"
# now CHECK for sanity
read h m s ms <<<"$output"
(test -n "$h" && test -n "$m" && test -n "$s" && test -n "$ms") || \
echo "Couldn't parse time '$1': got hour $h min $m sec $s msec $ms" 1>&2
echo "$output"
}
# parse the "helpful" terminal messages back into .tracks format
highest_track_seen () {
highest=0
while read line; do
num="$( echo "$line" | sed -r 's/Track([0-9]+).*/\1/' )"
if [[ -n "$num" ]] && [[ "$num" -gt "$highest" ]]; then
highest="$num"
fi
done
echo "$highest"
}
info_to_tracks () {
tracks="$(sed -rn '/^([0-9]+) +((start|end)s) +at +([0-9:\.]+)( \(length:.*\))?/ {s//Track\1\3=\4 # \5/;p}')"
echo "$tracks"
echo -n 'Number_of_tracks='; echo "$tracks" | highest_track_seen
}
print_time_ms () {
printf "%01d:%02d:%02d.%03d" \
$(( $1 / ( 60 * 60 * 1000 ) )) \
$(( ( $1 / ( 60 * 1000 ) ) % 60 )) \
$(( ( $1 / ( 1000 ) ) % 60 )) \
$(( $1 % 1000 ))
}
time_in_ms () {
#echo "Got time: $@" 1>&2
h="$1"
m="$2"
s="$3"
ms="$4"
echo $(( ( $h * 60 * 60 * 1000 ) + ($m * 60 * 1000) + ($s * 1000) + $ms ))
}
ms_to_s_decimal () {
input="$(cat)"
#printf "%04d\n" "$input" | sed -r 's/([0-9])([0-9]{3})$/\1.\2/'
echo "$input" | awk '{print $0 / 1000.0}'
}
s_decimal_to_ms () {
input="$(cat)"
echo "$input" | awk '{print $0 * 1000}'
}
sox_extract_wav_ms_start_duration () {
start_ms="$1"
duration_ms="$2"
if [[ -n "$TRACE_SOX" ]]; then
echo sox "$wav" -t wav - trim $( echo $start_ms | ms_to_s_decimal ) \
$( echo $duration_ms | ms_to_s_decimal ) 1>&2
fi
sox "$wav" -t wav - trim $( echo $start_ms | ms_to_s_decimal ) \
$( echo $duration_ms | ms_to_s_decimal )
}
play_ms_start_duration () {
start_ms="$1"
duration_ms="$2"
#echo "Doing: "
#"${mplayer}" ${mplayer_playback_opts} -ss $( echo $start_ms | ms_to_s_decimal ) \
# -endpos $( echo $duration_ms | ms_to_s_decimal ) \
# "$wav" <"$tty"
#sox "$wav" - trim "$( echo "$start_ms" | ms_to_s_decimal )" "$( echo "$duration_ms" | ms_to_s_decimal )" \
# "${mplayer}" ${mplayer_playback_opts} -
tmpfile="$(mktemp --suffix ".wav" )"
sox_extract_wav_ms_start_duration "$start_ms" "$duration_ms" >"$tmpfile"
#echo "tmpfile is $tmpfile" 1>&2
${mplayer} ${mplayer_playback_opts} \
"$tmpfile" <"$tty"
# -ss $( echo $start_ms | ms_to_s_decimal ) \
# -endpos $( echo $duration_ms | ms_to_s_decimal ) \
rm -f "$tmpfile" 1>&2
}
save_ms_start_duration () {
destfile="$1"
start_ms="$2"
duration_ms="$3"
sox_extract_wav_ms_start_duration "$start_ms" "$duration_ms" > "$destfile"
}
save_state () {
# comment any uncommented track boundary/count lines
# (we leave Fileend alone)
tracksfile="${1:-"$wav".tracks}"
sed -ri '/^[^#]*(Track([0-9]+)(start|end)|Number_of_tracks)=/ s//# &/' "$tracksfile"
# append our actual state
(for n in `seq 1 $count`; do
printf "Track%02dstart=${starts[$n]}\n" "$n"
printf "Track%02dend=${ends[$n]}\n" "$n"
done; printf "Number_of_tracks=%d\n" "$count") >> "$tracksfile"
}
print_state () {
for n in `seq 1 $count`; do
echo "$n starts at ${starts[$n]}" 1>&2
echo -n "$n ends at ${ends[$n]} " 1>&2
local length=$(( $(time_in_ms $(parse_time ${ends[$n]}) ) - $(time_in_ms $(parse_time ${starts[$n]}) ) ))
echo "(length: $(print_time_ms "$length"))" 1>&2
done
}
parse_into () {
local input="$1"
# echo "parsing: $input (into vars: $2 $3 $4 $5)" 1>&2
regex="([a-z])([0-9]+)?(([-\+])([-\+]?[0-9]+(\.[0-9]+)?)?)?"
# FIXME: why does 'declare' not work here?
eval "${2}='$( echo "$input" | sed -rn "/$regex/ {s//\1/;p}" )'"
eval "${3}='$( echo "$input" | sed -rn "/$regex/ {s//\2/;p}" )'"
eval "${4}='$( echo "$input" | sed -rn "/$regex/ {s//\4/;p}" )'"
eval "${5}='$( echo "$input" | sed -rn "/$regex/ {s//\5/;p}" )'"
}
parse () {
parse_into "$1" ins tgt dir arg
# remember which track we're acting on
if [[ -n "$tgt" ]]; then last_tgt="$tgt"; fi
}
drop_prog_instr () {
local idx="$1"
for n in `seq $(( $idx + 1 )) $(( $proglen - 1 ))`; do
program_cmds[$(( $n - 1 ))]="${program_cmds[$n]}"
program_prompts[$(( $n - 1 ))]="${program_prompts[$n]}"
done
proglen=$(( $proglen - 1 ))
if [[ $cmdidx -ge $idx ]]; then
cmdidx=$(( $cmdidx - 1 ))
echo "cmdidx fixed up to $cmdidx" 1>&2
fi
}
dup_prog_instr () {
local idx="$1"
local new_tgt="$2"
for n in `seq $proglen -1 $(( $idx + 1 ))`; do
program_cmds[$(( $n + 1 ))]="${program_cmds[$n]}"
program_prompts[$(( $n + 1 ))]="${program_prompts[$n]}"
done
program_cmds[$(( $idx + 1 ))]="$(echo "${program_cmds[$idx]}" | sed -r "s/([a-z])($idx)(\$|[-\+])/\1${new_tgt}\2/" )"
program_prompts[$(( $idx + 1 ))]="${program_prompts[$idx]}"
proglen=$(( $proglen + 1 ))
if [[ $cmdidx -gt $idx ]]; then
cmdidx=$(( $cmdidx + 1 ))
echo "cmdidx fixed up to $cmdidx" 1>&2
fi
}
drop_at () {
local to_drop=${1:-${tgt:-${last_tgt}}}
# To drop a track, it means:
# - shuffle the starts and ends of later tracks back 1
# - delete any pending commands on the dropped track
# - in-place-rewrite any pending commands so that track numbers in them are rewritten
# The command stuff may be easier if the commands
# are kept in an array
# and we have a "program counter" that is the next-to-execute array index
for n in `seq $(( $to_drop + 1 )) $count`; do
echo "We would shuffle back track $n by one"
starts[$(($n - 1))]=${starts[$n]}
ends[$(($n - 1))]=${ends[$n]}
done
starts[$count]=""
ends[$count]=""
# Now rewrite the program so that
# - any command whose target is n, for n > tgt, is renumbered
# - any command whose target is tgt is deleted,
# and if cmdidx is >=
local pos=0
while ! [[ $pos -eq $proglen ]]; do
local cmd="${program_cmds[$pos]}"
parse_into "$cmd" this_ins this_tgt this_dir this_arg
if [[ $this_tgt -eq $to_drop ]]; then
# we need to drop this instr
echo "Dropping instruction: $cmd" 1>&2
drop_prog_instr $pos
# ... and go back to the start of the program, so our pos is not out of whack
pos=0
continue
elif [[ $this_tgt -gt $to_drop ]]; then
# all track#s greater than tgt are now one less
echo "Rewriting instruction: $cmd" 1>&2
program_cmds[$pos]="${this_ins}$(($this_tgt - 1))${this_dir}${this_arg}"
fi
pos=$(( $pos + 1 ))
done
count=$(( $count - 1 ))
echo "We now have $count tracks"
}
dup_at () {
local to_dup=${1:-${tgt:-${last_tgt}}}
echo "DEBUG: duping $to_dup" 1>&2
# To duplicate a track, it means:
# - shuffle the starts and ends of later tracks forward 1
# - duplicate any pending commands on the dropped track,...
# - ... rewriting the
# - ... an track numbers in them are rewritten
for n in `seq $count -1 $(( $to_dup ))`; do
echo "We would shuffle forward track $n by one"
starts[$(($n + 1))]=${starts[$n]}
ends[$(($n + 1))]=${ends[$n]}
done
count=$(( $count + 1 ))
# Now rewrite the program so that
# - any command whose target is n, for n = tgt, is duplicated
# - any command whose target is n, for n > tgt, is renumbered
local pos=0
while ! [[ $pos -eq $proglen ]]; do
local cmd="${program_cmds[$pos]}"
parse_into "$cmd" this_ins this_tgt this_dir this_arg
if [[ $this_tgt -gt $to_dup ]]; then
# all track#s greater than tgt are now one more
echo "Rewriting instruction: $cmd" 1>&2
program_cmds[$pos]="${this_ins}$(($this_tgt - 1))${this_dir}${this_arg}"
fi
pos=$(( $pos + 1 ))
done
pos=0
while ! [[ $pos -eq $proglen ]]; do
if [[ $this_tgt -eq $to_dup ]]; then
# we need to duplicate this instr
echo "Duplicating-with-renum instruction: $cmd" 1>&2
dup_prog_instr $pos $(( $to_dup + 1 ))
# ... and bump up our pos, to avoid infinite loop duplicating same instr
pos=$(( $pos + 1 ))
fi
pos=$(( $pos + 1 ))
done
echo "We now have $count tracks"
}
eval_it () {
case "$ins" in
('y') # we should not get here!
echo "This should not happen!" 1>&2
false
;;
('') # we didn't understand; go round again
echo "Did not understand (empty command)"
false
;;
('p') # play the track
echo "Doing play ('q' to stop)" 1>&2
case "$dir" in
('-')
# play from end - Ns, for Ns
play_ms_start_duration $(( $(time_in_ms $(parse_time ${ends[$tgt]}) ) - $(s_decimal_to_ms <<<${arg:-5}) )) $(s_decimal_to_ms<<<${arg:-5})
;;
('+')
# play from start, for Ns
play_ms_start_duration $(time_in_ms $(parse_time ${starts[$tgt]}) ) $(s_decimal_to_ms<<<${arg:-5})
;;
(*)
echo "Did not understand play dir: $dir"
false
;;
esac
;;
('c') # chop/cut
echo "Doing chop" 1>&2
dup_at "$tgt"
echo "DEBUG: after dup" 1>&2
print_state
echo "DEBUG: now fixing up start/end" 1>&2
case "$dir" in
('-')
new_boundary="$(print_time_ms $(( $(time_in_ms $(parse_time ${ends[$tgt]}) ) - $(s_decimal_to_ms<<<${arg:-5}) )) )"
;;
('+')
new_boundary="$(print_time_ms $(( $(time_in_ms $(parse_time ${starts[$tgt]}) ) + $(s_decimal_to_ms<<<${arg:-5}) )) )"
;;
(*)
echo "Did not understand chop dir: $dir"
false
;;
esac
ends[$tgt]="$new_boundary"
starts[$(( $tgt + 1 ))]="$new_boundary"
print_state
;;
('d') # drop
echo "Doing drop" 1>&2
drop_at "$tgt"
print_state
;;
('l') # list
print_state
;;
('m') # merge
# we impl this like drop, but remember+restore the dropped boundary
case "$dir" in
('-')
echo "Doing merge backwards of $tgt" 1>&2
remembered_start="${starts[$(($tgt - 1))]}"
drop_at $(( $tgt - 1 ))
tgt=$(( $tgt - 1 ))
starts[$tgt]="${remembered_start}"
print_state
;;
('+')
echo "Doing merge forwards of $tgt" 1>&2
remembered_end="${ends[$(($tgt + 1))]}"
drop_at $(( $tgt + 1 ))
ends[$tgt]="${remembered_end}"
print_state
;;
(*)
echo "Did not understand merge dir: $dir"
false
;;
esac
;;
('n') # nudge
echo "Doing nudge" 1>&2
# nN-5 means move the end of track N back 5s
# i.e. also move the start of track N+1 back 5s ONLY IF they are abutting
# nN+5 means move the start of track N forward 5s
# i.e. also move the end of track N-1 forward 5s ONLY IF they are abutting
case "$dir" in
('-')
new_boundary="$(print_time_ms $(( $(time_in_ms $(parse_time ${ends[$tgt]}) ) - $(s_decimal_to_ms<<<${arg:-5}) )) )"
if [[ $tgt -lt $count ]] && \
[[ $(time_in_ms $(parse_time "${starts[$(($tgt +1))]}" "starts $tgt + 1 neg" ) ) -eq \
$(time_in_ms $(parse_time "${ends[$tgt]}" "ends $tgt neg" ) ) ]]; then
starts[$(($tgt +1))]="$new_boundary"
else
echo "Not nudging the start of $(( $tgt + 1 )); discontinuity detected"
fi
ends[$tgt]="$new_boundary"
print_state
;;
('+')
new_boundary="$(print_time_ms $(( $(time_in_ms $(parse_time ${starts[$tgt]}) "starts $tgt pos") + $(s_decimal_to_ms<<<${arg:-5}) )) )"
if [[ $tgt -gt 1 ]] && \
[[ $(time_in_ms $(parse_time "${ends[$(($tgt -1))]}" ) ) -eq \
$(time_in_ms $(parse_time "${starts[$tgt]}" ) ) ]]; then
ends[$(($tgt -1))]="$new_boundary"
else
echo "Not nudging the end of $(( $tgt - 1 )); discontinuity detected"
fi
starts[$tgt]="$new_boundary"
print_state
;;
(*)
echo "Did not understand nudge dir: $dir"
false
;;
esac
;;
('s')
echo "Saving to .tracks file" 1>&2
save_state
;;
('v') # visualize it
echo "Doing viz" 1>&2
;;
('w') # write out files
destdir="$( mktemp -d ./split-XXXXXX )" || (echo "Couldn't create output dir"; false) || exit 1
echo "Doing write to $destdir" 1>&2
for n in `seq 1 $count`; do
save_ms_start_duration "${destdir}/track${n}.wav" \
$(time_in_ms $(parse_time ${starts[$n]}) ) \
$(( $(time_in_ms $(parse_time ${ends[$n]}) ) - $(time_in_ms $(parse_time ${starts[$n]}) ) ))
done
# save a copy of the tracks file
cp "$wav".tracks "$destdir"/disc.tracks && save_state "$destdir"/disc.tracks
;;
('x') # exit -- not 'quit' to avoid risk that a 'q' intended for mplayer will hit us
echo "Doing exit" 1>&2
exit 0
;;
(*)
echo "Did not understand command [$cmd]: $resp"
false
;;
esac
}
# sometimes sox generates a WAV file that confuses mplayer's default (lavcpref)
# demuxer... amazing really
mplayer=${MPLAYER:-mplayer -really-quiet -demuxer lavf}
mplayer_playback_opts=${MPLAYER_OPTS:-}
tty=`tty`
# allow us to be sourced, or run as a command...
case "$(basename -- "$0")" in
(edit-split*)
# parse the options etc
TEMP=`getopt -q -o n -n $(basename $0) -- "$@"`
#echo "TEMP is $TEMP" 1>&2
eval set -- "$TEMP"
while true; do case "$1" in
(-n)
#echo "Setting no-bump" 1>&2
NO_BUMP=1
;;
(--)
shift
break
;;
esac; shift || break
done
#echo "\$0 is $0" 1>&2
#echo "\$1 is $1" 1>&2
wav="$1"
test -e "$wav".tracks || \
(echo "Did not found tracks file: ${wav}.tracks" 1>&2; false) || \
exit 1
declare -a starts
declare -a ends
while read line; do
case "$line" in
('#'*) ;;
('[Tracks]') ;;
('') ;;
(Track*)
num="$( echo "$line" | sed -rn '/Track([0-9]+)(start|end)=.*/ {s//\1/;p}' | sed 's/^0*//' )"
which="$( echo "$line" | sed -rn '/Track([0-9]+)(start|end)=.*/ {s//\2/;p}' )"
time="$( echo "$line" | sed -rn '/Track([0-9]+)(start|end)=(.*)/ {s//\3/;p}' )"
case "$which" in
(start)
echo "$(($num)) starts at $time" 1>&2
starts[$(($num))]="$time"
;;
(end)
echo "$(($num)) ends at $time" 1>&2
ends[$(($num))]="$time"
;;
(*) echo "Did not understand: $line" 1>&2; exit 1 ;;
esac
;;
('Number_of_tracks='*)
count="$( echo "$line" | sed 's/^Number_of_tracks=//' )"
;;
('Fileend='*)
fileend="$( echo "$line" | sed 's/^Fileend=//' )"
;;
esac
done < "$wav".tracks
echo "Count is $count"
if [[ -z "$NO_BUMP" ]]; then
if [[ $count -gt 1 ]]; then
# hack the ends to go up to the next track's beginning
for n in `seq 1 $(( $count - 1 ))`; do
echo "Bumping the end of $n (${ends[$n]}) up to the start of $(($n + 1)) (${starts[$(($n + 1))]})" 1>&2
ends[$n]="${starts[$(($n + 1))]}"
done
fi
echo "Bumping the end of $(($count)) (${ends[$(($count - 1))]}) up to the file end ($fileend)" 1>&2
ends[$(($count))]="$fileend"
fi
print_state
# build the program
for n in `seq 1 $count`; do
if [[ "$program" == "" ]]; then
sep=''
else
sep=$'\n'
fi
# Prompts shouldn't mention tracks by name, as w don't try to rewrite them
# when track numbers change. Instead we shell-expand them, so just say '$tgt'
program="${program}${sep}p${n}+10 Playing "'$tgt'" from start, 10s; OK?"
sep=$'\n'
program="${program}${sep}p${n}-10 Playing "'$tgt'" to end, 10s; OK?"
done
program="${program}${sep}w Writing the output .wav files; OK?"
#echo "Program is:" 1>&2
#echo "$program" 1>&2
# turn the program into arrays
declare -a program_cmds
declare -a program_prompts
proglen=0
while read cmd prompt; do
program_cmds[$proglen]="$cmd"
program_prompts[$proglen]="$prompt"
proglen=$(( $proglen + 1 ))
done<<<"$program"
# "last target" is really "the current track", so initially 1
last_tgt=1
cmdidx=0
while true; do
# we might go multiple iterations without advancing
# the program position
while true; do
cmd="${program_cmds[$cmdidx]}"
program_cmd="$cmd"
prompt="${program_prompts[$cmdidx]}"
saved_cmdidx="$cmdidx"
parse "$program_cmd" # so that we have ${tgt} defined
echo -n "[$cmd] "
eval "echo -n \"${prompt}\""
echo -n " "
read resp <"$tty"
# p1+5
# p1-5
# l
# l1
# m2+
# m2-
# s1+100
# s1-20
parse "$resp"
case "$ins" in
('y'|'') # "y" means "do what the program says"
echo "Taking from program: $program_cmd" 1>&2
parse "$program_cmd" # now we've taken "cmd" from the program
eval_it && break
;;
('k') # "k" means "skip this command straight away"
break
;;
(*) # the user entered a command, so do it
# doing the eval_it will parse *again*
eval_it # don't break -- we don't advance the program yet
# Perhaps this deleted the current instruction; if so
# we go around the outer loop again, to increment the ctr
if [[ $cmdidx -lt $saved_cmdidx ]]; then
break
fi
;;
esac
done
cmdidx=$(( $cmdidx + 1 ))
done
# if we're being sourced...
;;
(*) ;;
esac