Asterisk AGI script that makes use of Microsoft Translator text to speech service.
Switch branches/tags


      Microsoft TTS script for Asterisk

This script makes use of Microsoft Translator text to speech service
in order to redner text to speech and play it back to the user.
It supports a variety of different languages.

Perl         The Perl Programming Language
perl-libwww  The World-Wide Web library for Perl
perl-Crypt-SSLeay or perl-IO-Socket-SSL for use of SSL/TLS encryption
sox          Sound eXchange, sound processing program
mpg123       MPEG Audio Player and decoder
Internet access in order to contact MS and get the voice data.

To use this script you have to subscribe to the Microsoft Translator API
on Azure Marketplace and register your application:

To install copy mstts.agi to your agi-bin directory.
Usually this is /var/lib/asterisk/agi-bin/
To make sure check your /etc/asterisk/asterisk.conf file

agi(mstts.agi,"text",[language],[intkey],[speed]): This will invoke the Microsoft TTS
engine, render the text string to speech and play it back to the user.
If 'intkey' is set the script will wait for user input. Any given interrupt keys will
cause the playback to immediately terminate and the dialplan to proceed to the
matching extension (this is mainly for use in IVR, see README for examples).
If 'speed' is set the speech rate is altered by that factor.

The script contacts MS TTS service in order to get the voice data
which then stores in a local cache (by default /tmp/) for future use.
Parameters like default language, enabling or disabling caching and cache dir
can be set up by editing the script.

sample dialplan code for your extensions.conf

;PLayback messages to user

exten => 1234,1,Answer()
    ;;Play mesage in English:
exten => 1234,n,agi(mstts.agi,"This is a simple microsoft text to speech test in english.",en)
    ;;Play message in Spanish
exten => 1234,n,agi(mstts.agi,"Esta es una simple prueba en español.",es)
    ;;Play message in Japanese
exten => 1234,n,agi(mstts.agi,"これは、日本の簡単なテストです。良い一日を。",ja)

;A simple dynamic IVR using MSTTS

exten => s,1,Answer()
exten => s,n,Set(TIMEOUT(digit)=5)
exten => s,n,agi(mstts.agi,"Welcome to my small interactive voice response menu.",en)
    ;;Wait for digit:
exten => s,n(start),agi(mstts.agi,"Please dial a digit.",en,any)
exten => s,n,WaitExten()

    ;;PLayback the name of the digit and wait for another one:
exten => _X,1,agi(mstts.agi,"You just pressed ${EXTEN}. Try another one please.",en,any)
exten => _X,n,WaitExten()

exten => i,1,agi(mstts.agi,"Invalid extension.",en)
exten => i,n,goto(s,start)

exten => t,1,agi(mstts.agi,"Request timed out.",en)
exten => t,n,goto(s,start)

exten => h,1,Hangup()

Supported Languages
ca,     ca-es,    da,    da-dk,
de,     de-de,    en,    en-au,
en-ca,  en-gb,    en-in, en-us,
es,     es-es,    es-mx, fi,
fi-fi,  fr,       fr-ca, fr-fr,
it,     it-it,    ja,    ja-jp,
ko,     ko-kr,    nb-no, nl,
nl-nl,  no,       pl,    pl-pl,
pt,     pt-br,    pt-pt, ru,
ru-ru,  sv,       sv-se, zh-chs,
zh-cht, zh-cn,    zh-hk, zh-tw

Security Considerations
This script contacts MS servers in order to get speech data.
The script uses TLS to encrypt all the traffic between your pbx and MS servers
so no 3rd party can eavesdrop your communication.

Tiny version
The '-tiny' suffixed scripts use the HTTP::Tiny perl module instead of LWP::UserAgent.
This makes them a lot faster when run in small embedded systems or boards like
the Raspberry pi. They can be used as an in-place replacement of the normal versions
of the TTS scripts and expose the same interface/cli args. To use them just make sure
you have HTTP::Tiny installed.
In debian or ubuntu based distros: 'apt-get install libhttp-tiny-perl'
In distros that don't have it in their repos: 'cpan HTTP::Tiny'

The MStts script for asterisk is distributed under the GNU General Public
License v2. See COPYING for details.


API Description


 Required. If the Authorization header is used, leave the appid field empty else
 a string containing "Bearer" + " " + access token.

 Required. A string containing a sentence or sentences of the specified language to be
 spoken for the wave stream.

 Required. A string representing the supported language code to speak the text in.
 The code must be present in the list of codes returned from the method GetLanguagesForSpeak.

 Optional. A string specifying the content-type ID. Currently, "audio/wav" and
 "audio/mp3" are available. The default value is "audio/wav".

 Optional. A string specifying the quality of the audio signals. Currently, "MaxQuality"
 and "MinSize" are available. With "MaxQuality", you can get the voice(s) with the highest
 quality, and with "MinSize", you can get the voices with the smallest size. If no value
 is provided, we default to "MinSize".

Returns a wave or mp3 stream of the passed-in text being spoken in the desired language.