Skip to content

Instantly share code, notes, and snippets.

@majenkotech
Last active July 27, 2022 23:25
Show Gist options
  • Save majenkotech/7b55c0396dfc77289db84628d2851e3f to your computer and use it in GitHub Desktop.
Save majenkotech/7b55c0396dfc77289db84628d2851e3f to your computer and use it in GitHub Desktop.
Asterisk TTS sender

This setup will create an audio file from some text, call a SIP extension, then play the converted text.

First you need to install and set up asterisk.

$ sudo apt install asterisk

Now for the configuration.

First create your SIP client connection. Look for the /etc/asterisk/sip.conf file and add to the end of it:

[100]
type=friend
secret=100
host=dynamic

Now set up the extensions. Find /etc/asterisk/extensions.conf and find the [public] section. Delete the existing content of that section (should be just an include of demo or something) and replace it with:

exten => 100,1,Dial(SIP/100)

Immediately under that add a new section like this:

[textmessage]
exten => s,1,Answer
exten => s,n,Wait(1)
exten => s,n,Playback(text/message)
exten => s,n,Wait(1)
exten => s,n,Playback(text/message)
exten => s,n,Wait(1)
exten => s,n,Playback(text/message)
exten => s,n,Wait(1)
exten => s,n,Hangup

I'm not sure if this bit is needed, but I have it. Find /etc/asterisk/acl.conf and create a new section at the top:

[open]
permit=0.0.0.0/0.0.0.0

Enable and start asterisk:

$ sudo systemctl enable asterisk
$ sudo systemctl start asterisk

And configure the SIP phone interface to connect to the asterisk machine with username 100 and password 100. You can test if the connection has worked by running:

$ asterisk -r
foo*CLI> sip show peers
Name/username             Host                                    Dyn Forcerport Comedia    ACL Port     Status      Description                      
100/100                   192.168.0.191                            D  Auto (No)  No             55214    Unmonitored                                  

As long as Host and Port show valid information it's connected OK.

Now for generating the call.

You will need festival, a better voice, and sox:

$ sudo apt install festival festvox-us-slt-hts sox

Then this script should make a call:

#!/usr/bin/env bash

echo "$1" | text2wave -eval '(voice_cmu_us_slt_arctic_hts)' > /tmp/tts-$$.wav
sox /tmp/tts-$$.wav -r 8000 /usr/share/asterisk/sounds/text/message.gsm
rm /tmp/tts-$$.wav

cat << EOF > /var/spool/asterisk/outgoing/$$.call
Channel: SIP/100
Callerid: 100
MaxRetries: 5
RetryTime: 300
WaitTime: 45
Context: textmessage
Extension: s
Priority: 1
EOF

You might want to tweak the first line as it's just really for testing and unsafe. Remove the echo "$1" | and it will take in text from stdin instead of a command line parameter (which is open to abuse).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment