mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
更新paddlespeechtts供应器
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
@@ -9,6 +8,7 @@ import wave
|
|||||||
import websockets
|
import websockets
|
||||||
from core.providers.tts.base import TTSProviderBase
|
from core.providers.tts.base import TTSProviderBase
|
||||||
from config.logger import setup_logging
|
from config.logger import setup_logging
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
TAG = __name__
|
TAG = __name__
|
||||||
logger = setup_logging()
|
logger = setup_logging()
|
||||||
@@ -19,11 +19,12 @@ class TTSProvider(TTSProviderBase):
|
|||||||
super().__init__(config, delete_audio_file)
|
super().__init__(config, delete_audio_file)
|
||||||
self.url = config.get("url", "ws://192.168.1.10:8092/paddlespeech/tts/streaming")
|
self.url = config.get("url", "ws://192.168.1.10:8092/paddlespeech/tts/streaming")
|
||||||
self.protocol = config.get("protocol", "websocket")
|
self.protocol = config.get("protocol", "websocket")
|
||||||
|
|
||||||
if config.get("private_voice"):
|
if config.get("private_voice"):
|
||||||
self.spk_id = int(config.get("private_voice"))
|
self.spk_id = int(config.get("private_voice"))
|
||||||
else:
|
else:
|
||||||
self.spk_id = int(config.get("spk_id", "0"))
|
self.spk_id = int(config.get("spk_id", "0"))
|
||||||
|
|
||||||
sample_rate = config.get("sample_rate", 24000)
|
sample_rate = config.get("sample_rate", 24000)
|
||||||
self.sample_rate = float(sample_rate) if sample_rate else 24000
|
self.sample_rate = float(sample_rate) if sample_rate else 24000
|
||||||
|
|
||||||
@@ -33,7 +34,12 @@ class TTSProvider(TTSProviderBase):
|
|||||||
volume = config.get("volume", 1.0)
|
volume = config.get("volume", 1.0)
|
||||||
self.volume = float(volume) if volume else 1.0
|
self.volume = float(volume) if volume else 1.0
|
||||||
|
|
||||||
self.save_path = config.get("save_path", "None")
|
self.delete_audio_file = config.get("delete_audio_file", True)
|
||||||
|
if not self.delete_audio_file:
|
||||||
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||||
|
self.save_path = config.get("save_path", f"./streaming_tts_{timestamp}.wav")
|
||||||
|
else:
|
||||||
|
self.save_path = None
|
||||||
|
|
||||||
async def pcm_to_wav(self, pcm_data: bytes, sample_rate: int = 24000, num_channels: int = 1,
|
async def pcm_to_wav(self, pcm_data: bytes, sample_rate: int = 24000, num_channels: int = 1,
|
||||||
bits_per_sample: int = 16) -> bytes:
|
bits_per_sample: int = 16) -> bytes:
|
||||||
@@ -56,22 +62,22 @@ class TTSProvider(TTSProviderBase):
|
|||||||
|
|
||||||
return wav_io.getvalue()
|
return wav_io.getvalue()
|
||||||
|
|
||||||
async def text_to_speak(self, text, output_file=None):
|
async def text_to_speak(self, text, output_file):
|
||||||
if self.protocol == "websocket":
|
if self.protocol == "websocket":
|
||||||
return await self.text_streaming(text)
|
return await self.text_streaming(text, output_file)
|
||||||
elif self.protocol == "http":
|
elif self.protocol == "http":
|
||||||
return await self.text(text)
|
return await self.text(text, output_file)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Unsupported protocol. Please use 'websocket' or 'http'.")
|
raise ValueError("Unsupported protocol. Please use 'websocket' or 'http'.")
|
||||||
|
|
||||||
async def text(self, text):
|
async def text(self, text, output_file):
|
||||||
request_json = {
|
request_json = {
|
||||||
"text": text,
|
"text": text,
|
||||||
"spk_id": self.spk_id,
|
"spk_id": self.spk_id,
|
||||||
"speed": self.speed,
|
"speed": self.speed,
|
||||||
"volume": self.volume,
|
"volume": self.volume,
|
||||||
"sample_rate": self.sample_rate,
|
"sample_rate": self.sample_rate,
|
||||||
"save_path": None,
|
"save_path": self.save_path
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -82,7 +88,11 @@ class TTSProvider(TTSProviderBase):
|
|||||||
if resp_json.get("success"):
|
if resp_json.get("success"):
|
||||||
data = resp_json["result"]
|
data = resp_json["result"]
|
||||||
audio_bytes = base64.b64decode(data["audio"])
|
audio_bytes = base64.b64decode(data["audio"])
|
||||||
return audio_bytes
|
if output_file:
|
||||||
|
with open(output_file, "wb") as file_to_save:
|
||||||
|
file_to_save.write(audio_bytes)
|
||||||
|
else:
|
||||||
|
return audio_bytes
|
||||||
else:
|
else:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
f"Error: {resp_json.get('message', 'Unknown error')} while processing text: {text}")
|
f"Error: {resp_json.get('message', 'Unknown error')} while processing text: {text}")
|
||||||
@@ -92,7 +102,7 @@ class TTSProvider(TTSProviderBase):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Error during TTS HTTP request: {e} while processing text: {text}")
|
raise Exception(f"Error during TTS HTTP request: {e} while processing text: {text}")
|
||||||
|
|
||||||
async def text_streaming(self, text):
|
async def text_streaming(self, text, output_file):
|
||||||
try:
|
try:
|
||||||
# 使用 websockets 异步连接到 WebSocket 服务器
|
# 使用 websockets 异步连接到 WebSocket 服务器
|
||||||
async with websockets.connect(self.url) as ws:
|
async with websockets.connect(self.url) as ws:
|
||||||
@@ -148,8 +158,18 @@ class TTSProvider(TTSProviderBase):
|
|||||||
# 接收结束响应避免服务抛出异常
|
# 接收结束响应避免服务抛出异常
|
||||||
await ws.recv()
|
await ws.recv()
|
||||||
|
|
||||||
# 直接返回音频数据
|
# 根据配置决定是否保存文件
|
||||||
return wav_data
|
if not self.delete_audio_file and self.save_path:
|
||||||
|
with open(self.save_path, "wb") as f:
|
||||||
|
f.write(wav_data)
|
||||||
|
logger.bind(tag=TAG).info(f"音频文件已保存到: {self.save_path}")
|
||||||
|
|
||||||
|
# 返回或保存音频数据
|
||||||
|
if output_file:
|
||||||
|
with open(output_file, "wb") as file_to_save:
|
||||||
|
file_to_save.write(wav_data)
|
||||||
|
else:
|
||||||
|
return wav_data
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f"Error during TTS WebSocket request: {e} while processing text: {text}")
|
raise Exception(f"Error during TTS WebSocket request: {e} while processing text: {text}")
|
||||||
Reference in New Issue
Block a user