Example 1:
We set up small script that will be executed from the Asterisk dialplan. This script will take as parameter the text that will be transform to a wav file to later be played.
On extensions.conf configure the following extensions:
exten => 1222,1,Answer()
exten => 1222,n,Set(TEST="Welcome to TTS example")
exten => 1222,n,System(/tmp/test2.sh ${TEST})
exten => 1222,n,Playback(/tmp/test)
exten => 1222,n,Hangup()
On /tmp/test2.sh (add necessary rights to the file) configure the following shell script:
#!/bin/bash
#delete any previous file
/bin/rm -rf /tmp/test.wav
#Convert the text to a wav file
/opt/swift/bin/swift -o /tmp/test.wav -p audio/sampling-rate=8000,audio/channels=1 " $@"
#Don't run sox until swift is done
wait
/usr/bin/sox /tmp/test.wav /tmp/newtest.wav trim 7.5
#copy the old file with the trimmed file
/bin/cp /tmp/newtest.wav /tmp/test.wav
Sox is an application that allows you manipulate/convert audio files from the command line. The page with information about it is http://sox.sourceforge.net/. The installation is quite simple on Debian "apt-get install sox".
The content of the asterisk variable ${TEST} is send as parameters of the shell script. The script uses $1...$n as parameters but you can use $@ to use all the parameters sent. (there must be a better way!! to do this). The following line convert the text into a wav file and save it as /tmp/test.wav.
/opt/swift/bin/swift -o /tmp/test.wav -p audio/sampling-rate=8000,audio/channels=1 " $@"
Once we run it we use sox to trim the first 7.5 seconds of the file that we don't need for this test. This creates a new file named newtest.wav (original no?)
Finally we rename the file to the original name and is later played by asterisk dialplan with the command "exten => 1222,n,Playback(/tmp/test)"
Note this is just a demo/test/proof of concept. For production you should buy some licenses and evaluate the usage of the app_swift from Asterisk to improve this.
Example2:
Using the example below we create a demo "time of the day" service.
The configuration of the extensions.conf file is pretty much the same but we remove the commands to send a text to it.
exten => 1222,1,Answer()
exten => 1222,n,System(/tmp/test2.sh)
exten => 1222,n,Playback(/tmp/test)
exten => 1222,n,Hangup()
The script only has the details to get the different elements of the date from the unix system and concatenate them on a single string to be converted to wav file later by swift. The shell looks like this:
#!/bin/bash
hour=`date +'%l'`
am=`date +'%p'`
minute=`date +'%M'`
day=`date +'%d'`
month=`date +'%B'`
year=`date '+20%y'`
texto="The current time is
#delete any previous file
/bin/rm -rf /tmp/test.wav
#Convert the text to a wav file
/opt/swift/bin/swift -o /tmp/test.wav -p audio/sampling-rate=8000,audio/channels=1 " $texto"
#Don't run sox until swift is done
wait
/usr/bin/sox /tmp/test.wav /tmp/newtest.wav trim 7.5
#copy the old file with the trimmed file
/bin/cp /tmp/newtest.wav /tmp/test.wav
#!/bin/bash
The played TTS can be improved to make it more user friendly.
Other date options:
man strftime
%A is replaced by the locale's full weekday name.
%a is replaced by the locale's abbreviated weekday name.
%B is replaced by the locale's full month name.
%b or %h is replaced by the locale's abbreviated month name.
%C is replaced by the century (a year divided by 100 and truncated to an integer) as a decimal number (00-99).
%c is replaced by the locale's appropriate date and time representation.
%D is replaced by the date in the format ``%m/%d/%y''.
%d is replaced by the day of the month as a decimal number (01-31).
%e is replaced by the day of month as a decimal number (1-31); single digits are preceded by a blank.
%H is replaced by the hour (24-hour clock) as a decimal number (00-23).
%I is replaced by the hour (12-hour clock) as a decimal number (01-12).
%j is replaced by the day of the year as a decimal number (001-366).
%k is replaced by the hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a blank.
%l is replaced by the hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a blank.
%M is replaced by the minute as a decimal number (00-59).
%m is replaced by the month as a decimal number (01-12).
%n is replaced by a newline.
%p is replaced by the locale's equivalent of either ``AM'' or ``PM''.
%R is replaced by the time in the format ``%H:%M''.
%r is replaced by the locale's representation of 12-hour clock time using AM/PM notation.
%T is replaced by the time in the format ``%H:%M:%S''.
%t is replaced by a tab.
%S is replaced by the second as a decimal number (00-60).
%s is replaced by the number of seconds since the Epoch, UCT (seemktime(3)).
%U is replaced by the week number of the year (Sunday as the first day of the week) as a decimal number (00-53).
%u is replaced by the weekday (Monday as the first day of the week) as a decimal number (1-7).
%V is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it is week 1; otherwise it is week 53 of the previous year, and the next week is week 1.
%W is replaced by the week number of the year (Monday as the first day of the week) as a decimal number (00-53).
%w is replaced by the weekday (Sunday as the first day of the week) as a decimal number (0-6).
%X is replaced by the locale's appropriate time representation.
%x is replaced by the locale's appropriate date representation.
%Y is replaced by the year with century as a decimal number.
%y is replaced by the year without century as a decimal number (00-99).
%Z is replaced by the time zone name.
%% is replaced by `%'.
1 comment:
Hello,
i don't know why Exaple 2 doesn't work ???
but example 1 work perfect!!!
Post a Comment