File size: 27,208 Bytes
8562e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#!/usr/bin/env python3
"""Build continuous-background HFD speech WAVs and their exact time mapping.

This is the CPU preparation stage.  It deliberately does not run Qwen.  Every
mixed WAV is made from one uninterrupted background session, with speech
starting 0.2 seconds after the first 0.6-second listen window begins.  The
following GPU stage can therefore reuse ``cache_omni_hidden_states.py``
unchanged for both mixed speech and pure background windows.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import math
import os
import sqlite3
import sys
import wave
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any

import numpy as np
import pandas as pd
from scipy.signal import resample_poly

PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

SAMPLE_RATE = 16_000
WINDOW_SECONDS = 0.6
STRIDE_SECONDS = 0.4
WINDOW_SAMPLES = int(round(WINDOW_SECONDS * SAMPLE_RATE))
STRIDE_SAMPLES = int(round(STRIDE_SECONDS * SAMPLE_RATE))
RTC_PREFIX_SAMPLES = WINDOW_SAMPLES - STRIDE_SAMPLES
BACKGROUND_SOURCE_NAME = "hfd_continuous_office_background"
SPEECH_SOURCE_NAME = "qwen3_tts_seed_audio_command"
TASK_NAME = "audio_command_text"
FORMAT = "hfd_continuous_background_assignment_v1"


def _normalise_audio_dir(value: str) -> str:
    parts = Path(str(value).replace("\\", "/")).parts
    try:
        start = parts.index("seed_audio")
    except ValueError:
        return Path(str(value)).as_posix().strip("/")
    return Path(*parts[start:]).as_posix()


def _write_json_atomic(path: Path, payload: Any) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
    temporary.write_text(
        json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
        encoding="utf-8",
    )
    os.replace(temporary, path)


def _write_jsonl_atomic(path: Path, rows: list[dict[str, Any]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
    with temporary.open("w", encoding="utf-8") as handle:
        for row in rows:
            handle.write(json.dumps(row, ensure_ascii=False) + "\n")
    os.replace(temporary, path)


def _read_pcm16(path: Path) -> np.ndarray:
    with wave.open(str(path), "rb") as handle:
        if (
            handle.getframerate() != SAMPLE_RATE
            or handle.getnchannels() != 1
            or handle.getsampwidth() != 2
            or handle.getcomptype() != "NONE"
        ):
            raise ValueError(f"Expected mono PCM16 {SAMPLE_RATE} Hz WAV: {path}")
        return np.frombuffer(
            handle.readframes(handle.getnframes()),
            dtype="<i2",
        ).copy()


def _read_speech_float(path: Path) -> np.ndarray:
    """Decode the known mono PCM16 TTS corpus without one ffmpeg process/file."""

    with wave.open(str(path), "rb") as handle:
        channels = int(handle.getnchannels())
        sample_width = int(handle.getsampwidth())
        source_rate = int(handle.getframerate())
        compression = str(handle.getcomptype())
        frames = int(handle.getnframes())
        raw = handle.readframes(frames)
    if (
        channels != 1
        or sample_width != 2
        or source_rate <= 0
        or compression != "NONE"
    ):
        raise ValueError(
            "Expected uncompressed mono PCM16 TTS WAV: "
            f"path={path} channels={channels} width={sample_width} "
            f"rate={source_rate} compression={compression}"
        )
    speech = (
        np.frombuffer(raw, dtype="<i2").astype(np.float32) / np.float32(32768.0)
    )
    if source_rate != SAMPLE_RATE:
        divisor = math.gcd(source_rate, SAMPLE_RATE)
        speech = resample_poly(
            speech,
            SAMPLE_RATE // divisor,
            source_rate // divisor,
        ).astype(np.float32, copy=False)
        expected = int(round(frames * SAMPLE_RATE / float(source_rate)))
        if speech.size > expected:
            speech = speech[:expected]
        elif speech.size < expected:
            speech = np.pad(speech, (0, expected - speech.size))
    if speech.size <= 0 or not np.isfinite(speech).all():
        raise RuntimeError(f"Invalid decoded speech source: {path}")
    return np.ascontiguousarray(speech, dtype=np.float32)


def _write_pcm16_atomic(path: Path, pcm: np.ndarray) -> None:
    value = np.asarray(pcm, dtype="<i2").reshape(-1)
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
    with wave.open(str(temporary), "wb") as handle:
        handle.setnchannels(1)
        handle.setsampwidth(2)
        handle.setframerate(SAMPLE_RATE)
        handle.writeframes(value.tobytes())
    os.replace(temporary, path)


def _sha256(path: Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def _load_valid_sources(
    hidden_root: Path,
    fallback_assignment_path: Path | None = None,
) -> dict[str, int]:
    index_dir = (
        hidden_root
        / "audio"
        / SPEECH_SOURCE_NAME
        / TASK_NAME
        / "sources"
    )
    result: dict[str, int] = {}
    for index_path in sorted(index_dir.glob("index.ws*.rank*.sqlite")):
        connection = sqlite3.connect(
            f"file:{index_path}?mode=ro",
            uri=True,
        )
        try:
            rows = connection.execute(
                "SELECT key, count FROM sources ORDER BY key"
            ).fetchall()
        finally:
            connection.close()
        for key, count in rows:
            result[str(key)] = int(count)
    if result:
        return result

    # The legacy cache is only a one-time migration whitelist.  Once the new
    # production cache has passed validation it may be deleted, so retain the
    # exact selected source set in the new assignment manifest and allow
    # deterministic waveform rebuilding from that manifest.
    if fallback_assignment_path is not None and fallback_assignment_path.is_file():
        with fallback_assignment_path.open("r", encoding="utf-8") as handle:
            for line in handle:
                if not line.strip():
                    continue
                row = json.loads(line)
                key = str(row.get("source_key", "")).strip()
                count = int(row.get("listen_window_count", 0))
                if not key or count <= 0:
                    raise RuntimeError(
                        "Invalid fallback source assignment row in "
                        f"{fallback_assignment_path}"
                    )
                previous = result.get(key)
                if previous is not None and previous != count:
                    raise RuntimeError(
                        f"Conflicting fallback source count for {key}: "
                        f"{previous} != {count}"
                    )
                result[key] = count
    if result:
        print(
            "[source-list] Reusing the validated source set from "
            f"{fallback_assignment_path}",
            flush=True,
        )
        return result
    raise RuntimeError(
        "No valid HFD speech source index and no reusable assignment manifest: "
        f"legacy_index={index_dir} fallback={fallback_assignment_path}"
    )


def _load_motion_frames(metadata_csv: Path) -> dict[str, int]:
    table = pd.read_csv(
        metadata_csv,
        usecols=lambda column: column in {"filename", "move_duration_frames"},
    )
    result: dict[str, int] = {}
    for row in table.itertuples(index=False):
        filename = str(getattr(row, "filename", "")).strip()
        value = getattr(row, "move_duration_frames", 0)
        if not filename:
            continue
        try:
            result[filename] = int(value) if not pd.isna(value) else 0
        except (TypeError, ValueError):
            result[filename] = 0
    return result


def _load_training_rows(
    manifest_path: Path,
    valid_directories: set[str],
    motion_frames: dict[str, int],
) -> tuple[list[dict[str, Any]], dict[str, int]]:
    rows: list[dict[str, Any]] = []
    max_frames_by_directory: dict[str, int] = {}
    with manifest_path.open("r", encoding="utf-8") as handle:
        for line in handle:
            if not line.strip():
                continue
            row = json.loads(line)
            audio_dir = _normalise_audio_dir(str(row.get("audio_path", "")))
            if audio_dir not in valid_directories:
                continue
            normalized = dict(row)
            normalized["audio_path"] = audio_dir
            rows.append(normalized)
            filename = str(row.get("filename", "")).strip()
            if filename in motion_frames:
                max_frames_by_directory[audio_dir] = max(
                    max_frames_by_directory.get(audio_dir, 0),
                    int(motion_frames[filename]),
                )
    missing = sorted(valid_directories - set(max_frames_by_directory))
    if missing:
        raise RuntimeError(
            "Valid speech directories have no matching motion metadata: "
            f"count={len(missing)} first={missing[:5]}"
        )
    return rows, max_frames_by_directory


def _load_sessions(manifest_path: Path) -> list[dict[str, Any]]:
    payload = json.loads(manifest_path.read_text(encoding="utf-8"))
    if payload.get("format") != "hfd_continuous_background_sessions_v1":
        raise RuntimeError(f"Unsupported background manifest: {manifest_path}")
    sessions: list[dict[str, Any]] = []
    for raw in payload.get("sessions", []):
        row = dict(raw)
        path = Path(str(row["path"])).expanduser().resolve()
        pcm = _read_pcm16(path)
        expected_count = (
            0
            if pcm.size < WINDOW_SAMPLES
            else (pcm.size - WINDOW_SAMPLES) // STRIDE_SAMPLES + 1
        )
        if int(row.get("window_count", -1)) != expected_count:
            raise RuntimeError(
                f"Background window count mismatch for {path}: "
                f"manifest={row.get('window_count')} actual={expected_count}"
            )
        row["path"] = str(path)
        row["pcm"] = pcm
        row["window_count"] = expected_count
        sessions.append(row)
    if not sessions:
        raise RuntimeError(f"No background sessions in {manifest_path}")
    return sessions


def _listen_window_count(speech_samples: int) -> int:
    # Speech begins after the 0.2-second RTC/background prefix.  Keep every
    # window whose start precedes the final speech sample.
    return int(
        math.ceil(
            (RTC_PREFIX_SAMPLES + int(speech_samples))
            / float(STRIDE_SAMPLES)
        )
    )


def _select_background_slot(
    *,
    source_key: str,
    listen_windows: int,
    max_motion_steps: int,
    sessions: list[dict[str, Any]],
    initial_idle_max_steps: int,
    safety_steps: int,
    seed: int,
) -> tuple[dict[str, Any], int]:
    choices: list[tuple[dict[str, Any], int, int]] = []
    total_slots = 0
    required_after_start = (
        int(listen_windows) + int(max_motion_steps) + int(safety_steps)
    )
    for session in sessions:
        max_start = int(session["window_count"]) - required_after_start
        count = max_start - int(initial_idle_max_steps) + 1
        if count <= 0:
            continue
        choices.append((session, int(initial_idle_max_steps), count))
        total_slots += count
    if total_slots <= 0:
        raise RuntimeError(
            "No continuous background can cover source: "
            f"key={source_key} listen={listen_windows} "
            f"motion={max_motion_steps} idle={initial_idle_max_steps}"
        )
    digest = hashlib.sha256(
        f"{int(seed)}::{source_key}".encode("utf-8")
    ).digest()
    slot = int.from_bytes(digest[:8], "big") % total_slots
    for session, first_start, count in choices:
        if slot < count:
            return session, first_start + slot
        slot -= count
    raise AssertionError("Background slot selection fell through")


def _build_one_source(
    *,
    source_key: str,
    speech_root: Path,
    mixed_root: Path,
    max_motion_frames: int,
    sessions: list[dict[str, Any]],
    initial_idle_max_steps: int,
    stationary_blend_frames: int,
    safety_steps: int,
    seed: int,
) -> dict[str, Any]:
    original_path = speech_root / f"{source_key}.wav"
    speech = _read_speech_float(original_path)
    if speech.size <= 0:
        raise RuntimeError(f"Empty speech source: {original_path}")
    listen_windows = _listen_window_count(int(speech.size))
    max_motion_steps = int(
        math.ceil(
            (
                max(int(max_motion_frames), 1)
                + max(int(stationary_blend_frames) - 1, 1)
            )
            / 20.0
        )
    )
    session, listen_start = _select_background_slot(
        source_key=source_key,
        listen_windows=listen_windows,
        max_motion_steps=max_motion_steps,
        sessions=sessions,
        initial_idle_max_steps=initial_idle_max_steps,
        safety_steps=safety_steps,
        seed=seed,
    )
    total_samples = (
        (listen_windows - 1) * STRIDE_SAMPLES + WINDOW_SAMPLES
    )
    background_start = listen_start * STRIDE_SAMPLES
    background_pcm = np.asarray(session["pcm"], dtype="<i2")[
        background_start : background_start + total_samples
    ].copy()
    if background_pcm.size != total_samples:
        raise RuntimeError(
            f"Background ended inside mixed source: {source_key}"
        )
    speech_peak = float(np.max(np.abs(speech), initial=0.0))
    peak_limit = 10.0 ** (-3.0 / 20.0)
    speech_scale = (
        min(1.0, peak_limit / speech_peak)
        if speech_peak > 0.0
        else 1.0
    )
    mixed_pcm = background_pcm.copy()
    speech_start = RTC_PREFIX_SAMPLES
    speech_end = speech_start + int(speech.size)
    background_float = (
        background_pcm[speech_start:speech_end].astype(np.float32)
        / 32768.0
    )
    mixed_float = background_float + speech * np.float32(speech_scale)
    if float(np.max(np.abs(mixed_float), initial=0.0)) >= 1.0:
        raise RuntimeError(
            f"Unsafe mixed peak for {source_key}; background must remain fixed"
        )
    mixed_pcm[speech_start:speech_end] = np.rint(
        mixed_float * 32768.0
    ).clip(-32768, 32767).astype("<i2")

    # Prove both phase boundaries use exactly the same PCM samples.
    if not np.array_equal(
        mixed_pcm[:RTC_PREFIX_SAMPLES],
        np.asarray(session["pcm"])[
            background_start : background_start + RTC_PREFIX_SAMPLES
        ],
    ):
        raise RuntimeError(f"Idle/listen overlap mismatch for {source_key}")
    next_background_start = (
        listen_start + listen_windows
    ) * STRIDE_SAMPLES
    if not np.array_equal(
        mixed_pcm[-RTC_PREFIX_SAMPLES:],
        np.asarray(session["pcm"])[
            next_background_start :
            next_background_start + RTC_PREFIX_SAMPLES
        ],
    ):
        raise RuntimeError(f"Listen/motion overlap mismatch for {source_key}")

    mixed_path = mixed_root / f"{source_key}.wav"
    _write_pcm16_atomic(mixed_path, mixed_pcm)
    return {
        "format": FORMAT,
        "source_key": source_key,
        "audio_dir": Path(source_key).parent.as_posix(),
        "original_audio_path": str(original_path.resolve()),
        "mixed_audio_path": str(mixed_path.resolve()),
        "mixed_audio_sha256": _sha256(mixed_path),
        "speech_samples": int(speech.size),
        "speech_scale": float(speech_scale),
        "speech_offset_samples": RTC_PREFIX_SAMPLES,
        "listen_window_count": listen_windows,
        "background_key": str(session["key"]),
        "background_sha256": str(session["sha256"]),
        "background_window_count": int(session["window_count"]),
        "background_listen_start_index": int(listen_start),
        "initial_idle_max_steps": int(initial_idle_max_steps),
        "max_motion_steps": max_motion_steps,
        "safety_steps": int(safety_steps),
    }


def _source_configs(
    *,
    output_root: Path,
    mixed_manifest: Path,
    background_manifest: Path,
) -> tuple[dict[str, Any], dict[str, Any]]:
    mixed = {
        "sources": [
            {
                "name": SPEECH_SOURCE_NAME,
                "modality": "audio",
                "dataset_root": str((output_root / "mixed_waveforms").resolve()),
                "manifest_path": str(mixed_manifest.resolve()),
                "allowed_tasks": [TASK_NAME],
                "instruction_key": "content_short_description",
                "audio_path_key": "audio_path",
                "is_audio_dir_manifest": True,
                "audio_file_extensions": [".wav"],
            }
        ]
    }
    background = {
        "sources": [
            {
                "name": BACKGROUND_SOURCE_NAME,
                "modality": "audio",
                "dataset_root": str((output_root / "background").resolve()),
                "manifest_path": str(background_manifest.resolve()),
                "allowed_tasks": [TASK_NAME],
                "audio_path_key": "audio_path",
                "is_audio_dir_manifest": False,
                "audio_file_extensions": [".wav"],
            }
        ]
    }
    return mixed, background


def build(args: argparse.Namespace) -> None:
    output_root = args.output_root.expanduser().resolve()
    manifests = output_root / "manifests"
    mixed_root = output_root / "mixed_waveforms"
    valid_sources = _load_valid_sources(
        args.old_hidden_root.expanduser().resolve(),
        manifests / "source_background_assignments.jsonl",
    )
    source_keys = sorted(valid_sources)
    if int(args.limit_sources) > 0:
        source_keys = source_keys[: int(args.limit_sources)]
    valid_directories = {
        Path(source_key).parent.as_posix()
        for source_key in source_keys
    }
    motion_frames = _load_motion_frames(args.motion_metadata.expanduser().resolve())
    training_rows, max_frames = _load_training_rows(
        args.speech_manifest.expanduser().resolve(),
        valid_directories,
        motion_frames,
    )
    sessions = _load_sessions(args.background_manifest.expanduser().resolve())

    def worker(source_key: str) -> dict[str, Any]:
        audio_dir = Path(source_key).parent.as_posix()
        return _build_one_source(
            source_key=source_key,
            speech_root=args.speech_root.expanduser().resolve(),
            mixed_root=mixed_root,
            max_motion_frames=max_frames[audio_dir],
            sessions=sessions,
            initial_idle_max_steps=int(args.initial_idle_max_steps),
            stationary_blend_frames=int(args.stationary_blend_frames),
            safety_steps=int(args.safety_steps),
            seed=int(args.seed),
        )

    with ThreadPoolExecutor(max_workers=max(int(args.workers), 1)) as executor:
        assignments = list(executor.map(worker, source_keys))
    assignments.sort(key=lambda row: str(row["source_key"]))

    assignment_path = manifests / "source_background_assignments.jsonl"
    mixed_manifest = manifests / "mixed_metadata.jsonl"
    background_jsonl = manifests / "background_sessions.jsonl"
    _write_jsonl_atomic(assignment_path, assignments)
    _write_jsonl_atomic(mixed_manifest, training_rows)
    _write_jsonl_atomic(
        background_jsonl,
        [
            {
                "filename": str(session["key"]),
                "audio_path": str(session["relative_path"]),
            }
            for session in sessions
        ],
    )
    mixed_config, background_config = _source_configs(
        output_root=output_root,
        mixed_manifest=mixed_manifest,
        background_manifest=background_jsonl,
    )
    _write_json_atomic(manifests / "mixed_source_config.json", mixed_config)
    _write_json_atomic(
        manifests / "background_source_config.json",
        background_config,
    )
    assignment_sha256 = _sha256(assignment_path)
    report = {
        "format": FORMAT,
        "output_root": str(output_root),
        "source_count": len(assignments),
        "mixed_window_count": sum(
            int(row["listen_window_count"]) for row in assignments
        ),
        "background_session_count": len(sessions),
        "background_window_count": sum(
            int(session["window_count"]) for session in sessions
        ),
        "assignment_path": str(assignment_path),
        "assignment_sha256": assignment_sha256,
        "initial_idle_max_steps": int(args.initial_idle_max_steps),
        "stationary_blend_frames": int(args.stationary_blend_frames),
        "safety_steps": int(args.safety_steps),
        "seed": int(args.seed),
        "limit_sources": int(args.limit_sources),
    }
    _write_json_atomic(output_root / "reports" / "waveform_build.json", report)
    print(
        "[ready] "
        f"sources={report['source_count']:,} "
        f"mixed_windows={report['mixed_window_count']:,} "
        f"background_windows={report['background_window_count']:,} "
        f"output={output_root}",
        flush=True,
    )


def verify(args: argparse.Namespace) -> None:
    output_root = args.output_root.expanduser().resolve()
    report_path = output_root / "reports" / "waveform_build.json"
    report = json.loads(report_path.read_text(encoding="utf-8"))
    assignment_path = Path(str(report["assignment_path"]))
    if _sha256(assignment_path) != report["assignment_sha256"]:
        raise RuntimeError("Assignment manifest changed after waveform build")
    sessions = {
        str(row["key"]): row
        for row in _load_sessions(args.background_manifest.expanduser().resolve())
    }
    with assignment_path.open("r", encoding="utf-8") as handle:
        assignments = [
            json.loads(line)
            for line in handle
            if line.strip()
        ]

    def verify_one(row: dict[str, Any]) -> None:
        session = sessions[str(row["background_key"])]
        mixed = _read_pcm16(Path(str(row["mixed_audio_path"])))
        start = int(row["background_listen_start_index"]) * STRIDE_SAMPLES
        expected = np.asarray(session["pcm"])[start : start + mixed.size]
        speech_start = int(row["speech_offset_samples"])
        speech_end = speech_start + int(row["speech_samples"])
        speech = _read_speech_float(Path(str(row["original_audio_path"])))
        if speech.size != int(row["speech_samples"]):
            raise RuntimeError(
                f"Speech length changed after build: {row['source_key']}"
            )
        speech_peak = float(np.max(np.abs(speech), initial=0.0))
        expected_scale = (
            min(1.0, (10.0 ** (-3.0 / 20.0)) / speech_peak)
            if speech_peak > 0.0
            else 1.0
        )
        if not math.isclose(
            float(row["speech_scale"]),
            expected_scale,
            rel_tol=0.0,
            abs_tol=1e-12,
        ):
            raise RuntimeError(
                f"Speech scale changed after build: {row['source_key']}"
            )
        if not np.array_equal(mixed[:speech_start], expected[:speech_start]):
            raise RuntimeError(
                f"Broken idle/listen boundary: {row['source_key']}"
            )
        if not np.array_equal(mixed[speech_end:], expected[speech_end:]):
            raise RuntimeError(
                f"Broken listen/motion background: {row['source_key']}"
            )
        expected_speech = (
            expected[speech_start:speech_end].astype(np.float32)
            / np.float32(32768.0)
            + speech * np.float32(expected_scale)
        )
        expected_speech_pcm = np.rint(
            expected_speech * 32768.0
        ).clip(-32768, 32767).astype("<i2")
        if not np.array_equal(
            mixed[speech_start:speech_end],
            expected_speech_pcm,
        ):
            raise RuntimeError(
                f"Broken waveform-level speech/background mix: "
                f"{row['source_key']}"
            )
        if (
            int(row["background_listen_start_index"])
            + int(row["listen_window_count"])
            + int(row["max_motion_steps"])
            + int(row["safety_steps"])
            > int(row["background_window_count"])
        ):
            raise RuntimeError(
                f"Background capacity is insufficient: {row['source_key']}"
            )

    with ThreadPoolExecutor(max_workers=max(int(args.workers), 1)) as executor:
        count = sum(1 for _ in executor.map(verify_one, assignments))
    if count != int(report["source_count"]):
        raise RuntimeError(
            f"Assignment count mismatch: verified={count} report={report['source_count']}"
        )
    print(
        f"[verified] {count:,} continuous mixed WAV sources under {output_root}",
        flush=True,
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--mode", choices=("build", "verify"), default="build")
    parser.add_argument("--output-root", type=Path, required=True)
    parser.add_argument("--background-manifest", type=Path, required=True)
    parser.add_argument(
        "--speech-root",
        type=Path,
        default=Path("/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/buwendong/user/code/qwen3-tts"),
    )
    parser.add_argument(
        "--speech-manifest",
        type=Path,
        default=Path("/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/buwendong/user/code/qwen3-tts/metadata.jsonl"),
    )
    parser.add_argument(
        "--motion-metadata",
        type=Path,
        default=Path(
            "/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0/dataset/seed_all/"
            "metadata/seed_metadata_v004.csv"
        ),
    )
    parser.add_argument(
        "--old-hidden-root",
        type=Path,
        default=Path(
            "/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/videogen/hfd_audio_command_source"
        ),
        help=(
            "One-time read-only whitelist of the 41,694 validated WAV sources. "
            "After the first successful build, the new assignment manifest is "
            "used as an exact fallback so this legacy cache can be removed."
        ),
    )
    parser.add_argument("--workers", type=int, default=32)
    parser.add_argument("--limit-sources", type=int, default=0)
    parser.add_argument("--initial-idle-max-steps", type=int, default=5)
    parser.add_argument("--stationary-blend-frames", type=int, default=20)
    parser.add_argument("--safety-steps", type=int, default=10)
    parser.add_argument("--seed", type=int, default=260730)
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    if args.mode == "build":
        build(args)
    else:
        verify(args)


if __name__ == "__main__":
    main()