#!/usr/bin/perl -w # # Speaks the text in $ARGV[0]. # Caches text-to-speech conversions as 8-kHz .WAV files. use Asterisk::AGI; use File::Basename; use Digest::MD5 qw(md5_hex); use strict; # set up communications w/ Asterisk my $agi = new Asterisk::AGI; $agi->ReadParse(); # figure out the WAV file to use based on a hash of the text to say my ($text_to_say) = @ARGV; my $hash = md5_hex($text_to_say); my $sounddir = "/var/lib/asterisk/sounds"; my $wavefile = "$sounddir/say-text-$hash.wav"; # unless we have already cached this text-to-speed conversion, # create the WAV file using Festival's text2wav (expensive) unless (-f $wavefile) { require File::Temp; my (undef, $tmpfile) = File::Temp::tempfile(DIR => $sounddir); my $pid = open my $pipe, "|-"; die "can't fork: $!" unless (defined $pid); if (!$pid) { # child open STDOUT, ">$tmpfile" or die "can't redir to $tmpfile: $!"; exec qw( text2wave -F 8000 - ); # text->speech conv; 8-kHz WAV output die "exec in child failed: $!"; } else { # parent print $pipe $text_to_say; close $pipe; waitpid $pid, 0; # wait until text->speech conv. is done rename $tmpfile, $wavefile or die "can't rename $tmpfile to $wavefile: $!"; } } # stream the WAV file down the phone line $agi->stream_file(basename($wavefile, ".wav"));