mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-29 05:09:46 +01:00
add: 部署文件
This commit is contained in:
parent
01f34bf5fa
commit
c194c39667
1 changed files with 73 additions and 69 deletions
|
|
@ -6,6 +6,7 @@ import json
|
||||||
import queue
|
import queue
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
import traceback
|
||||||
import uuid
|
import uuid
|
||||||
import base64
|
import base64
|
||||||
import wave
|
import wave
|
||||||
|
|
@ -254,85 +255,88 @@ class FishSpeech_TTS(TTS):
|
||||||
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||||
|
|
||||||
async def text_to_speak_queue(self, text, queue: queue.Queue):
|
async def text_to_speak_queue(self, text, queue: queue.Queue):
|
||||||
|
try:
|
||||||
idstr = None
|
idstr = None
|
||||||
# priority: ref_id > [{text, audio},...]
|
# priority: ref_id > [{text, audio},...]
|
||||||
if idstr is None:
|
if idstr is None:
|
||||||
ref_audios = self.ref_audios
|
ref_audios = self.ref_audios
|
||||||
ref_texts = self.ref_texts
|
ref_texts = self.ref_texts
|
||||||
if ref_audios is None:
|
if ref_audios is None:
|
||||||
|
byte_audios = []
|
||||||
|
else:
|
||||||
|
byte_audios = [audio_to_bytes(ref_audio) for ref_audio in ref_audios]
|
||||||
|
if ref_texts is None:
|
||||||
|
ref_texts = []
|
||||||
|
else:
|
||||||
|
ref_texts = [read_ref_text(ref_text) for ref_text in ref_texts]
|
||||||
|
else:
|
||||||
byte_audios = []
|
byte_audios = []
|
||||||
else:
|
|
||||||
byte_audios = [audio_to_bytes(ref_audio) for ref_audio in ref_audios]
|
|
||||||
if ref_texts is None:
|
|
||||||
ref_texts = []
|
ref_texts = []
|
||||||
else:
|
pass # in api.py
|
||||||
ref_texts = [read_ref_text(ref_text) for ref_text in ref_texts]
|
|
||||||
else:
|
|
||||||
byte_audios = []
|
|
||||||
ref_texts = []
|
|
||||||
pass # in api.py
|
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"text": text,
|
"text": text,
|
||||||
"references": [
|
"references": [
|
||||||
ServeReferenceAudio(
|
ServeReferenceAudio(
|
||||||
audio=ref_audio if ref_audio is not None else b"", text=ref_text
|
audio=ref_audio if ref_audio is not None else b"", text=ref_text
|
||||||
)
|
)
|
||||||
for ref_text, ref_audio in zip(ref_texts, byte_audios)
|
for ref_text, ref_audio in zip(ref_texts, byte_audios)
|
||||||
],
|
],
|
||||||
"reference_id": idstr,
|
"reference_id": idstr,
|
||||||
"normalize": self.normalize,
|
"normalize": self.normalize,
|
||||||
"format": self.format,
|
"format": self.format,
|
||||||
"max_new_tokens": self.max_new_tokens,
|
"max_new_tokens": self.max_new_tokens,
|
||||||
"chunk_length": self.chunk_length,
|
"chunk_length": self.chunk_length,
|
||||||
"top_p": self.top_p,
|
"top_p": self.top_p,
|
||||||
"repetition_penalty": self.repetition_penalty,
|
"repetition_penalty": self.repetition_penalty,
|
||||||
"temperature": self.temperature,
|
"temperature": self.temperature,
|
||||||
"streaming": self.streaming,
|
"streaming": self.streaming,
|
||||||
"use_memory_cache": self.use_memory_cache,
|
"use_memory_cache": self.use_memory_cache,
|
||||||
"seed": self.seed,
|
"seed": self.seed,
|
||||||
}
|
}
|
||||||
|
|
||||||
pydantic_data = ServeTTSRequest(**data)
|
pydantic_data = ServeTTSRequest(**data)
|
||||||
last_chunk = b''
|
last_chunk = b''
|
||||||
overlap = 882
|
overlap = 882
|
||||||
chunk_total = b''
|
chunk_total = b''
|
||||||
with requests.post(
|
with requests.post(
|
||||||
self.url,
|
self.url,
|
||||||
data=ormsgpack.packb(pydantic_data, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
|
data=ormsgpack.packb(pydantic_data, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
|
||||||
stream=self.streaming,
|
stream=self.streaming,
|
||||||
headers={
|
headers={
|
||||||
"authorization": f"Bearer {self.api_key}",
|
"authorization": f"Bearer {self.api_key}",
|
||||||
"content-type": "application/msgpack",
|
"content-type": "application/msgpack",
|
||||||
},
|
},
|
||||||
) as response:
|
) as response:
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
for chunk in response.iter_content(chunk_size=52920):
|
for chunk in response.iter_content(chunk_size=52920):
|
||||||
# 拼接当前块和上一块数据
|
# 拼接当前块和上一块数据
|
||||||
chunk_total += chunk
|
chunk_total += chunk
|
||||||
if len(chunk_total) >= 52920 and len(chunk_total)%2==0:
|
if len(chunk_total) >= 52920 and len(chunk_total)%2==0:
|
||||||
|
current_audio = last_chunk + chunk_total
|
||||||
|
# 只保留当前块的最后16个字节
|
||||||
|
last_chunk = current_audio[-overlap:] # 这里可以根据实际音频样本长度调整
|
||||||
|
queue.put({
|
||||||
|
"data": current_audio,
|
||||||
|
"end": False
|
||||||
|
})
|
||||||
|
chunk_total = b''
|
||||||
|
if len(chunk_total) > 0:
|
||||||
current_audio = last_chunk + chunk_total
|
current_audio = last_chunk + chunk_total
|
||||||
# 只保留当前块的最后16个字节
|
|
||||||
last_chunk = current_audio[-overlap:] # 这里可以根据实际音频样本长度调整
|
|
||||||
queue.put({
|
queue.put({
|
||||||
"data": current_audio,
|
"data": current_audio,
|
||||||
"end": False
|
"end": False
|
||||||
})
|
})
|
||||||
chunk_total = b''
|
|
||||||
if len(chunk_total) > 0:
|
|
||||||
current_audio = last_chunk + chunk_total
|
|
||||||
queue.put({
|
|
||||||
"data": current_audio,
|
|
||||||
"end": False
|
|
||||||
})
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('请求失败:', response.status_code, response.text)
|
print('请求失败:', response.status_code, response.text)
|
||||||
queue.put({
|
queue.put({
|
||||||
"data": None,
|
"data": None,
|
||||||
"end": True
|
"end": True
|
||||||
})
|
})
|
||||||
|
except Exception as e:
|
||||||
|
print(f"请求tts-fishspeech发生错误: {e}")
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
async def text_to_speak(self, text, output_file):
|
async def text_to_speak(self, text, output_file):
|
||||||
raise "不支持"
|
raise "不支持"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue