cortico-world-fish-sing 0.4.10 → 0.4.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/);版本号遵循语义化。
4
4
 
5
+ ## [0.4.12] - 2026-11-03
6
+
7
+ ### 修复
8
+ - **变调只改人声、伴奏留在原调**:pitch≠0 时人声被移调、伴奏却不动,两者相差 N 个半音(三全音是最刺耳的那档)。听感是「这版听着怪、说不上哪儿不对」,唱男声歌几乎必中(自动变调会给 +6)。现在**伴奏同步变调**(`asetrate` + `aresample` + `atempo`,保持时长),整首歌一起移调,等于正常的升调/降调翻唱。
9
+ - 用户实测确认:同一段《山丘》+6,B 版(伴奏一起移)明显更好听。
10
+ - 新增单测:变调滤镜的倍率必须等于 2^(n/12)、atempo 必须是其倒数。
11
+
12
+ ## [0.4.11] - 2026-11-03
13
+
14
+ ### 修复
15
+ - **长歌越唱越快、和伴奏越到后面越不合拍**:0.4.0 的接缝处理用的是"重叠交叉淡化",每条缝吃掉 50ms。4 分半的歌 4 条缝 = 人声比伴奏少 200ms,听感就是越唱越快。改成**等长淡出/淡入**(各 8ms),一个样本都不删,既压住接缝爆音又保证时长精确。
16
+ - 验证:115.67 秒真实素材(3 片)转换后输出 115.67 秒、样本数完全相同(差 0.0ms)。
17
+ - 单测改为「接缝处理必须不改时长」,这类事故以后进不了 CI。
18
+
5
19
  ## [0.4.10] - 2026-11-02
6
20
 
7
21
  ### 修复
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-fish-sing",
3
- "version": "0.4.10",
3
+ "version": "0.4.12",
4
4
  "description": "让她真的开口唱:B 站点歌或本地文件 → 分离 → 换声 → 混音,本机出声或发 QQ 语音条",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -1,130 +1,142 @@
1
- """引擎侧纯逻辑测试(需要 numpy + soundfile;torch/librosa 不需要)。
2
-
3
- 跑法:python -m unittest discover -s tests -v
4
- """
5
- import os
6
- import sys
7
- import tempfile
8
- import unittest
9
- from pathlib import Path
10
-
11
- ROOT = Path(__file__).resolve().parent.parent
12
- sys.path.insert(0, str(ROOT / "worker"))
13
-
14
- # RVC_ROOT 探测链在 import 时求值:指向一个确定的空目录,别让测试落到开发机的旧路径
15
- _TMP = tempfile.mkdtemp(prefix="fishsing-test-")
16
- os.environ["FISHSING_RVC_ROOT"] = _TMP
17
-
18
- import numpy as np # noqa: E402
19
- import engine # noqa: E402
20
-
21
- SR = 40000
22
-
23
-
24
- class SuggestPitch(unittest.TestCase):
25
- def setUp(self):
26
- self.model = {"p5": 313.0, "median": 356.0, "p95": 477.0}
27
-
28
- def test_female_source_stays_zero(self):
29
- # 源曲低音比模型数据下限低 7.9 半音——在 8 半音容忍范围内,不移调
30
- s = engine.SingEngine.suggest_pitch({"p5": 198.0}, self.model)
31
- self.assertEqual(s["pitch"], 0)
32
-
33
- def test_male_source_capped_plus6(self):
34
- s = engine.SingEngine.suggest_pitch({"p5": 120.0}, self.model)
35
- self.assertEqual(s["pitch"], 6)
36
- self.assertIn("男声", s["reason"])
37
-
38
- def test_moderate_male(self):
39
- # 差 12.75 半音 → ceil(12.75-8)=5
40
- s = engine.SingEngine.suggest_pitch({"p5": 150.0}, self.model)
41
- self.assertEqual(s["pitch"], 5)
42
-
43
- def test_way_higher_capped_minus3(self):
44
- s = engine.SingEngine.suggest_pitch({"p5": 700.0}, self.model)
45
- self.assertEqual(s["pitch"], -3)
46
-
47
- def test_missing_data_zero(self):
48
- self.assertEqual(engine.SingEngine.suggest_pitch(None, self.model)["pitch"], 0)
49
- self.assertEqual(engine.SingEngine.suggest_pitch({"p5": 200}, None)["pitch"], 0)
50
-
51
-
52
- class ResolveRvcRoot(unittest.TestCase):
53
- def test_env_wins(self):
54
- os.environ["FISHSING_RVC_ROOT"] = str(Path(_TMP) / "custom")
55
- try:
56
- self.assertEqual(str(engine._resolve_rvc_root()), str(Path(_TMP) / "custom"))
57
- finally:
58
- os.environ["FISHSING_RVC_ROOT"] = _TMP
59
-
60
- def test_fallback_workdir_sibling(self):
61
- os.environ.pop("FISHSING_RVC_ROOT", None)
62
- work = Path(_TMP) / "data" / "fish-sing" / "work"
63
- rvc = Path(_TMP) / "data" / "fish-sing" / "runtime" / "rvc"
64
- (rvc / "assets" / "weights").mkdir(parents=True, exist_ok=True)
65
- os.environ["FISHSING_WORK"] = str(work)
66
- try:
67
- self.assertEqual(engine._resolve_rvc_root(), rvc)
68
- finally:
69
- os.environ.pop("FISHSING_WORK", None)
70
-
71
-
72
- class SmoothSeams(unittest.TestCase):
73
- @staticmethod
74
- def sine(freq, sec, amp=0.3, phase=0.0):
75
- t = np.arange(int(SR * sec)) / SR
76
- return (amp * np.sin(2 * np.pi * freq * t + phase)).reshape(-1, 1).astype(np.float32)
77
-
78
- def test_click_removed(self):
79
- # 两段同频正弦,第二段开头有 0.5 的台阶——硬拼会咔哒一声
80
- a = self.sine(440, 1.0)
81
- b = self.sine(440, 1.0) + np.float32(0.5)
82
- raw_jump = abs(float(a[-1, 0]) - float(b[0, 0]))
83
- self.assertGreater(raw_jump, 0.1) # 确实有跳变
84
- out = engine.SingEngine._smooth_seams([a.copy(), b.copy()], SR)
85
- n = int(SR * 0.05)
86
- seam = out[0][-n:] # 交叉淡化区
87
- d = np.abs(np.diff(seam[:, 0]))
88
- # 0.5 的台阶被摊到 2000 个样本上,逐样本差应只剩正弦斜率量级
89
- self.assertLess(float(np.max(d)), 0.05)
90
-
91
- def test_dynamics_preserved(self):
92
- # 安静主歌 vs 炸副歌的响度差是音乐动态,不许被拉平
93
- # (接缝淡化会把少量响侧能量混进静侧尾部,比值略降是正常物理,不许趋近 1)
94
- quiet = self.sine(440, 1.0, amp=0.02)
95
- loud = self.sine(220, 1.0, amp=0.30)
96
- out = engine.SingEngine._smooth_seams([quiet.copy(), loud.copy()], SR)
97
- r = [float(np.sqrt(np.mean(p.astype(np.float64) ** 2))) for p in out]
98
- self.assertGreater(r[1] / max(r[0], 1e-9), 5.0) # 响度差还在(完全拉平会是 ~1)
99
-
100
- def test_lengths(self):
101
- a = self.sine(440, 2.0)
102
- b = self.sine(440, 2.0)
103
- c = self.sine(440, 2.0)
104
- out = engine.SingEngine._smooth_seams([a, b, c], SR)
105
- n = int(SR * 0.05)
106
- # 每条缝损失 50ms:首片丢右侧,中片左右各丢一次,尾片丢左侧
107
- self.assertEqual(out[0].shape[0], 2 * SR)
108
- self.assertEqual(out[1].shape[0], 2 * SR - n)
109
- self.assertEqual(out[2].shape[0], 2 * SR - n)
110
-
111
- def test_single_piece_untouched(self):
112
- a = self.sine(440, 1.0)
113
- out = engine.SingEngine._smooth_seams([a.copy()], SR)
114
- self.assertTrue(np.array_equal(out[0], a))
115
-
116
-
117
- class AugmentWeightExpansion(unittest.TestCase):
118
- """trainer 的同名逻辑在这里再验一遍口径(两处共用命名约定)。"""
119
-
120
- def test_naming_matches_trainer(self):
121
- import trainer
122
- w = {"slice001.wav": 3.0}
123
- e = trainer.expand_aug_weights(w, [-2, -4])
124
- # 增强副本文件名(prepare 生成)必须能被权重表覆盖
125
- for shift in (-2, -4):
126
- self.assertIn(f"aug{shift}_slice001.wav", e)
127
-
128
-
129
- if __name__ == "__main__":
130
- unittest.main()
1
+ """引擎侧纯逻辑测试(需要 numpy + soundfile;torch/librosa 不需要)。
2
+
3
+ 跑法:python -m unittest discover -s tests -v
4
+ """
5
+ import os
6
+ import sys
7
+ import tempfile
8
+ import unittest
9
+ from pathlib import Path
10
+
11
+ ROOT = Path(__file__).resolve().parent.parent
12
+ sys.path.insert(0, str(ROOT / "worker"))
13
+
14
+ # RVC_ROOT 探测链在 import 时求值:指向一个确定的空目录,别让测试落到开发机的旧路径
15
+ _TMP = tempfile.mkdtemp(prefix="fishsing-test-")
16
+ os.environ["FISHSING_RVC_ROOT"] = _TMP
17
+
18
+ import numpy as np # noqa: E402
19
+ import engine # noqa: E402
20
+
21
+ SR = 40000
22
+
23
+
24
+ class SuggestPitch(unittest.TestCase):
25
+ def setUp(self):
26
+ self.model = {"p5": 313.0, "median": 356.0, "p95": 477.0}
27
+
28
+ def test_female_source_stays_zero(self):
29
+ # 源曲低音比模型数据下限低 7.9 半音——在 8 半音容忍范围内,不移调
30
+ s = engine.SingEngine.suggest_pitch({"p5": 198.0}, self.model)
31
+ self.assertEqual(s["pitch"], 0)
32
+
33
+ def test_male_source_capped_plus6(self):
34
+ s = engine.SingEngine.suggest_pitch({"p5": 120.0}, self.model)
35
+ self.assertEqual(s["pitch"], 6)
36
+ self.assertIn("男声", s["reason"])
37
+
38
+ def test_moderate_male(self):
39
+ # 差 12.75 半音 → ceil(12.75-8)=5
40
+ s = engine.SingEngine.suggest_pitch({"p5": 150.0}, self.model)
41
+ self.assertEqual(s["pitch"], 5)
42
+
43
+ def test_way_higher_capped_minus3(self):
44
+ s = engine.SingEngine.suggest_pitch({"p5": 700.0}, self.model)
45
+ self.assertEqual(s["pitch"], -3)
46
+
47
+ def test_missing_data_zero(self):
48
+ self.assertEqual(engine.SingEngine.suggest_pitch(None, self.model)["pitch"], 0)
49
+ self.assertEqual(engine.SingEngine.suggest_pitch({"p5": 200}, None)["pitch"], 0)
50
+
51
+
52
+ class ResolveRvcRoot(unittest.TestCase):
53
+ def test_env_wins(self):
54
+ os.environ["FISHSING_RVC_ROOT"] = str(Path(_TMP) / "custom")
55
+ try:
56
+ self.assertEqual(str(engine._resolve_rvc_root()), str(Path(_TMP) / "custom"))
57
+ finally:
58
+ os.environ["FISHSING_RVC_ROOT"] = _TMP
59
+
60
+ def test_fallback_workdir_sibling(self):
61
+ os.environ.pop("FISHSING_RVC_ROOT", None)
62
+ work = Path(_TMP) / "data" / "fish-sing" / "work"
63
+ rvc = Path(_TMP) / "data" / "fish-sing" / "runtime" / "rvc"
64
+ (rvc / "assets" / "weights").mkdir(parents=True, exist_ok=True)
65
+ os.environ["FISHSING_WORK"] = str(work)
66
+ try:
67
+ self.assertEqual(engine._resolve_rvc_root(), rvc)
68
+ finally:
69
+ os.environ.pop("FISHSING_WORK", None)
70
+
71
+
72
+ class SmoothSeams(unittest.TestCase):
73
+ @staticmethod
74
+ def sine(freq, sec, amp=0.3, phase=0.0):
75
+ t = np.arange(int(SR * sec)) / SR
76
+ return (amp * np.sin(2 * np.pi * freq * t + phase)).reshape(-1, 1).astype(np.float32)
77
+
78
+ def test_click_removed(self):
79
+ # 两段同频正弦,第二段开头有 0.5 的台阶——硬拼会咔哒一声
80
+ a = self.sine(440, 1.0)
81
+ b = self.sine(440, 1.0) + np.float32(0.5)
82
+ raw_jump = abs(float(a[-1, 0]) - float(b[0, 0]))
83
+ self.assertGreater(raw_jump, 0.1) # 确实有跳变
84
+ out = engine.SingEngine._smooth_seams([a.copy(), b.copy()], SR)
85
+ n = int(SR * 0.05)
86
+ seam = out[0][-n:] # 交叉淡化区
87
+ d = np.abs(np.diff(seam[:, 0]))
88
+ # 0.5 的台阶被摊到 2000 个样本上,逐样本差应只剩正弦斜率量级
89
+ self.assertLess(float(np.max(d)), 0.05)
90
+
91
+ def test_dynamics_preserved(self):
92
+ # 安静主歌 vs 炸副歌的响度差是音乐动态,不许被拉平
93
+ # (接缝淡化会把少量响侧能量混进静侧尾部,比值略降是正常物理,不许趋近 1)
94
+ quiet = self.sine(440, 1.0, amp=0.02)
95
+ loud = self.sine(220, 1.0, amp=0.30)
96
+ out = engine.SingEngine._smooth_seams([quiet.copy(), loud.copy()], SR)
97
+ r = [float(np.sqrt(np.mean(p.astype(np.float64) ** 2))) for p in out]
98
+ self.assertGreater(r[1] / max(r[0], 1e-9), 5.0) # 响度差还在(完全拉平会是 ~1)
99
+
100
+ def test_lengths_preserved(self):
101
+ # 接缝处理**必须不改时长**:0.4.0 的重叠交叉淡化每条缝吃掉 50ms,
102
+ # 4 分半的歌人声就比伴奏快 200ms(用户实测越唱越不合拍)。
103
+ a = self.sine(440, 2.0)
104
+ b = self.sine(440, 2.0)
105
+ c = self.sine(440, 2.0)
106
+ out = engine.SingEngine._smooth_seams([a, b, c], SR)
107
+ for i in range(3):
108
+ self.assertEqual(out[i].shape[0], 2 * SR, f'第 {i} 片长度被改了')
109
+ self.assertEqual(sum(p.shape[0] for p in out), 6 * SR)
110
+
111
+ def test_single_piece_untouched(self):
112
+ a = self.sine(440, 1.0)
113
+ out = engine.SingEngine._smooth_seams([a.copy()], SR)
114
+ self.assertTrue(np.array_equal(out[0], a))
115
+
116
+
117
+ class AugmentWeightExpansion(unittest.TestCase):
118
+ """trainer 的同名逻辑在这里再验一遍口径(两处共用命名约定)。"""
119
+
120
+ def test_naming_matches_trainer(self):
121
+ import trainer
122
+ w = {"slice001.wav": 3.0}
123
+ e = trainer.expand_aug_weights(w, [-2, -4])
124
+ # 增强副本文件名(prepare 生成)必须能被权重表覆盖
125
+ for shift in (-2, -4):
126
+ self.assertIn(f"aug{shift}_slice001.wav", e)
127
+
128
+
129
+ def test_pitch_shift_filter_keeps_ratio(self):
130
+ # 变调滤镜:asetrate 的倍率必须等于 2^(n/12),atempo 必须是它的倒数(保时长)
131
+ import re as _re
132
+ for semi in (6, -5, 3):
133
+ flt = engine.SingEngine.pitch_shift_filter(44100, semi)
134
+ m = _re.search(r'asetrate=(\d+)', flt)
135
+ t = _re.search(r'atempo=([\d.]+)', flt)
136
+ self.assertIsNotNone(m, flt)
137
+ ratio = int(m.group(1)) / 44100
138
+ self.assertAlmostEqual(ratio, 2 ** (semi / 12), places=3)
139
+ self.assertAlmostEqual(float(t.group(1)), 1 / ratio, places=3)
140
+
141
+ if __name__ == "__main__":
142
+ unittest.main()
package/worker/engine.py CHANGED
@@ -557,26 +557,28 @@ class SingEngine:
557
557
  shutil.rmtree(tmp, ignore_errors=True)
558
558
 
559
559
  @staticmethod
560
- def _smooth_seams(pieces: list, out_sr: int, *, fade_ms: int = 50) -> list:
560
+ def _smooth_seams(pieces: list, out_sr: int, *, fade_ms: int = 8) -> list:
561
561
  """
562
562
  分片接缝处理:各片独立推理,片边容易有响度跳变和相位不连续(听感是
563
563
  "接缝处咯噔一下")。
564
564
 
565
- 只做边界 50ms 线性交叉淡化——相邻片是连续时间轴上切开的,把跳变摊到
566
- 50ms 里就听不出来了。**不做**片间响度归一:55 秒一片的歌,安静主歌和
567
- 炸副歌的响度差是音乐本身的动态,拉平会毁掉听感(本地实测教训)。
565
+ **绝不改时长**:只做边界等长的淡出/淡入(各 fade_ms),一个样本都不删。
566
+ 0.4.0 第一版用的是"重叠交叉淡化",每条缝吃掉 50ms——4 分半的歌 4 条缝
567
+ 就少 200ms,人声比伴奏越唱越快(用户实测"越到后面越不合拍")。改成等长
568
+ 淡化后,既压住爆音,时长又精确对齐伴奏。
569
+ **也不做**片间响度归一:主歌/副歌的响度差是音乐动态,拉平会毁听感。
568
570
  """
569
571
  if len(pieces) < 2:
570
572
  return pieces
571
573
  n = int(out_sr * fade_ms / 1000)
572
- if n > 0:
573
- w = np.linspace(0.0, 1.0, n).reshape(-1, 1)
574
- for i in range(1, len(pieces)):
575
- a, b = pieces[i - 1], pieces[i]
576
- if a.shape[0] > n and b.shape[0] > n:
577
- seam = a[-n:] * (1 - w) + b[:n] * w
578
- pieces[i - 1] = np.concatenate([a[:-n], seam], axis=0)
579
- pieces[i] = b[n:]
574
+ if n <= 0:
575
+ return pieces
576
+ ramp = np.linspace(0.0, 1.0, n).reshape(-1, 1)
577
+ for i in range(1, len(pieces)):
578
+ a, b = pieces[i - 1], pieces[i]
579
+ if a.shape[0] > n and b.shape[0] > n:
580
+ pieces[i - 1] = np.concatenate([a[:-n], a[-n:] * (1.0 - ramp)], axis=0) # 尾巴淡出
581
+ pieces[i] = np.concatenate([b[:n] * ramp, b[n:]], axis=0) # 开头淡入
580
582
  return pieces
581
583
 
582
584
  # -- 混音 ------------------------------------------------------
@@ -744,9 +746,22 @@ class SingEngine:
744
746
  shutil.copyfile(conv, out)
745
747
  mix_info = {"sr": info["sr"], "seconds": info["seconds"]}
746
748
  else:
749
+ inst_for_mix = inst
750
+ if pitch and inst is not None:
751
+ # 整体变调:伴奏必须跟着一起移,否则人声和伴奏差 N 个半音。
752
+ # (0.4.12 之前的真实问题:pitch≠0 时"听着怪但说不出哪不对",
753
+ # 唱男声歌几乎必中——自动变调会给 +6,伴奏却一直在原调。)
754
+ report(0.82, f"伴奏同步变调 {pitch:+d} 半音")
755
+ inst_for_mix = work / "inst_shifted.wav"
756
+ try:
757
+ sr_i = int(sf.info(str(inst)).samplerate)
758
+ except Exception: # noqa: BLE001
759
+ sr_i = 44100
760
+ run_ff([FFMPEG, "-y", "-hide_banner", "-loglevel", "error", "-i", str(inst),
761
+ "-af", self.pitch_shift_filter(sr_i, pitch), str(inst_for_mix)])
747
762
  report(0.85, "混音")
748
763
  out = work / "result.wav"
749
- mix_info = self.mix(conv, inst, out)
764
+ mix_info = self.mix(conv, inst_for_mix, out)
750
765
 
751
766
  report(1.0, "完成")
752
767
  result = {
@@ -762,6 +777,17 @@ class SingEngine:
762
777
  result["pitchSuggest"] = suggestion
763
778
  return result
764
779
 
780
+ @staticmethod
781
+ def pitch_shift_filter(sr: int, semitones: float) -> str:
782
+ """
783
+ 变调但**保持时长**的 ffmpeg 滤镜串。
784
+
785
+ asetrate 改采样率等价于"升调+加速",所以后面必须用 atempo 把时长拉回来
786
+ (atempo=1/factor)。这样人声/伴奏都能整段移调而不改变节奏。
787
+ """
788
+ f = 2.0 ** (float(semitones) / 12.0)
789
+ return f"asetrate={int(round(sr * f))},aresample={sr},atempo={1.0 / f:.6f}"
790
+
765
791
  # -- 选模对比试听 -----------------------------------------------
766
792
 
767
793
  def ab_test(self, test: Path, out_dir: Path, variants: list, *, pitch: int = 0,