Prevent file descriptor leakage or overuse
[Rockbox.git] / tools / genvoice.sh
blob4e58801205b1ab8b750c3c96b1cde77787992489
1 #!/bin/sh
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
10 # Copyright 2006 Jonas Häggqvist, some parts Copyright 2004 Daniel Gudlat
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 # Include voicecommon.sh from the same dir as this script
19 # Any settings from voicecommon can be overridden if added below the following
20 # line.
21 source `dirname $0`'/voicecommon.sh'
23 ####################
24 # General settings #
25 ####################
27 # These settings can be overridden by passing a file with definitions as
28 # the fourth parameter to this script
30 # which TTS engine to use. Available: festival, flite, espeak
31 TTS_ENGINE=festival
32 # which encoder to use, available: lame, speex, vorbis (only lame will produce
33 # functional voice clips at this point)
34 ENCODER=lame
35 # Where to save temporary files
36 TEMPDIR=/tmp
37 # List of IDs to send to voicefont
38 VOICEFONTIDS=voicefontids
40 ###################
41 # End of settings #
42 ###################
44 TARGET_ID="$4"
45 createvoicefile() {
46 RLANG="$1"
47 $GENLANG -e=$ENGLISH -o -t=$TARGET $LANG_FILE > $VOICEFONTIDS
48 $VOICEFONT "$VOICEFONTIDS" "$TARGET_ID" "$TEMPDIR/" "./$RLANG.voice"
49 rm -f $VIOCEFONTIDS
52 deletefiles() {
53 # XXX: might be unsafe depending on the value of TEMPDIR
54 rm -f "${TEMPDIR}"/LANG_*
55 rm -f "${TEMPDIR}"/VOICE_*
56 rm -f "${TEMPDIR}"/NOT_USED_*
59 generateclips() {
60 ROCKBOX_DIR="$1"
61 RLANG="$2"
62 TARGET="$3"
63 GENLANG="$ROCKBOX_DIR"/tools/genlang
64 ENGLISH="$ROCKBOX_DIR"/apps/lang/english.lang
65 LANG_FILE="$ROCKBOX_DIR"/apps/lang/$RLANG.lang
67 $GENLANG -e=$ENGLISH -o -t=$TARGET $LANG_FILE |(
68 i=0
69 while read line; do
70 case `expr $i % 3` in
72 # String ID no.
73 NUMBER=`echo $line |cut -b 2-`
76 # String ID
77 ID=`echo $line |cut -b 5-`
80 # String
81 STRING=`echo $line |cut -b 8-`
82 # xxx: Should the hash include encoder/tts options?
83 POOL_FILE=${POOL}/`echo "$STRING" |md5sum|cut -b-32`-${RLANG}.mp3
85 if [ -n "$POOL" ]; then
86 # we have a common pool of snippets, check that first
87 # for available mp3 sounds, and if it is available copy
88 # (symlink!) it over
89 if [ -f "$POOL_FILE" ]; then
90 echo "Re-using $ID from pool (${POOL_FILE})"
91 if [ ! -e "$TEMPDIR/$ID".mp3 ]; then
92 # only do this if not present
93 cp -f "$POOL_FILE" "$TEMPDIR/$ID".mp3
98 # only make an mp3 if not already present
99 if [ ! -e "$TEMPDIR/$ID".mp3 ]; then
100 # Now generate the file
101 voice "$STRING" "$TEMPDIR/$ID".wav
102 if [ -n "$POOL" ]; then
103 # create it in the pool, symlink it back
104 encode "$TEMPDIR/$ID".wav "$POOL_FILE"
105 cp -f "$POOL_FILE" "$TEMPDIR/$ID".mp3
106 else
107 encode "$TEMPDIR/$ID".wav "$TEMPDIR/$ID".mp3
111 esac
112 i=`expr $i + 1`
113 done
117 if [ -z "$4" ]; then
118 echo "Usage: $0 rockboxdirectory language target targetid [settingsfile]";
119 exit 32
120 else
121 if [ ! -d "$1" ] || [ ! -f "$1/tools/genlang" ]; then
122 echo "Error: $1 is not a Rockbox directory"
123 exit 33
125 # Check for valid language
126 if [ ! -f "$1/apps/lang/$2.lang" ]; then
127 echo "Error: $2 is not a valid language"
128 exit 34
130 if [ ! -z "$5" ]; then
131 if [ -f "$5" ]; then
132 # Read settings from file
133 . "$5"
134 else
135 echo "Error: $5 does not exist"
136 exit 36
139 # XXX: check for valid $TARGET?
142 VOICEFONT=`dirname $0`/voicefont
143 if [ ! -x $VOICEFONT ]; then
144 echo "Error: $VOICEFONT does not exist or is not executable"
145 exit 35
148 init_tts
149 init_encoder
150 generateclips "$1" "$2" "$3"
151 stop_tts
152 createvoicefile "$2"
153 deletefiles