Text to Speech - Revisited

For those who are interested I have made another TTS test program.
A simple conversion of a message to Speech, and then dial a number and play the message.

I used a simple JScript TTS converison before Christmas.
But now I have revisited it and converted to PHP
Much simpler in my view.

Here is the basic code for a text to speech conversion with an auto dial out afterwards.

First you will need to install the AWS PHP SDK


Here is the code

// Text to Speech test

defined(’_JEXEC’) or die(‘Restricted access’);

// point to the PHP SDK helper
require ‘ttsaws/aws-autoloader.php’;

Variables passed from upper program.
$fnnto = isset($fnnto) ? $fnnto : ‘12345678’;
$message = isset($message) ? $message : ‘Hello World’;

$awsAccessKeyId = ‘Your Access key goes here’;
$awsSecretKey = ‘Your Secret Key goes here’;

// set the credentials
$credentials = new Aws\Credentials\Credentials($awsAccessKeyId, $awsSecretKey);

// create request
$client = new Aws\Polly\PollyClient([
‘version’ => ‘2016-06-10’,
‘credentials’ => $credentials,
‘region’ => ‘us-east-1’,

// set output type and Voice
$result = $client->synthesizeSpeech([
‘OutputFormat’ => ‘mp3’,
‘Text’ => $message,
‘TextType’ => ‘text’,
‘VoiceId’ => ‘Russell’,

$resultData = $result->get(‘AudioStream’)->getContents();



$local_mp3_file = $filePath . $fileName;

// write it out
file_put_contents($local_mp3_file, $resultData); // write mp3 file content to file

// That is all that is required to create the MP3 file

// now create the outbound call

// I am using a form from Joomla. Use straight php if required
jimport( ‘joomla.filesystem.file’ );

// remove previous call file if there
$myFile = “ttsout/testcall.call”;
unlink($myFile) or die(“Couldn’t delete file”);

$strc=“Channel: SIP/Your-SIP-Trunk-Name-goes-here/”;
$str2=“Callerid: 777888999”;
$str3=“MaxRetries: 5\n”;
$str4=“RetryTime: 300\n”;
$str5=“WaitTime: 45\n”;
$str6=“Context: outboundmsg1\n”;
$str7=“Extension: s\n”;
$str8=“Priority: 1\n”;

$output1 = $str1.$str2.$str3.$str4.$str5.$str6.$str7.$str8;


// create the file
$status = Jfile::write($file, $output1, $use_streams=false);

// wait to make sure it is flushed out to disk

// output to outgoing calls so Asterisk will action it

copy(‘ttsout/testcall.call’, ‘ttsout/outgoing/testcall.call’);

You will also need a context in Freepbx called outboundmsg1
See above code

In FreePBX GUI goto Config edit
Edit extensions_custom.conf
add this code

exten => s,1,Set(TIMEOUT(digit)=5) ; Set Digit Timeout to 5 seconds
exten => s,2,Set(TIMEOUT(response)=10) ; Set Response Timeout to 10 seconds
exten => s,3,Answer
exten => s,4,Wait(1)
exten => s,5,Background(/var/spool/asterisk/outboundmsgs/call12345678) ;"play outbound msg"
exten => t,2,Hangup

That’s All Folks.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.