File size: 1,898 Bytes
f615884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24d9393
f615884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()