move iriver checksums to its own file to remove unneeded inclusion and kill warnings...
[Rockbox.git] / tools / voicecommon.sh
bloba21bf49efa55f6664110db50d7b6d50678e47c4a
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=speak
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 # The wavtrim executable
50 WAVTRIM=`dirname $0`/wavtrim
52 #####################
53 # Festival settings #
54 #####################
56 # If you're not using festival, leave untouched
58 # whether to start the Festival server locally (Y/N)
59 FESTIVAL_START=Y
60 # the host of the Festival server
61 # this is set to localhost automatically when FESTIVAL_START is Y
62 FESTIVAL_HOST=localhost
63 # the port of the Festival server
64 FESTIVAL_PORT=1314
65 # where to log the Festival client output
66 FESTIVAL_LOG=/dev/null
67 # other options to the festival client
68 FESTIVAL_OPTS=""
70 ##################
71 # Flite settings #
72 ##################
74 # If you're not using flite, leave untouched
75 FLITE_OPTS=""
77 ###################
78 # eSpeak settings #
79 ###################
81 # If you're not using eSpeak, leave untouched
82 ESPEAK_OPTS=""
84 ####################
85 # Wavtrim settings #
86 ####################
88 # The maximum sample value that will be treated as silence by the wavtrim tool.
89 # The value is expressed as an absolute 16 bit integer sample value (0 dB equals
90 # 32767).
92 # 500 is a good guess - at least for Festival
94 NOISEFLOOR='500'
96 #####################
97 # Encoding settings #
98 #####################
99 # where to log the encoder output
100 ENC_LOG=/dev/null
102 # Suggested: --vbr-new -t --nores -S
103 # VBR, independent frames, silent mode
104 LAME_OPTS="--vbr-new -t --nores -S"
106 # Suggested:
107 # XXX: suggest a default
108 SPEEX_OPTS=""
110 # Suggested: -q0 --downmix
111 # Low quality, mono
112 VORBIS_OPTS="-q0 --downmix"
114 ###################
115 # End of settings #
116 ###################
118 # Check if executables exist and perform any necessary initialisation
119 init_tts() {
120 case $TTS_ENGINE in
121 festival)
122 # Check for festival_client
123 if [ ! `which $FESTIVAL_CLIENT` ]; then
124 echo "Error: $FESTIVAL_CLIENT not found"
125 exit 4
128 # Check for, and start festival server if specified
129 if [ X$FESTIVAL_START = XY ]; then
130 if [ ! `which $FESTIVAL_BIN` ]; then
131 echo "Error: $FESTIVAL_BIN not found"
132 exit 3
134 FESTIVAL_HOST='localhost'
135 $FESTIVAL_BIN --server 2>&1 > /dev/null &
136 FESTIVAL_SERVER_PID=$!
137 sleep 3
138 if [ `ps | grep -c "^\ *$FESTIVAL_SERVER_PID"` -ne 1 ]; then
139 echo "Error: Festival not started"
140 exit 9
143 # Test connection to festival server
144 output=`echo -E "Rockbox" | $FESTIVAL_CLIENT --server \
145 $FESTIVAL_HOST --otype riff --ttw --output \
146 /dev/null 2>&1`
147 if [ $? -ne 0 ]; then
148 echo "Error: Couldn't connect to festival server at" \
149 "$FESTIVAL_HOST ($output)"
150 exit 8
153 flite)
154 # Check for flite
155 if [ ! `which $FLITE_BIN` ]; then
156 echo "Error: $FLITE_BIN not found"
157 exit 5
160 espeak)
161 # Check for flite
162 if [ ! `which $ESPEAK_BIN` ]; then
163 echo "Error: $ESPEAK_BIN not found"
164 exit 5
168 echo "Error: no valid TTS engine selected: $TTS_ENGINE"
169 exit 2
171 esac
172 if [ ! -x $WAVTRIM ]; then
173 echo "Error: $WAVTRIM is not available"
174 exit 11
178 # Perform any necessary shutdown for TTS engine
179 stop_tts() {
180 case $TTS_ENGINE in
181 festival)
182 if [ X$FESTIVAL_START = XY ]; then
183 # XXX: This is probably possible to do using festival_client
184 kill $FESTIVAL_SERVER_PID > /dev/null 2>&1
187 esac
190 # Check if executables exist and perform any necessary initialisation
191 init_encoder() {
192 case $ENCODER in
193 lame)
194 # Check for lame binary
195 if [ ! `which $LAME_BIN` ]; then
196 echo "Error: $LAME_BIN not found"
197 exit 6
200 speex)
201 # Check for speexenc binary
202 if [ ! `which $SPEEX_BIN` ]; then
203 echo "Error: $SPEEX_BIN not found"
204 exit 7
207 vorbis)
208 # Check for vorbis encoder binary
209 if [ ! `which $VORBIS_BIN` ]; then
210 echo "Error: $VORBIS_BIN not found"
211 exit 10
215 echo "Error: no valid encoder selected: $ENCODER"
216 exit 1
218 esac
222 # Encode file $1 with ENCODER and save the result in $2, delete $1 if specified
223 encode() {
224 INPUT=$1
225 OUTPUT=$2
227 if [ ! -f "$INPUT" ]; then
228 echo "Warning: missing input file: \"$INPUT\""
229 else
230 echo "Action: Encode $OUTPUT with $ENCODER"
231 case $ENCODER in
232 lame)
233 $LAME_BIN $LAME_OPTS "$WAV_FILE" "$OUTPUT" >>$ENC_LOG 2>&1
235 speex)
236 $SPEEX_BIN $SPEEX_OPTS "$WAV_FILE" "$OUTPUT" >>$ENC_LOG 2>&1
238 vorbis)
239 $VORBIS_BIN $VORBIS_OPTS "$WAV_FILE" -o "$OUTPUT" >>$ENC_LOG 2>&1
240 esac
241 if [ ! -f "$OUTPUT" ]; then
242 echo "Warning: missing output file \"$OUTPUT\""
247 # Generate file $2 containing $1 spoken by TTS_ENGINE, trim silence
248 voice() {
249 TO_SPEAK=$1
250 WAV_FILE=$2
251 if [ ! -f "$WAV_FILE" ] || [ X$OVERWRITE_WAV = XY ]; then
252 if [ "${TO_SPEAK}" = "" ]; then
253 touch "$WAV_FILE"
254 else
255 case $TTS_ENGINE in
256 festival)
257 echo "Action: Generate $WAV_FILE with festival"
258 echo -E "$TO_SPEAK" | $FESTIVAL_CLIENT $FESTIVAL_OPTS \
259 --server $FESTIVAL_HOST \
260 --otype riff --ttw --output "$WAV_FILE" 2>"$WAV_FILE"
262 espeak)
263 echo "Action: Generate $WAV_FILE with eSpeak"
264 echo $ESPEAK_BIN $ESPEAK_OPTS -w "$WAV_FILE"
265 echo -E "$TO_SPEAK" | $ESPEAK_BIN $ESPEAK_OPTS -w "$WAV_FILE"
267 flite)
268 echo "Action: Generate $WAV_FILE with flite"
269 echo -E "$TO_SPEAK" | $FLITE_BIN $FLITE_OPTS -o "$WAV_FILE"
271 esac
274 trim "$WAV_FILE"
277 # Trim wavefile $1
278 trim() {
279 WAVEFILE="$1"
280 echo "Action: Trim $WAV_FILE"
281 $WAVTRIM "$WAVEFILE" $NOISEFLOOR