-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-practise-chunks
executable file
·77 lines (65 loc) · 2.16 KB
/
make-practise-chunks
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
#!/bin/sh
USAGE="
usage: $(basename $0) name
name.mp3 is the source file in the current directory
name.txt contains a list of timestamps where the audio should be split
the first line in the audio is the start of the first chunk
timestamps are in seconds, like the labels files exported from audacity
practise files include the previous chunk, so include an intro chunk
the last line of the timestamp file is the end of the last chunk
start a new voice with a numeric-only label
"
[ ! $1 ] && echo "$USAGE" && exit 1
# floating point arithmetic in posix
calc() { echo "$*" | bc -l ; }
#
# cut a single chunk at the given tempo from the input file
#
# current timestamp
# next timestamp
# tempo
# output file
#
# note: -nostdin must be specified or ffmpeg consumes bytes from the data source (!!)
#
cut_chunk() {
ffmpeg -nostdin -loglevel error \
-ss $(calc $1-$padding) -to $(calc $2+$padding) \
-i $name.$format -f $format -ac 1 -ar 48000 -q 4 \
-af afade=d=$padding,afade=t=out:st=$(calc $2-$1+$padding):d=$padding,atempo=$3,adelay=${silence}s:all=true \
"$4"
}
# set up the environment
name=$1; silence=5; padding=3; format=mp3; count=1
outputdir=00.$name
[ -d $outputdir ] || mkdir "$outputdir"
# temporary files
tmp=$(mktemp -d)
trap "rm -r $tmp" EXIT
trap exit INT HUP TERM
echo "
file $tmp/100.$format
file $tmp/050.$format
file $tmp/050.$format
file $tmp/100.$format
" > $tmp/list
# process timestamps
timestamps=3; exec 3< $name.txt
read current x label <& $timestamps
while read next x nextlabel; do
if expr match "$label" "^[0-9]\+$" > /dev/null; then
voice=$label
count=1
elif expr match "$label" "^[^X]" > /dev/null; then
# cut practise chunks at different tempos
cut_chunk $current $next 0.50 "$tmp/050.$format"
cut_chunk $current $next 1.00 "$tmp/100.$format"
# concatenate the practise chunks
output="$outputdir/00.$voice$(printf %02d $count).$label.$format"
ffmpeg -nostdin -loglevel error -f concat -safe 0 -i "$tmp/list" -codec copy "$output"
count=$((count + 1))
echo "chunked $name: $label"
rm "$tmp"/???.$format
fi
prev=$current; current=$next; label=$nextlabel;
done <& $timestamps