mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
TTS 百炼平台的CosyVoice 非流式
非流式 支持默认音色 支持百炼cosy v1 v2 自定义复刻的音色
This commit is contained in:
parent
9f8ae62ebb
commit
3ea84212d4
1 changed files with 66 additions and 0 deletions
66
main/xiaozhi-server/core/providers/tts/bailian.py
Normal file
66
main/xiaozhi-server/core/providers/tts/bailian.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
import uuid
|
||||||
|
import json
|
||||||
|
import hmac
|
||||||
|
import hashlib
|
||||||
|
import base64
|
||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
from core.providers.tts.base import TTSProviderBase
|
||||||
|
from config.logger import setup_logging
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
from urllib import parse
|
||||||
|
|
||||||
|
from dashscope.audio.tts_v2 import SpeechSynthesizer, AudioFormat
|
||||||
|
import dashscope
|
||||||
|
import os
|
||||||
|
|
||||||
|
TAG = __name__
|
||||||
|
logger = setup_logging()
|
||||||
|
|
||||||
|
|
||||||
|
class TTSProvider(TTSProviderBase):
|
||||||
|
|
||||||
|
def __init__(self, config, delete_audio_file):
|
||||||
|
super().__init__(config, delete_audio_file)
|
||||||
|
|
||||||
|
|
||||||
|
self.api_key = config.get("api_key")
|
||||||
|
self.voice = config.get("private_voice")
|
||||||
|
if self.voice.startswith("cosyvoice-v3-"):
|
||||||
|
self.model = "cosyvoice-v3"
|
||||||
|
elif self.voice.startswith("cosyvoice-v2-"):
|
||||||
|
self.model = "cosyvoice-v2"
|
||||||
|
else:
|
||||||
|
self.model = "cosyvoice-v1"
|
||||||
|
|
||||||
|
def generate_filename(self, extension=".wav"):
|
||||||
|
return os.path.join(self.output_file,
|
||||||
|
f"tts-{__name__}{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||||
|
|
||||||
|
async def text_to_speak(self, text, output_file):
|
||||||
|
try:
|
||||||
|
# 设置 DashScope 的 API 密钥
|
||||||
|
dashscope.api_key = self.api_key
|
||||||
|
|
||||||
|
# 创建语音合成器实例,指定模型、声音和音频格式
|
||||||
|
synthesizer = SpeechSynthesizer(
|
||||||
|
model=self.model,
|
||||||
|
voice=self.voice,
|
||||||
|
format=AudioFormat.WAV_16000HZ_MONO_16BIT,
|
||||||
|
# callback=callback
|
||||||
|
)
|
||||||
|
|
||||||
|
# 调用语音合成接口,获取音频数据
|
||||||
|
audiodata = synthesizer.call(text)
|
||||||
|
|
||||||
|
# 如果指定了输出文件,则将音频数据写入文件,并返回文件路径
|
||||||
|
if output_file:
|
||||||
|
with open(output_file, 'wb') as f:
|
||||||
|
f.write(audiodata)
|
||||||
|
return output_file
|
||||||
|
else:
|
||||||
|
# 否则直接返回音频数据
|
||||||
|
return audiodata
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(f"{__name__} error: {e}")
|
||||||
Loading…
Reference in a new issue