"""完整原声允许官方40Hz latent量化误差;禁止为对齐而剪尾台词。""" import sys import wave from types import SimpleNamespace import numpy as np import pytest def native_module(): from llm_services import native_av return native_av @pytest.mark.parametrize( "frames,samples", [(124, 165600), (141, 188000), (158, 210400), (345, 460000)] ) def test_native_audio_preserves_every_sample_at_official_latent_grid(frames, samples): audio = np.zeros((1, 2, samples), dtype=np.float32) audio[0, :, -1] = [0.3, -0.4] result, metadata = native_module().prepare_audio(audio, 32000, frames) assert result.shape == (2, samples) assert np.allclose(result[:, -1], [0.3, -0.4]) assert metadata["audio_samples"] == samples assert metadata["sample_rate"] == 32000 and metadata["channels"] == 2 assert metadata["audio_duration"] == samples / 32000 assert metadata["video_duration"] == frames / 24 @pytest.mark.parametrize( "audio,rate", [ (np.zeros((1, 1, 165600)), 32000), (np.zeros((1, 2, 165600)), 48000), (np.zeros((1, 2, 160000)), 32000), (np.full((1, 2, 165600), np.nan), 32000), ], ) def test_native_audio_rejects_bad_shape_rate_and_sync(audio, rate): with pytest.raises(RuntimeError, match="audio|Audio"): native_module().prepare_audio(audio, rate, 124) def test_native_export_archives_exact_stereo_pcm_and_passes_full_audio_to_mux( tmp_path, monkeypatch ): encoded = {} monkeypatch.setitem(sys.modules, "torch", SimpleNamespace(from_numpy=lambda a: a)) audio = np.zeros((1, 2, 165600), dtype=np.float32) audio[0, :, -1] = [0.25, -0.5] def encode(frames, **kwargs): encoded.update(kwargs) assert len(frames) == 124 assert kwargs["audio"].shape == (2, 165600) assert np.allclose(kwargs["audio"][:, -1], [0.25, -0.5]) metadata = native_module().export_native_av( np.zeros((124, 2, 2, 3)), audio, 32000, tmp_path / "video_raw.mp4", tmp_path / "audio_native.wav", encode, ) with wave.open(str(tmp_path / "audio_native.wav"), "rb") as wav: assert ( wav.getnframes() == 165600 and wav.getnchannels() == 2 and wav.getframerate() == 32000 ) tail = np.frombuffer(wav.readframes(165600), dtype="