Location>code7788 >text

FreeSWITCH interfaces with http protocol for tts services

Popularity:833 ℃/2024-08-11 18:23:53
  • Supporting Resources Download

I. Self-constructed tts service simulation test environment

This section can be skipped if there is already a tts service for the http protocol.

Here is a simple tts service implementation using pytts3 in a Windows 10 environment.

The overall structure is as follows:

The sample code is as follows:

The full source code is available from the following sources:

Get it by replying 20240811 after following WeChat (chatting about blogging, you can scan the code at the end of the article).
Test url:
http://127.0.0.1:8091/?text='just a test'
The running effect is as follows:

 For more information, please refer to the following article:

/MikeZhang/p/

Compile and configure the mod_tts_commandline module.

1. Compile mod_tts_commandline module

File: freeswitch-1.10.9.-release/

Open or add the following:

asr_tts/mod_tts_commandline

Compile and install:

./ 
#CFLAGS="-O3 -fPIC" ./configure --disable-signalwire (optional)
CFLAGS="-O3 -fPIC" ./configure 
make -j
make install

The key information is described here. For more information on FreeSWITCH source code compilation, please refer to the following article:

/MikeZhang/p/
 

2、Configure mod_tts_commandline module

2.1 Opening the module

First you need to enable the mod_tts_commandline module in the configuration file, file path:
/usr/local/freeswitch/conf/autoload_configs/
Configure the content:

<load module="mod_tts_commandline"/>

2.2 Configuration module

Configuration file path: /usr/local/freeswitch/conf/autoload_configs/tts_commandline.

Configuration example:

<configuration name="tts_commandline.conf" description="TextToSpeech Commandline configuration">
    <settings>
    <!--
    Some variables will be replaced :
    ${text}: input text (quoted)
    ${rate}: sample rate (example: 8000)
    ${voice}: voice_name passed to TTS(quoted)
    ${file}: output file (quoted, including .wav extension)

    Example commands can be found at:
    /confluence/display/FREESWITCH/mod_tts_commandline#mod_tts_commandline-Examplecommands
    -->
    <!--param name="command" value="echo ${text} | text2wave -f ${rate} > ${file}"/-->
    <param name="command" value="/bin/bash /root/test/ ${text} ${file}" />
    </settings>
</configuration>

The contents of the bash script (/root/test/):

#! /bin/bash

content=$1
file=$2
echo $content
echo $file
#wget http://192.168.137.1:8091/?text='just a test' -O /tmp/
wget http://192.168.137.1:8091/?text="$content" -O $file

Description:
fs machine address: 192.168.137.32
tts machine address: 192.168.137.1

Third, test to verify the effect of tts

The tts effect is demonstrated here.

1. Configure the dialing plan

dialplan configuration ():

<extension name="ttsTest1">
<condition field="destination_number" expression="^654321$">
  <action application="log" data="INFO dialed 654321."/>
  <action application="lua" data=""/>
</condition>
</extension>

2. Add lua script

The contents of the lua script (/usr/local/freeswitch/scripts/):

session:answer()
session:setVariable("tts_engine", "tts_commandline")
session:setVariable("tts_voice", "girl")
--session:execute("speak", "just test!")
session:execute("speak", "It's a beautiful day!")

3、Registered extensions dialing verification

Registered extension: 1001
Call number: 654321

The test results are as follows:

A video of the running results is available from the following sources:
Get it by replying 2024081101 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

IV. Module code analysis

module path: freeswitch-1.10.9.-release\src\mod\asr_tts\mod_tts_commandline

Module code file: mod_tts_commandline.c

1. mod_tts_commandline_load function

module entry function, the main thing to do is to initialize the tts resource and bind the callback function.

2、tts_commandline_speech_open function
Used to generate temporary audio file names.

3. tts_commandline_speech_close function
Delete the previously generated temporary audio file.

More module function analysisAvailable from the following sources:

Get it by replying 20240811 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

V. Playing tts audio to a specific channel and fire playback completion event

If you want to build a higher tier application (e.g. a robotics service), you need to provide the ability to play tts sound to a specific channel, and generate an event to notify the caller when the playback is complete, so here's a simple example.

Script name: tts_to_channel.lua

The contents of the document can be obtained from the following sources:

Get it by replying 20240811 after following WeChat (chatting about blogging, you can scan the code at the end of the article).
Use the format:
tts_to_channel.lua {channel_uuid} {text}
Parameter Description:
channel_uuid => The uuid of the channel to be played.
text => text content of the playback
Event Subscription:
/event plain CUSTOM MYTTS_TTS_DONE
Running effects:

Event Effect:

A video of the running results is available from the following sources:

Get it by replying 2024081102 after following WeChat (chatting about blogging, you can scan the code at the end of the article).

VI. Downloading of resources

This article covers the source code and related documentation, which can be obtained from the following sources:

Get it by replying 20240811 after following WeChat (chatting about blogging, you can scan the code at the end of the article).