94 lines
3.1 KiB
Python
94 lines
3.1 KiB
Python
"""完整原声允许官方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="<i2").reshape(-1, 2)[-1]
|
||
assert tail.tolist() == [8192, -16384]
|
||
assert encoded["fps"] == 24 and encoded["audio_sample_rate"] == 32000
|
||
assert metadata["audio_path"] == str((tmp_path / "audio_native.wav").resolve())
|
||
|
||
|
||
def test_native_export_never_deletes_an_existing_audio_archive(tmp_path, monkeypatch):
|
||
monkeypatch.setitem(sys.modules, "torch", SimpleNamespace(from_numpy=lambda a: a))
|
||
path = tmp_path / "audio_native.wav"
|
||
path.write_bytes(b"user archive")
|
||
with pytest.raises(FileExistsError):
|
||
native_module().export_native_av(
|
||
range(124),
|
||
np.zeros((1, 2, 165600), dtype=np.float32),
|
||
32000,
|
||
tmp_path / "video_raw.mp4",
|
||
path,
|
||
lambda *a, **kw: None,
|
||
)
|
||
assert path.read_bytes() == b"user archive"
|