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
407e873416
commit
17c51b982b
2 changed files with 28 additions and 12 deletions
|
|
@ -153,7 +153,7 @@ async def sendAudioMessageStream(conn, audios_queue, duration, text):
|
|||
last_duration = conn.tts_duration - queue_duration
|
||||
if last_duration <= 0 :
|
||||
last_duration = 0
|
||||
opus_datas, duration = conn.tts.wav_to_opus_data_audio(conn,audio_data_chunke_data)
|
||||
opus_datas, duration = conn.tts.wav_to_opus_data_byte(conn,audio_data_chunke_data)
|
||||
conn.tts_duration = duration + last_duration + 0.5
|
||||
for opus_packet in opus_datas:
|
||||
await conn.websocket.send(opus_packet)
|
||||
|
|
|
|||
|
|
@ -258,6 +258,14 @@ class FishSpeech_TTS(TTS):
|
|||
def generate_filename(self, extension=".wav"):
|
||||
return os.path.join(self.output_file, f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}")
|
||||
|
||||
def _get_audio_from_tts(self,data_bytes):
|
||||
tts_speech = torch.from_numpy(np.array(np.frombuffer(data_bytes, dtype=np.int16))).unsqueeze(dim=0)
|
||||
with io.BytesIO() as bf:
|
||||
torchaudio.save(bf, tts_speech, 44100, format="wav")
|
||||
audio = AudioSegment.from_file(bf, format="wav")
|
||||
audio.set_channels(1).set_frame_rate(16000)
|
||||
return audio
|
||||
|
||||
async def text_to_speak_queue(self, text, queue: queue.Queue):
|
||||
try:
|
||||
idstr = None
|
||||
|
|
@ -303,6 +311,7 @@ class FishSpeech_TTS(TTS):
|
|||
last_chunk = b''
|
||||
overlap = 882
|
||||
chunk_total = b''
|
||||
audio_buff=None
|
||||
with requests.post(
|
||||
self.url,
|
||||
data=ormsgpack.packb(pydantic_data, option=ormsgpack.OPT_SERIALIZE_PYDANTIC),
|
||||
|
|
@ -313,22 +322,29 @@ class FishSpeech_TTS(TTS):
|
|||
},
|
||||
) as response:
|
||||
if response.status_code == 200:
|
||||
for chunk in response.iter_content(chunk_size=1024):
|
||||
for chunk in response.iter_content():
|
||||
# 拼接当前块和上一块数据
|
||||
chunk_total += chunk
|
||||
if len(chunk_total) >= 44100 and len(chunk_total)%44100==0:
|
||||
current_audio = chunk_total
|
||||
# 只保留当前块的最后16个字节
|
||||
last_chunk = current_audio[-overlap:] # 这里可以根据实际音频样本长度调整
|
||||
queue.put({
|
||||
"data": current_audio,
|
||||
"end": False
|
||||
})
|
||||
if len(chunk_total) >= 86400 and len(chunk_total)%86400==0:
|
||||
audio = self._get_audio_from_tts(chunk_total)
|
||||
if not audio_buff:
|
||||
audio_buff = audio
|
||||
else:
|
||||
audio_buff += audio
|
||||
if len(audio_buff.raw_data)%1920 == 0:
|
||||
queue.put({
|
||||
"data": audio_buff,
|
||||
"end": False
|
||||
})
|
||||
audio_buff = None
|
||||
chunk_total = b''
|
||||
if len(chunk_total) > 0:
|
||||
current_audio = chunk_total
|
||||
if not audio_buff:
|
||||
audio_buff = self._get_audio_from_tts(chunk_total)
|
||||
else:
|
||||
audio_buff += self._get_audio_from_tts(chunk_total)
|
||||
queue.put({
|
||||
"data": current_audio,
|
||||
"data": audio_buff,
|
||||
"end": False
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue