|
@@ -1,24 +1,50 @@
|
|
|
"""Output sinks for transcripts."""
|
|
"""Output sinks for transcripts."""
|
|
|
|
|
|
|
|
import json
|
|
import json
|
|
|
|
|
+import wave
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
+import numpy as np
|
|
|
|
|
+
|
|
|
from .transcriber import TranscriptResult
|
|
from .transcriber import TranscriptResult
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def write_wav(path: Path, audio: np.ndarray, sample_rate: int) -> None:
|
|
|
|
|
+ """Write float32 mono audio in [-1, 1] to a 16-bit PCM WAV file."""
|
|
|
|
|
+ pcm = (np.clip(audio, -1.0, 1.0) * 32767.0).astype(np.int16)
|
|
|
|
|
+ with wave.open(str(path), "wb") as wav:
|
|
|
|
|
+ wav.setnchannels(1)
|
|
|
|
|
+ wav.setsampwidth(2)
|
|
|
|
|
+ wav.setframerate(sample_rate)
|
|
|
|
|
+ wav.writeframes(pcm.tobytes())
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
class TranscriptSink:
|
|
class TranscriptSink:
|
|
|
"""Emits transcripts to stdout and/or JSONL files."""
|
|
"""Emits transcripts to stdout and/or JSONL files."""
|
|
|
|
|
|
|
|
- def __init__(self, stdout: bool = True, jsonl_dir: str | None = None):
|
|
|
|
|
|
|
+ def __init__(
|
|
|
|
|
+ self,
|
|
|
|
|
+ stdout: bool = True,
|
|
|
|
|
+ jsonl_dir: str | None = None,
|
|
|
|
|
+ save_audio: bool = False,
|
|
|
|
|
+ audio_dir: str = "audio",
|
|
|
|
|
+ sample_rate: int = 16000,
|
|
|
|
|
+ ):
|
|
|
"""Initialize output sinks.
|
|
"""Initialize output sinks.
|
|
|
|
|
|
|
|
Args:
|
|
Args:
|
|
|
stdout: If True, emit to stdout.
|
|
stdout: If True, emit to stdout.
|
|
|
jsonl_dir: If set, append JSONL lines to daily files in this directory.
|
|
jsonl_dir: If set, append JSONL lines to daily files in this directory.
|
|
|
|
|
+ save_audio: If True, write each transcript's audio as a WAV file.
|
|
|
|
|
+ audio_dir: Directory for WAV files (dated subfolders).
|
|
|
|
|
+ sample_rate: Audio sample rate, written into the WAV header.
|
|
|
"""
|
|
"""
|
|
|
self.stdout = stdout
|
|
self.stdout = stdout
|
|
|
self.jsonl_dir = Path(jsonl_dir) if jsonl_dir else None
|
|
self.jsonl_dir = Path(jsonl_dir) if jsonl_dir else None
|
|
|
|
|
+ self.save_audio = save_audio
|
|
|
|
|
+ self.audio_dir = Path(audio_dir)
|
|
|
|
|
+ self.sample_rate = sample_rate
|
|
|
|
|
|
|
|
def emit(
|
|
def emit(
|
|
|
self,
|
|
self,
|
|
@@ -26,6 +52,7 @@ class TranscriptSink:
|
|
|
duration_s: float,
|
|
duration_s: float,
|
|
|
rms: float = 0.0,
|
|
rms: float = 0.0,
|
|
|
reason: str = "unknown",
|
|
reason: str = "unknown",
|
|
|
|
|
+ audio: np.ndarray | None = None,
|
|
|
) -> None:
|
|
) -> None:
|
|
|
"""Emit a transcript result.
|
|
"""Emit a transcript result.
|
|
|
|
|
|
|
@@ -34,10 +61,15 @@ class TranscriptSink:
|
|
|
duration_s: Duration of the utterance in seconds.
|
|
duration_s: Duration of the utterance in seconds.
|
|
|
rms: Root-mean-square level of the utterance audio.
|
|
rms: Root-mean-square level of the utterance audio.
|
|
|
reason: Why the utterance ended (end_of_turn/pause/hard_cap/flush).
|
|
reason: Why the utterance ended (end_of_turn/pause/hard_cap/flush).
|
|
|
|
|
+ audio: Utterance samples (float32, mono); saved when save_audio is enabled.
|
|
|
"""
|
|
"""
|
|
|
if not result.text:
|
|
if not result.text:
|
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
+ audio_path = None
|
|
|
|
|
+ if self.save_audio and audio is not None:
|
|
|
|
|
+ audio_path = self._save_audio(audio, reason)
|
|
|
|
|
+
|
|
|
# Stdout line: [HH:MM:SS] (lang) text [rms=0.041 end_of_turn]
|
|
# Stdout line: [HH:MM:SS] (lang) text [rms=0.041 end_of_turn]
|
|
|
if self.stdout:
|
|
if self.stdout:
|
|
|
now = datetime.now()
|
|
now = datetime.now()
|
|
@@ -60,11 +92,22 @@ class TranscriptSink:
|
|
|
"end_reason": reason,
|
|
"end_reason": reason,
|
|
|
"text": result.text,
|
|
"text": result.text,
|
|
|
}
|
|
}
|
|
|
|
|
+ if audio_path is not None:
|
|
|
|
|
+ record["audio"] = str(audio_path)
|
|
|
|
|
|
|
|
with open(jsonl_path, "a") as f:
|
|
with open(jsonl_path, "a") as f:
|
|
|
json.dump(record, f, ensure_ascii=False)
|
|
json.dump(record, f, ensure_ascii=False)
|
|
|
f.write("\n")
|
|
f.write("\n")
|
|
|
|
|
|
|
|
|
|
+ def _save_audio(self, audio: np.ndarray, reason: str) -> Path:
|
|
|
|
|
+ now = datetime.now()
|
|
|
|
|
+ day_dir = self.audio_dir / now.strftime("%Y-%m-%d")
|
|
|
|
|
+ day_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
+ name = f"{now.strftime('%H%M%S')}-{now.strftime('%f')[:3]}-{reason}.wav"
|
|
|
|
|
+ path = day_dir / name
|
|
|
|
|
+ write_wav(path, audio, self.sample_rate)
|
|
|
|
|
+ return path
|
|
|
|
|
+
|
|
|
def close(self) -> None:
|
|
def close(self) -> None:
|
|
|
"""Clean up (no-op for file-based output)."""
|
|
"""Clean up (no-op for file-based output)."""
|
|
|
pass
|
|
pass
|