Hack37.Make Your Computer Talk to You


Hack 37. Make Your Computer Talk to You

Install Festival on a computer to convert text to speech.

Whenever I see a science fiction movie set in the future, computers almost always fit a certain profile. They are either a form of artificial intelligence (AI) that can talk to people, or they are bent on world domination (or sometimes both). While the AI part isn't exactly there yet, and hopefully the world domination bit is a ways off, computers talking to us is something we can have today. This hack shows you how to use Festival to convert text into speech.

Festival is a speech synthesis system hosted by the Center for Speech Technology Research at the University of Edinburgh (http://www.cstr.ed.ac.uk/projects/festival). Actually, using Festival for basic text-to-speech is an underuse of the technology, because the system provides for all sorts of advanced speech synthesis in any number of languages.

To make Festival convert text to speech, you will need a few different components. First you need the core Festival program itself; next you need the Edinburgh Speech Tools library; after that you need some sort of lexicon database Festival can use (popular ones are the festlex OALD (Oxford Advanced Learners' Dictionary) and festlex POSLEX (Parts of Speech Lexicons); and finally you need a speech database. Which speech database you choose basically depends on what sort of voices you want to hear. These festvox packages provide voices for American English males and females, British English voices, Castilian Spanish voices, and others. Most major desktop distributions have packages for all of these components either directly in the distribution or in third-party repositories, so check your packaging tool first. Alternatively you can download these source packages from the Center's official download site at http://www.cstr.ed.ac.uk/projects/festival/download.html:

  • festival-*.tar.gz (choose the latest version)

  • speech_tools-*-beta.tar.gz (choose the latest version)

  • festlex_OALD.tar.gz

  • festlex_POSLEX.tar.gz

  • festvox_rablpc16k.tar.gz (or choose a different speaker)

If you go the compilation route, extract all the files from these packages, then build first the speech_tools directory, and then the festival directory:

 greenfly@moses:~$ cd speech_tools greenfly@moses:~/speech_tools$ ./configure greenfly@moses:~/speech_tools$ make greenfly@moses:~/speech_tools$ cd ../festival greenfly@moses:~/festival$ ./configure greenfly@moses:~/festival$ make 

Once Festival is installed, send it some sample text to test it:

 greenfly@moses:~$ echo "Hello World" | festival --tts 

By default Festival runs in an interactive shell, but for general text-to-speech uses, you can just use the --tts argument and pipe text to it or, alternatively, pass it a text file to read as an argument:

 greenfly@moses:~$ festival --tts filename 

Festival works rather well when combined with Project Gutenberg's ebooks at http://www.gutenberg.org. I've found that I need to strip out the official Project Gutenberg text from the beginning of the ebook before Festival reads it, but otherwise it works reasonably well.

One issue I've had is that sometimes I'd like to be able to read the text that is being read to me. I've also found that ebooks and regular text files in different formats don't always have the proper pauses when being read. So I wrote a basic Perl script that runs through the text a paragraph at a time, converts some of the punctuation to make Festival pause where I want, and prints the paragraph to the terminal while simultaneously reading it. Since the script is only a few lines, it lends itself to outside modification to tweak Festival's reading habits even further. I named the following script speak, and it can accept either STDIN or a text file as an argument:

 #!/usr/bin/perl # usage: speak <filename> $/ = ""; while(<>) {         @paragraphs = split /((\n\W){1,})|(\n\r{2,})/; foreach $paragraph (@paragraphs) {            next if($paragraph =~ /^\W*$/);    $paragraph_unedited = $paragraph;     # strip away symbols $paragraph =~ s/[^\w\s\d.,;:!?]//g; # add appropriate pauses to newlines, periods, commas, etc            $paragraph =~ s/\n\r/ /g;    $paragraph =~ s/\s{2,}/ /g;    $paragraph =~ s/\.(?=\w)/ /g;    $paragraph =~ s/(\w)\./$1\. /g;    $paragraph =~ s/(\w)\;/$1\. /g;     $paragraph =~ s/\.{2,}/ /g;            $paragraph =~ s/(^\.)$/./g;            print "$paragraph_unedited\n";    system("echo '$paragraph' | festival --tts") == 0         or die "festival exited$!";         }     } 

Save the file somewhere in your path (such as /usr/local/bin/speak), and then to use it, either pass STDIN:

 $ echo  "Hello World " | speak  

or pass a filename containing text you want to speak as an argument:

 $ speak  hello.txt  




Linux Multimedia Hacks
Linux Multimedia Hacks
ISBN: 596100760
EAN: N/A
Year: 2005
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net