Skip to content

Commit

Permalink
Merge pull request #235 from ProGEEK/google-tts
Browse files Browse the repository at this point in the history
Added Basic Base Google TTS Support.
  • Loading branch information
hollie committed Aug 31, 2013
2 parents 24bdb23 + 5251f28 commit 951f5ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions bin/mh.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions lib/Voice_Text.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 951f5ee

Please sign in to comment.