FreeSWITCH对接微软edge-tts(mod_tts_commandline使用)

FreeSWITCH对接asr/tts目前最简单且容易上手的还是mrcp模块,今天介绍另外一种方式来对接微软开源的edge-tts,即mod_tts_commandline模块的使用

mod_tts_commandline模块在编译freeswitch的时候,默认是不安装的。如需使用可以在编译freeswitch的时候修改modules.conf启用该模块;当然freeswitch编译后也可以单独再编译mod_tts_commandline模块,编译过程就省略了。

edge-tts:edge的安装此处就省略了,安装完成后,可以通过如下命令生成需要的音频文件

edge-tts --voice zh-CN-YunxiNeural --text "Hello, world!" --write-media hello.mp3

mod_tts_commandline模块安装成功后,在autoload_configs目录下有一个tts_commandline.conf.xml配置文件,主要就是通过这个配置文件完成第三方tts的对接。

#tts_commandline.conf.xml默认配置:

<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:
    http://wiki.freeswitch.org/wiki/Mod_tts_commandline#Example_commands
        -->
        <param name="command" value="echo ${text} | text2wave -f ${rate} > ${file}"/>
       
    </settings>
</configuration>

mod_tts_commandline工作原理

mod_tts_commandline模块提供{text},{rate},{voice},{file} 这4个tts相关变量,然后允许你通过这4个变量,去调用第三方tts(调用方式可以是脚本也可以是命令等方式),最终是在tmp目录下生成一个音频文件,freeswitch再通过speak把这个音频文件播放出来。

从这个流程可以看出,mod_tts_commandline模块调用tts不是直接通过流式的方式得到语音流在freeswitch中播放,而是通过调用tts得到一个音频文件,然后拿着音频文件在freeswitch中播放,相比mrcp来说,耗时会明显有增加,但是网络稳定的情况下,也是可以进行一些尝试的。

具体案例:

通过mod_tts_commandline调用edge-tts:

第一步:我们在tts_commandline.conf.xml文件中,将settings下的默认command删除,然后在里面加入自己需要执行的脚本或指令(脚本的类型是不限的,shell,python,go等等都是可以的),将自己需要播放的内容和音频文件传参给脚本或指令。

#在该文件中,我们调用了一个shell脚本,脚本内容其实就是去执行edge-tts命令,得到一个音频文件,freeswitch会通过commandline模块去播放这个音频文件。

这里需要注意,edge-tts生成的音频文件是mp3格式,而commandline模块默认只支持wav格式的音频文件,需要在脚本里通过ffmpeg将mp3格式转成wav格式;也可以尝试修改commandline源码,commandline源码应该是写死了wav,freeswitch结合shout模块其实是可以播放mp3格式的音频文件的。

<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:
    http://wiki.freeswitch.org/wiki/Mod_tts_commandline#Example_commands
        -->
        <!--<param name="command" value="echo ${text} | text2wave -f ${rate} > ${file}"/>-->
        <param name="command" value="sh tts_commandline.sh ${text} ${voice} ${file}"/>
    </settings>
</configuration>

第二步:

在freeswitch的拨号规则或脚本中调用mod_tts_commandline,以lua脚本为例,写一个test.lua脚本,内容如下:

session:answer()
session:sleep(3000)
session:set_tts_params("tts_commandline", "zh-CN-YunxiNeural");
session:speak("今天天气不错啊");
session:hangup()

第三步:

外呼测试

其实对接其他非mrcp方式的tts(比如阿里的百炼TTS),都可以考虑这种方式的,当然直接写脚本也是可以的。

更多讨论可加QQ群:482489124或联系个人QQ:736984328

  • 28
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值