project_charles / debug_000.py
sohojoe's picture
4096 chunk sizes seams to be more robust
24d9393
raw
history blame
1.9 kB
import os
import subprocess
from elevenlabs import generate, play
from elevenlabs import set_api_key
from elevenlabs import generate, stream
from dotenv import load_dotenv
load_dotenv()
account_sid = os.environ["ELEVENLABS_API_KEY"]
voice_id="2OviOUQc1JsQRQgNkVBj"
model_id="eleven_monolingual_v1"
set_api_key(account_sid)
def stream_tts(prompt):
audio_stream = generate(
text=prompt,
voice=voice_id,
model=model_id,
stream_chunk_size=4096,
stream=True,
)
return audio_stream
prompts=[
"erm",
"Cabbages, my dear friend!",
"Did you know that the world's largest cabbage weighed 62.71 kilograms?",
"Simply remarkable!",
"How are you today?",
]
mpv_command = ["mpv", "--no-cache", "--no-terminal", "--", "fd://0"]
mpv_process = subprocess.Popen(
mpv_command,
stdin=subprocess.PIPE,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
load_chunks = False
load_chunks = os.path.exists("chunks.pkl")
# check if chunks.pkl exists
if load_chunks:
# try open chunks
with open("chunks.pkl", "rb") as f:
import pickle
chunks = pickle.load(f)
for chunk in chunks:
mpv_process.stdin.write(chunk)
mpv_process.stdin.flush()
else:
chunks = []
for prompt in prompts:
for chunk in stream_tts(prompt):
if chunk is not None:
chunks.append(chunk)
mpv_process.stdin.write(chunk) # type: ignore
mpv_process.stdin.flush() # type: ignore
# save chunks to file as a pickled list of bytes
with open("chunks.pkl", "wb") as f:
import pickle
pickle.dump(chunks, f)
with open("chunks.mp3", "wb") as f:
for chunk in chunks:
f.write(chunk)
if mpv_process.stdin:
mpv_process.stdin.close()
mpv_process.wait()