From 5251f28235485a7d35e10b4c24ea1dc74b4af380 Mon Sep 17 00:00:00 2001 From: Ryan Davies Date: Thu, 18 Jul 2013 01:11:40 +1200 Subject: [PATCH] Added Basic Base Google TTS Support --- bin/mh.ini | 6 +++--- lib/Voice_Text.pm | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/bin/mh.ini b/bin/mh.ini index aa65a3df8..2e999aaf0 100644 --- a/bin/mh.ini +++ b/bin/mh.ini @@ -1085,9 +1085,9 @@ ipaddress_xpl_broadcast = @ See the mh/docs/install.html on how to install various speech engines. -@ voice_text can be MS, MSV4, MSV5, festival, flite, theta or swift (for the Cepstral engine on Linux), -@ viavoice, vv_tts (for IBM's viavoice Outloud), NaturalVoice, NaturalVoiceWine, or -@ program xyz (for a stand alone xyz program), +@ voice_text can be MS, MSV4, MSV5, festival, flite, google (for the Google TTS API, requires ffmpeg), +@ theta or swift (for the Cepstral engine on Linux), viavoice, vv_tts (for IBM's viavoice Outloud), +@ NaturalVoice, NaturalVoiceWine, or program xyz (for a stand alone xyz program), @ Set to blank to disable. @ Use voice_text = MS on windows for all Microsoft SAPI compatible TTS engines, including AT&T NaturalVoice. @ Other parms specify paths to various TTS engines on unix systems. diff --git a/lib/Voice_Text.pm b/lib/Voice_Text.pm index 3c91ab04b..bf515a4b1 100644 --- a/lib/Voice_Text.pm +++ b/lib/Voice_Text.pm @@ -4,6 +4,8 @@ package Voice_Text; # $Revision$ use strict; +use LWP::UserAgent; +use LWP::ConnCache; use vars '$VTxt_version'; my (@VTxt, $VTxt_stream1, $VTxt_stream2, %VTxt_cards, $VTxt_festival, $VTxt_mac); @@ -429,6 +431,42 @@ sub speak_text { # exit; # nothing left for the child to do } } + }elsif ($speak_engine =~ /google/) { + # Speak to tile using the Google TTS Engine + + # Define a basic LWP agent for retrieving the Google MP3 + my $ua; + $ua = LWP::UserAgent->new; + $ua->agent("Mozilla/5.0 (X11; Linux; rv:8.0) Gecko/20100101"); + $ua->env_proxy; + $ua->conn_cache(LWP::ConnCache->new()); + $ua->timeout(10); + + my $random = int rand 1000; # Use random file name so we can talk 2+ at once. + + # If being forced to file, use the filename being forced, otherwise, use a random temp file. + my $out_file = ($parms{to_file}) ? $parms{to_file} : "$main::config_parms{data_dir}/mh_temp.google-$random.wav"; + + # The temp file to store google's MP3 as + my $google_file = "$main::config_parms{data_dir}/mh_temp.google-$random.mp3"; + + # Make the request, store the result in the google temp file + my $ua_request = HTTP::Request->new('GET' => "http://translate.google.com/translate_tts?tl=en&q=".qq[ $parms{text} ]); + my $ua_response = $ua->request($ua_request, $google_file); + + # Log the failure + if (!$ua_response->is_success) { + print "Failed to contact the Google TTS API.\n"; + return; + } + + # Convert the returned mp3 file to a wav, and clean up the temp file + system("ffmpeg", "-loglevel", "panic", "-i", "$google_file", "$out_file"); + unlink($google_file); + + # Play the wav file, clean up only if we are not being forced to file + system($main::config_parms{sound_program}, $out_file) unless $parms{to_file}; + unlink($out_file) unless $parms{to_file}; } elsif ($speak_pgm) { my $fork = 1 unless $parms{to_file} and !$parms{async}; # Must wait for to_file requests, so http requests work