Perl Text to Speech Example

From FreeSWITCH Wiki
Jump to: navigation, search

About this script

The following script is a sub routine I wrote to set up and execute your TTS (Text-to-Speech).

Script requirements

* mod_perl
* A tts engine such as cepstra, flite, etc.
* If you're going to use the Dumper line below, please make sure you have use Data::Dumper - available on most builds and on CPAN.

Example script

Usage:

speak("My name is Kareem Hamdy.");
# This sub routine performs the tts function.  It will set up the tts params,
# such as the engine and voice; and will dictate the string passed.  If no
# variables are passed, it will load defaults.
#
# by Kareem Hamdy 2009-01-19

sub speak {


   # Variable initialization - here we assign the
   # parameters passed to their corresponding variables.
 
   my ($text, $engine, $voice) =  @_;


   # If the tts engine wasn't specified, it will default
   # to flite - an open source engine.

   if (!$engine){
     $engine = 'flite';
   }

   # If the tts voice isn't specified, it will default to Kal.

   if (!$voice){
     $voice = 'kal';
   }

   # If the text to be read is missing, it assigns it
   # an empty character set.

   if (!$text){
     $text = ;
   }


   # Here is where we set up the tts parameters -
   # setting the engine and the voice.

   $session->set_tts_parms($engine, $voice);

   # This line isn't important - however I generally like
   # to see all my variables for troubleshooting.

   print "\n\n".Dumper(\@_)."\n\n";

   # This line waits until the session is ready.  If so,
   # it will engage the speech engine, thereby dictating the entered text.

   if ($session->ready ()) {
     $session->speak($text);
   }

   return 1;

}