linux c++ 播放baidu-aip生成的语音

语音播放有类似pyttsx这种包,可将文本直接进行语音播放,但试了一下感觉效果不是很好。

最后还是推荐使用baidu-aip,对中文的支持比较好,读出来的语音也比较有情感,支持人物、音速等的设置。

1、先登录https://login.bce.baidu.com/ 注册一个语音应用,记住APP_ID、API_KEY、SECRET_KEY

2、终端安装baidu-aip

pip install baidu-aip     

3、写一个python脚本用于从文本生成mp3文件:

# -*- coding: utf-8 -*

from aip import AipSpeech

APP_ID = 'xx'
API_KEY = 'xx'
SECRET_KEY = 'xx'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result  = client.synthesis('你好', 'zh', 1, {
    'vol': 5,
    'per': 1,
})

if not isinstance(result, dict):
    with open('audio.mp3', 'wb') as f:
        f.write(result)

synthesis函数接口说明文档:https://cloud.baidu.com/doc/SPEECH/s/Gk4nlz8tc

4、用c++播放生成的mp3文件

void function() {
    system("play ../audio/audio.mp3");
}

在播放之前,我们需要安装一个播放声音的软件sox,用于实现在终端下播放声音

sudo apt-get install sox

安装完成后,还需要安装sox的音频支持插件(选择所有格式)

sudo apt-get install libsox-fmt-all

完事儿~

你可能感兴趣的:(linux,cpp,baidu-aip,语音,python)