temp commit, woring on playht

This commit is contained in:
Kwindla Hultman Kramer
2024-06-06 10:48:22 -04:00
parent 06ff9cfede
commit 1a542c91fa
3 changed files with 140 additions and 25 deletions

View File

@@ -6,6 +6,7 @@
import io
import struct
import time
from typing import AsyncGenerator
@@ -25,7 +26,7 @@ except ModuleNotFoundError as e:
raise Exception(f"Missing module: {e}")
class PlayHTAIService(TTSService):
class PlayHTTTSService(TTSService):
def __init__(self, *, api_key: str, user_id: str, voice_url: str, **kwargs):
super().__init__(**kwargs)
@@ -47,28 +48,40 @@ class PlayHTAIService(TTSService):
self._client.close()
async def run_tts(self, text: str) -> AsyncGenerator[Frame, None]:
b = bytearray()
in_header = True
for chunk in self._client.tts(text, self._options):
# skip the RIFF header.
if in_header:
b.extend(chunk)
if len(b) <= 36:
continue
else:
fh = io.BytesIO(b)
fh.seek(36)
(data, size) = struct.unpack('<4sI', fh.read(8))
logger.debug(
f"first attempt: data: {data}, size: {hex(size)}, position: {fh.tell()}")
while data != b'data':
fh.read(size)
start_time = time.time()
ttfb = None
logger.debug(f"Generating TTS: [{text}]")
try:
b = bytearray()
in_header = True
sync_gen = self._client.tts(
text,
voice_engine="PlayHT2.0-turbo",
options=self._options)
# need to ask Aleix about this. frames are getting pushed.
# but playback is blocked
for chunk in sync_gen:
# skip the RIFF header.
if in_header:
b.extend(chunk)
if len(b) <= 36:
continue
else:
fh = io.BytesIO(b)
fh.seek(36)
(data, size) = struct.unpack('<4sI', fh.read(8))
logger.debug(
f"subsequent data: {data}, size: {hex(size)}, position: {fh.tell()}, data != data: {data != b'data'}")
logger.debug("position: ", fh.tell())
in_header = False
else:
if len(chunk):
frame = AudioRawFrame(chunk, 16000, 1)
yield frame
while data != b'data':
fh.read(size)
(data, size) = struct.unpack('<4sI', fh.read(8))
in_header = False
else:
if len(chunk):
if ttfb is None:
ttfb = time.time() - start_time
logger.debug(f"TTS ttfb: {ttfb}")
frame = AudioRawFrame(chunk, 16000, 1)
yield frame
except Exception as e:
logger.error(f"Error generating TTS: {e}")