Asterisk cmd Festival
Synopsis
Say text to the callerDescription:
Festival(text,intkeys)Uses the Festival open-source speech synthesizer (which you need to have installed) to generate the specified text as a sound stream. The intkeys parameter is optional. If present, it specifies the buttons that, if dialed by the caller, will cause the playing of the sound stream to stop, and for Festival returns the button pressed. If you specify the intkeys parameter as "any", then Festival will return at the first button dialed, regardless of what it is. Please note that the intkeys parameter has to be enclosed by single quotes (') and there must not be a space between text, the comma and intkeys (see example).
The text parameter should be enclosed in quotation marks if there are any spaces.
Festival is not an application that will automatically answer the channel - so you need to do it yourself.
Example
exten => 003,1,Answerexten => 003,2,Festival('Hello asterisk user, how are you today?') ; <-- note the quotes ...
exten => 003,3,Festival('And an example the user can stop by pressing any key','any') ; note quotes and no spaces
Remember to Answer the channel before using the Festival command.
Tip
You may get better results using the System command to access Festival's text2wave program to generate a .WAV file, and then use the Background or Playback commands, rather than using Asterisk's Festival command.% text2wave -o myfile.ulaw -otype ulaw
Will take text from stdin and make a sound file of the text in ulaw format. You can simply use Playback(myfile) to play it through asterisk.
If you have problems with Festival seeming to drop some of the text, this may be for you. Also, if you have a lot of intensive processing (this in itself is intensive processing) you may want to spin this off into the background, and then play music or instructions for your user in the meantime. For example:
exten => s, 1, System(do_stuff &)
exten => s, 2, Playback(introduction-instructions)
exten => s, 3, Playback(my_new_sound)
***** do_stuff *****
#!/usr/bin/php
<?php
// Take care of all your processing.
$data = "A lot of text or something here.";
system("echo $data | text2wave -o my_new_sound.ulaw -otype ulaw -");
?>
Something like that will allow you to do a bunch of heavy processing without forcing the user to wait on you.
See also
- Festival: Festival open source speech synthesis software
- Installing Festival for Asterisk
- Festival Configuration
- Configuration
- The Dialplan - extensions.conf
Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ

Comments
333test version of app_festival.c
the data from the Festival server as pure samples even though there is a 1024 byte (minimum but typical) header. It tries to use incoming
voice data packets to clock out its own audio but does not take into account queued packets. This can result in the beginning (or all) of a
synthesized phrase being lost. I initially managed to make the clocking scheme work, but I couldn't find any other application that worked
that way and I was concerned that it could fail in the presence of VAD. So I changed the clocking to use real time as other applications do.
A version of app_festival.c with these modifications is temporarily available here:
ftp://ftp.danlan.com/ftp.danlan.com/homeauto/app_festival.c
If it is useful I might try to get the changes into the distribution. Also, here is the command I use rather than modifying Festival itself:
festivalcommand=(begin (voice_mwm_diphone) (utt.send.wave.client (utt.wave.resample (utt.wave.rescale (utt.synth (Utterance Text "%s")) 5) 8000)))(quit)\n
333Problem with Festival and text2wave
PD: Festival is not running as a daemon.
333Another php snippet to handel festival
in dialplan:
exten => s,n,AGI(say.php|Welcome\, bla bla bla)
in say.php:
- !/usr/bin/php -q
<?php$tts_dir = "/var/lib/asterisk/sounds/tts";
set_error_handler('file_errors');
//$input = fread(STDIN, 1024);
if ($_SERVERargv>'argv'1){
$tmp_name = md5($_SERVERargv>'argv'1);
if (!(file_exists($tts_dir."/".$tmp_name.".wav"))){
$cmd_txt2wave = "echo ".$_SERVERargv>'argv'1."| text2wave -o ".$tts_dir."/".$tmp_name.".wav";
exec($cmd_txt2wave);
$cmd_sox = "sox ".$tts_dir."/".$tmp_name.".wav -r 8000 -c 1 ".$tts_dir."/".$tmp_name.".gsm resample -ql";
exec($cmd_sox);
//we'll leave the gsm file as a cache
unlink($tts_dir."/".$tmp_name.".wav");
}
fputs(STDOUT,"EXEC BACKGROUND \"tts/".$tmp_name."\"");
}
function file_errors($errno, $errmsg, $filename, $linenum, $vars){
$logfile = fopen("//var//log//say.log","a+");
fwrite($logfile, "(".date("Y-M-d").") $errno - $errmsg\r\n");
fclose($logfile);
}
?>
333Change voice for Festival
333Mbrola works also
(hash)!/bin/sh
cat /dev/stdin | \
sed 's/@/ <E4>t /g' | \
pipefilt | \
preproc ???/preproc/Rules.lst ???/preproc/Hadifix.abk | \
txt2pho -m | \
mbrola -f 0.8 -t0.8 -l 15000 ????/de2/de2 - -.au > /tmp/asterisk.au
sox /tmp/asterisk.au -r 8000 -c 1 /var/lib/asterisk/sounds/tts.gsm<br />
rm /tmp/asterisk.au
Exten:
exten => 31,1,Answer
exten => 31,2,system(echo \'Welcome to the wonderful world of Asterisk! Your phone number is ${CALLERIDNUM}.\' | ???/asterisk.sh)
exten => 31,3,wait(1)
exten => 31,4,Playback(tts)
exten => 31,5,Hangup
333text2wave php script
i made a php script... you could also make it in perl or python or whatever you like...
/scripts/fest.php:
<?php
$dir="/var/lib/asterisk/sounds/";
$textfilename="newfile.txt";
$textfile=$dir.$textfilename;
if (file_exists($textfile)){
unlink ($textfile);
}
touch($textfile);
$tfile=fopen($textfile, "w");
//echo $argv1;
fwrite($tfile, $argv1);
fclose($tfile);
if(file_exists($dir."newsound.gsm")){
unlink ($dir."newsound.gsm");
}
system("text2wave -f 8000 -o ".$dir."newsound.wav ".$textfile);
system("sox ".$dir."newsound.wav -r 8000 -c 1 ".$dir."newsound.gsm");
unlink($dir."newsound.wav");
?>
...and create an extension for testing...
exten => 31,1,Answer
exten => 31,2,system(/scripts/fest.php \'Welcome to the wonderful world of Asterisk! Your phone number is ${CALLERIDNUM}.\')
exten => 31,3,wait(1)
exten => 31,4,Playback(newsound)
exten => 31,5,Hangup
the script should have execute rights and the asterisk sounds directory should be writable by the script... have fun!
333weather forecasts
create a cron job e.g. /etc/cron.daily/weather
add to it something like this:
wget -O - ftp://tgftp.nws.noaa.gov/data/forecasts/city/pa/pittsburgh.txt | \
tail --lines=+3 | text2wave -f 8000 -o /usr/share/asterisk/sounds/my-weathe\
r.wav
then in extensions.conf
; 091 for the weather
exten => 091,1,Answer
exten => 091,2,Playback(my-weather)
exten => 091,3,Hangup
333Example of text2wave
Thanks!
333How can i change the language in asterisk?