--- trunk/mbrola.pl 2006/08/05 15:35:19 4 +++ trunk/mbrola.pl 2006/08/08 15:06:33 5 @@ -7,11 +7,7 @@ use strict; use Data::Dump qw/dump/; use Term::ReadLine; - -my $term = new Term::ReadLine 'Simple Perl calc'; -my $prompt = "Pričaj: "; -my $OUT = $term->OUT || \*STDOUT; - +use File::Slurp; my $letters = { 'a' => [['a'], @@ -194,7 +190,7 @@ }; my $recovery; -foreach my $df (qw/ bp oo uks/) { +foreach my $df (qw/bp oo uks/) { $recovery->{$df}++; } @@ -216,18 +212,34 @@ my $last_c = ''; - foreach my $i ( 0 .. $#chars ) { - my $c = $chars[$i]; + my $i = 0; + while (@chars) { + my $c = shift @chars; $g .= $c; - if (defined( $phonemes->{$c} )) { - $c = $phonemes->{$c}; - } elsif (defined( $token_to_grapheme->{$c} )) { - $c = $token_to_grapheme->{$c}; + if (defined( $phonemes->{ lc($c) } )) { + $c = $phonemes->{ lc($c) }; + } elsif (defined( $token_to_grapheme->{ lc($c) } )) { + my $tmp = $token_to_grapheme->{ lc($c) }; + if (length($tmp) > 1) { + my @tmp_c = split(//, $tmp); + warn "### $c --> $tmp\n"; + $c = shift @tmp_c; + unshift @chars, ( @tmp_c ); + } else { + $c = $tmp; + } } - my $d = $durations->{$c} || $silence->{word}; + my $d = $durations->{$c} || $durations->{ lc($c) }; + + if (! $d) { + next if ($c =~ m/\s/); + warn "### skipped: $c\n"; + $c = '_'; + $d = $silence->{word}; + } my @dur = ( $d ); @@ -255,7 +267,7 @@ $pho[ $#pho ]->{dur}->[0] += $silence->{word}; push @pho, { char => '_', - dur => [ $silence->{word} ], + dur => [ $silence->{sent} ], }; $last_c = $c; next; @@ -301,11 +313,13 @@ my $mbrola = './bin/mbrola-linux-i386 ./cr1/cr1'; -while ( defined ($_ = $term->readline($prompt)) ) { +my $term = new Term::ReadLine 'Mbrola croatian speaker'; +my $OUT = $term->OUT || \*STDOUT; - $term->addhistory($_); +sub play_speak_hr { + my $text = shift || return; - my ($out,$g,$f) = speak_hr( $_ ); + my ($out,$g,$f) = speak_hr( $text ); open(my $fh, "| $mbrola - tmp.wav") || die "can't open $mbrola: $!"; @@ -318,7 +332,28 @@ print $OUT "# $out\n"; system 'play tmp.wav'; +} + +if (my $path = shift @ARGV) { + my $text = read_file($path) || die "can't read $path: $!"; + + # strip html + $text =~ s!]+>! !gs; + + $text =~ s!\s+! !gs; + + print "-- $text --"; + + play_speak_hr( $text ); + exit; +} + +my $prompt = "Pričaj: "; + +while ( defined ($_ = $term->readline($prompt)) ) { + $term->addhistory( $_ ); + play_speak_hr( $_ ); }