Skip to content

Instantly share code, notes, and snippets.

@jerryOkafor
Forked from andrius/tts.sh
Created November 17, 2020 19:37
Show Gist options
  • Save jerryOkafor/4539f18b1041491e8059bd059024fa08 to your computer and use it in GitHub Desktop.
Save jerryOkafor/4539f18b1041491e8059bd059024fa08 to your computer and use it in GitHub Desktop.
Quick and dirty IVR sounds for your Asterisk PBX
#!/bin/bash
#title : tts.sh
#description : This script will convert text to speech using Google translate API
# and then to Asterisk formats (wav, sln, gsm)
#
#author : Andrius Kairiukstis <[email protected]>
#date : 26 July 2013
#version : 0.1
#usage : bash tts.sh FILENAME VOICE PHRASE
#notes : SOX packages needed:
# sudo apt-get install sox libsox-fmt-mp3 libsox-fmt-gsm libsox-fmt-base
#================================================================================================================
language="en"
filename=$1
if [ "${filename}" != "" ]; then shift; text_phrase=$@; else text_phrase=""; fi
# echo "Filename: ${filename}, and text phrase: ${text_phrase}."
if ( [[ "${filename}" == "" ]] || [[ "${text_phrase}" == "" ]] ); then
echo "Please call this script with two arguments containing file name (without extension) and text phrase to be converted into voice file."
echo "Sample: tts.sh TEST This is a test"
exit
fi
rm -f ${filename}_source.*
rm -f ${filename}.*
# Download an mp3 from Google translate
wget -q -U Mozilla -O ${filename}_source.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=${language}&q=$(echo ${text_phrase} | sed 's#\ #+#g')" > /dev/null 2>&1
source=$(cd $(dirname ${filename}_source.mp3); pwd)/$(basename ${filename}_source.mp3)
absolute_path=$(echo ${source}|sed "s/_source.mp3//")
# Step 2 - convert downloaded mp3 to the Asterisk PBX compatible wav file
sox ${filename}_source.mp3 -t wav -r 8000 -b 16 -e signed-integer -c 1 ${filename}.wav
#sox ${filename}_source.mp3 -t gsm -r 8000 -b 16 -c 1 ${filename}.gsm
sox ${filename}_source.mp3 -t gsm -r 8000 -c 1 ${filename}.gsm
sox ${filename}_source.mp3 -t raw -r 8000 -b 16 -e signed-integer -c 1 ${filename}.sln
# Note, do we really need this format with simple TTS?
sox ${filename}_source.mp3 -t raw -r 16000 -b 16 -e signed-integer -c 1 ${filename}.sln16
# Step 3 - convert files with Asterisk PBX (note, current Linux user should have permissions to run Asterisk
rasterisk -rx "file convert ${absolute_path}.sln16 ${absolute_path}.g722" > /dev/null 2>&1
rasterisk -rx "file convert ${absolute_path}.sln ${absolute_path}.alaw" > /dev/null 2>&1
rasterisk -rx "file convert ${absolute_path}.sln ${absolute_path}.ulaw" > /dev/null 2>&1
rasterisk -rx "file convert ${absolute_path}.sln ${absolute_path}.g729" > /dev/null 2>&1
rasterisk -rx "file convert ${absolute_path}.sln ${absolute_path}.g723" > /dev/null 2>&1
rasterisk -rx "file convert ${absolute_path}.sln ${absolute_path}.ilbc" > /dev/null 2>&1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment