#!/usr/bin/env python3 """Shared deterministic helpers for HFD third-party audio construction.""" from __future__ import annotations import hashlib import json import math import os import re import subprocess import wave from pathlib import Path from typing import Any, Iterable, Sequence import numpy as np SAMPLE_RATE = 16_000 WINDOW_SAMPLES = 9_600 STRIDE_SAMPLES = 6_400 OVERLAP_SAMPLES = WINDOW_SAMPLES - STRIDE_SAMPLES PCM16_SCALE = np.float32(32768.0) def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def canonical_json_bytes(payload: Any) -> bytes: return json.dumps( payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"), ).encode("utf-8") def canonical_json_sha256(payload: Any) -> str: return hashlib.sha256(canonical_json_bytes(payload)).hexdigest() def stable_u64(*parts: object) -> int: payload = "\0".join(str(part) for part in parts).encode("utf-8") return int.from_bytes(hashlib.sha256(payload).digest()[:8], "big") def write_json_atomic(path: Path, payload: Any) -> None: path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp") temporary.write_text( json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True) + "\n", encoding="utf-8", ) os.replace(temporary, path) def write_jsonl_atomic(path: Path, rows: Iterable[dict[str, Any]]) -> None: path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp") with temporary.open("w", encoding="utf-8") as handle: for row in rows: handle.write( json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n" ) os.replace(temporary, path) def read_jsonl(path: Path) -> list[dict[str, Any]]: rows: list[dict[str, Any]] = [] with path.open("r", encoding="utf-8") as handle: for line_number, line in enumerate(handle, 1): if not line.strip(): continue payload = json.loads(line) if not isinstance(payload, dict): raise ValueError(f"Expected JSON object at {path}:{line_number}") rows.append(payload) return rows def decode_audio_float(path: Path, sample_rate: int = SAMPLE_RATE) -> np.ndarray: process = subprocess.run( [ "ffmpeg", "-v", "error", "-i", str(path), "-ac", "1", "-ar", str(sample_rate), "-f", "f32le", "pipe:1", ], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) audio = np.frombuffer(process.stdout, dtype=" np.ndarray: with wave.open(str(path), "rb") as handle: contract = ( handle.getnchannels(), handle.getsampwidth(), handle.getframerate(), handle.getcomptype(), ) expected = (1, 2, sample_rate, "NONE") if contract != expected: raise ValueError( f"Expected mono PCM16 {sample_rate} Hz WAV at {path}, " f"got {contract}" ) raw = handle.readframes(handle.getnframes()) return np.frombuffer(raw, dtype=" np.ndarray: value = np.asarray(audio, dtype=np.float32).reshape(-1) if not np.isfinite(value).all(): raise ValueError("Audio contains NaN or Inf") return ( np.rint(value * PCM16_SCALE) .clip(-32768, 32767) .astype(" None: pcm = quantize_pcm16(audio) path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp") with wave.open(str(temporary), "wb") as handle: handle.setnchannels(1) handle.setsampwidth(2) handle.setframerate(sample_rate) handle.writeframes(pcm.tobytes()) os.replace(temporary, path) def active_rms(audio: np.ndarray) -> float: value = np.asarray(audio, dtype=np.float32).reshape(-1) if value.size == 0: return 0.0 peak = float(np.max(np.abs(value), initial=0.0)) threshold = max(1e-5, peak * 0.02) active = value[np.abs(value) >= threshold] if active.size == 0: active = value return float(np.sqrt(np.mean(np.square(active), dtype=np.float64))) def audio_stats(audio: np.ndarray) -> dict[str, float | int]: value = np.asarray(audio, dtype=np.float32).reshape(-1) if value.size == 0 or not np.isfinite(value).all(): raise ValueError("Cannot summarize empty or non-finite audio") return { "sample_count": int(value.size), "duration_seconds": float(value.size / SAMPLE_RATE), "peak": float(np.max(np.abs(value))), "rms": float(np.sqrt(np.mean(np.square(value), dtype=np.float64))), "active_rms": active_rms(value), } def gain_for_db(reference_rms: float, interference_rms: float, db: float) -> float: if reference_rms <= 0.0 or interference_rms <= 0.0: raise ValueError( f"RMS values must be positive: reference={reference_rms} " f"interference={interference_rms}" ) return float(reference_rms / (interference_rms * math.pow(10.0, db / 20.0))) def measured_db(reference_rms: float, interference_rms: float) -> float: if reference_rms <= 0.0 or interference_rms <= 0.0: raise ValueError("Cannot measure a dB ratio with zero RMS") return float(20.0 * math.log10(reference_rms / interference_rms)) def signal_to_residual_db_on_active_signal( signal: np.ndarray, mixture: np.ndarray, ) -> float: target = np.asarray(signal, dtype=np.float32).reshape(-1) combined = np.asarray(mixture, dtype=np.float32).reshape(-1) if target.shape != combined.shape or target.size == 0: raise ValueError( f"Signal and mixture must have the same non-empty shape: " f"{target.shape} != {combined.shape}" ) if not np.isfinite(target).all() or not np.isfinite(combined).all(): raise ValueError("Signal or mixture contains NaN or Inf") peak = float(np.max(np.abs(target), initial=0.0)) threshold = max(1e-5, peak * 0.02) mask = np.abs(target) >= threshold if not np.any(mask): raise ValueError("Signal has no active samples") target_rms = float( np.sqrt(np.mean(np.square(target[mask]), dtype=np.float64)) ) residual = combined[mask] - target[mask] residual_rms = float( np.sqrt(np.mean(np.square(residual), dtype=np.float64)) ) if residual_rms <= 1e-12: return float("inf") return measured_db(target_rms, residual_rms) def reconstruct_overlapping_windows( windows: Sequence[np.ndarray], *, atol: float = 1.0 / 32768.0, ) -> np.ndarray: if not windows: raise ValueError("At least one audio window is required") normalized = [np.asarray(window, dtype=np.float32).reshape(-1) for window in windows] if any(window.shape != (WINDOW_SAMPLES,) for window in normalized): raise ValueError("All HFD windows must contain exactly 9,600 samples") for previous, current in zip(normalized, normalized[1:]): if not np.allclose( previous[-OVERLAP_SAMPLES:], current[:OVERLAP_SAMPLES], rtol=0.0, atol=atol, ): raise ValueError("Adjacent HFD windows disagree in their overlap") pieces = [normalized[0]] pieces.extend(window[OVERLAP_SAMPLES:] for window in normalized[1:]) return np.ascontiguousarray(np.concatenate(pieces), dtype=np.float32) def expected_window_count(sample_count: int) -> int: count = int(sample_count) if count < WINDOW_SAMPLES: return 0 remainder = count - WINDOW_SAMPLES if remainder % STRIDE_SAMPLES != 0: raise ValueError( f"Waveform length is off the HFD grid: samples={sample_count}" ) return remainder // STRIDE_SAMPLES + 1 def normalize_transcript(text: str) -> str: return " ".join(re.findall(r"[a-z]+(?:'[a-z]+)?", text.lower())) COMMAND_WORDS = frozenset( { "backward", "bend", "bow", "clap", "close", "come", "continue", "crawl", "dance", "down", "forward", "freeze", "go", "hold", "jump", "kick", "kneel", "lean", "left", "lift", "look", "lower", "move", "nod", "open", "pause", "point", "punch", "raise", "reach", "right", "rotate", "run", "salute", "shake", "sit", "spin", "squat", "stand", "step", "stop", "stretch", "swing", "turn", "up", "wait", "walk", "wave", } ) def semantic_filter_reason( transcript: str, exact_command_phrases: set[str] | None = None, ) -> str | None: normalized = normalize_transcript(transcript) words = normalized.split() if len(words) < 4: return "too_few_words" matches = sorted(COMMAND_WORDS.intersection(words)) if matches: return "command_words:" + ",".join(matches) for phrase in sorted(exact_command_phrases or set()): if len(phrase.split()) >= 2 and phrase in normalized: return f"target_phrase:{phrase}" return None