File size: 12,837 Bytes
9fc9873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Generate and audit the small HFD emergency-stop/safe-hold TTS corpus.

Generation is shardable by voice so four local GPUs can run independently.
The final manifest is written only by ``--mode finalize`` after every expected
file passes the exact PCM16/16 kHz contract consumed by HFD training.
"""

from __future__ import annotations

import argparse
import hashlib
import json
import math
import os
import wave
from pathlib import Path
from typing import Any

import numpy as np


FORMAT = "hfd_control_command_tts_v1"
TARGET_RATE = 16_000
DEFAULT_MODEL = Path(
    "/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/team_workspace/public_ckpt/Qwen/"
    "Qwen3-TTS-12Hz-1.7B-VoiceDesign"
)
DEFAULT_OUTPUT = Path(
    "/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/videogen/hfd_control_commands_v1"
)

# Each phrase ID represents one bilingual meaning.  Every meaning is rendered
# by female/male × speaker 01/02 × Chinese/English = exactly eight WAVs.
PHRASES: tuple[dict[str, str], ...] = (
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_01",
        "zh": "停。",
        "en": "Stop.",
    },
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_02",
        "zh": "马上停下来。",
        "en": "Stop now.",
    },
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_03",
        "zh": "先停一下。",
        "en": "Pause for a moment.",
    },
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_04",
        "zh": "停止动作。",
        "en": "Stop the movement.",
    },
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_05",
        "zh": "立刻停止。",
        "en": "Stop immediately.",
    },
    {
        "intent": "emergency-stop",
        "phrase_id": "stop_06",
        "zh": "不要再继续了。",
        "en": "Do not continue.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_01",
        "zh": "别动。",
        "en": "Don't move.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_02",
        "zh": "不要动。",
        "en": "Stay still.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_03",
        "zh": "不对,先别动。",
        "en": "That's not right. Hold still.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_04",
        "zh": "保持现在的姿势。",
        "en": "Hold your current pose.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_05",
        "zh": "先保持不动。",
        "en": "Keep still for now.",
    },
    {
        "intent": "safe-hold",
        "phrase_id": "hold_06",
        "zh": "站稳,别动。",
        "en": "Stay balanced and don't move.",
    },
)


def _voice_specs() -> tuple[dict[str, str], ...]:
    return (
        {
            "name": "female_01_en",
            "language": "en",
            "qwen_language": "English",
            "instruction": (
                "Use a natural everyday adult female voice. Speak the short "
                "urgent command clearly and calmly, without theatrical acting."
            ),
        },
        {
            "name": "female_02_en",
            "language": "en",
            "qwen_language": "English",
            "instruction": (
                "Use a natural young adult female voice. Speak clearly in a "
                "concise, neutral, everyday tone."
            ),
        },
        {
            "name": "male_01_en",
            "language": "en",
            "qwen_language": "English",
            "instruction": (
                "Use a natural everyday adult male voice. Speak the short "
                "command clearly and firmly, but do not shout."
            ),
        },
        {
            "name": "male_02_en",
            "language": "en",
            "qwen_language": "English",
            "instruction": (
                "Use a natural young adult male voice. Speak concisely in a "
                "relaxed, neutral conversational tone."
            ),
        },
        {
            "name": "female_01_zh",
            "language": "zh",
            "qwen_language": "Chinese",
            "instruction": "使用自然日常的成年女性声音,清晰简短地说出指令,语气坚定但不要喊叫。",
        },
        {
            "name": "female_02_zh",
            "language": "zh",
            "qwen_language": "Chinese",
            "instruction": "使用自然日常的年轻女性声音,语速适中,语气中性简洁,不要戏剧化。",
        },
        {
            "name": "male_01_zh",
            "language": "zh",
            "qwen_language": "Chinese",
            "instruction": "使用自然日常的成年男性声音,清晰简短地说出指令,坚定但不要大喊。",
        },
        {
            "name": "male_02_zh",
            "language": "zh",
            "qwen_language": "Chinese",
            "instruction": "使用自然日常的年轻男性声音,语气放松中性,简洁自然地说出指令。",
        },
    )


def _audio_path(root: Path, phrase: dict[str, str], voice: dict[str, str]) -> Path:
    return root / "audio" / phrase["intent"] / phrase["phrase_id"] / f"{voice['name']}.wav"


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 _atomic_jsonl(path: Path, rows: list[dict[str, Any]]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(path.suffix + f".tmp.{os.getpid()}")
    with temporary.open("w", encoding="utf-8") as handle:
        for row in rows:
            handle.write(json.dumps(row, ensure_ascii=False, sort_keys=True) + "\n")
        handle.flush()
        os.fsync(handle.fileno())
    temporary.replace(path)


def _write_pcm16(path: Path, waveform: np.ndarray) -> None:
    values = np.asarray(waveform, dtype=np.float32).reshape(-1)
    if values.size <= 0 or not np.isfinite(values).all():
        raise RuntimeError(f"TTS produced invalid waveform for {path}")
    peak = float(np.max(np.abs(values), initial=0.0))
    if peak > 1.0:
        values = values / np.float32(peak)
    pcm = np.rint(np.clip(values, -1.0, 1.0) * 32767.0).astype("<i2")
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(f".tmp.{os.getpid()}.wav")
    with wave.open(str(temporary), "wb") as handle:
        handle.setnchannels(1)
        handle.setsampwidth(2)
        handle.setframerate(TARGET_RATE)
        handle.writeframes(pcm.tobytes())
    temporary.replace(path)


def _valid_pcm16(path: Path) -> bool:
    if not path.is_file():
        return False
    try:
        with wave.open(str(path), "rb") as handle:
            return (
                handle.getnchannels() == 1
                and handle.getsampwidth() == 2
                and handle.getframerate() == TARGET_RATE
                and handle.getnframes() > 0
                and handle.getcomptype() == "NONE"
            )
    except (EOFError, wave.Error):
        return False


def generate(args: argparse.Namespace) -> None:
    import torch
    from scipy.signal import resample_poly
    from qwen_tts import Qwen3TTSModel

    if not 0 <= args.shard_index < args.shard_count:
        raise ValueError(
            f"Invalid shard {args.shard_index}/{args.shard_count}"
        )
    voices = tuple(
        voice
        for index, voice in enumerate(_voice_specs())
        if index % args.shard_count == args.shard_index
    )
    if not voices:
        print(f"[ready] shard={args.shard_index} has no assigned voices")
        return
    torch.backends.cuda.enable_cudnn_sdp(False)
    torch.backends.cuda.enable_flash_sdp(True)
    torch.backends.cuda.enable_mem_efficient_sdp(True)
    torch.backends.cuda.enable_math_sdp(True)
    torch.backends.cuda.matmul.allow_tf32 = True
    model = Qwen3TTSModel.from_pretrained(
        str(args.model_path),
        device_map=args.device,
        dtype=torch.bfloat16,
        attn_implementation="flash_attention_2",
    )
    for voice in voices:
        pending = [
            phrase
            for phrase in PHRASES
            if not _valid_pcm16(_audio_path(args.output_root, phrase, voice))
        ]
        for start in range(0, len(pending), max(args.batch_size, 1)):
            batch = pending[start : start + max(args.batch_size, 1)]
            texts = [phrase[voice["language"]] for phrase in batch]
            waveforms, source_rate = model.generate_voice_design(
                text=texts,
                language=[voice["qwen_language"]] * len(batch),
                instruct=[voice["instruction"]] * len(batch),
            )
            if len(waveforms) != len(batch):
                raise RuntimeError("Qwen TTS returned the wrong batch size")
            for phrase, waveform in zip(batch, waveforms):
                values = np.asarray(waveform, dtype=np.float32).reshape(-1)
                if int(source_rate) != TARGET_RATE:
                    divisor = math.gcd(int(source_rate), TARGET_RATE)
                    values = resample_poly(
                        values,
                        TARGET_RATE // divisor,
                        int(source_rate) // divisor,
                    ).astype(np.float32, copy=False)
                _write_pcm16(_audio_path(args.output_root, phrase, voice), values)
            print(
                f"[tts] shard={args.shard_index}/{args.shard_count} "
                f"voice={voice['name']} done={min(start + len(batch), len(pending))}/"
                f"{len(pending)}",
                flush=True,
            )
    del model
    torch.cuda.empty_cache()


def finalize(args: argparse.Namespace) -> None:
    rows: list[dict[str, Any]] = []
    for phrase in PHRASES:
        for voice in _voice_specs():
            path = _audio_path(args.output_root, phrase, voice).resolve()
            if not _valid_pcm16(path):
                raise RuntimeError(f"Missing/invalid expected control WAV: {path}")
            with wave.open(str(path), "rb") as handle:
                samples = int(handle.getnframes())
                raw = handle.readframes(samples)
            values = np.frombuffer(raw, dtype="<i2").astype(np.float32) / 32768.0
            peak = float(np.max(np.abs(values), initial=0.0))
            speech_scale = (
                min(1.0, (10.0 ** (-3.0 / 20.0)) / peak)
                if peak > 0.0
                else 1.0
            )
            rows.append(
                {
                    "format": FORMAT,
                    "intent": phrase["intent"],
                    "phrase_id": phrase["phrase_id"],
                    "voice_name": voice["name"],
                    "language": voice["language"],
                    "text": phrase[voice["language"]],
                    "audio_path": str(path),
                    "sample_rate": TARGET_RATE,
                    "speech_samples_16k": samples,
                    "duration_seconds": samples / float(TARGET_RATE),
                    "peak": peak,
                    "speech_scale": speech_scale,
                    "sha256": _sha256(path),
                }
            )
    expected = len(PHRASES) * len(_voice_specs())
    if len(rows) != expected:
        raise AssertionError(f"Manifest row count drifted: {len(rows)} != {expected}")
    _atomic_jsonl(args.output_root / "manifest.jsonl", rows)
    durations = [float(row["duration_seconds"]) for row in rows]
    print(
        f"[ready] rows={len(rows)} meanings={len(PHRASES)} voices=8 "
        f"duration={min(durations):.2f}-{max(durations):.2f}s "
        f"manifest={args.output_root / 'manifest.jsonl'}",
        flush=True,
    )


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--mode", choices=("generate", "finalize"), required=True)
    parser.add_argument("--output-root", type=Path, default=DEFAULT_OUTPUT)
    parser.add_argument("--model-path", type=Path, default=DEFAULT_MODEL)
    parser.add_argument("--device", default="cuda:0")
    parser.add_argument("--batch-size", type=int, default=6)
    parser.add_argument("--shard-index", type=int, default=0)
    parser.add_argument("--shard-count", type=int, default=1)
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    args.output_root = args.output_root.expanduser().resolve()
    args.model_path = args.model_path.expanduser().resolve()
    if args.mode == "generate":
        generate(args)
    else:
        finalize(args)


if __name__ == "__main__":
    main()