Upgrade 3CX to v18 and get it hosted free!

OS X native text-to-speech with Asterisk

Author image

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”.


#!/bin/bash

PHRASE=$1
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
fi

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

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})

Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.
Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.