OS X native text-to-speech with Asterisk

It is possible to create an AGI script that uses the native text-to-speech services in Mac OS X to play phrases within the PBX.

Create an AGI shell script called "osx_say.agi" in the Asterisk agi-bin directory. Be sure that the file is flagged as executable using "chmod 755 osx_say.agi".


FILENAME=`echo "${PHRASE}" | md5`

if [ ! -f /tmp/${FILENAME}.ulaw ]; then
	say -o /tmp/${FILENAME}.aiff "$PHRASE"
	afconvert -f "NeXT" -d 'ulaw@8196' /tmp/${FILENAME}.aiff /tmp/${FILENAME}.tmp
	dd ibs=1 skip=28 if=/tmp/${FILENAME}.tmp of=/tmp/${FILENAME}.ulaw
	rm /tmp/${FILENAME}.tmp
	rm /tmp/${FILENAME}.aiff

if [ "$2" ]
		echo "SET VARIABLE ${2} /tmp/${FILENAME} \"\"\n";
		print "\n";
		echo "EXEC PLAYBACK /tmp/${FILENAME} \"\"\n";

The script is configured to use the default voice, which is fairly good in OS X 10.5 Leopard or later. It also caches phrases based on their MD5 hash, so repeated sounds will not need to be regenerated very often.

Invoking the script from a dialplan is fairly straightforward:
exten => 1000,1,AGI(osx_say.agi, "Welcome to Asterisk.")

Instead of immediately calling Playback, the script can set a variable to the audio file path. This is useful for integrating the sound with other scripts or modules, such as with Background. The variable name should be specified as the second parameter.

exten => 1000,1,AGI(osx_say.agi, "Welcome to Asterisk.", PHRASE_VAR)
exten => 1000,2,Background(${PHRASE_VAR})

Created by: dustinwenz, Last modification: Thu 06 of May, 2010 (16:17 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+