Add w32 mountpoint resolving based on disc number correctly this time.
[Rockbox.git] / tools / voice.pl
blobeb6bfc1bf6d3246bbcdfb47c4e7e91fd9d3c3c0e
1 #!/usr/bin/perl -s
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id:
10 # Copyright (C) 2007 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 use strict;
19 use warnings;
20 use File::Basename;
21 use File::Copy;
22 use Switch;
23 use vars qw($V $C $t $l $e $E $s $S $i $v);
24 use IPC::Open2;
25 use IPC::Open3;
26 use Digest::MD5 qw(md5_hex);
27 use DirHandle;
29 sub printusage {
30 print <<USAGE
32 Usage: voice.pl [options] [path to dir]
34 Create voice file. You must also specify -t and -l.
37 Create .talk clips.
39 -t=<target>
40 Specify which target you want to build voicefile for. Must include
41 any features that target supports.
43 -i=<target_id>
44 Numeric target id. Needed for voice building.
46 -l=<language>
47 Specify which language you want to build. Without .lang extension.
49 -e=<encoder>
50 Which encoder to use for voice strings
52 -E=<encoder options>
53 Which encoder options to use when compressing voice strings. Enclose
54 in double quotes if the options include spaces.
56 -s=<TTS engine>
57 Which TTS engine to use.
59 -S=<TTS engine options>
60 Options to pass to the TTS engine. Enclose in double quotes if the
61 options include spaces.
64 Be verbose
65 USAGE
69 # Initialize TTS engine. May return an object or value which will be passed
70 # to voicestring and shutdown_tts
71 sub init_tts {
72 our $verbose;
73 my ($tts_engine, $tts_engine_opts, $language) = @_;
74 my %ret = ("name" => $tts_engine);
75 switch($tts_engine) {
76 case "festival" {
77 print("> festival $tts_engine_opts --server\n") if $verbose;
78 my $pid = open(FESTIVAL_SERVER, "| festival $tts_engine_opts --server > /dev/null 2>&1");
79 my $dummy = *FESTIVAL_SERVER; #suppress warning
80 $SIG{INT} = sub { kill TERM => $pid; print("foo"); panic_cleanup(); };
81 $SIG{KILL} = sub { kill TERM => $pid; print("boo"); panic_cleanup(); };
82 $ret{"pid"} = $pid;
84 case "sapi" {
85 my $toolsdir = dirname($0);
86 my $path = `cygpath $toolsdir -a -w`;
87 chomp($path);
88 $path = $path . '\\';
89 my $cmd = $path . "sapi_voice.vbs /language:$language $tts_engine_opts";
90 $cmd =~ s/\\/\\\\/g;
91 print("> cscript //nologo $cmd\n") if $verbose;
92 my $pid = open2(*CMD_OUT, *CMD_IN, "cscript //nologo $cmd");
93 $SIG{INT} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); };
94 $SIG{KILL} = sub { print(CMD_IN "QUIT\r\n"); panic_cleanup(); };
95 print(CMD_IN "QUERY\tVENDOR\r\n");
96 my $vendor = readline(CMD_OUT);
97 $vendor =~ s/\r\n//;
98 %ret = (%ret,
99 "stdin" => *CMD_IN,
100 "stdout" => *CMD_OUT,
101 "vendor" => $vendor);
104 return \%ret;
107 # Shutdown TTS engine if necessary.
108 sub shutdown_tts {
109 my ($tts_object) = @_;
110 switch($$tts_object{"name"}) {
111 case "festival" {
112 # Send SIGTERM to festival server
113 kill TERM => $$tts_object{"pid"};
115 case "sapi" {
116 print({$$tts_object{"stdin"}} "QUIT\r\n");
117 close($$tts_object{"stdin"});
122 # Apply corrections to a voice-string to make it sound better
123 sub correct_string {
124 our $verbose;
125 my ($string, $language, $tts_object) = @_;
126 my $orig = $string;
127 switch($language) {
128 # General for all engines and languages
129 $string =~ s/USB/U S B/g;
130 $string =~ s/ID3/I D 3/g;
132 case "english" {
133 switch($$tts_object{"name"}) {
134 case ["sapi","festival"] {
135 $string =~ s/plugin(s?)/plug-in$1/ig; next
137 case "festival" {
138 $string =~ s/\ba\b/ay/ig;
142 case "deutsch" {
143 # for all german engines (e.g. for english words)
144 $string =~ s/alkaline/alkalein/ig;
145 $string =~ s/byte(s?)/beit$1/ig;
146 $string =~ s/clip(s?)/klipp$1/ig;
147 $string =~ s/cuesheet/kjuschiet/ig;
148 $string =~ s/dither/didder/ig;
149 $string =~ s/equalizer/iquileiser/ig;
150 $string =~ s/\bflash\b/fläsh/ig;
151 $string =~ s/\bfirmware(s?)\b/firmwer$1/ig;
152 $string =~ s/\bI D 3 tag\b/I D 3 täg/ig; # can't just use "tag" here
153 $string =~ s/\bloudness\b/laudness/ig;
154 $string =~ s/\bunicode\b/unikod/ig;
155 switch($$tts_object{"name"}) {
156 case "sapi" { # just for SAPI
157 switch($$tts_object{"vendor"}) {
158 case "AT&T Labs" {
159 $string =~ s/alphabet/alfabet/ig;
160 $string =~ s/ampere/amper/ig;
161 $string =~ s/\bdezibel\b/de-zibell/ig;
162 $string =~ s/diddering/didde-ring/ig;
163 $string =~ s/energie\b/ener-gie/ig;
164 $string =~ s/\Blauf\b/-lauf/ig;
165 $string =~ s/\bnumerisch\b/numehrisch/ig;
171 case "svenska" {
172 # for all swedish engines (e.g. for english words)
173 $string =~ s/kilobyte/kilobajt/ig;
174 $string =~ s/megabyte/megabajt/ig;
175 $string =~ s/gigabyte/gigabajt/ig;
176 $string =~ s/\bloudness\b/laudness/ig;
178 switch($$tts_object{"name"}) {
179 case "espeak" { # just for eSpeak
180 $string =~ s/ampere/ampär/ig;
181 $string =~ s/bokmärken/bok-märken/ig;
182 $string =~ s/generella/schenerella/ig;
183 $string =~ s/dithering/diddering/ig;
184 $string =~ s/\bunicode\b/jynikod/ig;
185 $string =~ s/uttoning/utoning/ig;
186 $string =~ s/procent/pro-cent/ig;
187 $string =~ s/spellistor/spelistor/ig;
188 $string =~ s/cuesheet/qjyschiit/ig;
192 case "italiano" {
193 # for all italian engines (e.g. for english words)
194 $string =~ s/Replaygain/Ripleyghein/ig;
195 $string =~ s/Crossfade/Crossfeid/ig;
196 $string =~ s/beep/Bip/ig;
197 $string =~ s/cuesheet/chiushit/ig;
198 $string =~ s/fade/feid/ig;
199 $string =~ s/Crossfeed/crossfid/ig;
200 $string =~ s/Cache/chash/ig;
201 $string =~ s/\bfirmware(s?)\b/firmuer$1/ig;
202 $string =~ s/\bFile(s?)\b/fail$1/ig;
203 $string =~ s/\bloudness\b/laudness/ig;
204 $string =~ s/\bunicode\b/unikod/ig;
205 $string =~ s/Playlist/pleylist/ig;
206 $string =~ s/WavPack/wave pak/ig;
207 $string =~ s/BITRATE/bit reit/ig;
208 $string =~ s/Codepage/cod page/ig;
209 $string =~ s/PCM Wave/pcm Ue'iv/ig;
210 $string =~ s/è/è/ig;
211 $string =~ s/\b(s*)ì\b/$1ì/ig;
212 $string =~ s/\b(s*)ù\b/$1ù/ig;
213 $string =~ s/\b(s*)à\b/$1à/ig;
214 switch($$tts_object{"name"}) {
215 case "sapi" { # just for SAPI
216 switch($$tts_object{"vendor"}) {
217 case "Loquendo" {
218 $string =~ s/Inizializza/inizializa/ig;
220 case "ScanSoft, Inc" {
221 $string =~ s/V/v/ig;
222 $string =~ s/X/x/ig;
223 $string =~ s/stop/stohp/ig;
230 if ($orig ne $string) {
231 printf("%s -> %s\n", $orig, $string) if $verbose;
233 return $string;
236 # Produce a wav file of the text given
237 sub voicestring {
238 our $verbose;
239 my ($string, $output, $tts_engine_opts, $tts_object) = @_;
240 my $cmd;
241 printf("Generate \"%s\" with %s in file %s\n", $string, $$tts_object{"name"}, $output) if $verbose;
242 switch($$tts_object{"name"}) {
243 case "festival" {
244 # festival_client lies to us, so we have to do awful soul-eating
245 # work with IPC::open3()
246 $cmd = "festival_client --server localhost --otype riff --ttw --output \"$output\"";
247 print("> $cmd\n") if $verbose;
248 # Open command, and filehandles for STDIN, STDOUT, STDERR
249 my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $cmd);
250 # Put the string to speak into STDIN and close it
251 print(CMD_IN $string);
252 close(CMD_IN);
253 # Read all output from festival_client (because it LIES TO US)
254 while (<CMD_ERR>) {
256 close(CMD_OUT);
257 close(CMD_ERR);
259 case "flite" {
260 $cmd = "flite $tts_engine_opts -t \"$string\" \"$output\"";
261 print("> $cmd\n") if $verbose;
262 `$cmd`;
264 case "espeak" {
265 # xxx: $tts_engine_opts isn't used
266 $cmd = "espeak $tts_engine_opts -w \"$output\"";
267 print("> $cmd\n") if $verbose;
268 open(ESPEAK, "| $cmd");
269 print ESPEAK $string . "\n";
270 close(ESPEAK);
272 case "sapi" {
273 print({$$tts_object{"stdin"}} "SPEAK\t$output\t$string\r\n");
275 case "swift" {
276 $cmd = "swift $tts_engine_opts -o \"$output\" \"$string\"";
277 print("> $cmd\n") if $verbose;
278 system($cmd);
283 # trim leading / trailing silence from the clip
284 sub wavtrim {
285 our $verbose;
286 my ($file, $threshold, $tts_object) = @_;
287 printf("Trim \"%s\"\n", $file) if $verbose;
288 my $cmd = "wavtrim \"$file\" $threshold";
289 if ($$tts_object{"name"} eq "sapi") {
290 print({$$tts_object{"stdin"}} "EXEC\t$cmd\r\n");
292 else {
293 print("> $cmd\n") if $verbose;
294 `$cmd`;
298 # Encode a wav file into the given destination file
299 sub encodewav {
300 our $verbose;
301 my ($input, $output, $encoder, $encoder_opts, $tts_object) = @_;
302 printf("Encode \"%s\" with %s in file %s\n", $input, $encoder, $output) if $verbose;
303 my $cmd = "$encoder $encoder_opts \"$input\" \"$output\"";
304 if ($$tts_object{"name"} eq "sapi") {
305 print({$$tts_object{"stdin"}} "EXEC\t$cmd\r\n");
307 else {
308 print("> $cmd\n") if $verbose;
309 `$cmd`;
313 # synchronize the clip generation / processing if it's running in another process
314 sub synchronize {
315 my ($tts_object) = @_;
316 if ($$tts_object{"name"} eq "sapi") {
317 print({$$tts_object{"stdin"}} "SYNC\t42\r\n");
318 my $wait = readline($$tts_object{"stdout"});
319 #ignore what's actually returned
323 # Run genlang and create voice clips for each string
324 sub generateclips {
325 our $verbose;
326 my ($language, $target, $encoder, $encoder_opts, $tts_engine, $tts_engine_opts) = @_;
327 my $english = dirname($0) . '/../apps/lang/english.lang';
328 my $langfile = dirname($0) . '/../apps/lang/' . $language . '.lang';
329 my $id = '';
330 my $voice = '';
331 my $cmd = "genlang -o -t=$target -e=$english $langfile 2>/dev/null";
332 my $pool_file;
333 open(VOICEFONTIDS, "> voicefontids");
334 my $i = 0;
335 local $| = 1; # make progress indicator work reliably
337 my $tts_object = init_tts($tts_engine, $tts_engine_opts, $language);
338 print("Generating voice clips");
339 print("\n") if $verbose;
340 for (`$cmd`) {
341 my $line = $_;
342 print(VOICEFONTIDS $line);
343 if ($line =~ /^id: (.*)$/) {
344 $id = $1;
346 elsif ($line =~ /^voice: "(.*)"$/) {
347 $voice = $1;
348 if ($id !~ /^NOT_USED_.*$/ && $voice ne "") {
349 my $wav = $id . '.wav';
350 my $mp3 = $id . '.mp3';
352 # Print some progress information
353 if (++$i % 10 == 0 and !$verbose) {
354 print(".");
357 # Apply corrections to the string
358 $voice = correct_string($voice, $language, $tts_object);
360 # If we have a pool of snippets, see if the string exists there first
361 if (defined($ENV{'POOL'})) {
362 $pool_file = sprintf("%s/%s-%s.mp3", $ENV{'POOL'},
363 md5_hex("$voice $tts_engine $tts_engine_opts $encoder_opts"),
364 $language);
365 if (-f $pool_file) {
366 printf("Re-using %s (%s) from pool\n", $id, $voice) if $verbose;
367 copy($pool_file, $mp3);
371 # Don't generate MP3 if it already exists (probably from the POOL)
372 if (! -f $mp3) {
373 if ($id eq "VOICE_PAUSE") {
374 print("Use distributed $wav\n") if $verbose;
375 copy(dirname($0)."/VOICE_PAUSE.wav", $wav);
377 else {
378 voicestring($voice, $wav, $tts_engine_opts, $tts_object);
379 wavtrim($wav, 500, $tts_object);
380 # 500 seems to be a reasonable default for now
383 encodewav($wav, $mp3, $encoder, $encoder_opts, $tts_object);
384 synchronize($tts_object);
385 if (defined($ENV{'POOL'})) {
386 copy($mp3, $pool_file);
388 unlink($wav);
390 $voice = "";
391 $id = "";
395 print("\n");
396 close(VOICEFONTIDS);
397 shutdown_tts($tts_object);
400 # Assemble the voicefile
401 sub createvoice {
402 our $verbose;
403 my ($language, $target_id) = @_;
404 my $outfile = "";
405 $outfile = sprintf("%s.voice", $language);
406 printf("Saving voice file to %s\n", $outfile) if $verbose;
407 my $cmd = "voicefont 'voicefontids' $target_id ./ $outfile";
408 print("> $cmd\n") if $verbose;
409 my $output = `$cmd`;
410 print($output) if $verbose;
413 sub deletemp3s() {
414 for (glob('*.mp3')) {
415 unlink($_);
417 for (glob('*.wav')) {
418 unlink($_);
422 sub panic_cleanup {
423 deletemp3s();
424 die "moo";
427 # Generate .talk clips
428 sub gentalkclips {
429 our $verbose;
430 my ($dir, $tts_object, $encoder, $encoder_opts, $tts_engine_opts, $i) = @_;
431 my $d = new DirHandle $dir;
432 while (my $file = $d->read) {
433 my ($voice, $wav, $mp3);
434 # Print some progress information
435 if (++$i % 10 == 0 and !$verbose) {
436 print(".");
439 # Convert to a complete path
440 my $path = sprintf("%s/%s", $dir, $file);
442 $voice = $file;
443 $wav = sprintf("%s.talk.wav", $path);
445 # Ignore dot-dirs and talk files
446 if ($file eq '.' || $file eq '..' || $file =~ /\.talk$/) {
447 next;
449 # Element is a dir
450 if ( -d $path) {
451 gentalkclips($path, $tts_object, $encoder, $encoder_opts, $tts_engine_opts, $i);
452 $mp3 = sprintf("%s/_dirname.talk", $path);
454 # Element is a file
455 else {
456 $mp3 = sprintf("%s.talk", $path);
457 $voice =~ s/\.[^\.]*$//; # Trim extension
460 printf("Talkclip %s: %s", $mp3, $voice) if $verbose;
462 voicestring($voice, $wav, $tts_engine_opts, $tts_object);
463 wavtrim($wav, 500, $tts_object);
464 # 500 seems to be a reasonable default for now
465 encodewav($wav, $mp3, $encoder, $encoder_opts, $tts_object);
466 synchronize($tts_object);
467 unlink($wav);
472 # Check parameters
473 my $printusage = 0;
474 unless (defined($V) or defined($C)) { print("Missing either -V or -C\n"); $printusage = 1; }
475 if (defined($V)) {
476 unless (defined($t)) { print("Missing -t argument\n"); $printusage = 1; }
477 unless (defined($l)) { print("Missing -l argument\n"); $printusage = 1; }
478 unless (defined($i)) { print("Missing -i argument\n"); $printusage = 1; }
480 elsif (defined($C)) {
481 unless (defined($ARGV[0])) { print "Missing path argument\n"; $printusage = 1; }
483 unless (defined($e)) { print("Missing -e argument\n"); $printusage = 1; }
484 unless (defined($E)) { print("Missing -E argument\n"); $printusage = 1; }
485 unless (defined($s)) { print("Missing -s argument\n"); $printusage = 1; }
486 unless (defined($S)) { print("Missing -S argument\n"); $printusage = 1; }
487 if ($printusage == 1) { printusage(); exit 1; }
489 if (defined($v) or defined($ENV{'V'})) {
490 our $verbose = 1;
493 # add the tools dir to the path temporarily, for calling various tools
494 $ENV{'PATH'} = dirname($0) . ':' . $ENV{'PATH'};
497 # Do what we're told
498 if ($V == 1) {
499 # Only do the panic cleanup for voicefiles
500 $SIG{INT} = \&panic_cleanup;
501 $SIG{KILL} = \&panic_cleanup;
503 printf("Generating voice\n Target: %s\n Language: %s\n Encoder (options): %s (%s)\n TTS Engine (options): %s (%s)\n",
504 $t, $l, $e, $E, $s, $S);
505 generateclips($l, $t, $e, $E, $s, $S);
506 createvoice($l, $i);
507 deletemp3s();
509 elsif ($C) {
510 printf("Generating .talk clips\n Path: %s\n Language: %s\n Encoder (options): %s (%s)\n TTS Engine (options): %s (%s)\n", $ARGV[0], $l, $e, $E, $s, $S);
511 my $tts_object = init_tts($s, $S, $l);
512 gentalkclips($ARGV[0], $tts_object, $e, $E, $S, 0);
513 shutdown_tts($tts_object);
515 else {
516 printusage();
517 exit 1;