forked from Lex-au/Orpheus-FastAPI
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstream_test.py
More file actions
63 lines (56 loc) · 1.96 KB
/
Copy pathstream_test.py
File metadata and controls
63 lines (56 loc) · 1.96 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
#!/usr/bin/env python3
import argparse
import sys
import requests
try:
import sounddevice as sd
except ImportError:
print("Please install sounddevice: pip install sounddevice", file=sys.stderr)
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description="Test streaming TTS endpoint")
parser.add_argument("text", help="Text to synthesize")
parser.add_argument("--url", default="http://localhost:8000/api/tts/stream", help="Streaming TTS endpoint URL")
parser.add_argument("--voice", default="Orpheus", help="Voice name")
parser.add_argument("--use_cuda", action="store_true", help="Enable CUDA")
parser.add_argument("--samplerate", type=int, default=24000, help="Sample rate")
args = parser.parse_args()
payload = {"text": args.text, "voice": args.voice, "use_cuda": args.use_cuda}
try:
resp = requests.post(args.url, json=payload, stream=True)
resp.raise_for_status()
except Exception as e:
print(f"Request failed: {e}", file=sys.stderr)
sys.exit(1)
# Set up audio stream
try:
stream = sd.RawOutputStream(samplerate=args.samplerate, channels=1, dtype='int16')
stream.start()
except Exception as e:
print(f"Audio stream error: {e}", file=sys.stderr)
sys.exit(1)
header_len = 44
buffer = b""
data_started = False
for chunk in resp.iter_content(chunk_size=4096):
if not chunk:
continue
buffer += chunk
if not data_started:
if len(buffer) < header_len:
continue
# Skip WAV header
buffer = buffer[header_len:]
data_started = True
if buffer:
try:
stream.write(buffer)
except Exception as e:
print(f"Playback error: {e}", file=sys.stderr)
break
buffer = b""
stream.stop()
stream.close()
print("Playback finished")
if __name__ == "__main__":
main()