File size: 5,064 Bytes
db172f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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,
    }