Wednesday, August 30, 2006

Add speech to your server

As all coders everywhere all the time say; "Why code it, when someone else already did?!"

Yes, normally adding speech to a server is a long, drawn out process. Not to mention the fact that they normally suck pretty hard... Enter AT&T Labs! (head over there; the demo is pretty fun to play with!)

They (or Wizzard) seem to have developed an engine that works rather well. Sadly, they think that you should have to pay money to use their impressive engine, no matter how limited the use. Normally, this would cause you to go back to something like festival for your text-to-speech needs, but not this time...

AT&T was kind enough to provide a demo of their service that allows you up to 300 characters of text, and offers to host the speech wav file for 5 minutes. This is more than enough for us...

We simply need to take web input, forward it off to AT&T's page, and when it replies with the file, download the file so it's hosted locally, and link to it on the cgi script's reply page...

The following code is supposed to be a CGI script. It will accept input from a page that sends the script one variable and one value for that variable. (Of course all of that is easily changed)

Even if you only know a bit of perl you shouldn't have a tough time understanding the code, otherwise, increase your coding skill by 2 points...

#!/usr/bin/perl
print "Content-type: text/html\r\n\r\n";

my @rray=split('=', $ENV{QUERY_STRING});
http://192.20.225.55/tts/cgi-bin/nph-talk 2> /dev/null|grep wav`;
if(`curl -d "voice=crystal&txt=@rray[1]&downloadButton=DOWNLOAD" http://192.20.225.55/tts/cgi-bin/nph-talk 2> /dev/null|grep wav`=~/href=".+\/speech\/(.+)"/i){
$filename=$1;
`wget -O "/var/www/html/dump/$filename" "http://192.20.225.55/tts/speech/$filename"`;
}

print "<bgsound src=\"../dump/$filename\">";


As with all my other scripts, modularity is the name of the game. This is why I didn't use Perl's download function, and used linux tools to process some of the input. Besides; why write the code when someone else has already written it!

2 comments:

blogagog said...

Didn't even know perl had a download function.

I'm a big fan or perl, but probably don't know it as well as I should. I can't get your script to run in Windows perl. Do I need to add a 'use LWP' thing? WTH is 'curl'?

Hmm, I guess I need to crack open the books again :(

Fletch said...

This is meant to run on linux (though I think you can get windows versions of the curl and grep.

Curl is pretty much a command line web server interaction tool. You can use it to create your own http requests (get, post, trace, etc). It can manage cookies, and maintain sessions. Since it's specially made for these functions it's more powerful than perl in this regard. (though I only use it for a basic function here)

I pretty much opened up firefox's tamperdata extension and completed a voice transaction with the AT&T server, then reviewed the requests that took place during the regular function, and copied it using curl to do the transaction, and perl to parse the important info out of it.

btw, I've always found books a good place to get started; but I've learned 10 times more from reviewing and reverse engineering code examples written by more skilled perlmongers than I.