Oops, remove some duplicated code.
[Rockbox.git] / tools / voicecommon.sh
blob458a7b2ec6f515e66e7f0857873ac3c8c59252fa
1 #!/bin/sh
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
10 # Copyright (c) 2006 Jonas Häggqvist
12 # All files in this archive are subject to the GNU General Public License.
13 # See the file COPYING in the source tree root for full license agreement.
15 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 # KIND, either express or implied.
18 # A selection of functions common to creating voicefiles for Rockbox.
20 # You may wish to change some of the settings below.
22 #####################
23 # Program locations #
24 #####################
26 # Leave any you're not using untouched, enter full path if the program is
27 # not found
29 # the festival main executable
30 FESTIVAL_BIN=festival
31 # the festival_client binary
32 FESTIVAL_CLIENT=festival_client
34 # The flite executable
35 FLITE_BIN=flite
37 # The eSpeak executable
38 ESPEAK_BIN=espeak
40 # The lame executable
41 LAME_BIN=lame
43 # The speexenc executable
44 SPEEX_BIN=speexenc
46 # The oggenc executable
47 VORBIS_BIN=oggenc
49 # Tools directory
50 TOOLSDIR=`dirname $0`
52 # The wavtrim executable
53 WAVTRIM=$TOOLSDIR/wavtrim
55 # The SAPI5 script directory
56 if [ -f "`which cygpath`" ]; then
57 SAPI5DIR=`cygpath $TOOLSDIR -a -w`
60 #####################
61 # Festival settings #
62 #####################
64 # If you're not using festival, leave untouched
66 # whether to start the Festival server locally (Y/N)
67 FESTIVAL_START=Y
68 # the host of the Festival server
69 # this is set to localhost automatically when FESTIVAL_START is Y
70 FESTIVAL_HOST=localhost
71 # the port of the Festival server
72 FESTIVAL_PORT=1314
73 # where to log the Festival client output
74 FESTIVAL_LOG=/dev/null
75 # other options to the festival server
76 FESTIVAL_OPTS=""
78 ##################
79 # Flite settings #
80 ##################
82 # If you're not using flite, leave untouched
83 FLITE_OPTS=""
85 ###################
86 # eSpeak settings #
87 ###################
89 # If you're not using eSpeak, leave untouched
90 ESPEAK_OPTS=""
92 ####################
93 # Wavtrim settings #
94 ####################
96 # The maximum sample value that will be treated as silence by the wavtrim tool.
97 # The value is expressed as an absolute 16 bit integer sample value (0 dB equals
98 # 32767).
100 # 500 is a good guess - at least for Festival
102 NOISEFLOOR='500'
104 #####################
105 # Encoding settings #
106 #####################
107 # where to log the encoder output
108 ENC_LOG=/dev/null
110 # Suggested: --vbr-new -t --nores -S
111 # VBR, independent frames, silent mode
112 LAME_OPTS="--vbr-new -t --nores -S"
114 # Suggested:
115 # XXX: suggest a default
116 SPEEX_OPTS=""
118 # Suggested: -q0 --downmix
119 # Low quality, mono
120 VORBIS_OPTS="-q0 --downmix"
122 ###################
123 # End of settings #
124 ###################
126 # Check if executables exist and perform any necessary initialisation
127 init_tts() {
128 case $TTS_ENGINE in
129 festival)
130 # Check for festival_client
131 if [ ! -f "`which $FESTIVAL_CLIENT`" ]; then
132 echo "Error: $FESTIVAL_CLIENT not found"
133 exit 4
136 # Check for, and start festival server if specified
137 if [ X$FESTIVAL_START = XY ]; then
138 if [ ! -f "`which $FESTIVAL_BIN`" ]; then
139 echo "Error: $FESTIVAL_BIN not found"
140 exit 3
142 FESTIVAL_HOST='localhost'
143 $FESTIVAL_BIN $FESTIVAL_OPTS --server 2>&1 > /dev/null &
144 FESTIVAL_SERVER_PID=$!
145 sleep 3
146 if [ `ps | grep -c "^\ *$FESTIVAL_SERVER_PID"` -ne 1 ]; then
147 echo "Error: Festival not started"
148 exit 9
151 # Test connection to festival server
152 output=`echo -E "Rockbox" | $FESTIVAL_CLIENT --server \
153 $FESTIVAL_HOST --otype riff --ttw --output \
154 /dev/null 2>&1`
155 if [ $? -ne 0 ]; then
156 echo "Error: Couldn't connect to festival server at" \
157 "$FESTIVAL_HOST ($output)"
158 exit 8
161 flite)
162 # Check for flite
163 if [ ! -f "`which $FLITE_BIN`" ]; then
164 echo "Error: $FLITE_BIN not found"
165 exit 5
168 espeak)
169 # Check for espeak
170 if [ ! -f "`which $ESPEAK_BIN`" ]; then
171 echo "Error: $ESPEAK_BIN not found"
172 exit 5
175 sapi5)
176 # Check for SAPI5
177 cscript /B $SAPI5DIR/sapi5_init_tts.vbs
178 if [ $? -ne 0 ]; then
179 echo "Error: SAPI 5 not available"
180 exit 5
184 echo "Error: no valid TTS engine selected: $TTS_ENGINE"
185 exit 2
187 esac
188 if [ ! -x $WAVTRIM ]; then
189 echo "Error: $WAVTRIM is not available"
190 exit 11
194 # Perform any necessary shutdown for TTS engine
195 stop_tts() {
196 case $TTS_ENGINE in
197 festival)
198 if [ X$FESTIVAL_START = XY ]; then
199 # XXX: This is probably possible to do using festival_client
200 kill $FESTIVAL_SERVER_PID > /dev/null 2>&1
203 esac
206 # Check if executables exist and perform any necessary initialisation
207 init_encoder() {
208 case $ENCODER in
209 lame)
210 # Check for lame binary
211 if [ ! -f "`which $LAME_BIN`" ]; then
212 echo "Error: $LAME_BIN not found"
213 exit 6
216 speex)
217 # Check for speexenc binary
218 if [ ! -f "`which $SPEEX_BIN`" ]; then
219 echo "Error: $SPEEX_BIN not found"
220 exit 7
223 vorbis)
224 # Check for vorbis encoder binary
225 if [ ! -f "`which $VORBIS_BIN`" ]; then
226 echo "Error: $VORBIS_BIN not found"
227 exit 10
231 echo "Error: no valid encoder selected: $ENCODER"
232 exit 1
234 esac
238 # Encode file $1 with ENCODER and save the result in $2, delete $1 if specified
239 encode() {
240 INPUT=$1
241 OUTPUT=$2
243 if [ ! -f "$INPUT" ]; then
244 echo "Warning: missing input file: \"$INPUT\""
245 else
246 echo "Action: Encode $OUTPUT with $ENCODER"
247 case $ENCODER in
248 lame)
249 $LAME_BIN $LAME_OPTS "$WAV_FILE" "$OUTPUT" >>$ENC_LOG 2>&1
251 speex)
252 $SPEEX_BIN $SPEEX_OPTS "$WAV_FILE" "$OUTPUT" >>$ENC_LOG 2>&1
254 vorbis)
255 $VORBIS_BIN $VORBIS_OPTS "$WAV_FILE" -o "$OUTPUT" >>$ENC_LOG 2>&1
256 esac
257 if [ ! -f "$OUTPUT" ]; then
258 echo "Warning: missing output file \"$OUTPUT\""
263 # Generate file $2 containing $1 spoken by TTS_ENGINE, trim silence
264 voice() {
265 TO_SPEAK=$1
266 WAV_FILE=$2
267 if [ ! -f "$WAV_FILE" ] || [ X$OVERWRITE_WAV = XY ]; then
268 if [ "${TO_SPEAK}" = "" ]; then
269 touch "$WAV_FILE"
270 else
271 case $TTS_ENGINE in
272 festival)
273 echo "Action: Generate $WAV_FILE with festival"
274 echo -E "$TO_SPEAK" | $FESTIVAL_CLIENT \
275 --server $FESTIVAL_HOST \
276 --otype riff --ttw --output "$WAV_FILE" 2>"$WAV_FILE"
278 espeak)
279 echo "Action: Generate $WAV_FILE with eSpeak"
280 echo $ESPEAK_BIN $ESPEAK_OPTS -w "$WAV_FILE"
281 echo -E "$TO_SPEAK" | $ESPEAK_BIN $ESPEAK_OPTS -w "$WAV_FILE"
283 flite)
284 echo "Action: Generate $WAV_FILE with flite"
285 echo -E "$TO_SPEAK" | $FLITE_BIN $FLITE_OPTS -o "$WAV_FILE"
287 sapi5)
288 cscript /B "$SAPI5DIR\sapi5_voice.vbs" ""$TO_SPEAK"" "$WAV_FILE"
290 esac
293 trim "$WAV_FILE"
296 # Trim wavefile $1
297 trim() {
298 WAVEFILE="$1"
299 echo "Action: Trim $WAV_FILE"
300 $WAVTRIM "$WAVEFILE" $NOISEFLOOR