-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_tts.py
More file actions
93 lines (77 loc) · 2.73 KB
/
Copy pathbench_tts.py
File metadata and controls
93 lines (77 loc) · 2.73 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
import argparse
import json
import resource
import subprocess
import time
import wave
from pathlib import Path
TEXT = "Codex finished the task and is ready for your next instruction."
def rss_mb() -> float:
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 / 1024
def write_wav(path: Path, sample_rate: int, chunks) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
with wave.open(str(path), "wb") as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(sample_rate)
for chunk in chunks:
data = chunk.detach().cpu().clamp(-1, 1).numpy()
pcm = (data * 32767).astype("<i2").tobytes()
wav.writeframes(pcm)
def bench_pocket() -> dict:
from pocket_tts.default_parameters import DEFAULT_FRAMES_AFTER_EOS, get_default_voice_for_language
from pocket_tts.models.tts_model import TTSModel
t0 = time.perf_counter()
model = TTSModel.load_model(language="english")
voice = get_default_voice_for_language("english")
state = model.get_state_for_audio_prompt(voice)
load_s = time.perf_counter() - t0
chunks = []
first_audio_s = None
t1 = time.perf_counter()
for chunk in model.generate_audio_stream(
model_state=state,
text_to_generate=TEXT,
frames_after_eos=DEFAULT_FRAMES_AFTER_EOS,
max_tokens=50,
):
if first_audio_s is None:
first_audio_s = time.perf_counter() - t1
chunks.append(chunk)
full_s = time.perf_counter() - t1
write_wav(Path("bench/pocket.wav"), model.sample_rate, chunks)
return {
"engine": "pocket",
"status": "ok",
"load_s": round(load_s, 3),
"warm_first_audio_s": round(first_audio_s or full_s, 3),
"warm_full_s": round(full_s, 3),
"rss_mb": round(rss_mb(), 1),
"wav": "bench/pocket.wav",
}
def bench_say() -> dict:
path = Path("bench/say.wav")
path.parent.mkdir(parents=True, exist_ok=True)
t0 = time.perf_counter()
subprocess.run(["say", "-v", "Samantha", "-r", "205", "-o", str(path), "--data-format=LEI16@22050", TEXT], check=True)
return {
"engine": "say",
"status": "ok",
"load_s": 0,
"warm_first_audio_s": round(time.perf_counter() - t0, 3),
"warm_full_s": round(time.perf_counter() - t0, 3),
"rss_mb": round(rss_mb(), 1),
"wav": "bench/say.wav",
}
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("engine", choices=["pocket", "say"])
args = parser.parse_args()
if args.engine == "pocket":
result = bench_pocket()
else:
result = bench_say()
print(json.dumps(result, indent=2))
if __name__ == "__main__":
main()