From 60f05cebd8e7968621a0ce015cf72987ec7577c7 Mon Sep 17 00:00:00 2001 From: "Somhairle H. Marisol" Date: Tue, 29 Sep 2026 10:50:12 +0800 Subject: feat(recorder): add offline voice clip pipeline MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [变更性质] - 本提交新增本机离线的语音片段录音能力,不涉及网络服务或语音识别。 [新增功能] - 通过 Pulse 默认输入持续采集音频,以本地 Silero VAD 触发片段。 - 以私有目录和权限写入 24 kbps Ogg/Opus 录音,并提供 systemd 用户服务安装器。 [实现方案] - 使用有限前置缓冲、静音封段和最长段滚动状态机,避免静音落盘及内存无限增长。 - 增加真实 Opus 编解码、服务 dry-run 和纯逻辑状态转换测试;记录模型归属和部署前置条件。 [影响范围] - 新增 Python CLI、运行时模块、中文运维文档和自动测试。 - 未安装、启用或启动任何用户 systemd 服务;不删除或修改录音数据。 --- tests/test_runtime.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/test_runtime.py (limited to 'tests/test_runtime.py') diff --git a/tests/test_runtime.py b/tests/test_runtime.py new file mode 100644 index 0000000..d0d45fb --- /dev/null +++ b/tests/test_runtime.py @@ -0,0 +1,82 @@ +from datetime import datetime, timezone + +import numpy as np + +from mic_clipper.audio_input import PulseAudioInput +from mic_clipper.runner import record_frames +from mic_clipper.segmenter import Segmenter + + +class FakeVad: + def __init__(self, probabilities: list[float]) -> None: + self.probabilities = iter(probabilities) + + def probability(self, _samples: np.ndarray) -> float: + return next(self.probabilities) + + +class FakeClip: + def __init__(self) -> None: + self.writes: list[np.ndarray] = [] + self.closed = False + + def write(self, samples: np.ndarray) -> None: + self.writes.append(samples) + + def close(self) -> None: + self.closed = True + + def abort(self) -> None: + self.closed = True + + +class FakeWriter: + def __init__(self) -> None: + self.opened: list[FakeClip] = [] + + def open(self, _started_at: datetime) -> FakeClip: + clip = FakeClip() + self.opened.append(clip) + return clip + + +def samples() -> np.ndarray: + return np.zeros(512, dtype=np.float32) + + +def test_silence_never_opens_an_output_clip(): + writer = FakeWriter() + + record_frames( + [samples(), samples()], + vad=FakeVad([0.1, 0.1]), + writer=writer, + segmenter=Segmenter(pre_roll_seconds=0.5, silence_seconds=1.5), + now=lambda: datetime(2026, 9, 29, tzinfo=timezone.utc), + ) + + assert writer.opened == [] + + +def test_end_event_closes_the_active_encoder(): + writer = FakeWriter() + + record_frames( + [samples()] + [samples()] * 47, + vad=FakeVad([0.9] + [0.1] * 47), + writer=writer, + segmenter=Segmenter(pre_roll_seconds=0.5, silence_seconds=1.5), + now=lambda: datetime(2026, 9, 29, tzinfo=timezone.utc), + ) + + assert len(writer.opened) == 1 + assert writer.opened[0].closed + + +def test_pulse_input_uses_dynamic_default_device_at_16khz_mono(): + command = PulseAudioInput.command() + + assert command[0] == "ffmpeg" + assert command[command.index("-f") + 1] == "pulse" + assert command[command.index("-i") + 1] == "default" + assert command[-2:] == ["f32le", "pipe:1"] -- cgit v1.2.3