Skip to content

Commit

Permalink
fix: alibaba
Browse files Browse the repository at this point in the history
  • Loading branch information
dingdangdog committed Apr 11, 2024
1 parent d54ce53 commit ac30370
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,23 @@ Heartbeat monitoring information printing interval.
"zh-CN-YunjieNeural1": "(男)",
"zh-CN-YunyiMultilingualNeural1": "(男)"
}
```
```

### alibaba

已支持阿里巴巴的【[智能语音交互](https://nls-portal.console.aliyun.com/overview)】,目前以测试方式集成,需要每天申请token。

```json
{
"alibaba_appkey":"",
"alibaba_token":"",
"alibaba_model":"xiaoyun",
"alibaba_endpoint":"nls-gateway-cn-shanghai.aliyuncs.com"
}
```

`alibaba` 智能语音交互的配置如上所示,参数介绍:
- `alibaba_appkey`:智能语音交互创建项目后,项目的`appkey`
- `alibaba_token`:目前仅支持通过控制台获取临时token,临时token有效时间为24小时,失效后需要重新获取,获取方式可参考:[阿里云文档](https://help.aliyun.com/zh/isi/getting-started/obtain-an-access-token-in-the-console)
- `alibaba_model`:参考官方文档:[语音合成-接口说明](https://help.aliyun.com/zh/isi/developer-reference/overview-of-speech-synthesis)
- `alibaba_endpoint`:阿里云接口节点,有上海`shanghai`、北京`beijing `、深圳`shenzhen`三个节点,自行修改即可。
3 changes: 2 additions & 1 deletion blivemd-voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
async def main():
init_config()
init_session()
print(f'语音机器人启动成功,启动模式:{MODE}')
try:
await run_single_client()
await run_multi_clients()
Expand Down Expand Up @@ -104,7 +105,7 @@ class MyHandler(blivedm.BaseHandler): # 类变量,将被所有类的实例共

# 心跳监听
def __init__(self):
self.heart = HEART_PRINT
self.heart = 0

def _on_heartbeat(self, client: blivedm.BLiveClient, message: web_models.HeartbeatMessage):
if self.heart == HEART_PRINT:
Expand Down
35 changes: 19 additions & 16 deletions speech_alibaba.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,32 @@ def alibaba_tts_speech(text):
audioSaveFile = 'aliAudio.wav'
format = 'wav'
sampleRate = 16000
if os.path.exists(audioSaveFile):
# 删除文件
os.remove(audioSaveFile)
# GET请求方式

processPOSTRequest(text, audioSaveFile, format, sampleRate)

pygame.mixer.init()
pygame.mixer.music.load(audioSaveFile)
pygame.mixer.music.play()

# 等待音频播放完成
# 循环处理事件,直到音频播放完成并删除文件
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
pygame.time.Clock().tick(5)
pygame.mixer.music.stop() # 停止音频播放
# 关闭文件
pygame.mixer.music.fadeout(200) # 淡出效果,1000毫秒
pygame.mixer.quit()
os.remove(audioSaveFile)


#
# def processGETRequest(text, audioSaveFile, format, sampleRate):
# 采用RFC 3986规范进行urlencode编码。
# textUrlencode = text
# Python 3.x请使用urllib.parse.quote_plus。
# textUrlencode = urllib.parse.quote_plus(textUrlencode)
# textUrlencode = textUrlencode.replace("+", "%20")
# textUrlencode = textUrlencode.replace("*", "%2A")
# textUrlencode = textUrlencode.replace("%7E", "~")
# print('text: ' + textUrlencode)
# 采用RFC 3986规范进行urlencode编码。
# textUrlencode = text
# Python 3.x请使用urllib.parse.quote_plus。
# textUrlencode = urllib.parse.quote_plus(textUrlencode)
# textUrlencode = textUrlencode.replace("+", "%20")
# textUrlencode = textUrlencode.replace("*", "%2A")
# textUrlencode = textUrlencode.replace("%7E", "~")
# print('text: ' + textUrlencode)
# url = f'https://{ENDPOINT}/stream/v1/tts'
# # 设置URL请求参数
# url = url + '?appkey=' + ALIBABA_CLOUD_TTS_APPKEY
Expand Down Expand Up @@ -98,7 +100,7 @@ def processPOSTRequest(text, audioSaveFile, format, sampleRate):
}
# 设置HTTPS Body。
body = {'appkey': ALIBABA_CLOUD_TTS_APPKEY, 'token': ALIBABA_CLOUD_TTS_TOKEN, 'text': text, 'format': format,
'sample_rate': sampleRate}
'voice': VOICE, 'sample_rate': sampleRate}
body = json.dumps(body)
# print('The POST request body content: ' + body)
# Python 2.x请使用httplib。
Expand All @@ -121,6 +123,7 @@ def processPOSTRequest(text, audioSaveFile, format, sampleRate):
print('The POST request failed: ' + str(body))
conn.close()


if __name__ == '__main__':
init_alibaba_config()
alibaba_tts_speech("你好,alibaba!")

0 comments on commit ac30370

Please sign in to comment.