Using cepstral webbased voice synthesized demo to generate voice prompts for asterisk on the fly
Prerequisites
- Time
- Asterisk
- Internet Connectivity
- PHP (in this example we use version 5.0.4)
Introduction
Why?
I personaly wanted to be able to generate occasional voice prompts dynamicaly without the need for recording on my home PC and uploading via FTP and then moving to the correct directory on my dedicated server.Getting Started
# mkdir /etc/asterisk/php/
# cd /etc/asterisk/php/
# nano speech.php
Paste into nano
<?php
$args = $_SERVER["argv"];
$i = 0;
while ($i<sizeof($args))
{
if ($i>0)
{
$arg[$i-2] = $args[$i];
}
$i++;
}
$text=urlencode(implode(" ",$arg));
system('rm /var/lib/asterisk/sounds/tikka.gsm');
system('/usr/bin/wget -O /tmp/tikka.wav "http://qs913.pair.com/cepstral/demos/demo.cgi/cepstral.wav?content='.$text.'&voice=Lawrence&rate=160&pitch=1&effect=none&submit=Synthesize+the+Text"');
?>
The above code will take commandline parameters passed to it from asterisk when we call it from extentions.conf.
Press CTRL + X to close nano, it will ask if you wish to save press y for yes and then press enter to save our file with the name speech.php.
Another file that we will be creating is time.php, this is based on the same file above but generates a fun time announcement. (UK Residents will recognise this hopefuly ;))
# nano time.php
Paste into nano
<?php
$today = date("h:i:s");
$text = "At the third stroke, the time sponsored by accurist will be, ";
$text .= date("'h'") . "" . date("'i'") . " and " . date("'s'") . " seconds BEEP BEEP BEEP";
system('rm /var/lib/asterisk/sounds/time.gsm');
system('/usr/bin/wget -O /tmp/time.wav "http://qs913.pair.com/cepstral/demos/demo.cgi/cepstral.wav?content='.$text.'&voice=Lawrence&rate=160&pitch=1&effect=none&submit=Synthesize+the+Text"');
?>
# cd ..
# nano extentions.conf
Paste into extentions.conf
[inbound]
exten => _X.,1,Answer
exten => _X.,2,System(php -q /etc/asterisk/php/speech.php Please Wait.) ; tells caller to "please wait"
exten => _X.,3,System(sox /tmp/tikka.wav -r 8000 -c 1 -s -w /var/lib/asterisk/sounds/tikka.gsm resample -ql) ; using sox (sox.sourceforge.net) encode our new wave file downloaded from cepstral website to a format more comfortable to asterisk for sending to our caller.
exten => _X.,4,Playback(tikka) ; playback the file tikka(.gsm) contained in /var/lib/asterisk/sounds/
exten => _X.,5,Wait,1
exten => _X.,6,System(php -q /etc/asterisk/php/time.php) ; this is our time generation script
exten => _X.,7,System(sox /tmp/time.wav -r 8000 -c 1 -s -w /var/lib/asterisk/sounds/time.gsm resample -ql)
exten => _X.,8,Wait,1
exten => _X.,9,Playback(time)
exten => _X.,10,Wait,5
Breakdown of PHP Script
<?php //begin php code
$text = "At the third stroke, the time sponsored by accurist will be, "; // set our intro message
$text .= date("'h'") . "" . date("'i'") . " and " . date("'s'") . " seconds BEEP BEEP BEEP"; // get hour, minute and seconds and complete our message sentance.
system('rm /var/lib/asterisk/sounds/time.gsm'); // delete the old time.gsm file
system('/usr/bin/wget -O /tmp/time.wav "http://qs913.pair.com/cepstral/demos/demo.cgi/cepstral.wav?content='.$text.'&voice=Lawrence&rate=160&pitch=1&effect=none&submit=Synthesize+the+Text"'); // using wget, grab the wave file generated by the cepstral demo website and save it to /tmp/ with the filename time.wav
?> // close script tag
Save your new time.php script, reload extentions.conf in asterisk.
Ring your asterisk box.. enjoy!
For more scripts in the future checkout my website http://www.tikka-d.co.uk (currently no asterisk information however).
Page Changes
A few modifications and a bugfix.
Thanks for your scripting - it works quite well, even with the time delay to collect the file from the web. I have modified your script to acomplish a few little extras.
a) their website now uses session ID's, and if you don't pass a valid SID, you just get back a "session expired" message.
b) I have changed the output filetype to .sln - aparrently this is asterisk's preferred format these days (not sure)
c) It now reads the text from a file, which allows me to dynamically create messages without rewriting the extensions.conf file each time
d) added -r to wget so it just overwrites any old .wav files that have been downloaded with the same name.
I will be using this to call me when an alarm goes off in the server room... We are using IPmonitor and MOM to email when there is a problem with a server. My next task is to monitor a spool folder and mangle sendmail or procmail so it only passes (and therefore speaks) the important parts of the emails.
usage for this script is:
php speakfile.php sometextfile.txt outputmessage
so in extensions.conf you can use something like:
exten => _X.,2,System(php -q /etc/asterisk/php/speakfile /var/spool/asterisk/sendmailconnector/test.txt outputmessage)
exten => _x.,2,Background(outputmessage);
<?php
$args = $_SERVER"argv";
// read the filename base as the second argument to the script
$filebase=$args2;
// read the textfile contents as the first arguement to the script
// print to screen during debug
//echo file_get_contents("$args1");
$text=urlencode(implode(" ",file("$args1")));
//remove newlines - I'm not sure how cepstral will deal with these.
$remove_nl = array("%0A" => "");
$speechstring=strtr($text,$remove_nl);
// remove the old .sln file, so if it doesn't work at least asterisk can't present incorrect speech
system('rm /var/lib/asterisk/sounds/'.$filebase.'.sln');
// get the session ID
$sid=system('/usr/bin/lynx -source http://qs913.pair.com/cepstral/demos/demo.cgi/cepstral.wav |grep sid |cut -b 43-106');
system('/usr/bin/wget -r -O /tmp/'.$filebase.'.wav "http://qs913.pair.com/cepstral/demos/demo.cgi/cepstral.wav?content='.$speechstring.'&voice=Diane&rate=160&pitch=1&effect=none&submit=Synthesize+the+Text&sid='.$sid.'"');
system('/usr/bin/sox /tmp/'.$filebase.'.wav -t raw -r 8000 -c 1 -s -w /var/lib/asterisk/sounds/p2voice/'.$filebase.'.sln');
?>
My ToDo list is also to incorporate a lockfile, so if one process is currently generating files, another cannot start the same process.
Thanks for your work
Tom :-)