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:
gabby committed 2026-10-17 04:53:51 +00:00
1 parent b1a7bfd272
commit ed894bd329
21 files changed
+1804 -78

No files matched your search

+87 -1
View File
@@ -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