home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / examples / festival_client.pl < prev    next >
Perl Script  |  1997-08-11  |  5KB  |  165 lines

  1. #!/usr/local/bin/perl
  2.  
  3. # festival_client.pl - a perl socket client for festival
  4. #
  5. # Copyright (C) 1997
  6. # Kevin A. Lenzo (lenzo@cs.cmu.edu) 7/97
  7. # All rights reserved.
  8. #
  9. # The authors hereby grant permission to use, copy, modify, distribute,
  10. # and license this software and its documentation for any purpose, provided
  11. # that existing copyright notices are retained in all copies and that this
  12. # notice is included verbatim in any distributions. No written agreement,
  13. # license, or royalty fee is required for any of the authorized uses.
  14. # Modifications to this software may be copyrighted by their authors
  15. # and need not follow the licensing terms described here, provided that
  16. # the new terms are clearly indicated on the first page of each file where
  17. # they apply.
  18. # IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  19. # FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  20. # ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  21. # DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  22. # POSSIBILITY OF SUCH DAMAGE.
  23. # THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  24. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  25. # FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
  26. # IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  27. # NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  28. # MODIFICATIONS.
  29. #
  30. #########################################################################
  31. #
  32. # A good deal of this was taken from the example in the
  33. # perlipc man page.  The rest is simply the Festival-specific 
  34. # stuff. 
  35.  
  36. use IO::Socket;
  37.  
  38. package Festival;
  39.  
  40. my ($audio_command, $audio_file, $file_stuff_key);
  41. my ($host, $port, $kidpid, $handle, $line);
  42.  
  43. $wave_type = "nist";                 # the type of the audio files
  44. $audio_command  = "na_play";         # your local audio play command
  45.  
  46. # uncomment the next line if your audio command requires a file
  47. # $audio_file = "/tmp/client_tmp$$.nist"; # temp file for waveforms
  48.  
  49. $file_stuff_key = "ft_StUfF_key";    # defined in speech tools
  50.  
  51. $host = shift || 'localhost';
  52. $port = shift || 1314;
  53. if ($host eq "-h") {
  54. print STDOUT "
  55.   Usage:
  56.  
  57.      $0 [<server> [<port>]]
  58.  
  59.          OR
  60.  
  61.      $0 -h 
  62.  
  63.   perl client for the Festival text-to-speech server
  64.  
  65.   Lines that do not begin with a ( are treated as text
  66.   to be spoken.  Those that do begin with a ( are assumed
  67.   to Scheme commands to festival and are sent without
  68.   alteration.  Use `quit' or `exit' to quit.
  69.  
  70.   Note that a server must be started before the client
  71.   will work.
  72.  
  73.   Use the `-h' (help) option for this message.
  74.  
  75. ";
  76.  
  77.     exit(1);
  78. }
  79.  
  80. # create a tcp connection to the specified host and port
  81.  
  82. $handle = IO::Socket::INET->new(Proto     => "tcp",
  83.                 PeerAddr  => $host,
  84.                 PeerPort  => $port)
  85.     or die "
  86.   Can't connect to port $port on $host: $!
  87.   (Are you sure the server is running and accepting connections?)
  88.  
  89. ";
  90.  
  91. $handle->autoflush(1);     # so output gets there right away
  92. print STDERR "[Connected to $host:$port]\n";
  93.  
  94. # tell the server to send us back a 'file' of the right type
  95. print $handle "(Parameter.set 'Wavefiletype '$wave_type)\n";
  96.  
  97. # split the program into two processes, identical twins
  98. die "can't fork: $!" unless defined($kidpid = fork());
  99.  
  100. # the if{} block runs only in the parent process
  101. if ($kidpid) {
  102.     # the parent handles the input so it can exit on quit
  103.  
  104.     while (defined ($line = <STDIN>)) {
  105.     last if ($line =~ /^(quit|exit)$/);
  106.  
  107.     if ($line =~ /^\(/) {
  108.         # just send it wholesale if it's a ( )
  109.         print $handle $line;
  110.     } else {
  111.         # otherwise assume it's text to be spoken
  112.         chomp $line;
  113.         print $handle "(tts_textall \"$line\" 'file)\n";
  114.     }
  115.     }
  116.     kill("TERM", $kidpid);# send SIGTERM to child
  117. }
  118. # the else{} block runs only in the child process
  119. else {
  120.     # the child is forked off to get the results from the server
  121.     undef $line;
  122.     while (($line = $remains) || defined ($line = <$handle>)) {
  123.     undef $remains;
  124.     if ($line eq "WV\n") { # we have a waveform coming
  125.         undef $result;
  126.         if ($audio_file) {
  127.         open(AUDIO, ">$audio_file");
  128.         } else {
  129.         open(AUDIO, "| $audio_command");
  130.         }
  131.         while ($line = <$handle>) {
  132.         if ($line =~ s/$file_stuff_key(.*)$//s) {
  133.             $remains = $1;
  134.             print AUDIO $line;
  135.             last;
  136.         }
  137.         print AUDIO $line;
  138.         }
  139.         close AUDIO;
  140.  
  141.         if ($audio_file) {
  142.         # call the command if we weren't piping
  143.         system("$audio_command $audio_file");
  144.         
  145.         # remove the evidence
  146.         unlink($audio_file);
  147.             }
  148.     } elsif ($line eq "LP\n") {
  149.         while ($line = <$handle>) {
  150.         if ($line =~ s/$file_stuff_key(.*)$//s) {
  151.             $remains = $1;
  152.             print STDOUT $line;
  153.             last;
  154.         }
  155.         print STDOUT $line;
  156.         }
  157.     } else {
  158.         # if we don't recognize it, echo it
  159.         print STDOUT $line;
  160.     }
  161.     }     
  162. }
  163.