Add description for the shortcuts plugin (FS#8829 by Alexander Levin).
[Rockbox.git] / tools / voice.pl
blobb83ea865aaf553480ccca80492a9c19cbe2dbbdb
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;
139 case "deutsch" {
140 # for all german engines (e.g. for english words)
141 $string =~ s/alkaline/alkalein/ig;
142 $string =~ s/byte(s?)/beit$1/ig;
143 $string =~ s/clip(s?)/klipp$1/ig;
144 $string =~ s/cuesheet/kjuschiet/ig;
145 $string =~ s/dither/didder/ig;
146 $string =~ s/equalizer/iquileiser/ig;
147 $string =~ s/\bflash\b/fläsh/ig;
148 $string =~ s/\bfirmware(s?)\b/firmwer$1/ig;
149 $string =~ s/\bI D 3 tag\b/I D 3 täg/ig; # can't just use "tag" here
150 $string =~ s/\bloudness\b/laudness/ig;
151 $string =~ s/\bunicode\b/unikod/ig;
152 switch($$tts_object{"name"}) {
153 case "sapi" { # just for SAPI
154 switch($$tts_object{"vendor"}) {
155 case "AT&T Labs" {
156 $string =~ s/alphabet/alfabet/ig;
157 $string =~ s/ampere/amper/ig;
158 $string =~ s/\bdezibel\b/de-zibell/ig;
159 $string =~ s/diddering/didde-ring/ig;
160 $string =~ s/energie\b/ener-gie/ig;
161 $string =~ s/\Blauf\b/-lauf/ig;
162 $string =~ s/\bnumerisch\b/numehrisch/ig;
168 case "svenska" {
169 # for all swedish engines (e.g. for english words)
170 $string =~ s/kilobyte/kilobajt/ig;
171 $string =~ s/megabyte/megabajt/ig;
172 $string =~ s/gigabyte/gigabajt/ig;
173 $string =~ s/\bloudness\b/laudness/ig;
175 switch($$tts_object{"name"}) {
176 case "espeak" { # just for eSpeak
177 $string =~ s/ampere/ampär/ig;
178 $string =~ s/bokmärken/bok-märken/ig;
179 $string =~ s/generella/schenerella/ig;
180 $string =~ s/dithering/diddering/ig;
181 $string =~ s/\bunicode\b/jynikod/ig;
182 $string =~ s/uttoning/utoning/ig;
183 $string =~ s/procent/pro-cent/ig;
184 $string =~ s/spellistor/spelistor/ig;
185 $string =~ s/cuesheet/qjyschiit/ig;
189 case "italiano" {
190 # for all italian engines (e.g. for english words)
191 $string =~ s/Replaygain/Ripleyghein/ig;
192 $string =~ s/Crossfade/Crossfeid/ig;
193 $string =~ s/beep/Bip/ig;
194 $string =~ s/cuesheet/chiushit/ig;
195 $string =~ s/fade/feid/ig;
196 $string =~ s/Crossfeed/crossfid/ig;
197 $string =~ s/Cache/chash/ig;
198 $string =~ s/\bfirmware(s?)\b/firmuer$1/ig;
199 $string =~ s/\bFile(s?)\b/fail$1/ig;
200 $string =~ s/\bloudness\b/laudness/ig;
201 $string =~ s/\bunicode\b/unikod/ig;
202 $string =~ s/Playlist/pleylist/ig;
203 $string =~ s/WavPack/wave pak/ig;
204 $string =~ s/BITRATE/bit reit/ig;
205 $string =~ s/Codepage/cod page/ig;
206 $string =~ s/PCM Wave/pcm Ue'iv/ig;
207 $string =~ s/è/è/ig;
208 $string =~ s/\b(s*)ì\b/$1ì/ig;
209 $string =~ s/\b(s*)ù\b/$1ù/ig;
210 $string =~ s/\b(s*)Ã*\b/$1à/ig;
211 switch($$tts_object{"name"}) {
212 case "sapi" { # just for SAPI
213 switch($$tts_object{"vendor"}) {
214 case "Loquendo" {
215 $string =~ s/Inizializza/inizializa/ig;
217 case "ScanSoft, Inc" {
218 $string =~ s/V/v/ig;
219 $string =~ s/X/x/ig;
220 $string =~ s/stop/stohp/ig;
227 if ($orig ne $string) {
228 printf("%s -> %s\n", $orig, $string) if $verbose;
230 return $string;
233 # Produce a wav file of the text given
234 sub voicestring {
235 our $verbose;
236 my ($string, $output, $tts_engine_opts, $tts_object) = @_;
237 my $cmd;
238 printf("Generate \"%s\" with %s in file %s\n", $string, $$tts_object{"name"}, $output) if $verbose;
239 switch($$tts_object{"name"}) {
240 case "festival" {
241 # festival_client lies to us, so we have to do awful soul-eating
242 # work with IPC::open3()
243 $cmd = "festival_client --server localhost --otype riff --ttw --output \"$output\"";
244 print("> $cmd\n") if $verbose;
245 # Open command, and filehandles for STDIN, STDOUT, STDERR
246 my $pid = open3(*CMD_IN, *CMD_OUT, *CMD_ERR, $cmd);
247 # Put the string to speak into STDIN and close it
248 print(CMD_IN $string);
249 close(CMD_IN);
250 # Read all output from festival_client (because it LIES TO US)
251 while (<CMD_ERR>) {
253 close(CMD_OUT);
254 close(CMD_ERR);
256 case "flite" {
257 $cmd = "flite $tts_engine_opts -t \"$string\" \"$output\"";
258 print("> $cmd\n") if $verbose;
259 `$cmd`;
261 case "espeak" {
262 # xxx: $tts_engine_opts isn't used
263 $cmd = "espeak $tts_engine_opts -w \"$output\"";
264 print("> $cmd\n") if $verbose;
265 open(ESPEAK, "| $cmd");
266 print ESPEAK $string . "\n";
267 close(ESPEAK);
269 case "sapi" {
270 print({$$tts_object{"stdin"}} "SPEAK\t$output\t$string\r\n");
272 case "swift" {
273 $cmd = "swift $tts_engine_opts -o \"$output\" \"$string\"";
274 print("> $cmd\n") if $verbose;
275 system($cmd);
280 # trim leading / trailing silence from the clip
281 sub wavtrim {
282 our $verbose;
283 my ($file, $threshold, $tts_object) = @_;
284 printf("Trim \"%s\"\n", $file) if $verbose;
285 my $cmd = "wavtrim \"$file\" $threshold";
286 if ($$tts_object{"name"} eq "sapi") {
287 print({$$tts_object{"stdin"}} "EXEC\t$cmd\r\n");
289 else {
290 print("> $cmd\n") if $verbose;
291 `$cmd`;
295 # Encode a wav file into the given destination file
296 sub encodewav {
297 our $verbose;
298 my ($input, $output, $encoder, $encoder_opts, $tts_object) = @_;
299 printf("Encode \"%s\" with %s in file %s\n", $input, $encoder, $output) if $verbose;
300 my $cmd = "$encoder $encoder_opts \"$input\" \"$output\"";
301 if ($$tts_object{"name"} eq "sapi") {
302 print({$$tts_object{"stdin"}} "EXEC\t$cmd\r\n");
304 else {
305 print("> $cmd\n") if $verbose;
306 `$cmd`;
310 # synchronize the clip generation / processing if it's running in another process
311 sub synchronize {
312 my ($tts_object) = @_;
313 if ($$tts_object{"name"} eq "sapi") {
314 print({$$tts_object{"stdin"}} "SYNC\t42\r\n");
315 my $wait = readline($$tts_object{"stdout"});
316 #ignore what's actually returned
320 # Run genlang and create voice clips for each string
321 sub generateclips {
322 our $verbose;
323 my ($language, $target, $encoder, $encoder_opts, $tts_engine, $tts_engine_opts) = @_;
324 my $english = dirname($0) . '/../apps/lang/english.lang';
325 my $langfile = dirname($0) . '/../apps/lang/' . $language . '.lang';
326 my $id = '';
327 my $voice = '';
328 my $cmd = "genlang -o -t=$target -e=$english $langfile 2>/dev/null";
329 my $pool_file;
330 open(VOICEFONTIDS, "> voicefontids");
331 my $i = 0;
332 local $| = 1; # make progress indicator work reliably
334 my $tts_object = init_tts($tts_engine, $tts_engine_opts, $language);
335 print("Generating voice clips");
336 print("\n") if $verbose;
337 for (`$cmd`) {
338 my $line = $_;
339 print(VOICEFONTIDS $line);
340 if ($line =~ /^id: (.*)$/) {
341 $id = $1;
343 elsif ($line =~ /^voice: "(.*)"$/) {
344 $voice = $1;
345 if ($id !~ /^NOT_USED_.*$/ && $voice ne "") {
346 my $wav = $id . '.wav';
347 my $mp3 = $id . '.mp3';
349 # Print some progress information
350 if (++$i % 10 == 0 and !$verbose) {
351 print(".");
354 # Apply corrections to the string
355 $voice = correct_string($voice, $language, $tts_object);
357 # If we have a pool of snippets, see if the string exists there first
358 if (defined($ENV{'POOL'})) {
359 $pool_file = sprintf("%s/%s-%s.mp3", $ENV{'POOL'},
360 md5_hex("$voice $tts_engine $tts_engine_opts $encoder_opts"),
361 $language);
362 if (-f $pool_file) {
363 printf("Re-using %s (%s) from pool\n", $id, $voice) if $verbose;
364 copy($pool_file, $mp3);
368 # Don't generate MP3 if it already exists (probably from the POOL)
369 if (! -f $mp3) {
370 if ($id eq "VOICE_PAUSE") {
371 print("Use distributed $wav\n") if $verbose;
372 copy(dirname($0)."/VOICE_PAUSE.wav", $wav);
374 else {
375 voicestring($voice, $wav, $tts_engine_opts, $tts_object);
376 wavtrim($wav, 500, $tts_object);
377 # 500 seems to be a reasonable default for now
380 encodewav($wav, $mp3, $encoder, $encoder_opts, $tts_object);
381 synchronize($tts_object);
382 if (defined($ENV{'POOL'})) {
383 copy($mp3, $pool_file);
385 unlink($wav);
387 $voice = "";
388 $id = "";
392 print("\n");
393 close(VOICEFONTIDS);
394 shutdown_tts($tts_object);
397 # Assemble the voicefile
398 sub createvoice {
399 our $verbose;
400 my ($language, $target_id) = @_;
401 my $outfile = "";
402 $outfile = sprintf("%s.voice", $language);
403 printf("Saving voice file to %s\n", $outfile) if $verbose;
404 my $cmd = "voicefont 'voicefontids' $target_id ./ $outfile";
405 print("> $cmd\n") if $verbose;
406 my $output = `$cmd`;
407 print($output) if $verbose;
410 sub deletemp3s() {
411 for (glob('*.mp3')) {
412 unlink($_);
414 for (glob('*.wav')) {
415 unlink($_);
419 sub panic_cleanup {
420 deletemp3s();
421 die "moo";
424 # Generate .talk clips
425 sub gentalkclips {
426 our $verbose;
427 my ($dir, $tts_object, $encoder, $encoder_opts, $tts_engine_opts, $i) = @_;
428 my $d = new DirHandle $dir;
429 while (my $file = $d->read) {
430 my ($voice, $wav, $mp3);
431 # Print some progress information
432 if (++$i % 10 == 0 and !$verbose) {
433 print(".");
436 # Convert to a complete path
437 my $path = sprintf("%s/%s", $dir, $file);
439 $voice = $file;
440 $wav = sprintf("%s.talk.wav", $path);
442 # Ignore dot-dirs and talk files
443 if ($file eq '.' || $file eq '..' || $file =~ /\.talk$/) {
444 next;
446 # Element is a dir
447 if ( -d $path) {
448 gentalkclips($path, $tts_object, $encoder, $encoder_opts, $tts_engine_opts, $i);
449 $mp3 = sprintf("%s/_dirname.talk", $path);
451 # Element is a file
452 else {
453 $mp3 = sprintf("%s.talk", $path);
454 $voice =~ s/\.[^\.]*$//; # Trim extension
457 printf("Talkclip %s: %s", $mp3, $voice) if $verbose;
459 voicestring($voice, $wav, $tts_engine_opts, $tts_object);
460 wavtrim($wav, 500, $tts_object);
461 # 500 seems to be a reasonable default for now
462 encodewav($wav, $mp3, $encoder, $encoder_opts, $tts_object);
463 synchronize($tts_object);
464 unlink($wav);
469 # Check parameters
470 my $printusage = 0;
471 unless (defined($V) or defined($C)) { print("Missing either -V or -C\n"); $printusage = 1; }
472 if (defined($V)) {
473 unless (defined($t)) { print("Missing -t argument\n"); $printusage = 1; }
474 unless (defined($l)) { print("Missing -l argument\n"); $printusage = 1; }
475 unless (defined($i)) { print("Missing -i argument\n"); $printusage = 1; }
477 elsif (defined($C)) {
478 unless (defined($ARGV[0])) { print "Missing path argument\n"; $printusage = 1; }
480 unless (defined($e)) { print("Missing -e argument\n"); $printusage = 1; }
481 unless (defined($E)) { print("Missing -E argument\n"); $printusage = 1; }
482 unless (defined($s)) { print("Missing -s argument\n"); $printusage = 1; }
483 unless (defined($S)) { print("Missing -S argument\n"); $printusage = 1; }
484 if ($printusage == 1) { printusage(); exit 1; }
486 if (defined($v) or defined($ENV{'V'})) {
487 our $verbose = 1;
490 # add the tools dir to the path temporarily, for calling various tools
491 $ENV{'PATH'} = dirname($0) . ':' . $ENV{'PATH'};
494 # Do what we're told
495 if ($V == 1) {
496 # Only do the panic cleanup for voicefiles
497 $SIG{INT} = \&panic_cleanup;
498 $SIG{KILL} = \&panic_cleanup;
500 printf("Generating voice\n Target: %s\n Language: %s\n Encoder (options): %s (%s)\n TTS Engine (options): %s (%s)\n",
501 $t, $l, $e, $E, $s, $S);
502 generateclips($l, $t, $e, $E, $s, $S);
503 createvoice($l, $i);
504 deletemp3s();
506 elsif ($C) {
507 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);
508 my $tts_object = init_tts($s, $S, $l);
509 gentalkclips($ARGV[0], $tts_object, $e, $E, $S, 0);
510 shutdown_tts($tts_object);
512 else {
513 printusage();
514 exit 1;