-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecording-tool.sh
executable file
·138 lines (127 loc) · 3.37 KB
/
recording-tool.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
#!/bin/sh
# a shell script to make our recordings easier
# (c) Hoang Minh Thang
# Licence: GPL-v3
# ---------------
# Remove silence
# rec -r 44100 -p | sox -p "audio_name-$(date '+%Y-%m-%d').ogg" silence -l 1 00:00:00.5 -45d -1 00:00:00.5 -45d
# to get key code, run this in terminal
# read -r -n 1 -s char
# How it work?
# it read the rhyme-list file and get index
# var i
# Usage
# * Run it
# * i start from 1
# * the rhyme to record is printed out (Something like "Please read this loud: ang")
# * press Up-Arrow to start/restart recording
# * in case of retry, a back-up should be created in ./back-ups
# * press Rigt-Arrow to go to next rhyme (increase i by 1) C
# * press Left-Arrow to go to previous rhyme (decrease i by 1) D
# * press Enter to exit. Var i (aka current progress) will be saved to a file name .rec-progress
# * if you want to restart all, remove .rec-progress
#if [ `which sox` == 'sox not found' ] ###fixme: not work in different shell
#then
#echo 'Please install sox before recording'
#echo 'To install sox in ubuntu:'
#echo 'apt-get install sox'
#exit
#fi
if [ ! -d db/bkup ]
then mkdir db/bkup -p
fi
if [ -f .rec-progress ]
then
read i < .rec-progress
else
i=1
fi
max=`cat rhyme-list.txt|wc -l`
function get_rhyme () {
if [ ${1} -eq 0 ]
then
echo 'BOF'
elif [ ${1} -eq $max ]
then
echo 'EOF'
else
echo `sed -n "${1}p" rhyme-list.txt`
fi
}
function rhyme_to_read () {
if [ $i -le $max ]
then
{
left=`get_rhyme $((i-1))`
center=`get_rhyme $i`
right=`get_rhyme $((i+1))`
echo -e "Please read: \033[30;47m $left \033[33;44;1m $center \033[37;42m $right \033[0m"
}
else
echo "Warning: end of rhyme list reached!!!"
fi
}
# record and "filter" silence which is lower than 45dB
# each recording timeouts after 2 seconds
function clean_record () {
rhyme=`get_rhyme $i`
if [ -f db/${rhyme}.wav ]
then
timestamp=`date +%F_%T`
echo "file exist, making a backup at db/bkup/${rhyme}_${timestamp}.wav"
mv db/${rhyme}.wav db/bkup/${rhyme}-${timestamp}.wav
fi
# 45 dB maybe too strict. You can try a lower silence filter, or
#timeout -s INT 2 rec -p | sox -p db/${rhyme}.wav silence -l 1 00:00:00.5 -45d -1 00:00:00.5 -45d
timeout -s INT 1 rec -p | sox -p db/${rhyme}.wav
}
function do_record () {
echo "recording `get_rhyme $i`..."
clean_record
}
#start/resume before loop
rhyme_to_read
#a loop to interact with user's keyboard input
while IFS= read -r -n 1 -s char
do
if [ "$char" == $'\x1b' ] # \x1b is the start of an escape sequence
then
# Get the rest of the escape sequence (3 characters total)
while IFS= read -r -n 2 -s rest
do
char+="$rest"
break
done
fi
if [ "$char" == $'\x1b[A' ]
then
# Up -> (Re)start recording
do_record
elif [ "$char" == $'\x1b[D' ]
then
# Left
i=$((i-1))
rhyme_to_read
do_record
elif [ "$char" == $'\x1b[C' ]
then
# Right
i=$((i+1))
rhyme_to_read
do_record
elif [ -z "$char" ]
then
# Newline
echo "If you really want to quit, I'd let you go"
echo "Current progress is ${i}"
if [ ${i} -gt 1 ]
then
echo "Saving progress to .rec-progress"
echo "..."
echo $i > .rec-progress
else
echo "Nothing to save!"
fi
exit
fi
done