Files
VoiceStudio/tests/test_pcm16_wire_encode.py
T
Palash Debnath 6ae094d6bf fix: restore green main (#3466)
* docs(features): list the cosyvoice-gguf engine in the docs inventory

* fix(stream): scale loud chunks instead of clipping them on the pcm16 wire

Streamed chunks are watermarked and then encoded to pcm16 with a hard clamp,
so a chunk that overshoots full scale was clipped after marking (#1169). The
/generate stream and /ws/tts encoders now share pcm16_mono, which scales an
over-full-scale chunk down instead.

The watermark tests' fake detector used an absolute 0.9 threshold, so the
louder leveler output from #3429 read as a lost mark. It is now
gain-invariant like AudioSeal, and still fails when the mark is missing.

* fix(failure): let the source-language hint reach a streaming client

561c62717 made SOURCE_LANGUAGE_REQUIRED terminal without adding it to the
context-free hint classes, so a render could end on it without the hint.

* fix(longform): a fast chunk no longer reports chunk_s 0.0

Progress snapshots rounded every measurement to 0.1 s, so chunks under 50 ms
read as taking no time at all. That also made the #3417 progress test flaky
whenever imports were already warm. A positive measurement now rounds to at
least one unit of resolution.

* test(dub-smoke): expect the chosen spoken language in the agent request

#3442 made a deliberate source-language choice win over the transcript's
detected language. The smoke picks French before transcribing, so the agent
translation request now carries 'fr', not the detected 'en'.

* docs(changelog): note the stream clipping and chunk timing fixes (#3466)

* test: restore module identity after the MCP client fixture; resolve network_share per test

The MCP REST client fixture purged api.*/services.* on teardown, which left
fresh module copies in sys.modules while earlier-collected test modules kept
the originals. test_network_share_self_check then patched one copy while
lan_ipv4_addresses ran in the other and did a real default-route lookup
(CI-only failure on #3466). The fixture now uses backend_module_state
snapshot/restore, and the network-share tests resolve the module per test
with patch.object.

* test: track engines in backend module restore

An engines.* module first imported while a test had swapped in a temporary
services twin kept that twin's ASRTimeoutError after restore. The whisper.cpp
timeout tests then could not catch the error they raised (CI-only failures
on #3466). backend_module_state now snapshots and restores engines with the
other backend packages.

* test(mcp): import the backend fresh per fixture instead of reloading in place

Both MCP fixtures reloaded core.config/core.db (and main) in place, which
swapped functions other modules had imported by name
(services.dub_pipeline.db_conn). The client fixture's purge also left fresh
module copies behind. Both now purge, import under the tmp env, and restore
the untouched originals (backend_module_state). That fixes the CI-only
network-share failure and test_backend_module_state in combined runs.

* test(pcm16): resolve the encoder at call time so module swaps can't go stale
2026-10-26 06:48:18 +05:30

47 lines
1.7 KiB
Python

"""Streamed pcm16 frames must never hard-clip marked audio (#1169).
Streamed chunks are provenance-marked and then encoded. A hard clip at that
last step can damage the mark, so the shared encoder scales an over-full-scale
chunk down instead, and every streaming encoder goes through it.
"""
from pathlib import Path
import importlib
import torch
_ROOT = Path(__file__).resolve().parents[1]
def pcm16_mono(wav):
# Resolved per call: other tests swap ``services.*`` in sys.modules, and a
# module-level import would keep exercising a stale copy of the helper.
return importlib.import_module("services.audio_io").pcm16_mono(wav)
def test_in_range_audio_is_a_plain_cast():
wav = torch.tensor([[0.0, 0.5, -0.5, 0.25]])
assert pcm16_mono(wav).tolist() == [0, 16384, -16384, 8192]
def test_over_full_scale_is_scaled_not_clipped():
wav = torch.tensor([[0.2, 1.6, -0.8, 1.2]])
pcm = pcm16_mono(wav)
assert int(pcm.abs().max()) == 32767
# Proportions survive: nothing else is pinned at the rails.
assert int((pcm.abs() >= 32767).sum()) == 1
ratio = pcm.float() / pcm[1].float()
assert torch.allclose(ratio, wav[0] / 1.6, atol=1e-4)
def test_takes_channel_zero_of_nd_input():
wav = torch.tensor([[[0.5, 0.5], [0.1, 0.1]]])
assert pcm16_mono(wav).tolist() == [16384, 16384]
def test_streaming_encoders_use_the_shared_encoder():
for rel in ("backend/api/routers/generation.py", "backend/api/routers/tts_stream.py"):
src = (_ROOT / rel).read_text(encoding="utf-8")
assert "pcm16_mono(" in src, f"{rel} must encode stream frames with pcm16_mono"
assert "32767).clamp(" not in src, f"{rel} hard-clips stream frames again"