Files
LangBot/tests/unit_tests/test_audio_utils.py
43c0ba7e44 feat(speech): implement TTS and STT model layer and pipeline stages (LANG-251) (#2839)
* feat(speech): implement TTS and STT model layer and pipeline stages (LANG-251)

Co-authored-by: multica-agent <github@multica.ai>

* fix(speech): fix invoke signatures and provider lookup in speech stages and model testing

Co-authored-by: multica-agent <github@multica.ai>

* feat(speech): rename STT to ASR, improve TTS voice parameter handling and failure recovery

Co-authored-by: multica-agent <github@multica.ai>

* fix(persistence, lint, web): fix migration revision id, legacy table check, ruff duplicates, and frontend types

Co-authored-by: multica-agent <github@multica.ai>

* fix(speech): restore migration and CI contracts

Co-authored-by: multica-agent <github@multica.ai>

* fix(pipeline,ui): fix select dropdown width gap, add speech to guided tour, and configure show_if for speech options

Co-authored-by: multica-agent <github@multica.ai>

* feat(speech,openclaw): support silk audio transcoding, graceful fallback, duration estimation, and multi-lang deploy docs

Co-authored-by: multica-agent <github@multica.ai>

* fix(audio): enhance ffmpeg executable detection via venv and imageio_ffmpeg

Co-authored-by: multica-agent <github@multica.ai>

* fix(provider): improve LiteLLM error classification and avoid falling back to Unknown error

Co-authored-by: multica-agent <github@multica.ai>

* fix(openclaw): serialize voice_item attributes and improve transcoding fallback logging

Co-authored-by: multica-agent <github@multica.ai>

* fix(audio,openclaw): handle transient TMPDIR deletion and add size attribute to voice_item

Co-authored-by: multica-agent <github@multica.ai>

* fix(openclaw): download and convert inbound voice messages and align voice sending protocol

Co-authored-by: multica-agent <github@multica.ai>

* fix(models): redesign add model popover tabs layout and include ASR/TTS models in provider counts and scanning

Co-authored-by: multica-agent <github@multica.ai>

* fix(audio): decrypt and transcode wecombot voice to mp3, align openclaw weixin voice delivery

Co-authored-by: multica-agent <github@multica.ai>

* fix(wecombot): transcode outbound voice to silk and support voice_delivery mode

Co-authored-by: multica-agent <github@multica.ai>

* fix(discord,web): map audio attachments to voice for ASR, prefer base64 voice in webui

Co-authored-by: multica-agent <github@multica.ai>

* fix(telegram): deliver voice replies in streaming and non-streaming modes with quote origin

Co-authored-by: multica-agent <github@multica.ai>

* feat(dingtalk): route voice messages through pipeline ASR and send TTS replies as voice

Inbound DingTalk audio now arrives as a Voice component with its duration,
so the pipeline speech recognition switch decides how it is transcribed.
DingTalk's own recognition text is used only when the audio cannot be
downloaded.

Outbound Voice components are transcoded to ogg/opus, uploaded as voice
media and sent as native sampleAudio messages in both the omni and legacy
adapters. Audio over 60 seconds, or audio that cannot be transcoded or is
rejected as a voice message, is sent as a file instead. Voice parts no longer
leak into the reply text as "[Voice]".

Also estimate ogg durations from the granule position and ignore non-audio
MIME hints when extracting voice bytes, so ffmpeg can probe the input.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* fix(ci): include speech model counts in provider responses and fix lint/image build

- Copy every count returned by get_provider_model_counts into the provider
  payload instead of indexing asr_count/tts_count, and update the test fakes
  to return the speech model counts.
- Install libc6-dev in the runtime image so the pilk audio extra can compile.
- Apply ruff and prettier formatting required by the lint jobs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

* test(e2e): expect the speech step in the pipeline setup guide

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 23:51:22 +08:00

96 lines
4.4 KiB
Python

from __future__ import annotations
import io
import unittest
from unittest.mock import patch
import wave
import langbot_plugin.api.entities.builtin.platform.message as platform_message
from langbot.pkg.utils import audio as audio_utils
def _create_sample_wav() -> bytes:
buf = io.BytesIO()
with wave.open(buf, 'wb') as wav:
wav.setnchannels(1)
wav.setsampwidth(2)
wav.setframerate(16000)
wav.writeframes(b'\x00' * 3200)
return buf.getvalue()
class TestAudioUtils(unittest.IsolatedAsyncioTestCase):
def test_detect_audio_format(self):
sample_wav = _create_sample_wav()
self.assertEqual(audio_utils.detect_audio_format(sample_wav), 'wav')
self.assertEqual(audio_utils.detect_audio_format(b'#!AMR\n12345'), 'amr')
self.assertEqual(audio_utils.detect_audio_format(b'#!SILK_V3\n'), 'silk')
self.assertEqual(audio_utils.detect_audio_format(b'\x02#!SILK_V3\n'), 'silk')
self.assertEqual(audio_utils.detect_audio_format(b'OggS\x00\x02...OpusHead...'), 'opus')
self.assertEqual(audio_utils.detect_audio_format(b'OggS\x00\x02...'), 'ogg')
self.assertEqual(audio_utils.detect_audio_format(b'ID3\x03\x00\x00'), 'mp3')
self.assertEqual(audio_utils.detect_audio_format(b'\xff\xfb\x90\x64'), 'mp3')
self.assertEqual(audio_utils.detect_audio_format(b'fLaC\x00\x00'), 'flac')
self.assertEqual(audio_utils.detect_audio_format(b''), 'unknown')
self.assertEqual(audio_utils.detect_audio_format(b'12'), 'unknown')
def test_is_ffmpeg_and_silk_available(self):
with patch.object(audio_utils.shutil, 'which', return_value='/usr/bin/ffmpeg'):
self.assertTrue(audio_utils.is_ffmpeg_available())
with (
patch.object(audio_utils.shutil, 'which', return_value=None),
patch.object(audio_utils.os.path, 'isfile', return_value=False),
patch.dict('sys.modules', {'imageio_ffmpeg': None}),
):
self.assertFalse(audio_utils.is_ffmpeg_available())
with patch.object(audio_utils.importlib.util, 'find_spec', return_value=object()):
self.assertTrue(audio_utils.is_silk_available())
with patch.object(audio_utils.importlib.util, 'find_spec', return_value=None):
self.assertFalse(audio_utils.is_silk_available())
@unittest.skipUnless(audio_utils.is_ffmpeg_available(), 'ffmpeg is not installed')
async def test_transcode_audio_wav_to_mp3_and_ogg(self):
sample_wav = _create_sample_wav()
mp3_bytes = await audio_utils.transcode_audio(sample_wav, 'mp3', 'wav')
self.assertTrue(len(mp3_bytes) > 0)
self.assertEqual(audio_utils.detect_audio_format(mp3_bytes), 'mp3')
ogg_bytes = await audio_utils.transcode_audio(sample_wav, 'ogg', 'wav')
self.assertTrue(len(ogg_bytes) > 0)
fmt = audio_utils.detect_audio_format(ogg_bytes)
self.assertIn(fmt, ('ogg', 'opus'))
async def test_transcode_same_format_returns_input(self):
sample_wav = _create_sample_wav()
result = await audio_utils.transcode_audio(sample_wav, 'wav', 'wav')
self.assertEqual(result, sample_wav)
def test_transcode_missing_ffmpeg_raises_error(self):
sample_wav = _create_sample_wav()
with patch.object(audio_utils, 'is_ffmpeg_available', return_value=False):
with self.assertRaises(audio_utils.AudioDependencyError):
audio_utils.transcode_audio_sync(sample_wav, 'mp3', 'wav')
def test_transcode_missing_silk_raises_error(self):
sample_wav = _create_sample_wav()
with patch.object(audio_utils, 'is_silk_available', return_value=False):
with self.assertRaises(audio_utils.AudioDependencyError):
audio_utils.transcode_audio_sync(sample_wav, 'silk', 'wav')
async def test_extract_voice_bytes_from_base64(self):
import base64
sample_wav = _create_sample_wav()
b64_raw = base64.b64encode(sample_wav).decode('ascii')
voice_raw = platform_message.Voice(base64=b64_raw)
data, fmt = await audio_utils.extract_voice_bytes(voice_raw)
self.assertEqual(data, sample_wav)
self.assertEqual(fmt, 'wav')
data_uri = f'data:audio/wav;base64,{b64_raw}'
voice_uri = platform_message.Voice(base64=data_uri)
data2, fmt2 = await audio_utils.extract_voice_bytes(voice_uri)
self.assertEqual(data2, sample_wav)
self.assertEqual(fmt2, 'wav')