vocal-cli 0.3.7__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/.gitignore +1 -2
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/MANIFEST.in +3 -3
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/PKG-INFO +9 -9
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/pyproject.toml +8 -8
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/vocal_cli/__init__.py +1 -1
- {vocal_cli-0.3.7 → vocal_cli-0.4.0}/vocal_cli/main.py +234 -19
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
include ../../../README.md
|
|
2
|
-
include ../../../LICENSE
|
|
3
|
-
recursive-include vocal_cli *.py
|
|
1
|
+
include ../../../README.md
|
|
2
|
+
include ../../../LICENSE
|
|
3
|
+
recursive-include vocal_cli *.py
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: vocal-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: CLI tool for Vocal - Ollama-style Voice Model Management
|
|
5
5
|
Project-URL: Homepage, https://github.com/niradler/vocal
|
|
6
6
|
Project-URL: Documentation, https://github.com/niradler/vocal/tree/master/docs
|
|
@@ -19,10 +19,10 @@ Classifier: Programming Language :: Python :: 3.13
|
|
|
19
19
|
Classifier: Topic :: Utilities
|
|
20
20
|
Requires-Python: >=3.11
|
|
21
21
|
Requires-Dist: numpy>=2.4.3
|
|
22
|
-
Requires-Dist: rich>=
|
|
23
|
-
Requires-Dist: sounddevice>=0.5.
|
|
24
|
-
Requires-Dist: typer
|
|
25
|
-
Requires-Dist: uvicorn>=0.
|
|
26
|
-
Requires-Dist: vocal-core>=0.
|
|
27
|
-
Requires-Dist: vocal-sdk>=0.
|
|
28
|
-
Requires-Dist: websockets>=
|
|
22
|
+
Requires-Dist: rich>=15.0.0
|
|
23
|
+
Requires-Dist: sounddevice>=0.5.6
|
|
24
|
+
Requires-Dist: typer<0.28,>=0.27.3
|
|
25
|
+
Requires-Dist: uvicorn>=0.54.0
|
|
26
|
+
Requires-Dist: vocal-core>=0.4.0
|
|
27
|
+
Requires-Dist: vocal-sdk>=0.4.0
|
|
28
|
+
Requires-Dist: websockets>=17.4
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "vocal-cli"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.0"
|
|
4
4
|
description = "CLI tool for Vocal - Ollama-style Voice Model Management"
|
|
5
5
|
requires-python = ">=3.11"
|
|
6
6
|
license = { text = "SSPL-1.0" }
|
|
@@ -19,14 +19,14 @@ classifiers = [
|
|
|
19
19
|
"Topic :: Utilities",
|
|
20
20
|
]
|
|
21
21
|
dependencies = [
|
|
22
|
-
"vocal-core>=0.
|
|
23
|
-
"vocal-sdk>=0.
|
|
24
|
-
"typer>=0.
|
|
25
|
-
"rich>=
|
|
26
|
-
"uvicorn>=0.
|
|
27
|
-
"sounddevice>=0.5.
|
|
22
|
+
"vocal-core>=0.4.0",
|
|
23
|
+
"vocal-sdk>=0.4.0",
|
|
24
|
+
"typer>=0.27.3,<0.28",
|
|
25
|
+
"rich>=15.0.0",
|
|
26
|
+
"uvicorn>=0.54.0",
|
|
27
|
+
"sounddevice>=0.5.6",
|
|
28
28
|
"numpy>=2.4.3",
|
|
29
|
-
"websockets>=
|
|
29
|
+
"websockets>=17.4",
|
|
30
30
|
]
|
|
31
31
|
|
|
32
32
|
[project.urls]
|
|
@@ -19,7 +19,7 @@ from rich.table import Table
|
|
|
19
19
|
|
|
20
20
|
from vocal_core.config import vocal_settings
|
|
21
21
|
from vocal_sdk import VocalClient
|
|
22
|
-
from vocal_sdk.api.audio import voice_clone_v1_audio_clone_post
|
|
22
|
+
from vocal_sdk.api.audio import text_to_speech_v1_audio_speech_post, voice_clone_v1_audio_clone_post
|
|
23
23
|
from vocal_sdk.api.models import (
|
|
24
24
|
delete_model_v1_models_model_id_delete,
|
|
25
25
|
list_models_v1_models_get,
|
|
@@ -35,6 +35,8 @@ from vocal_sdk.models import (
|
|
|
35
35
|
BodyVoiceCloneV1AudioClonePost,
|
|
36
36
|
BodyVoiceCloneV1AudioClonePostResponseFormat,
|
|
37
37
|
TranscriptionFormat,
|
|
38
|
+
TTSRequest,
|
|
39
|
+
TTSRequestResponseFormat,
|
|
38
40
|
)
|
|
39
41
|
from vocal_sdk.types import UNSET, File, Unset
|
|
40
42
|
|
|
@@ -296,6 +298,75 @@ def models_delete(
|
|
|
296
298
|
raise typer.Exit(1)
|
|
297
299
|
|
|
298
300
|
|
|
301
|
+
@app.command()
|
|
302
|
+
def speak(
|
|
303
|
+
text: str = typer.Argument(..., help="Text to synthesize"),
|
|
304
|
+
output: Path | None = typer.Option(None, "--output", "-o", help="Output file path (default: play audio)"),
|
|
305
|
+
model: str = typer.Option(
|
|
306
|
+
vocal_settings.TTS_DEFAULT_MODEL,
|
|
307
|
+
"--model",
|
|
308
|
+
"-m",
|
|
309
|
+
help="TTS model to use (e.g. 'pyttsx3', 'k2-fsa/OmniVoice')",
|
|
310
|
+
),
|
|
311
|
+
models: bool = typer.Option(False, "--models", help="Interactively select from downloaded TTS models"),
|
|
312
|
+
voice: str | None = typer.Option(None, "--voice", help="Voice ID or instruction (model-specific, e.g. 'female, young adult, american accent')"),
|
|
313
|
+
speed: float = typer.Option(1.0, "--speed", "-s", min=0.25, max=4.0, help="Speech speed multiplier"),
|
|
314
|
+
response_format: str = typer.Option("wav", "--format", "-f", help="Output audio format: wav, mp3, flac, pcm, aac, opus"),
|
|
315
|
+
api_url: str = typer.Option("http://localhost:8000", "--api-url", envvar="VOCAL_API_URL", help="Vocal API URL"),
|
|
316
|
+
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show synthesis timing and output size"),
|
|
317
|
+
) -> None:
|
|
318
|
+
"""Synthesize text to speech"""
|
|
319
|
+
if models:
|
|
320
|
+
selected_model = _model_wizard(api_url, task="tts")
|
|
321
|
+
if selected_model is None:
|
|
322
|
+
raise typer.Exit(1)
|
|
323
|
+
model = selected_model
|
|
324
|
+
|
|
325
|
+
try:
|
|
326
|
+
fmt = TTSRequestResponseFormat(response_format.lower())
|
|
327
|
+
except ValueError:
|
|
328
|
+
valid = ", ".join(f.value for f in TTSRequestResponseFormat)
|
|
329
|
+
console.print(f"[red]Error:[/red] Invalid format '{response_format}'. Valid options: {valid}")
|
|
330
|
+
raise typer.Exit(1)
|
|
331
|
+
|
|
332
|
+
if model != "pyttsx3":
|
|
333
|
+
_check_model_ready(api_url, model)
|
|
334
|
+
|
|
335
|
+
try:
|
|
336
|
+
vc = _make_client(api_url)
|
|
337
|
+
t0 = time.monotonic()
|
|
338
|
+
|
|
339
|
+
body = TTSRequest(
|
|
340
|
+
model=model,
|
|
341
|
+
input_=text,
|
|
342
|
+
voice=voice if voice is not None else UNSET,
|
|
343
|
+
speed=speed,
|
|
344
|
+
response_format=fmt,
|
|
345
|
+
)
|
|
346
|
+
resp = text_to_speech_v1_audio_speech_post.sync_detailed(client=vc, body=body)
|
|
347
|
+
|
|
348
|
+
elapsed = time.monotonic() - t0
|
|
349
|
+
|
|
350
|
+
if resp.status_code != 200:
|
|
351
|
+
_clone_error(resp.content, resp.status_code)
|
|
352
|
+
raise typer.Exit(1)
|
|
353
|
+
|
|
354
|
+
audio_bytes = resp.content
|
|
355
|
+
_speak_output(audio_bytes, response_format, output, elapsed, verbose)
|
|
356
|
+
|
|
357
|
+
except typer.Exit:
|
|
358
|
+
raise
|
|
359
|
+
except UnexpectedStatus as e:
|
|
360
|
+
msg = _api_error_message(e)
|
|
361
|
+
console.print(f"[red]Error:[/red] {msg}")
|
|
362
|
+
if e.status_code == 503:
|
|
363
|
+
console.print("[dim]The model or a required package is not available on the server.[/dim]")
|
|
364
|
+
raise typer.Exit(1)
|
|
365
|
+
except Exception as e:
|
|
366
|
+
console.print(f"[red]Error:[/red] {e}")
|
|
367
|
+
raise typer.Exit(1)
|
|
368
|
+
|
|
369
|
+
|
|
299
370
|
@app.command()
|
|
300
371
|
def clone(
|
|
301
372
|
text: str = typer.Argument(..., help="Text to synthesize in the cloned voice"),
|
|
@@ -898,7 +969,9 @@ def _print_output_devices_table() -> None:
|
|
|
898
969
|
@app.command()
|
|
899
970
|
def chat(
|
|
900
971
|
model: str = typer.Option(vocal_settings.STT_DEFAULT_MODEL, "--model", "-m", help="STT model to use"),
|
|
901
|
-
models: bool = typer.Option(False, "--models", help="Interactively select from downloaded STT models"),
|
|
972
|
+
models: bool = typer.Option(False, "--models", help="Interactively select from downloaded STT and TTS models"),
|
|
973
|
+
tts_model: str = typer.Option(vocal_settings.TTS_DEFAULT_MODEL, "--tts-model", help="TTS model to use for voice responses"),
|
|
974
|
+
tts_voice: str | None = typer.Option(None, "--tts-voice", help="TTS voice ID or instruction (model-specific)"),
|
|
902
975
|
language: str | None = typer.Option(None, "--language", "-l", help="Language code (e.g. 'en'). Auto-detected if omitted."),
|
|
903
976
|
device: str | None = typer.Option(None, "--device", "-d", help="Input device index or name (use --devices to pick interactively)"),
|
|
904
977
|
output_device: str | None = typer.Option(None, "--output-device", "-o", help="Output device index or name (run `vocal devices --output` to list)"),
|
|
@@ -923,6 +996,10 @@ def chat(
|
|
|
923
996
|
if selected_model is None:
|
|
924
997
|
raise typer.Exit(1)
|
|
925
998
|
model = selected_model
|
|
999
|
+
selected_tts = _model_wizard(api_url, task="tts")
|
|
1000
|
+
if selected_tts is None:
|
|
1001
|
+
raise typer.Exit(1)
|
|
1002
|
+
tts_model = selected_tts
|
|
926
1003
|
try:
|
|
927
1004
|
device_idx = _resolve_device(device)
|
|
928
1005
|
output_device_idx = _resolve_output_device(output_device)
|
|
@@ -936,11 +1013,11 @@ def chat(
|
|
|
936
1013
|
device_label = f"[dim]{active_device['name']}[/dim]"
|
|
937
1014
|
threshold_hint = f" vad=[cyan]{silence_threshold:.0f}[/cyan]" if silence_threshold is not None else ""
|
|
938
1015
|
|
|
939
|
-
console.print(f"[green]Voice chat started[/green]
|
|
1016
|
+
console.print(f"[green]Voice chat started[/green] stt=[cyan]{model}[/cyan] tts=[cyan]{tts_model}[/cyan] device={device_label}{threshold_hint} Ctrl+C to stop\n")
|
|
940
1017
|
console.print("[dim]Speak — I'll transcribe, think, and respond with audio.[/dim]\n")
|
|
941
1018
|
|
|
942
1019
|
try:
|
|
943
|
-
asyncio.run(_chat_async(ws_url, device_idx, output_device_idx, model, language, system_prompt, silence_threshold, verbose))
|
|
1020
|
+
asyncio.run(_chat_async(ws_url, device_idx, output_device_idx, model, language, system_prompt, silence_threshold, verbose, tts_model=tts_model, tts_voice=tts_voice))
|
|
944
1021
|
except KeyboardInterrupt:
|
|
945
1022
|
console.print("\n[yellow]Stopped.[/yellow]")
|
|
946
1023
|
except Exception as e:
|
|
@@ -948,6 +1025,25 @@ def chat(
|
|
|
948
1025
|
raise typer.Exit(1)
|
|
949
1026
|
|
|
950
1027
|
|
|
1028
|
+
def _speak_output(audio_bytes: bytes, response_format: str, output: Path | None, elapsed: float, verbose: bool) -> None:
|
|
1029
|
+
if output:
|
|
1030
|
+
output.write_bytes(audio_bytes)
|
|
1031
|
+
timing = f" [dim]{elapsed:.1f}s[/dim]" if verbose else ""
|
|
1032
|
+
console.print(f"[green]Saved[/green] {len(audio_bytes):,} bytes -> [cyan]{output}[/cyan]{timing}")
|
|
1033
|
+
elif sys.stdout.isatty():
|
|
1034
|
+
if response_format == "wav":
|
|
1035
|
+
timing = f" [dim]{elapsed:.1f}s[/dim]" if verbose else ""
|
|
1036
|
+
console.print(f"[green]Playing[/green] {len(audio_bytes):,} bytes{timing}")
|
|
1037
|
+
_play_wav_bytes(audio_bytes)
|
|
1038
|
+
else:
|
|
1039
|
+
console.print(f"[yellow]Tip:[/yellow] Use [cyan]--output file.{response_format}[/cyan] to save non-WAV audio, or omit [cyan]--format[/cyan] for auto-play.")
|
|
1040
|
+
sys.stdout.buffer.write(audio_bytes)
|
|
1041
|
+
else:
|
|
1042
|
+
if verbose:
|
|
1043
|
+
sys.stderr.write(f" {len(audio_bytes):,} bytes ({response_format}) in {elapsed:.1f}s\n")
|
|
1044
|
+
sys.stdout.buffer.write(audio_bytes)
|
|
1045
|
+
|
|
1046
|
+
|
|
951
1047
|
def _clone_error(content: bytes, status_code: int) -> None:
|
|
952
1048
|
try:
|
|
953
1049
|
detail = json.loads(content).get("detail", "")
|
|
@@ -963,7 +1059,7 @@ def _clone_output(audio_bytes: bytes, fmt: BodyVoiceCloneV1AudioClonePostRespons
|
|
|
963
1059
|
if output:
|
|
964
1060
|
output.write_bytes(audio_bytes)
|
|
965
1061
|
timing = f" [dim]{elapsed:.1f}s[/dim]" if verbose else ""
|
|
966
|
-
console.print(f"[green]Saved[/green] {len(audio_bytes):,} bytes
|
|
1062
|
+
console.print(f"[green]Saved[/green] {len(audio_bytes):,} bytes -> [cyan]{output}[/cyan]{timing}")
|
|
967
1063
|
elif sys.stdout.isatty():
|
|
968
1064
|
if fmt == BodyVoiceCloneV1AudioClonePostResponseFormat.WAV:
|
|
969
1065
|
timing = f" [dim]{elapsed:.1f}s[/dim]" if verbose else ""
|
|
@@ -988,6 +1084,77 @@ def _play_wav_bytes(audio_bytes: bytes, device: int | None = None) -> None:
|
|
|
988
1084
|
sd.wait()
|
|
989
1085
|
|
|
990
1086
|
|
|
1087
|
+
class _StreamingPlayback:
|
|
1088
|
+
"""Plays PCM16 chunks as they arrive instead of waiting for a whole reply."""
|
|
1089
|
+
|
|
1090
|
+
def __init__(self, device: int | None, sample_rate: int = 24000) -> None:
|
|
1091
|
+
self._device = device
|
|
1092
|
+
self._rate = sample_rate
|
|
1093
|
+
self._buffer = bytearray()
|
|
1094
|
+
self._lock = threading.Lock()
|
|
1095
|
+
self._stream = None
|
|
1096
|
+
self._drained = threading.Event()
|
|
1097
|
+
self._drained.set()
|
|
1098
|
+
|
|
1099
|
+
def _callback(self, outdata, frames, _time_info, _status) -> None:
|
|
1100
|
+
need = frames * 2
|
|
1101
|
+
with self._lock:
|
|
1102
|
+
chunk = bytes(self._buffer[:need])
|
|
1103
|
+
del self._buffer[:need]
|
|
1104
|
+
empty = not self._buffer
|
|
1105
|
+
if len(chunk) < need:
|
|
1106
|
+
chunk += b"\x00" * (need - len(chunk))
|
|
1107
|
+
if empty:
|
|
1108
|
+
self._drained.set()
|
|
1109
|
+
outdata[:] = np.frombuffer(chunk, dtype=np.int16).reshape(-1, 1)
|
|
1110
|
+
|
|
1111
|
+
def _ensure_stream(self) -> None:
|
|
1112
|
+
if self._stream is not None:
|
|
1113
|
+
return
|
|
1114
|
+
self._stream = sd.OutputStream(
|
|
1115
|
+
samplerate=self._rate,
|
|
1116
|
+
channels=1,
|
|
1117
|
+
dtype="int16",
|
|
1118
|
+
device=self._device,
|
|
1119
|
+
callback=self._callback,
|
|
1120
|
+
)
|
|
1121
|
+
self._stream.start()
|
|
1122
|
+
|
|
1123
|
+
def feed(self, data: bytes) -> bool:
|
|
1124
|
+
if not data:
|
|
1125
|
+
return
|
|
1126
|
+
self._ensure_stream()
|
|
1127
|
+
self._drained.clear()
|
|
1128
|
+
with self._lock:
|
|
1129
|
+
self._buffer.extend(data)
|
|
1130
|
+
|
|
1131
|
+
def stop(self) -> None:
|
|
1132
|
+
with self._lock:
|
|
1133
|
+
self._buffer.clear()
|
|
1134
|
+
self._close()
|
|
1135
|
+
|
|
1136
|
+
def close(self, timeout: float = 30.0) -> int:
|
|
1137
|
+
"""Wait for the buffer to drain, then release the device."""
|
|
1138
|
+
deadline = time.monotonic() + timeout
|
|
1139
|
+
while not self._drained.is_set() and time.monotonic() < deadline:
|
|
1140
|
+
time.sleep(0.02)
|
|
1141
|
+
if self._drained.is_set():
|
|
1142
|
+
time.sleep(0.12)
|
|
1143
|
+
remaining = len(self._buffer)
|
|
1144
|
+
self._close()
|
|
1145
|
+
return remaining
|
|
1146
|
+
|
|
1147
|
+
def _close(self) -> None:
|
|
1148
|
+
stream, self._stream = self._stream, None
|
|
1149
|
+
if stream is not None:
|
|
1150
|
+
try:
|
|
1151
|
+
stream.stop()
|
|
1152
|
+
stream.close()
|
|
1153
|
+
except Exception:
|
|
1154
|
+
pass
|
|
1155
|
+
self._drained.set()
|
|
1156
|
+
|
|
1157
|
+
|
|
991
1158
|
def _play_pcm16(pcm_bytes: bytes, sample_rate: int = 24000, device: int | None = None) -> None:
|
|
992
1159
|
if not pcm_bytes:
|
|
993
1160
|
return
|
|
@@ -1010,9 +1177,22 @@ def _chat_trace(etype: str, event: dict, state: dict) -> None:
|
|
|
1010
1177
|
console.print(f"[dim] [{etype}{extra}][/dim]", markup=False)
|
|
1011
1178
|
|
|
1012
1179
|
|
|
1013
|
-
async def _chat_handle_delta(
|
|
1180
|
+
async def _chat_handle_delta(
|
|
1181
|
+
etype: str,
|
|
1182
|
+
event: dict,
|
|
1183
|
+
state: dict,
|
|
1184
|
+
verbose: bool,
|
|
1185
|
+
playing: asyncio.Event,
|
|
1186
|
+
player: _StreamingPlayback | None = None,
|
|
1187
|
+
) -> None:
|
|
1014
1188
|
if etype == "conversation.item.input_audio_transcription.delta":
|
|
1015
|
-
|
|
1189
|
+
if event.get("partial"):
|
|
1190
|
+
state["transcript"] = [event.get("partial_transcript") or event.get("delta", "")]
|
|
1191
|
+
else:
|
|
1192
|
+
state["transcript"].append(event.get("delta", ""))
|
|
1193
|
+
if verbose:
|
|
1194
|
+
sys.stdout.write("\r[you: " + "".join(state["transcript"]).strip()[-70:] + "]")
|
|
1195
|
+
sys.stdout.flush()
|
|
1016
1196
|
elif etype == "response.output_audio_transcript.delta":
|
|
1017
1197
|
delta = event.get("delta", "")
|
|
1018
1198
|
state["text"].append(delta)
|
|
@@ -1021,12 +1201,16 @@ async def _chat_handle_delta(etype: str, event: dict, state: dict, verbose: bool
|
|
|
1021
1201
|
elif etype == "response.output_audio.delta":
|
|
1022
1202
|
playing.set()
|
|
1023
1203
|
try:
|
|
1024
|
-
|
|
1204
|
+
pcm = base64.b64decode(event.get("delta", ""))
|
|
1025
1205
|
except Exception:
|
|
1026
|
-
|
|
1206
|
+
return
|
|
1207
|
+
if player is not None:
|
|
1208
|
+
player.feed(pcm)
|
|
1209
|
+
else:
|
|
1210
|
+
state["audio"].append(pcm)
|
|
1027
1211
|
|
|
1028
1212
|
|
|
1029
|
-
async def _chat_handle_done(
|
|
1213
|
+
async def _chat_handle_done( # noqa: C901
|
|
1030
1214
|
etype: str,
|
|
1031
1215
|
event: dict,
|
|
1032
1216
|
state: dict,
|
|
@@ -1034,9 +1218,11 @@ async def _chat_handle_done(
|
|
|
1034
1218
|
loop: asyncio.AbstractEventLoop,
|
|
1035
1219
|
output_device_idx: int | None,
|
|
1036
1220
|
playing: asyncio.Event,
|
|
1037
|
-
) -> None:
|
|
1221
|
+
) -> None: # noqa: C901
|
|
1038
1222
|
if etype == "conversation.item.input_audio_transcription.completed":
|
|
1039
|
-
transcript = event.get("transcript", "").strip() or "
|
|
1223
|
+
transcript = event.get("transcript", "").strip() or "".join(state["transcript"]).strip()
|
|
1224
|
+
if state["transcript"]:
|
|
1225
|
+
sys.stdout.write("\r" + " " * 80 + "\r")
|
|
1040
1226
|
state["transcript"] = []
|
|
1041
1227
|
sys.stdout.write("\r" + " " * 60 + "\r")
|
|
1042
1228
|
if transcript:
|
|
@@ -1045,16 +1231,30 @@ async def _chat_handle_done(
|
|
|
1045
1231
|
if state["text"]:
|
|
1046
1232
|
sys.stdout.write("\n")
|
|
1047
1233
|
sys.stdout.flush()
|
|
1048
|
-
|
|
1234
|
+
player: _StreamingPlayback | None = state.get("player")
|
|
1235
|
+
if player is not None:
|
|
1236
|
+
await loop.run_in_executor(None, player.close)
|
|
1237
|
+
state["player"] = None
|
|
1238
|
+
elif state["audio"]:
|
|
1049
1239
|
pcm = b"".join(state["audio"])
|
|
1050
1240
|
await loop.run_in_executor(None, _play_pcm16, pcm, 24000, output_device_idx)
|
|
1051
1241
|
state["audio"] = []
|
|
1052
1242
|
state["text"] = []
|
|
1053
1243
|
await asyncio.sleep(_PLAYBACK_COOLDOWN)
|
|
1054
1244
|
playing.clear()
|
|
1245
|
+
elif etype == "response.cancelled":
|
|
1246
|
+
state["text"] = []
|
|
1247
|
+
state["audio"] = []
|
|
1248
|
+
player = state.get("player")
|
|
1249
|
+
if player is not None:
|
|
1250
|
+
player.stop()
|
|
1251
|
+
state["player"] = None
|
|
1252
|
+
playing.clear()
|
|
1253
|
+
elif etype == "response.metrics" and verbose:
|
|
1254
|
+
console.print(f"[dim] metrics {event.get('metrics')}[/dim]")
|
|
1055
1255
|
|
|
1056
1256
|
|
|
1057
|
-
async def _chat_receiver(ws, output_device_idx: int | None, verbose: bool, loop: asyncio.AbstractEventLoop, stop_event: asyncio.Event, playing: asyncio.Event) -> None:
|
|
1257
|
+
async def _chat_receiver(ws, output_device_idx: int | None, verbose: bool, loop: asyncio.AbstractEventLoop, stop_event: asyncio.Event, playing: asyncio.Event) -> None: # noqa: C901
|
|
1058
1258
|
state: dict = {"audio": [], "text": [], "transcript": []}
|
|
1059
1259
|
|
|
1060
1260
|
async for msg in ws:
|
|
@@ -1066,11 +1266,22 @@ async def _chat_receiver(ws, output_device_idx: int | None, verbose: bool, loop:
|
|
|
1066
1266
|
if verbose:
|
|
1067
1267
|
_chat_trace(etype, event, state)
|
|
1068
1268
|
|
|
1069
|
-
if "delta"
|
|
1070
|
-
|
|
1269
|
+
if etype == "response.output_audio.delta":
|
|
1270
|
+
if state.get("player") is None:
|
|
1271
|
+
state["player"] = _StreamingPlayback(output_device_idx)
|
|
1272
|
+
if etype in ("response.cancelled", "response.metrics"):
|
|
1273
|
+
# Neither name contains "done"/"completed", so route them explicitly.
|
|
1274
|
+
await _chat_handle_done(etype, event, state, verbose, loop, output_device_idx, playing)
|
|
1275
|
+
elif "delta" in etype:
|
|
1276
|
+
await _chat_handle_delta(etype, event, state, verbose, playing, state.get("player"))
|
|
1071
1277
|
elif "done" in etype or "completed" in etype:
|
|
1072
1278
|
await _chat_handle_done(etype, event, state, verbose, loop, output_device_idx, playing)
|
|
1073
1279
|
elif etype == "input_audio_buffer.speech_started":
|
|
1280
|
+
player = state.get("player")
|
|
1281
|
+
if player is not None:
|
|
1282
|
+
player.stop()
|
|
1283
|
+
state["player"] = None
|
|
1284
|
+
playing.clear()
|
|
1074
1285
|
sys.stdout.write("\r[detecting speech...] \r")
|
|
1075
1286
|
sys.stdout.flush()
|
|
1076
1287
|
elif etype == "response.done":
|
|
@@ -1080,16 +1291,20 @@ async def _chat_receiver(ws, output_device_idx: int | None, verbose: bool, loop:
|
|
|
1080
1291
|
console.print(f"\n[red]error:[/red] {event.get('error', {}).get('message', 'unknown')}")
|
|
1081
1292
|
|
|
1082
1293
|
|
|
1083
|
-
def _build_chat_session_cfg(model: str, language: str | None, system_prompt: str, vad_threshold: float | None) -> dict:
|
|
1294
|
+
def _build_chat_session_cfg(model: str, language: str | None, system_prompt: str, vad_threshold: float | None, tts_model: str | None = None, tts_voice: str | None = None) -> dict:
|
|
1084
1295
|
cfg: dict = {"type": "realtime", "model": model, "input_sample_rate": _SAMPLE_RATE, "system_prompt": system_prompt}
|
|
1085
1296
|
if language:
|
|
1086
1297
|
cfg["language"] = language
|
|
1087
1298
|
if vad_threshold is not None:
|
|
1088
1299
|
cfg["turn_detection"] = {"threshold": vad_threshold / 32768.0}
|
|
1300
|
+
if tts_model:
|
|
1301
|
+
cfg["tts_model"] = tts_model
|
|
1302
|
+
if tts_voice:
|
|
1303
|
+
cfg["tts_voice"] = tts_voice
|
|
1089
1304
|
return cfg
|
|
1090
1305
|
|
|
1091
1306
|
|
|
1092
|
-
async def _chat_async(ws_url: str, device_idx: int | None, output_device_idx: int | None, model: str, language: str | None, system_prompt: str, vad_threshold: float | None, verbose: bool) -> None:
|
|
1307
|
+
async def _chat_async(ws_url: str, device_idx: int | None, output_device_idx: int | None, model: str, language: str | None, system_prompt: str, vad_threshold: float | None, verbose: bool, tts_model: str | None = None, tts_voice: str | None = None) -> None:
|
|
1093
1308
|
audio_q: queue.SimpleQueue = queue.SimpleQueue()
|
|
1094
1309
|
loop = asyncio.get_running_loop()
|
|
1095
1310
|
stop_event = asyncio.Event()
|
|
@@ -1116,7 +1331,7 @@ async def _chat_async(ws_url: str, device_idx: int | None, output_device_idx: in
|
|
|
1116
1331
|
async with websockets.connect(f"{ws_url}/v1/realtime", open_timeout=10) as ws:
|
|
1117
1332
|
await asyncio.wait_for(ws.recv(), timeout=5.0)
|
|
1118
1333
|
|
|
1119
|
-
session_cfg = _build_chat_session_cfg(model, language, system_prompt, vad_threshold)
|
|
1334
|
+
session_cfg = _build_chat_session_cfg(model, language, system_prompt, vad_threshold, tts_model=tts_model, tts_voice=tts_voice)
|
|
1120
1335
|
await ws.send(json.dumps({"type": "session.update", "session": session_cfg}))
|
|
1121
1336
|
await asyncio.wait_for(ws.recv(), timeout=5.0)
|
|
1122
1337
|
|