code-HFD_lzk / tests /test_continuous_audio_command_sources.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
db172f6 verified
Raw History Blame Contribute Delete
5.06 kB
from __future__ import annotations
import importlib.util
import json
import wave
from pathlib import Path
import numpy as np
PROJECT_ROOT = Path(__file__).resolve().parents[1]
SCRIPT = (
PROJECT_ROOT
/ "scripts"
/ "data"
/ "build_continuous_audio_command_sources.py"
)
def _load_module():
spec = importlib.util.spec_from_file_location(
"build_continuous_audio_command_sources_test",
SCRIPT,
)
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def _write_wav(path: Path, samples: np.ndarray) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
pcm = np.rint(
np.clip(np.asarray(samples), -1.0, 1.0) * 32767.0
).astype("<i2")
with wave.open(str(path), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(16_000)
handle.writeframes(pcm.tobytes())
def test_mixed_source_keeps_one_background_timeline_at_both_boundaries(
tmp_path: Path,
) -> None:
module = _load_module()
source_key = "seed_audio/000001/female_01_en"
speech_root = tmp_path / "speech"
mixed_root = tmp_path / "mixed"
time = np.arange(16_000, dtype=np.float32) / 16_000.0
speech = 0.1 * np.sin(2.0 * np.pi * 440.0 * time)
_write_wav(speech_root / f"{source_key}.wav", speech)
background_pcm = (
(np.arange(160_000, dtype=np.int32) % 2000) - 1000
).astype("<i2")
session = {
"key": "sessions/office",
"sha256": "test-background",
"window_count": (
(background_pcm.size - module.WINDOW_SAMPLES)
// module.STRIDE_SAMPLES
+ 1
),
"pcm": background_pcm,
}
row = module._build_one_source(
source_key=source_key,
speech_root=speech_root,
mixed_root=mixed_root,
max_motion_frames=40,
sessions=[session],
initial_idle_max_steps=2,
stationary_blend_frames=20,
safety_steps=1,
seed=123,
)
mixed = module._read_pcm16(Path(row["mixed_audio_path"]))
background_start = (
row["background_listen_start_index"] * module.STRIDE_SAMPLES
)
expected = background_pcm[
background_start : background_start + mixed.size
]
speech_start = row["speech_offset_samples"]
speech_end = speech_start + row["speech_samples"]
np.testing.assert_array_equal(mixed[:speech_start], expected[:speech_start])
np.testing.assert_array_equal(mixed[speech_end:], expected[speech_end:])
decoded_speech = module._read_speech_float(
speech_root / f"{source_key}.wav"
)
expected_mixed = expected.copy()
expected_mixed[speech_start:speech_end] = np.rint(
(
expected[speech_start:speech_end].astype(np.float32)
/ np.float32(32768.0)
+ decoded_speech * np.float32(row["speech_scale"])
)
* np.float32(32768.0)
).clip(-32768, 32767).astype("<i2")
np.testing.assert_array_equal(mixed, expected_mixed)
actual_windows = (
(mixed.size - module.WINDOW_SAMPLES) // module.STRIDE_SAMPLES + 1
)
assert actual_windows == row["listen_window_count"]
assert row["background_listen_start_index"] >= 2
def test_background_slot_is_deterministic_and_never_wraps() -> None:
module = _load_module()
sessions = [
{"key": "short", "window_count": 10},
{"key": "long", "window_count": 100},
]
first = module._select_background_slot(
source_key="seed_audio/000001/voice",
listen_windows=20,
max_motion_steps=30,
sessions=sessions,
initial_idle_max_steps=5,
safety_steps=10,
seed=42,
)
second = module._select_background_slot(
source_key="seed_audio/000001/voice",
listen_windows=20,
max_motion_steps=30,
sessions=sessions,
initial_idle_max_steps=5,
safety_steps=10,
seed=42,
)
assert first[0]["key"] == "long"
assert (first[0]["key"], first[1]) == (second[0]["key"], second[1])
assert first[1] >= 5
assert first[1] + 20 + 30 + 10 <= first[0]["window_count"]
def test_valid_source_list_can_be_reused_after_legacy_cache_cleanup(
tmp_path: Path,
) -> None:
module = _load_module()
assignment_path = tmp_path / "source_background_assignments.jsonl"
rows = [
{
"source_key": "seed_audio/000001/female_01_en",
"listen_window_count": 7,
},
{
"source_key": "seed_audio/000002/male_01_zh",
"listen_window_count": 11,
},
]
assignment_path.write_text(
"".join(json.dumps(row) + "\n" for row in rows),
encoding="utf-8",
)
result = module._load_valid_sources(
tmp_path / "deleted_legacy_cache",
assignment_path,
)
assert result == {
"seed_audio/000001/female_01_en": 7,
"seed_audio/000002/male_01_zh": 11,
}