解决百度语音合成的一些问题和参数化设置 (#676)

* 解决百度语音合成的一些问题和参数化设置

* 补充百度语音说明
This commit is contained in:
zhanws
2023-03-30 14:59:52 +08:00
committed by GitHub
parent 83136e3142
commit 06b02f5df8
3 changed files with 98 additions and 16 deletions

55
voice/baidu/README.md Normal file
View File

@@ -0,0 +1,55 @@
## 说明
百度语音识别与合成参数说明
百度语音依赖,经常会出现问题,可能就是缺少依赖:
pip install baidu-aip
pip install pydub
pip install pysilk
还有ffmpeg不同系统安装方式不同
系统中收到的语音文件为mp3格式wx或者sil格式wxy如果要识别需要转换为pcm格式转换后的文件为16k采样率单声道16bit的pcm文件
发送时又需要wx转换为mp3格式转换后的文件为16k采样率单声道16bit的pcm文件,wxy转换为sil格式,还要计算声音长度,发送时需要带上声音长度
这些事情都在audio_convert.py中封装了直接调用即可
参数说明
识别参数
https://ai.baidu.com/ai-doc/SPEECH/Vk38lxily
合成参数
https://ai.baidu.com/ai-doc/SPEECH/Gk38y8lzk
## 使用说明
分两个地方配置
1、对于def voiceToText(self, filename)函数中调用的百度语音识别API,中接口调用asr参数这个配置见CHATGPT-ON-WECHAT工程目录下的`config.json`文件和config.py文件。
参数 可需 描述
app_id 必填 应用的APPID
api_key 必填 应用的APIKey
secret_key 必填 应用的SecretKey
dev_pid 必填 语言选择,填写语言对应的dev_pid值
2、对于def textToVoice(self, text)函数中调用的百度语音合成API,中接口调用synthesis参数在本目录下的`config.json`文件中进行配置。
参数 可需 描述
tex 必填 合成的文本使用UTF-8编码请注意文本长度必须小于1024字节
lan 必填 固定值zh。语言选择,目前只有中英文混合模式填写固定值zh
spd 选填 语速取值0-15默认为5中语速
pit 选填 音调取值0-15默认为5中语调
vol 选填 音量取值0-15默认为5中音量取值为0时为音量最小值并非为无声
per基础音库 选填 度小宇=1度小美=0度逍遥基础=3度丫丫=4
per精品音库 选填 度逍遥(精品)=5003度小鹿=5118度博文=106度小童=110度小萌=111度米朵=103度小娇=5
aue 选填 3为mp3格式(默认) 4为pcm-16k5为pcm-8k6为wav内容同pcm-16k; 注意aue=4或者6是语音识别要求的格式但是音频内容不是语音识别要求的自然人发音所以识别效果会受影响。
关于per参数的说明注意您购买的哪个音库就填写哪个音库的参数否则会报错。如果您购买的是基础音库那么per参数只能填写0到4如果您购买的是精品音库那么per参数只能填写50035118106,110,111,103,5其他的都会报错。
### 配置文件
将文件夹中`config.json.template`复制为`config.json`
``` json
{
"lang": "zh",
"ctp": 1,
"spd": 5,
"pit": 5,
"vol": 5,
"per": 0
}
```

View File

@@ -2,6 +2,8 @@
""" """
baidu voice service baidu voice service
""" """
import json
import os
import time import time
from aip import AipSpeech from aip import AipSpeech
from bridge.reply import Reply, ReplyType from bridge.reply import Reply, ReplyType
@@ -21,29 +23,47 @@ from config import conf
- 1837四川话 - 1837四川话
要使用本模块, 首先到 yuyin.baidu.com 注册一个开发者账号, 要使用本模块, 首先到 yuyin.baidu.com 注册一个开发者账号,
之后创建一个新应用, 然后在应用管理的"查看key"中获得 API Key 和 Secret Key 之后创建一个新应用, 然后在应用管理的"查看key"中获得 API Key 和 Secret Key
填入 config.json 中. 然后在 config.json 中填入这两个值, 以及 app_id, dev_pid
baidu_app_id: '' """
baidu_api_key: ''
baidu_secret_key: ''
baidu_dev_pid: '1536'
"""
class BaiduVoice(Voice): class BaiduVoice(Voice):
APP_ID = conf().get('baidu_app_id')
API_KEY = conf().get('baidu_api_key')
SECRET_KEY = conf().get('baidu_secret_key')
DEV_ID = conf().get('baidu_dev_pid')
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
def __init__(self): def __init__(self):
pass try:
curdir = os.path.dirname(__file__)
config_path = os.path.join(curdir, "config.json")
bconf = None
if not os.path.exists(config_path): #如果没有配置文件,创建本地配置文件
bconf = { "lang": "zh", "ctp": 1, "spd": 5,
"pit": 5, "vol": 5, "per": 0}
with open(config_path, "w") as fw:
json.dump(bconf, fw, indent=4)
else:
with open(config_path, "r") as fr:
bconf = json.load(fr)
self.app_id = conf().get('baidu_app_id')
self.api_key = conf().get('baidu_api_key')
self.secret_key = conf().get('baidu_secret_key')
self.dev_id = conf().get('baidu_dev_pid')
self.lang = bconf["lang"]
self.ctp = bconf["ctp"]
self.spd = bconf["spd"]
self.pit = bconf["pit"]
self.vol = bconf["vol"]
self.per = bconf["per"]
self.client = AipSpeech(self.app_id, self.api_key, self.secret_key)
except Exception as e:
logger.warn("BaiduVoice init failed: %s, ignore " % e)
def voiceToText(self, voice_file): def voiceToText(self, voice_file):
# 识别本地文件 # 识别本地文件
logger.debug('[Baidu] voice file name={}'.format(voice_file)) logger.debug('[Baidu] voice file name={}'.format(voice_file))
pcm = get_pcm_from_wav(voice_file) pcm = get_pcm_from_wav(voice_file)
res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.DEV_ID}) res = self.client.asr(pcm, "pcm", 16000, {"dev_pid": self.dev_id})
if res["err_no"] == 0: if res["err_no"] == 0:
logger.info("百度语音识别到了:{}".format(res["result"])) logger.info("百度语音识别到了:{}".format(res["result"]))
text = "".join(res["result"]) text = "".join(res["result"])
@@ -57,9 +77,8 @@ class BaiduVoice(Voice):
return reply return reply
def textToVoice(self, text): def textToVoice(self, text):
result = self.client.synthesis(text, 'zh', 1, { result = self.client.synthesis(text, self.lang, self.ctp, {
'spd': 5, 'pit': 5, 'vol': 5, 'per': 111 'spd': self.spd, 'pit': self.pit, 'vol': self.vol, 'per': self.per})
})
if not isinstance(result, dict): if not isinstance(result, dict):
fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3' fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
with open(fileName, 'wb') as f: with open(fileName, 'wb') as f:

View File

@@ -0,0 +1,8 @@
{
"lang": "zh",
"ctp": 1,
"spd": 5,
"pit": 5,
"vol": 5,
"per": 0
}