lxmf: Sideband voice messages (transcribe + Piper voice replies)
The LXMF gateway now handles Sideband push-to-talk voice messages end to end. Inbound: a PTT message carries its audio in LXMF.FIELD_AUDIO (`[audio_mode, bytes]`); `lxmf_transport.parse_audio_field` normalizes it and the adapter transcribes it into an ordinary command when `gateway.lxmf.voice.enabled` is set. HQ Opus/OGG goes through faster-whisper (requirements/transcribe.txt); non-HQ Codec2 through the optional pycodec2 bindings (requirements/codec2.txt). No microphone stack is needed. Outbound: a chat that last messaged by voice is answered with a synthesized voice message when `gateway.lxmf.voice.reply_with_voice` is on (the default). `voice_transport.build_synthesizer` speaks the reply with Piper and `voice_transport.pcm_to_opus_ogg` encodes it to OGG/Opus (48 kHz, PyAV's libopus); `lxmf_transport.audio_fields` attaches it as a Sideband HQ PTT message, with the reply text as the content so a text-only client still sees it. The reply mode follows the chat's most recent inbound message. A missing synthesizer or any synthesis/encode failure is traced and the reply falls back to text. Deps: requirements/tts.txt (piper-tts + PyAV), still no microphone. Also make LXMF replies result-only (`LxmfAdapter.result_only`, overridable via `gateway.lxmf.result_only: false`): send the skill's result line, dropping scheduler bookkeeping and the `<skill>: ok —` wrapper. Wiring: new config keys under `gateway.lxmf` (result_only) and `gateway.lxmf.voice` (reply_with_voice, max_speak_chars, reply_bitrate, tts); regenerated config.example.json; the setup interview asks how to handle voice messages and the install plan adds `install_lxmf_tts` (bootstrap installs requirements/tts.txt and downloads the reply voice); cli anchors the LXMF voice_dir to .runtime/voice/tts. Tests cover the audio field shape, the outbound queue, the adapter's voice-chat tracking and synthesis fallbacks, result-only routing, the synthesis/encode helpers, and the setup plan. Docs updated.
This commit is contained in:
1 parent
b1a7bfd272
commit
ed894bd329
21 files changed
+1804
-78
No files matched your search
@@ -10,6 +10,7 @@ live-daemon integration concern, not a unit test.
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import queue
|
||||
import time
|
||||
|
||||
from semif_agent.cli import _gateway_address_callback
|
||||
@@ -549,6 +550,350 @@ def test_lxmf_denies_unknown_sender_without_dispatch():
|
||||
assert seen == []
|
||||
|
||||
|
||||
def test_lxmf_adapter_is_result_only():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
# The LXMF gateway replies with the skill's result line only (drop the
|
||||
# scheduler's bookkeeping and the `<skill>: ok —` wrapper), overridable via
|
||||
# `gateway.lxmf.result_only: false`.
|
||||
assert LxmfAdapter({}).result_only is True
|
||||
|
||||
|
||||
def test_lxmf_service_sends_result_only(tmp_path):
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
scheduler = build_scheduler(tmp_path)
|
||||
service = GatewayService(scheduler, LxmfAdapter({"allow_all_users": True}))
|
||||
service._reply("lxmf", "c", "time.now: ok — 12:00", kind="result")
|
||||
service._reply("lxmf", "c", "queued", kind="status")
|
||||
service._reply("lxmf", "c", "Which account?", kind="question")
|
||||
assert drain_outbound(service) == ["12:00", "Which account?"]
|
||||
|
||||
|
||||
def test_lxmf_config_can_turn_result_only_off(tmp_path):
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
scheduler = build_scheduler(tmp_path)
|
||||
service = GatewayService(
|
||||
scheduler,
|
||||
LxmfAdapter({"allow_all_users": True}),
|
||||
config={"lxmf": {"result_only": False}},
|
||||
)
|
||||
service._reply("lxmf", "c", "time.now: ok — 12:00", kind="result")
|
||||
service._reply("lxmf", "c", "queued", kind="status")
|
||||
assert drain_outbound(service) == ["time.now: ok — 12:00", "queued"]
|
||||
|
||||
|
||||
# ---- LXMF voice (Sideband push-to-talk) messages ----
|
||||
|
||||
OPUS_AUDIO = {
|
||||
"codec": 0x10,
|
||||
"codec_name": "opus_ogg",
|
||||
"data": b"OggS-payload",
|
||||
"kind": "opus_ogg",
|
||||
"codec2_rate": None,
|
||||
}
|
||||
CODEC2_AUDIO = {
|
||||
"codec": 0x08,
|
||||
"codec_name": "codec2_2400",
|
||||
"data": b"\x01\x02\x03\x04\x05\x06",
|
||||
"kind": "codec2",
|
||||
"codec2_rate": 2400,
|
||||
}
|
||||
|
||||
|
||||
class FakeTranscriber:
|
||||
"""A transcriber double: records the raw payloads, returns fixed text."""
|
||||
|
||||
def __init__(self, text="set a timer for five minutes", error=None):
|
||||
self.text = text
|
||||
self.error = error
|
||||
self.calls: list = []
|
||||
|
||||
def transcribe_bytes(self, data):
|
||||
self.calls.append(data)
|
||||
if self.error is not None:
|
||||
raise self.error
|
||||
return self.text
|
||||
|
||||
|
||||
def _wait(predicate, timeout=2.0):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
if predicate():
|
||||
return True
|
||||
time.sleep(0.02)
|
||||
return False
|
||||
|
||||
|
||||
def test_lxmf_transcribes_voice_message_into_a_command():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
transcriber = FakeTranscriber("what time is it")
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}}, transcriber=transcriber
|
||||
)
|
||||
seen: list = []
|
||||
adapter._on_inbound = seen.append
|
||||
adapter._on_item(
|
||||
{
|
||||
"source_hash": "ab" * 16,
|
||||
"content": "",
|
||||
"display_name": None,
|
||||
"audio": dict(OPUS_AUDIO),
|
||||
}
|
||||
)
|
||||
assert _wait(lambda: seen)
|
||||
assert seen[0].text == "what time is it"
|
||||
assert seen[0].chat_id == "ab" * 16
|
||||
assert transcriber.calls == [b"OggS-payload"]
|
||||
|
||||
|
||||
def test_lxmf_voice_disabled_ignores_audio():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
# A transcriber is present, but `voice.enabled` is not — nothing runs.
|
||||
adapter = LxmfAdapter({"allow_all_users": True}, transcriber=FakeTranscriber("hi"))
|
||||
seen: list = []
|
||||
adapter._on_inbound = seen.append
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
time.sleep(0.1)
|
||||
assert seen == []
|
||||
|
||||
|
||||
def test_lxmf_voice_unsupported_codec_notifies_instead_of_dropping():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter, VOICE_UNSUPPORTED_MESSAGE
|
||||
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber(),
|
||||
)
|
||||
adapter._outbound = queue.Queue()
|
||||
adapter._on_item(
|
||||
{
|
||||
"source_hash": "ab" * 16,
|
||||
"content": "",
|
||||
"audio": {"codec": 0x01, "codec_name": "codec2_450pwb", "data": b"x",
|
||||
"kind": "unknown", "codec2_rate": None},
|
||||
}
|
||||
)
|
||||
message = adapter._outbound.get(timeout=2)
|
||||
assert message.chat_id == "ab" * 16
|
||||
assert message.text == VOICE_UNSUPPORTED_MESSAGE
|
||||
|
||||
|
||||
def test_lxmf_transcribes_codec2_voice_message(monkeypatch):
|
||||
import semif_agent.gateway.lxmf as lxmf
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
monkeypatch.setattr(lxmf, "check_codec2_requirements", lambda: (True, None))
|
||||
decoded = []
|
||||
|
||||
def fake_decode(data, rate):
|
||||
decoded.append((data, rate))
|
||||
return b"\x00\x00" * 320 # 20 ms at 8 kHz
|
||||
|
||||
monkeypatch.setattr(lxmf, "decode_codec2", fake_decode)
|
||||
transcriber = FakeTranscriber("turn on the kitchen light")
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}}, transcriber=transcriber
|
||||
)
|
||||
seen: list = []
|
||||
adapter._on_inbound = seen.append
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(CODEC2_AUDIO)}
|
||||
)
|
||||
assert _wait(lambda: seen)
|
||||
assert seen[0].text == "turn on the kitchen light"
|
||||
assert decoded == [(CODEC2_AUDIO["data"], 2400)]
|
||||
# The Codec2 PCM is wrapped in a WAV before transcription.
|
||||
assert transcriber.calls and transcriber.calls[0][:4] == b"RIFF"
|
||||
|
||||
|
||||
def test_lxmf_codec2_unavailable_notifies(monkeypatch):
|
||||
import semif_agent.gateway.lxmf as lxmf
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter, VOICE_CODEC2_MESSAGE
|
||||
|
||||
monkeypatch.setattr(lxmf, "check_codec2_requirements", lambda: (False, "no pycodec2"))
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber(),
|
||||
)
|
||||
adapter._outbound = queue.Queue()
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(CODEC2_AUDIO)}
|
||||
)
|
||||
message = adapter._outbound.get(timeout=2)
|
||||
assert message.text == VOICE_CODEC2_MESSAGE
|
||||
|
||||
|
||||
def test_lxmf_transcribe_failure_notifies():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter, VOICE_FAILED_MESSAGE
|
||||
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber(error=RuntimeError("boom")),
|
||||
)
|
||||
adapter._outbound = queue.Queue()
|
||||
seen: list = []
|
||||
adapter._on_inbound = seen.append
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
message = adapter._outbound.get(timeout=2)
|
||||
assert message.text == VOICE_FAILED_MESSAGE
|
||||
assert seen == []
|
||||
|
||||
|
||||
def test_lxmf_voice_keeps_caption_when_transcription_fails():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber(error=RuntimeError("boom")),
|
||||
)
|
||||
adapter._outbound = queue.Queue()
|
||||
seen: list = []
|
||||
adapter._on_inbound = seen.append
|
||||
adapter._on_item(
|
||||
{
|
||||
"source_hash": "ab" * 16,
|
||||
"content": "please create a task",
|
||||
"audio": dict(OPUS_AUDIO),
|
||||
}
|
||||
)
|
||||
assert _wait(lambda: seen)
|
||||
assert seen[0].text == "please create a task"
|
||||
assert adapter._outbound.empty()
|
||||
|
||||
|
||||
# ---- LXMF voice replies (Piper -> Opus/OGG) ----
|
||||
|
||||
class FakeSynthesizer:
|
||||
"""A synthesizer double: records texts, returns fixed PCM/rate."""
|
||||
|
||||
def __init__(self, pcm=b"\x00\x00" * 100, sample_rate=22050, error=None):
|
||||
self.pcm = pcm
|
||||
self.sample_rate = sample_rate
|
||||
self.error = error
|
||||
self.calls: list = []
|
||||
|
||||
def synthesize(self, text):
|
||||
self.calls.append(text)
|
||||
if self.error is not None:
|
||||
raise self.error
|
||||
return self.pcm, self.sample_rate
|
||||
|
||||
|
||||
def _outbound(chat, text):
|
||||
from semif_agent.gateway.base import OutboundMessage
|
||||
|
||||
return OutboundMessage(chat_id=chat, text=text)
|
||||
|
||||
|
||||
def test_lxmf_voice_message_marks_chat_and_replies_with_voice(monkeypatch):
|
||||
import semif_agent.gateway.lxmf as lxmf
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
monkeypatch.setattr(lxmf, "pcm_to_opus_ogg", lambda pcm, rate, bitrate=0: b"OggS")
|
||||
synth = FakeSynthesizer()
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber("what time is it"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
# A voice message marks the chat for voice replies.
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
assert "ab" * 16 in adapter._voice_chats
|
||||
audio = adapter._voice_reply(_outbound("ab" * 16, "It is 12:00."))
|
||||
assert audio == b"OggS"
|
||||
assert synth.calls == ["It is 12:00."]
|
||||
|
||||
|
||||
def test_lxmf_text_chat_replies_with_text():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
synth = FakeSynthesizer()
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber("hi"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
# No voice message from this chat: a text reply.
|
||||
assert adapter._voice_reply(_outbound("ab" * 16, "hello")) is None
|
||||
assert synth.calls == []
|
||||
|
||||
|
||||
def test_lxmf_text_message_reverts_chat_to_text_replies():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
synth = FakeSynthesizer()
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber("hi"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
assert "ab" * 16 in adapter._voice_chats
|
||||
adapter._on_item({"source_hash": "ab" * 16, "content": "now in text"})
|
||||
assert "ab" * 16 not in adapter._voice_chats
|
||||
|
||||
|
||||
def test_lxmf_reply_with_voice_disabled_sends_text():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
synth = FakeSynthesizer()
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True, "reply_with_voice": False}},
|
||||
transcriber=FakeTranscriber("hi"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
assert "ab" * 16 not in adapter._voice_chats
|
||||
assert adapter._voice_reply(_outbound("ab" * 16, "hello")) is None
|
||||
|
||||
|
||||
def test_lxmf_voice_reply_falls_back_to_text_on_synth_failure():
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
synth = FakeSynthesizer(error=RuntimeError("piper boom"))
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True}},
|
||||
transcriber=FakeTranscriber("hi"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
assert adapter._voice_reply(_outbound("ab" * 16, "hello")) is None
|
||||
|
||||
|
||||
def test_lxmf_voice_reply_bounds_length(monkeypatch):
|
||||
import semif_agent.gateway.lxmf as lxmf
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
|
||||
monkeypatch.setattr(lxmf, "pcm_to_opus_ogg", lambda pcm, rate, bitrate=0: b"OggS")
|
||||
synth = FakeSynthesizer()
|
||||
adapter = LxmfAdapter(
|
||||
{"allow_all_users": True, "voice": {"enabled": True, "max_speak_chars": 10}},
|
||||
transcriber=FakeTranscriber("hi"),
|
||||
synthesizer=synth,
|
||||
)
|
||||
adapter._on_item(
|
||||
{"source_hash": "ab" * 16, "content": "", "audio": dict(OPUS_AUDIO)}
|
||||
)
|
||||
adapter._voice_reply(_outbound("ab" * 16, "one two three four five six"))
|
||||
assert len(synth.calls[0]) <= 10
|
||||
|
||||
|
||||
# ---- service: multiple platforms ----
|
||||
|
||||
class FakeLxmfAdapter:
|
||||
|
||||
@@ -7,7 +7,14 @@ a live-integration concern.
|
||||
"""
|
||||
|
||||
from semif_agent.gateway.lxmf import LxmfAdapter
|
||||
from semif_agent.lxmf_transport import LxmfDaemon, normalize_message
|
||||
from semif_agent.lxmf_transport import (
|
||||
AM_OPUS_OGG,
|
||||
FIELD_AUDIO,
|
||||
LxmfDaemon,
|
||||
audio_fields,
|
||||
normalize_message,
|
||||
parse_audio_field,
|
||||
)
|
||||
|
||||
|
||||
# ---- requirements ----
|
||||
@@ -94,3 +101,82 @@ def test_normalize_message_handles_none_content():
|
||||
assert normalized["source_hash"] == ""
|
||||
assert normalized["content"] == ""
|
||||
assert normalized["title"] == ""
|
||||
|
||||
|
||||
# ---- voice (Sideband push-to-talk) audio field ----
|
||||
|
||||
def test_parse_audio_field_opus_ogg_uses_the_container_path():
|
||||
audio = parse_audio_field([0x10, b"OggS-payload"])
|
||||
assert audio == {
|
||||
"codec": 0x10,
|
||||
"codec_name": "opus_ogg",
|
||||
"data": b"OggS-payload",
|
||||
"kind": "opus_ogg",
|
||||
"codec2_rate": None,
|
||||
}
|
||||
|
||||
|
||||
def test_parse_audio_field_codec2_maps_to_a_bitrate():
|
||||
audio = parse_audio_field([0x08, b"\x00\x01"])
|
||||
assert audio["codec_name"] == "codec2_2400"
|
||||
assert audio["kind"] == "codec2"
|
||||
assert audio["codec2_rate"] == 2400
|
||||
|
||||
|
||||
def test_parse_audio_field_450_codec2_has_no_binding():
|
||||
# 450PWB/450 have no pycodec2 bindings, so they stay undecodable.
|
||||
audio = parse_audio_field([0x01, b"\x00\x01"])
|
||||
assert audio["kind"] == "unknown"
|
||||
assert audio["codec2_rate"] is None
|
||||
|
||||
|
||||
def test_parse_audio_field_rejects_malformed():
|
||||
assert parse_audio_field("nope") is None
|
||||
assert parse_audio_field([0x10]) is None
|
||||
assert parse_audio_field([0x10, "not-bytes"]) is None
|
||||
assert parse_audio_field(["x", b"data"]) is None
|
||||
|
||||
|
||||
def test_normalize_message_extracts_audio_field():
|
||||
class _Msg:
|
||||
source_hash = bytes.fromhex("ab" * 16)
|
||||
content = b""
|
||||
title = b""
|
||||
fields = {0x07: [0x10, b"OggS"]}
|
||||
|
||||
normalized = normalize_message(_Msg())
|
||||
assert normalized["audio"]["codec_name"] == "opus_ogg"
|
||||
assert normalized["audio"]["data"] == b"OggS"
|
||||
assert normalized["audio"]["kind"] == "opus_ogg"
|
||||
assert normalized["fields"][0x07][0] == 0x10
|
||||
|
||||
|
||||
def test_normalize_message_without_fields_has_no_audio():
|
||||
class _Msg:
|
||||
content = b"plain text"
|
||||
|
||||
normalized = normalize_message(_Msg())
|
||||
assert normalized["audio"] is None
|
||||
assert normalized["fields"] == {}
|
||||
|
||||
|
||||
# ---- outbound voice replies ----
|
||||
|
||||
def test_audio_fields_wraps_ogg_as_sideband_hq_ptt():
|
||||
fields = audio_fields(b"OggS-payload")
|
||||
assert fields == {FIELD_AUDIO: [AM_OPUS_OGG, b"OggS-payload"]}
|
||||
assert AM_OPUS_OGG == 0x10
|
||||
|
||||
|
||||
def test_audio_fields_none_for_text_or_empty():
|
||||
assert audio_fields(None) is None
|
||||
assert audio_fields(b"") is None
|
||||
|
||||
|
||||
def test_enqueue_carries_audio_on_the_outbound_queue():
|
||||
daemon = LxmfDaemon(config_dir="x", storage_path="y")
|
||||
daemon.enqueue("ab" * 16, "hello", audio=b"OggS")
|
||||
assert daemon._outbound.get_nowait() == ("ab" * 16, "hello", b"OggS")
|
||||
# The stop sentinel is unchanged.
|
||||
daemon.enqueue(None)
|
||||
assert daemon._outbound.get_nowait() is None
|
||||
@@ -87,6 +87,7 @@ def test_minimum_plan_installs_nothing_heavy():
|
||||
assert plan["install_simplex_forward"] is False
|
||||
assert plan["install_browser"] is False
|
||||
assert plan["install_voice"] is False
|
||||
assert plan["install_transcribe"] is False
|
||||
assert plan["packs"] == ["essentials"]
|
||||
assert plan["pack_requirements"] == []
|
||||
|
||||
@@ -177,6 +178,41 @@ def test_gateway_platforms_exclude_voice():
|
||||
assert plan["install_gateway_service"] is True
|
||||
|
||||
|
||||
def test_lxmf_voice_opts_into_transcription_dep():
|
||||
answers = Answers(gateways=["lxmf"], lxmf_voice=True)
|
||||
config = build_config(answers, _packs())
|
||||
assert config["gateway"]["lxmf"]["voice"]["enabled"] is True
|
||||
assert config["gateway"]["lxmf"]["voice"]["reply_with_voice"] is True
|
||||
assert validate_config(config, _packs()) == []
|
||||
|
||||
plan = build_plan(answers, _packs())
|
||||
assert plan["install_transcribe"] is True
|
||||
# A voice reply also pulls the Piper TTS deps.
|
||||
assert plan["install_lxmf_tts"] is True
|
||||
# The rerun path re-derives the same plan from the written config.
|
||||
assert plan_from_config(config, _packs())["install_transcribe"] is True
|
||||
assert plan_from_config(config, _packs())["install_lxmf_tts"] is True
|
||||
|
||||
|
||||
def test_lxmf_voice_transcribe_only_skips_tts_dep():
|
||||
answers = Answers(gateways=["lxmf"], lxmf_voice=True, lxmf_voice_reply=False)
|
||||
config = build_config(answers, _packs())
|
||||
assert config["gateway"]["lxmf"]["voice"]["enabled"] is True
|
||||
assert config["gateway"]["lxmf"]["voice"]["reply_with_voice"] is False
|
||||
plan = build_plan(answers, _packs())
|
||||
assert plan["install_transcribe"] is True
|
||||
assert plan["install_lxmf_tts"] is False
|
||||
assert plan_from_config(config, _packs())["install_lxmf_tts"] is False
|
||||
|
||||
|
||||
def test_lxmf_without_voice_does_not_install_transcription():
|
||||
answers = Answers(gateways=["lxmf"])
|
||||
config = build_config(answers, _packs())
|
||||
assert config["gateway"]["lxmf"]["voice"]["enabled"] is False
|
||||
assert build_plan(answers, _packs())["install_transcribe"] is False
|
||||
assert build_plan(answers, _packs())["install_lxmf_tts"] is False
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------- #
|
||||
# Interview branching
|
||||
# --------------------------------------------------------------------------- #
|
||||
@@ -201,6 +237,7 @@ def test_interview_collects_authoring_endpoints_and_packs():
|
||||
"bob", # nextcloud username
|
||||
"pw", # nextcloud app password
|
||||
"b", # gateways: lxmf
|
||||
"3", # lxmf voice: transcribe + reply with voice
|
||||
"Europe/Paris", # timezone
|
||||
]
|
||||
)
|
||||
@@ -223,6 +260,8 @@ def test_interview_collects_authoring_endpoints_and_packs():
|
||||
assert answers.packs == ["essentials", "simplex", "nextcloud"]
|
||||
assert answers.nextcloud_url == "https://nc.example"
|
||||
assert answers.gateways == ["lxmf"]
|
||||
assert answers.lxmf_voice is True
|
||||
assert answers.lxmf_voice_reply is True
|
||||
assert answers.timezone == "Europe/Paris"
|
||||
|
||||
|
||||
|
||||
@@ -459,6 +459,144 @@ def test_resolve_platforms_includes_enabled_voice():
|
||||
# ---- wake cue (beep) -------------------------------------------------------
|
||||
|
||||
|
||||
def test_transcription_requirements_never_raises():
|
||||
from semif_agent.voice_transport import check_transcription_requirements
|
||||
|
||||
ok, hint = check_transcription_requirements()
|
||||
assert isinstance(ok, bool)
|
||||
if not ok:
|
||||
assert "faster-whisper" in hint
|
||||
|
||||
|
||||
def test_check_codec2_requirements_never_raises():
|
||||
from semif_agent.voice_transport import check_codec2_requirements
|
||||
|
||||
ok, hint = check_codec2_requirements()
|
||||
assert isinstance(ok, bool)
|
||||
if not ok:
|
||||
assert "pycodec2" in hint
|
||||
|
||||
|
||||
def test_decode_codec2_without_a_full_frame_is_none():
|
||||
from semif_agent.voice_transport import decode_codec2
|
||||
|
||||
# Nothing to decode (shorter than a frame, empty, or no bitrate) -> None,
|
||||
# whether or not pycodec2 is installed.
|
||||
assert decode_codec2(b"x", 2400) is None
|
||||
assert decode_codec2(b"", 2400) is None
|
||||
assert decode_codec2(b"data", 0) is None
|
||||
|
||||
|
||||
def test_decode_codec2_uses_bindings(monkeypatch):
|
||||
import sys
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
np = pytest.importorskip("numpy")
|
||||
calls = []
|
||||
|
||||
class FakeCodec2:
|
||||
def __init__(self, rate):
|
||||
self.rate = rate
|
||||
|
||||
def bytes_per_frame(self):
|
||||
return 4
|
||||
|
||||
def samples_per_frame(self):
|
||||
return 8
|
||||
|
||||
def decode(self, data):
|
||||
calls.append(data)
|
||||
return np.full(8, 7, dtype=np.int16)
|
||||
|
||||
fake = types.ModuleType("pycodec2")
|
||||
fake.Codec2 = FakeCodec2
|
||||
monkeypatch.setitem(sys.modules, "pycodec2", fake)
|
||||
from semif_agent.voice_transport import decode_codec2
|
||||
|
||||
pcm = decode_codec2(b"aaaa" * 2, 2400) # two 4-byte frames
|
||||
assert len(pcm) == 2 * 8 * 2 # frames * samples/frame * 2 bytes
|
||||
assert calls == [b"aaaa", b"aaaa"]
|
||||
assert pcm[:2] == b"\x07\x00"
|
||||
|
||||
|
||||
def test_pcm_to_wav_is_a_readable_wav():
|
||||
import io
|
||||
import wave
|
||||
|
||||
from semif_agent.voice_transport import pcm_to_wav
|
||||
|
||||
pcm = b"\x01\x00\x02\x00"
|
||||
data = pcm_to_wav(pcm, 8000)
|
||||
assert data[:4] == b"RIFF"
|
||||
with wave.open(io.BytesIO(data), "rb") as wav:
|
||||
assert wav.getnchannels() == 1
|
||||
assert wav.getsampwidth() == 2
|
||||
assert wav.getframerate() == 8000
|
||||
assert wav.readframes(2) == pcm
|
||||
|
||||
|
||||
# ---- voice replies (Piper + Opus/OGG) --------------------------------------
|
||||
|
||||
|
||||
def test_synthesis_requirements_never_raises():
|
||||
from semif_agent.voice_transport import check_synthesis_requirements
|
||||
|
||||
ok, hint = check_synthesis_requirements()
|
||||
assert isinstance(ok, bool)
|
||||
if not ok:
|
||||
assert "piper" in hint
|
||||
|
||||
|
||||
def test_build_synthesizer_reads_the_tts_block(monkeypatch):
|
||||
import semif_agent.voice_transport as vt
|
||||
|
||||
captured = {}
|
||||
|
||||
class FakePiper:
|
||||
def __init__(self, voice, speaker_id=0, length_scale=1.0, volume=1.0):
|
||||
captured.update(
|
||||
voice=voice, speaker_id=speaker_id,
|
||||
length_scale=length_scale, volume=volume,
|
||||
)
|
||||
|
||||
monkeypatch.setattr(vt, "PiperSynthesizer", FakePiper)
|
||||
vt.build_synthesizer(
|
||||
{"tts": {"voice": "en_US-lessac-medium", "speaker_id": 3, "length_scale": 0.9}}
|
||||
)
|
||||
assert captured["voice"] == "en_US-lessac-medium"
|
||||
assert captured["speaker_id"] == 3
|
||||
assert captured["length_scale"] == 0.9
|
||||
|
||||
|
||||
def test_pcm_to_opus_ogg_empty_input_is_empty():
|
||||
from semif_agent.voice_transport import pcm_to_opus_ogg
|
||||
|
||||
assert pcm_to_opus_ogg(b"", 22050) == b""
|
||||
assert pcm_to_opus_ogg(b"\x01", 22050) == b""
|
||||
|
||||
|
||||
def test_pcm_to_opus_ogg_encodes_when_pyav_is_present():
|
||||
import pytest
|
||||
|
||||
pytest.importorskip("av")
|
||||
import io
|
||||
|
||||
import av
|
||||
|
||||
from semif_agent.voice_transport import pcm_to_opus_ogg
|
||||
|
||||
# 0.5 s of 22.05 kHz silence -> resampled + encoded to OGG/Opus.
|
||||
pcm = b"\x00\x00" * 11025
|
||||
data = pcm_to_opus_ogg(pcm, 22050)
|
||||
assert data[:4] == b"OggS"
|
||||
container = av.open(io.BytesIO(data))
|
||||
stream = container.streams.audio[0]
|
||||
assert stream.codec_context.name == "opus"
|
||||
assert stream.sample_rate == 48000
|
||||
|
||||
|
||||
def test_tone_pcm_length():
|
||||
from semif_agent.voice_transport import _tone_pcm
|
||||
|
||||
|
||||
Reference in new issue
Block a user