#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""生成 04 s11 送审版（花酱 id=227/228）：一声 xiān + 逗号断句，并把时长压回原量级

实测（chili04_tts_s11_probe.log + 波形分析）：
  - 原版「鲜」3.52s，中间**没有任何句中停顿**（鲜辣连读）；
  - 同音字「先」+ 句号版 4.32s，在 2.54–3.30s 自动出现 0.76s 句中停顿 —— 断句有了；
  - 但 0.76s 偏拖沓，且开头有 0.58s 静音。
处理：只裁静音（不碰任何语音波形、不加压缩/EQ）：
  - 开头 0.58s 静音裁掉 0.40s（保留 0.18s 自然起音）；
  - 句中 0.76s 停顿保留 0.32s（删 0.44s）。
  => 3.48s，与原 3.52s 几乎等长，s11 之后各段位移 -0.04s。

⚠ 声调 ASR 验不了，本版只是**送审候选**，须花酱验 → 刚耳检拍板。
   5 个原始候选（chili04_s11_*.wav）一并留在交换目录，随时可换。
"""
import json, os, shutil
import numpy as np
import soundfile as sf

HERE = r"D:/Download/muse_exchange"
PROD = r"D:/WorkBuddyProd/chili04"
SRC = os.path.join(PROD, "probe_s11", "xian_juhao.wav")
OUT = os.path.join(PROD, "probe_s11", "s11_review.wav")

w, sr = sf.read(SRC, dtype="float32")
assert sr == 48000
t = lambda x: int(round(x * sr))

keep_a = w[t(0.40):t(2.86)]   # 0.18s 前导 + 前半句 + 0.32s 逗号停顿
keep_b = w[t(3.30):]          # 后半句 + 尾部
out = np.concatenate([keep_a, keep_b])

sf.write(OUT, out, sr)
shutil.copyfile(OUT, os.path.join(HERE, "chili04_s11_review.wav"))

rep = {
    "source": "probe_s11/xian_juhao.wav",
    "tts_text": "味觉只有酸甜苦咸先。辣不在里头。",
    "sub_text": "味觉只有酸甜苦咸鲜，辣不在里头。（字幕一字未改）",
    "orig_s": 3.52,
    "candidate_s": round(len(w) / sr, 4),
    "review_s": round(len(out) / sr, 4),
    "delta_vs_orig_s": round(len(out) / sr - 3.52, 4),
    "cuts": [
        {"range": [0.0, 0.40], "what": "开头静音裁掉 0.40s，保留 0.18s 自然起音"},
        {"range": [2.86, 3.30], "what": "句中停顿由 0.76s 裁到 0.32s"},
    ],
    "pause_at_comma_s": 0.32,
    "note": "只裁静音，语音波形零改动；一声 xiān 靠同音字「先」从输入侧保证，需刚耳检确认",
}
json.dump(rep, open(os.path.join(HERE, "chili04_s11_review_report.json"), "w", encoding="utf-8"),
          ensure_ascii=False, indent=2)
print(json.dumps(rep, ensure_ascii=False, indent=2))
