dsh-plugin-cyrene-live2d 0.2.9 → 0.3.0

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.
Files changed (210) hide show
  1. package/README.md +8 -2
  2. package/assets/default-prefs.json +8 -8
  3. package/assets/icon.svg +89 -89
  4. package/assets/locale/notes/en.json +6 -6
  5. package/assets/locale/notes/zh-CN.json +6 -6
  6. package/assets/locale/reminder/en.json +6 -6
  7. package/assets/locale/reminder/zh-CN.json +6 -6
  8. package/assets/model/Cyrene.model3.json +441 -441
  9. package/assets/model/bongo-standard/Live2d_expression0.exp3.json +9 -9
  10. package/assets/model/bongo-standard/cat.cdi3.json +296 -296
  11. package/assets/model/bongo-standard/cat.model3.json +33 -33
  12. package/assets/model/bongo-standard/cat.physics3.json +232 -232
  13. package/assets/model/expressions/0.exp3.json +24 -24
  14. package/assets/model/expressions/1.exp3.json +11 -11
  15. package/assets/model/expressions/10.exp3.json +9 -9
  16. package/assets/model/expressions/11.exp3.json +9 -9
  17. package/assets/model/expressions/2.exp3.json +9 -9
  18. package/assets/model/expressions/3.exp3.json +9 -9
  19. package/assets/model/expressions/4.exp3.json +11 -11
  20. package/assets/model/expressions/5.exp3.json +11 -11
  21. package/assets/model/expressions/6.exp3.json +11 -11
  22. package/assets/model/expressions/7.exp3.json +11 -11
  23. package/assets/model/expressions/8.exp3.json +11 -11
  24. package/assets/model/expressions/9.exp3.json +9 -9
  25. package/assets/model/model.physics3.json +641 -641
  26. package/assets/model/motions/Tick3_3.motion3.json +1347 -1347
  27. package/assets/model/motions//345/212/250/344/275/2346_0.motion3.json +699 -699
  28. package/assets/model/motions//345/212/250/344/275/2346_1.motion3.json +1241 -1241
  29. package/assets/model/motions//345/212/250/344/275/2346_2.motion3.json +1117 -1117
  30. package/assets/model/motions//345/212/250/344/275/2346_3.motion3.json +1106 -1106
  31. package/assets/model/we-q/expressions/FileReferences_Expressions_0_File_0.exp3.json +19 -19
  32. package/assets/model/we-q/expressions/FileReferences_Expressions_1_File_0.exp3.json +41 -41
  33. package/assets/model/we-q/expressions/FileReferences_Expressions_2_File_0.exp3.json +104 -104
  34. package/assets/model/we-q/model0.model3.json +245 -245
  35. package/assets/model/we-q/motions/FileReferences_Motions_shake#1_0_File_0.motion3.json +520 -520
  36. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_0_File_0.motion3.json +499 -499
  37. package/assets/model/we-q/motions/FileReferences_Motions_talk#1_1_File_0.motion3.json +453 -453
  38. package/assets/model/we-q/motions/FileReferences_Motions_wink#1_0_File_0.motion3.json +478 -478
  39. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_0_File_0.motion3.json +450 -450
  40. package/assets/model/we-q/motions/FileReferences_Motions_/346/212/254/346/211/213_1_File_0.motion3.json +450 -450
  41. package/assets/model/we-q/motions/idle/FileReferences_Motions_Idleidle_0_File_0.motion3.json +422 -422
  42. package/assets/model/we-q/physics/FileReferences_Physics_0.physic3.json +236 -236
  43. package/assets/runtime/live2dcubismcore.min.js +8 -8
  44. package/assets/stickers.json +84 -84
  45. package/assets/vendor/cubism4.min.js +1 -1
  46. package/assets/vendor/pixi.min.js +9 -9
  47. package/assets/vendor/theme-cyrene-factory.js +4314 -4314
  48. package/assets/vendor/theme-cyrene.js +4317 -4317
  49. package/assets/voice/angry-00.opus +0 -0
  50. package/assets/voice/angry-02.opus +0 -0
  51. package/assets/voice/approval-00.opus +0 -0
  52. package/assets/voice/approval-01.opus +0 -0
  53. package/assets/voice/approval-02.opus +0 -0
  54. package/assets/voice/askAck-00.opus +0 -0
  55. package/assets/voice/away-01.opus +0 -0
  56. package/assets/voice/away-02.opus +0 -0
  57. package/assets/voice/away-03.opus +0 -0
  58. package/assets/voice/away-05.opus +0 -0
  59. package/assets/voice/back-00.opus +0 -0
  60. package/assets/voice/back-01.opus +0 -0
  61. package/assets/voice/backLong-05.opus +0 -0
  62. package/assets/voice/backShort-03.opus +0 -0
  63. package/assets/voice/backShort-04.opus +0 -0
  64. package/assets/voice/backShort-07.opus +0 -0
  65. package/assets/voice/backVeryLong-01.opus +0 -0
  66. package/assets/voice/backVeryLong-02.opus +0 -0
  67. package/assets/voice/backVeryLong-03.opus +0 -0
  68. package/assets/voice/backVeryLong-04.opus +0 -0
  69. package/assets/voice/backVeryLong-05.opus +0 -0
  70. package/assets/voice/backVeryLong-07.opus +0 -0
  71. package/assets/voice/backVeryLong-08.opus +0 -0
  72. package/assets/voice/balLow-00.opus +0 -0
  73. package/assets/voice/balLow-01.opus +0 -0
  74. package/assets/voice/birthday-02.opus +0 -0
  75. package/assets/voice/click-00.opus +0 -0
  76. package/assets/voice/click-01.opus +0 -0
  77. package/assets/voice/click-07.opus +0 -0
  78. package/assets/voice/click-10.opus +0 -0
  79. package/assets/voice/click-11.opus +0 -0
  80. package/assets/voice/click-12.opus +0 -0
  81. package/assets/voice/click-16.opus +0 -0
  82. package/assets/voice/click-17.opus +0 -0
  83. package/assets/voice/click-18.opus +0 -0
  84. package/assets/voice/comfort-03.opus +0 -0
  85. package/assets/voice/comfort-04.opus +0 -0
  86. package/assets/voice/cost10-00.opus +0 -0
  87. package/assets/voice/cost2-00.opus +0 -0
  88. package/assets/voice/cost5-00.opus +0 -0
  89. package/assets/voice/costTurn-01.opus +0 -0
  90. package/assets/voice/costTurn-02.opus +0 -0
  91. package/assets/voice/ctxFull-01.opus +0 -0
  92. package/assets/voice/ctxHigh-00.opus +0 -0
  93. package/assets/voice/dawn-01.opus +0 -0
  94. package/assets/voice/dawn-04.opus +0 -0
  95. package/assets/voice/dawn-06.opus +0 -0
  96. package/assets/voice/dawn-07.opus +0 -0
  97. package/assets/voice/deepWake-03.opus +0 -0
  98. package/assets/voice/deepWake-04.opus +0 -0
  99. package/assets/voice/deepWake-06.opus +0 -0
  100. package/assets/voice/deepWake-07.opus +0 -0
  101. package/assets/voice/done-05.opus +0 -0
  102. package/assets/voice/done-08.opus +0 -0
  103. package/assets/voice/done-09.opus +0 -0
  104. package/assets/voice/done-11.opus +0 -0
  105. package/assets/voice/empty-00.opus +0 -0
  106. package/assets/voice/encourage-02.opus +0 -0
  107. package/assets/voice/failed-01.opus +0 -0
  108. package/assets/voice/failed-05.opus +0 -0
  109. package/assets/voice/failkind-auth-01.opus +0 -0
  110. package/assets/voice/failkind-auth-02.opus +0 -0
  111. package/assets/voice/failkind-ctx-00.opus +0 -0
  112. package/assets/voice/failkind-missing-01.opus +0 -0
  113. package/assets/voice/focusEnd-02.opus +0 -0
  114. package/assets/voice/focusEnd-04.opus +0 -0
  115. package/assets/voice/forgive-00.opus +0 -0
  116. package/assets/voice/hello-00.opus +0 -0
  117. package/assets/voice/hello-03.opus +0 -0
  118. package/assets/voice/hold-00.opus +0 -0
  119. package/assets/voice/idleTap-00.opus +0 -0
  120. package/assets/voice/idleTapLong-02.opus +0 -0
  121. package/assets/voice/lateNight-00.opus +0 -0
  122. package/assets/voice/lateNight-02.opus +0 -0
  123. package/assets/voice/lateNight-03.opus +0 -0
  124. package/assets/voice/lateNight-06.opus +0 -0
  125. package/assets/voice/lateNight-07.opus +0 -0
  126. package/assets/voice/murmur-04.opus +0 -0
  127. package/assets/voice/night-01.opus +0 -0
  128. package/assets/voice/night-03.opus +0 -0
  129. package/assets/voice/night-05.opus +0 -0
  130. package/assets/voice/noon-04.opus +0 -0
  131. package/assets/voice/petArm-02.opus +0 -0
  132. package/assets/voice/petCheek-00.opus +0 -0
  133. package/assets/voice/petCheek-01.opus +0 -0
  134. package/assets/voice/petCheek-02.opus +0 -0
  135. package/assets/voice/petHead-01.opus +0 -0
  136. package/assets/voice/petHead-03.opus +0 -0
  137. package/assets/voice/petHead-04.opus +0 -0
  138. package/assets/voice/praised-01.opus +0 -0
  139. package/assets/voice/praised-03.opus +0 -0
  140. package/assets/voice/question-00.opus +0 -0
  141. package/assets/voice/question-03.opus +0 -0
  142. package/assets/voice/question-04.opus +0 -0
  143. package/assets/voice/question-06.opus +0 -0
  144. package/assets/voice/shy-00.opus +0 -0
  145. package/assets/voice/spot-0-0.opus +0 -0
  146. package/assets/voice/spot-0-1.opus +0 -0
  147. package/assets/voice/spot-0-2.opus +0 -0
  148. package/assets/voice/spot-3-0.opus +0 -0
  149. package/assets/voice/spot-4-0.opus +0 -0
  150. package/assets/voice/spot-4-2.opus +0 -0
  151. package/assets/voice/spot-5-2.opus +0 -0
  152. package/assets/voice/spot-6-0.opus +0 -0
  153. package/assets/voice/spot-6-1.opus +0 -0
  154. package/assets/voice/spot-7-0.opus +0 -0
  155. package/assets/voice/spot-7-2.opus +0 -0
  156. package/assets/voice/spot-8-1.opus +0 -0
  157. package/assets/voice/tagent-00.opus +0 -0
  158. package/assets/voice/thinkFast-01.opus +0 -0
  159. package/assets/voice/thinkFast-02.opus +0 -0
  160. package/assets/voice/thinkFast-03.opus +0 -0
  161. package/assets/voice/thinkSlow-01.opus +0 -0
  162. package/assets/voice/thinkSlow-03.opus +0 -0
  163. package/assets/voice/thinking-01.opus +0 -0
  164. package/assets/voice/thinking-02.opus +0 -0
  165. package/assets/voice/thinking-03.opus +0 -0
  166. package/assets/voice/thinking-09.opus +0 -0
  167. package/assets/voice/tread-00.opus +0 -0
  168. package/assets/voice/tread-01.opus +0 -0
  169. package/assets/voice/trun-02.opus +0 -0
  170. package/assets/voice/tsun-00.opus +0 -0
  171. package/assets/voice/tsun-01.opus +0 -0
  172. package/assets/voice/tsun-04.opus +0 -0
  173. package/assets/voice/twrite-00.opus +0 -0
  174. package/assets/voice/unknown-01.opus +0 -0
  175. package/assets/voice/waitApproval-00.opus +0 -0
  176. package/assets/voice/waitApproval-01.opus +0 -0
  177. package/assets/voice/waitApproval-02.opus +0 -0
  178. package/assets/voice/waitApproval-03.opus +0 -0
  179. package/assets/voice/waitApprovalLate-02.opus +0 -0
  180. package/assets/voice/waitApprovalLate-03.opus +0 -0
  181. package/assets/voice/waitAsk-00.opus +0 -0
  182. package/assets/voice/waitAsk-02.opus +0 -0
  183. package/assets/voice/waitAskLate-03.opus +0 -0
  184. package/assets/voice/wake-03.opus +0 -0
  185. package/assets/voice/wake-04.opus +0 -0
  186. package/assets/voice/wake-05.opus +0 -0
  187. package/assets/voice/wake-06.opus +0 -0
  188. package/lib/client.js +7 -7
  189. package/lib/features/notes.js +172 -172
  190. package/lib/features/reminder.js +216 -216
  191. package/lib/l2d-plan.js +295 -295
  192. package/lib/l2d-tools.js +293 -293
  193. package/lib/ledger.js +163 -163
  194. package/lib/mys/config.js +183 -183
  195. package/lib/mys/ds.js +209 -209
  196. package/lib/mys/http.js +85 -85
  197. package/lib/mys/mys.js +419 -419
  198. package/lib/mys/qr.js +434 -434
  199. package/lib/mys/scheduler.js +206 -206
  200. package/lib/mys/store.js +248 -248
  201. package/lib/mys/tz.js +174 -174
  202. package/lib/wavtrim.js +245 -245
  203. package/lib/worldbook/NOTICE.md +18 -18
  204. package/lib/worldbook/data/Cyrene.md +111 -111
  205. package/lib/worldbook/data/_glossary.md +181 -181
  206. package/lib/worldbook/data/characters.md +289 -289
  207. package/lib/worldbook/data/story.md +130 -130
  208. package/lib/worldbook/data/world.md +182 -182
  209. package/lib/worldbook.js +189 -189
  210. package/package.json +72 -69
package/lib/wavtrim.js CHANGED
@@ -1,245 +1,245 @@
1
- /**
2
- * 语音边缘处理:裁掉句首的那口气、句尾的静音,两端各加一小段淡入淡出。
3
- *
4
- * 为什么需要它:
5
- * 参考音本身带着起音的气声时,合成结果会在每句开头多出半拍鼻音,
6
- * ASR 会把它听成「啊 / 哎 / 嗯」;句尾又会拖 140–330ms 的死静音。
7
- * 这两件事都跟模型无关,纯粹是生成的边缘没收拾干净。
8
- *
9
- * 只处理 16-bit PCM 的单/多声道 WAV;其它格式原样返回,不改一个字。
10
- */
11
-
12
- const DEFAULTS = {
13
- headPadMs: 40, // 第一个有声点之前保留多少
14
- headFadeMs: 20, // 开头淡入
15
- tailPadMs: 80, // 最后一个有声点之后保留多少
16
- tailFadeMs: 40, // 结尾淡出
17
- // 有声判定:相对满量程的比例。0.02 够切掉起音那口气,又不会把软辅音当成静音。
18
- threshold: 0.02,
19
- // 兜底:无论判定成什么样,开头最多只砍 120ms、结尾最多砍 400ms。
20
- // 没有这条,遇到「秋」这种轻声起头的句子会一口气砍掉半句(实测砍过 1.05 秒)。
21
- maxHeadTrimMs: 120,
22
- maxTailTrimMs: 400,
23
- }
24
-
25
- // 合并选项时把 undefined / null 跳过。
26
- // 踩过的坑:宿主传的是 `Number(conf.head_pad_ms) || undefined`,配置里没写的项
27
- // 会变成显式的 undefined,Object.assign 一盖就把默认值冲成 undefined ——
28
- // 于是 Math.round(rate * undefined) = NaN,整个函数静默变成 no-op。
29
- function withDefaults(def, opts) {
30
- const o = Object.assign({}, def)
31
- if (opts && typeof opts === 'object') {
32
- for (const k of Object.keys(opts)) {
33
- const v = opts[k]
34
- if (v !== undefined && v !== null && !(typeof v === 'number' && Number.isNaN(v))) o[k] = v
35
- }
36
- }
37
- return o
38
- }
39
-
40
- function findChunks(buf) {
41
- if (buf.length < 64) return null
42
- if (buf.toString('ascii', 0, 4) !== 'RIFF') return null
43
- if (buf.toString('ascii', 8, 12) !== 'WAVE') return null
44
- let off = 12
45
- let fmt = null
46
- let dataOff = -1
47
- let dataSize = 0
48
- while (off + 8 <= buf.length) {
49
- const id = buf.toString('ascii', off, off + 4)
50
- const size = buf.readUInt32LE(off + 4)
51
- if (id === 'fmt ') {
52
- fmt = {
53
- format: buf.readUInt16LE(off + 8),
54
- channels: buf.readUInt16LE(off + 10),
55
- rate: buf.readUInt32LE(off + 12),
56
- bits: buf.readUInt16LE(off + 22),
57
- }
58
- } else if (id === 'data') {
59
- dataOff = off + 8
60
- dataSize = Math.min(size, buf.length - dataOff)
61
- break
62
- }
63
- off += 8 + size + (size & 1)
64
- }
65
- if (!fmt || dataOff < 0) return null
66
- return { fmt, dataOff, dataSize }
67
- }
68
-
69
- export default function trimWav(buf, opts) {
70
- try {
71
- const o = withDefaults(DEFAULTS, opts)
72
- const info = findChunks(buf)
73
- if (!info) return buf
74
- const { fmt, dataOff, dataSize } = info
75
- if (fmt.format !== 1 || fmt.bits !== 16 || !fmt.rate) return buf
76
-
77
- const ch = Math.max(1, fmt.channels)
78
- const total = Math.floor(dataSize / 2 / ch)
79
- if (total < 8) return buf
80
-
81
- const pcm = new Int16Array(total * ch)
82
- for (let i = 0; i < pcm.length; i++) pcm[i] = buf.readInt16LE(dataOff + i * 2)
83
-
84
- const thr = Math.max(80, Math.round(o.threshold * 32767))
85
- let first = -1
86
- let last = -1
87
- for (let i = 0; i < total; i++) {
88
- let peak = 0
89
- for (let c = 0; c < ch; c++) {
90
- const v = Math.abs(pcm[i * ch + c])
91
- if (v > peak) peak = v
92
- }
93
- if (peak >= thr) {
94
- if (first < 0) first = i
95
- last = i
96
- }
97
- }
98
- if (first < 0 || last <= first) return buf
99
-
100
- const perMs = fmt.rate / 1000
101
- const maxHead = Math.round(perMs * o.maxHeadTrimMs)
102
- const maxTail = Math.round(perMs * o.maxTailTrimMs)
103
- const start = Math.max(Math.max(0, first - Math.round(perMs * o.headPadMs)), first - maxHead)
104
- const end = Math.min(Math.min(total, last + Math.round(perMs * o.tailPadMs)), last + maxTail)
105
- if (end <= start) return buf
106
-
107
- // 整段都没被裁就不动它(避免每句都重建缓冲)
108
- const trimmedHead = start > 0
109
- const trimmedTail = end < total
110
- const outFrames = end - start
111
- const fadeIn = trimmedHead ? Math.min(Math.round(perMs * o.headFadeMs), outFrames) : 0
112
- const fadeOut = trimmedTail ? Math.min(Math.round(perMs * o.tailFadeMs), outFrames) : 0
113
-
114
- const payload = Buffer.alloc(outFrames * ch * 2)
115
- for (let i = 0; i < outFrames; i++) {
116
- let g = 1
117
- if (fadeIn && i < fadeIn) g = i / fadeIn
118
- if (fadeOut && i >= outFrames - fadeOut) g = Math.min(g, (outFrames - i - 1) / fadeOut)
119
- for (let c = 0; c < ch; c++) {
120
- payload.writeInt16LE(Math.round(pcm[(start + i) * ch + c] * g), (i * ch + c) * 2)
121
- }
122
- }
123
-
124
- const header = Buffer.from(buf.subarray(0, dataOff))
125
- header.writeUInt32LE(payload.length, dataOff - 4)
126
- header.writeUInt32LE(header.length + payload.length - 8, 4)
127
- return Buffer.concat([header, payload])
128
- } catch {
129
- return buf
130
- }
131
- }
132
-
133
- // ─────────────────── 句中的空当 ───────────────────
134
- // cut5 分句之后,段与段之间会夹进 0.7–1.1 秒的纯静音:一句七个字能拖到 4.9 秒,
135
- // 听感像她在每句话中间发呆。那些空当实测峰值只有 -46 dBFS、RMS -72 dB,
136
- // 是真静音,不是压低的软辅音 —— 所以可以放心压短。
137
- // 只压「被语音包住的」静音;首尾静音归 trimWav 管,这里一律不碰。
138
- const SQ_DEFAULTS = {
139
- maxGapMs: 260, // 长过这个的句中静音才处理
140
- keepMs: 110, // 压到这个长度(左右各留一半)
141
- threshold: 0.006, // 静音判定(相对满量程)
142
- winMs: 10, // 判定窗
143
- }
144
-
145
- export function squeezeWav(buf, opts) {
146
- try {
147
- const o = withDefaults(SQ_DEFAULTS, opts)
148
- const info = findChunks(buf)
149
- if (!info) return buf
150
- const { fmt, dataOff, dataSize } = info
151
- if (fmt.format !== 1 || fmt.bits !== 16 || !fmt.rate) return buf
152
-
153
- const ch = Math.max(1, fmt.channels)
154
- const total = Math.floor(dataSize / 2 / ch)
155
- if (total < 8) return buf
156
-
157
- const pcm = new Int16Array(total * ch)
158
- for (let i = 0; i < pcm.length; i++) pcm[i] = buf.readInt16LE(dataOff + i * 2)
159
-
160
- const thr = Math.max(60, Math.round(o.threshold * 32767))
161
- const win = Math.max(1, Math.round((fmt.rate * o.winMs) / 1000))
162
- const nWin = Math.floor(total / win)
163
- if (nWin < 4) return buf
164
-
165
- const silent = new Uint8Array(nWin)
166
- for (let w = 0; w < nWin; w++) {
167
- let peak = 0
168
- for (let i = w * win; i < (w + 1) * win; i++) {
169
- for (let c = 0; c < ch; c++) {
170
- const v = pcm[i * ch + c]
171
- const a = v < 0 ? -v : v
172
- if (a > peak) peak = a
173
- }
174
- if (peak >= thr) break
175
- }
176
- silent[w] = peak < thr ? 1 : 0
177
- }
178
-
179
- // 连续静音段(按样本)
180
- const runs = []
181
- let s = -1
182
- for (let w = 0; w <= nWin; w++) {
183
- const q = w < nWin && silent[w]
184
- if (q && s < 0) s = w
185
- else if (!q && s >= 0) {
186
- runs.push([s * win, w * win])
187
- s = -1
188
- }
189
- }
190
- if (!runs.length) return buf
191
-
192
- let firstVoice = 0
193
- while (firstVoice < nWin && silent[firstVoice]) firstVoice++
194
- let lastVoice = nWin - 1
195
- while (lastVoice > 0 && silent[lastVoice]) lastVoice--
196
- if (lastVoice <= firstVoice) return buf
197
-
198
- const keep = Math.round((fmt.rate * o.keepMs) / 1000)
199
- const maxGap = Math.round((fmt.rate * o.maxGapMs) / 1000)
200
- const cuts = []
201
- for (const [a, b] of runs) {
202
- if (b <= firstVoice * win || a >= lastVoice * win) continue // 首尾的不归它管
203
- const len = b - a
204
- if (len <= maxGap) continue
205
- // 删「空当开头」那一截,把靠后、紧挨着下一个字的 keepMs 留下。
206
- //
207
- // 这里踩过一个真 bug:原来写的是
208
- // from = a + (len - keep) / 2; cuts.push([from, from + (len - keep)])
209
- // —— 想「留中间」,可删的区间从中间起算、长度又是 len - keep,
210
- // 终点等于 a + 1.5×(len-keep),直接越过 b 伸进下一个字。
211
- // 实测 1200ms 的空当删了 1090ms,其中约 435ms 是下一个字的起音,
212
- // 听感就是「字被剪掉、字间抽一下」。keepMs 必须留在尾巴上。
213
- const cutEnd = a + (len - keep)
214
- if (cutEnd > b) continue // 兜底:任何情况下都不许删过 b
215
- cuts.push([a, cutEnd])
216
- }
217
- if (!cuts.length) return buf
218
-
219
- let frames = total
220
- for (const [a, b] of cuts) frames -= b - a
221
- if (frames < 8) return buf
222
-
223
- const payload = Buffer.alloc(frames * ch * 2)
224
- let src = 0
225
- let dst = 0
226
- for (const [a, b] of cuts) {
227
- const n = a - src
228
- for (let i = 0; i < n; i++) {
229
- for (let c = 0; c < ch; c++) payload.writeInt16LE(pcm[(src + i) * ch + c], (dst + i) * ch * 2 + c * 2)
230
- }
231
- dst += n
232
- src = b
233
- }
234
- for (let i = src; i < total; i++) {
235
- for (let c = 0; c < ch; c++) payload.writeInt16LE(pcm[i * ch + c], (dst + i - src) * ch * 2 + c * 2)
236
- }
237
-
238
- const header = Buffer.from(buf.subarray(0, dataOff))
239
- header.writeUInt32LE(payload.length, dataOff - 4)
240
- header.writeUInt32LE(header.length + payload.length - 8, 4)
241
- return Buffer.concat([header, payload])
242
- } catch {
243
- return buf
244
- }
245
- }
1
+ /**
2
+ * 语音边缘处理:裁掉句首的那口气、句尾的静音,两端各加一小段淡入淡出。
3
+ *
4
+ * 为什么需要它:
5
+ * 参考音本身带着起音的气声时,合成结果会在每句开头多出半拍鼻音,
6
+ * ASR 会把它听成「啊 / 哎 / 嗯」;句尾又会拖 140–330ms 的死静音。
7
+ * 这两件事都跟模型无关,纯粹是生成的边缘没收拾干净。
8
+ *
9
+ * 只处理 16-bit PCM 的单/多声道 WAV;其它格式原样返回,不改一个字。
10
+ */
11
+
12
+ const DEFAULTS = {
13
+ headPadMs: 40, // 第一个有声点之前保留多少
14
+ headFadeMs: 20, // 开头淡入
15
+ tailPadMs: 80, // 最后一个有声点之后保留多少
16
+ tailFadeMs: 40, // 结尾淡出
17
+ // 有声判定:相对满量程的比例。0.02 够切掉起音那口气,又不会把软辅音当成静音。
18
+ threshold: 0.02,
19
+ // 兜底:无论判定成什么样,开头最多只砍 120ms、结尾最多砍 400ms。
20
+ // 没有这条,遇到「秋」这种轻声起头的句子会一口气砍掉半句(实测砍过 1.05 秒)。
21
+ maxHeadTrimMs: 120,
22
+ maxTailTrimMs: 400,
23
+ }
24
+
25
+ // 合并选项时把 undefined / null 跳过。
26
+ // 踩过的坑:宿主传的是 `Number(conf.head_pad_ms) || undefined`,配置里没写的项
27
+ // 会变成显式的 undefined,Object.assign 一盖就把默认值冲成 undefined ——
28
+ // 于是 Math.round(rate * undefined) = NaN,整个函数静默变成 no-op。
29
+ function withDefaults(def, opts) {
30
+ const o = Object.assign({}, def)
31
+ if (opts && typeof opts === 'object') {
32
+ for (const k of Object.keys(opts)) {
33
+ const v = opts[k]
34
+ if (v !== undefined && v !== null && !(typeof v === 'number' && Number.isNaN(v))) o[k] = v
35
+ }
36
+ }
37
+ return o
38
+ }
39
+
40
+ function findChunks(buf) {
41
+ if (buf.length < 64) return null
42
+ if (buf.toString('ascii', 0, 4) !== 'RIFF') return null
43
+ if (buf.toString('ascii', 8, 12) !== 'WAVE') return null
44
+ let off = 12
45
+ let fmt = null
46
+ let dataOff = -1
47
+ let dataSize = 0
48
+ while (off + 8 <= buf.length) {
49
+ const id = buf.toString('ascii', off, off + 4)
50
+ const size = buf.readUInt32LE(off + 4)
51
+ if (id === 'fmt ') {
52
+ fmt = {
53
+ format: buf.readUInt16LE(off + 8),
54
+ channels: buf.readUInt16LE(off + 10),
55
+ rate: buf.readUInt32LE(off + 12),
56
+ bits: buf.readUInt16LE(off + 22),
57
+ }
58
+ } else if (id === 'data') {
59
+ dataOff = off + 8
60
+ dataSize = Math.min(size, buf.length - dataOff)
61
+ break
62
+ }
63
+ off += 8 + size + (size & 1)
64
+ }
65
+ if (!fmt || dataOff < 0) return null
66
+ return { fmt, dataOff, dataSize }
67
+ }
68
+
69
+ export default function trimWav(buf, opts) {
70
+ try {
71
+ const o = withDefaults(DEFAULTS, opts)
72
+ const info = findChunks(buf)
73
+ if (!info) return buf
74
+ const { fmt, dataOff, dataSize } = info
75
+ if (fmt.format !== 1 || fmt.bits !== 16 || !fmt.rate) return buf
76
+
77
+ const ch = Math.max(1, fmt.channels)
78
+ const total = Math.floor(dataSize / 2 / ch)
79
+ if (total < 8) return buf
80
+
81
+ const pcm = new Int16Array(total * ch)
82
+ for (let i = 0; i < pcm.length; i++) pcm[i] = buf.readInt16LE(dataOff + i * 2)
83
+
84
+ const thr = Math.max(80, Math.round(o.threshold * 32767))
85
+ let first = -1
86
+ let last = -1
87
+ for (let i = 0; i < total; i++) {
88
+ let peak = 0
89
+ for (let c = 0; c < ch; c++) {
90
+ const v = Math.abs(pcm[i * ch + c])
91
+ if (v > peak) peak = v
92
+ }
93
+ if (peak >= thr) {
94
+ if (first < 0) first = i
95
+ last = i
96
+ }
97
+ }
98
+ if (first < 0 || last <= first) return buf
99
+
100
+ const perMs = fmt.rate / 1000
101
+ const maxHead = Math.round(perMs * o.maxHeadTrimMs)
102
+ const maxTail = Math.round(perMs * o.maxTailTrimMs)
103
+ const start = Math.max(Math.max(0, first - Math.round(perMs * o.headPadMs)), first - maxHead)
104
+ const end = Math.min(Math.min(total, last + Math.round(perMs * o.tailPadMs)), last + maxTail)
105
+ if (end <= start) return buf
106
+
107
+ // 整段都没被裁就不动它(避免每句都重建缓冲)
108
+ const trimmedHead = start > 0
109
+ const trimmedTail = end < total
110
+ const outFrames = end - start
111
+ const fadeIn = trimmedHead ? Math.min(Math.round(perMs * o.headFadeMs), outFrames) : 0
112
+ const fadeOut = trimmedTail ? Math.min(Math.round(perMs * o.tailFadeMs), outFrames) : 0
113
+
114
+ const payload = Buffer.alloc(outFrames * ch * 2)
115
+ for (let i = 0; i < outFrames; i++) {
116
+ let g = 1
117
+ if (fadeIn && i < fadeIn) g = i / fadeIn
118
+ if (fadeOut && i >= outFrames - fadeOut) g = Math.min(g, (outFrames - i - 1) / fadeOut)
119
+ for (let c = 0; c < ch; c++) {
120
+ payload.writeInt16LE(Math.round(pcm[(start + i) * ch + c] * g), (i * ch + c) * 2)
121
+ }
122
+ }
123
+
124
+ const header = Buffer.from(buf.subarray(0, dataOff))
125
+ header.writeUInt32LE(payload.length, dataOff - 4)
126
+ header.writeUInt32LE(header.length + payload.length - 8, 4)
127
+ return Buffer.concat([header, payload])
128
+ } catch {
129
+ return buf
130
+ }
131
+ }
132
+
133
+ // ─────────────────── 句中的空当 ───────────────────
134
+ // cut5 分句之后,段与段之间会夹进 0.7–1.1 秒的纯静音:一句七个字能拖到 4.9 秒,
135
+ // 听感像她在每句话中间发呆。那些空当实测峰值只有 -46 dBFS、RMS -72 dB,
136
+ // 是真静音,不是压低的软辅音 —— 所以可以放心压短。
137
+ // 只压「被语音包住的」静音;首尾静音归 trimWav 管,这里一律不碰。
138
+ const SQ_DEFAULTS = {
139
+ maxGapMs: 260, // 长过这个的句中静音才处理
140
+ keepMs: 110, // 压到这个长度(左右各留一半)
141
+ threshold: 0.006, // 静音判定(相对满量程)
142
+ winMs: 10, // 判定窗
143
+ }
144
+
145
+ export function squeezeWav(buf, opts) {
146
+ try {
147
+ const o = withDefaults(SQ_DEFAULTS, opts)
148
+ const info = findChunks(buf)
149
+ if (!info) return buf
150
+ const { fmt, dataOff, dataSize } = info
151
+ if (fmt.format !== 1 || fmt.bits !== 16 || !fmt.rate) return buf
152
+
153
+ const ch = Math.max(1, fmt.channels)
154
+ const total = Math.floor(dataSize / 2 / ch)
155
+ if (total < 8) return buf
156
+
157
+ const pcm = new Int16Array(total * ch)
158
+ for (let i = 0; i < pcm.length; i++) pcm[i] = buf.readInt16LE(dataOff + i * 2)
159
+
160
+ const thr = Math.max(60, Math.round(o.threshold * 32767))
161
+ const win = Math.max(1, Math.round((fmt.rate * o.winMs) / 1000))
162
+ const nWin = Math.floor(total / win)
163
+ if (nWin < 4) return buf
164
+
165
+ const silent = new Uint8Array(nWin)
166
+ for (let w = 0; w < nWin; w++) {
167
+ let peak = 0
168
+ for (let i = w * win; i < (w + 1) * win; i++) {
169
+ for (let c = 0; c < ch; c++) {
170
+ const v = pcm[i * ch + c]
171
+ const a = v < 0 ? -v : v
172
+ if (a > peak) peak = a
173
+ }
174
+ if (peak >= thr) break
175
+ }
176
+ silent[w] = peak < thr ? 1 : 0
177
+ }
178
+
179
+ // 连续静音段(按样本)
180
+ const runs = []
181
+ let s = -1
182
+ for (let w = 0; w <= nWin; w++) {
183
+ const q = w < nWin && silent[w]
184
+ if (q && s < 0) s = w
185
+ else if (!q && s >= 0) {
186
+ runs.push([s * win, w * win])
187
+ s = -1
188
+ }
189
+ }
190
+ if (!runs.length) return buf
191
+
192
+ let firstVoice = 0
193
+ while (firstVoice < nWin && silent[firstVoice]) firstVoice++
194
+ let lastVoice = nWin - 1
195
+ while (lastVoice > 0 && silent[lastVoice]) lastVoice--
196
+ if (lastVoice <= firstVoice) return buf
197
+
198
+ const keep = Math.round((fmt.rate * o.keepMs) / 1000)
199
+ const maxGap = Math.round((fmt.rate * o.maxGapMs) / 1000)
200
+ const cuts = []
201
+ for (const [a, b] of runs) {
202
+ if (b <= firstVoice * win || a >= lastVoice * win) continue // 首尾的不归它管
203
+ const len = b - a
204
+ if (len <= maxGap) continue
205
+ // 删「空当开头」那一截,把靠后、紧挨着下一个字的 keepMs 留下。
206
+ //
207
+ // 这里踩过一个真 bug:原来写的是
208
+ // from = a + (len - keep) / 2; cuts.push([from, from + (len - keep)])
209
+ // —— 想「留中间」,可删的区间从中间起算、长度又是 len - keep,
210
+ // 终点等于 a + 1.5×(len-keep),直接越过 b 伸进下一个字。
211
+ // 实测 1200ms 的空当删了 1090ms,其中约 435ms 是下一个字的起音,
212
+ // 听感就是「字被剪掉、字间抽一下」。keepMs 必须留在尾巴上。
213
+ const cutEnd = a + (len - keep)
214
+ if (cutEnd > b) continue // 兜底:任何情况下都不许删过 b
215
+ cuts.push([a, cutEnd])
216
+ }
217
+ if (!cuts.length) return buf
218
+
219
+ let frames = total
220
+ for (const [a, b] of cuts) frames -= b - a
221
+ if (frames < 8) return buf
222
+
223
+ const payload = Buffer.alloc(frames * ch * 2)
224
+ let src = 0
225
+ let dst = 0
226
+ for (const [a, b] of cuts) {
227
+ const n = a - src
228
+ for (let i = 0; i < n; i++) {
229
+ for (let c = 0; c < ch; c++) payload.writeInt16LE(pcm[(src + i) * ch + c], (dst + i) * ch * 2 + c * 2)
230
+ }
231
+ dst += n
232
+ src = b
233
+ }
234
+ for (let i = src; i < total; i++) {
235
+ for (let c = 0; c < ch; c++) payload.writeInt16LE(pcm[i * ch + c], (dst + i - src) * ch * 2 + c * 2)
236
+ }
237
+
238
+ const header = Buffer.from(buf.subarray(0, dataOff))
239
+ header.writeUInt32LE(payload.length, dataOff - 4)
240
+ header.writeUInt32LE(header.length + payload.length - 8, 4)
241
+ return Buffer.concat([header, payload])
242
+ } catch {
243
+ return buf
244
+ }
245
+ }
@@ -1,18 +1,18 @@
1
- # 世界书数据来源
2
-
3
- `data/` 下的五份 md(`_glossary.md` / `Cyrene.md` / `world.md` / `characters.md` / `story.md`,
4
- 共 61 条)不是本项目原创,来源如下:
5
-
6
- - 原始整理:[Cyrene-Agent](https://github.com/Playa-Cyrene/Cyrene-Agent)
7
- —— MIT License, Copyright (c) 2026 Playa
8
- - 转手仓库:[Cyrene-xl/DSH-Cyrene](https://github.com/Cyrene-xl/DSH-Cyrene) 的 `worldbook/data/`
9
-
10
- 条目格式(`- 触发词:` / `- 常驻:` / `- 内在价值:` / `- 优先级:` / `- 连带触发词:`)
11
- 沿用上游定义,未做改动;正文一字未改。
12
-
13
- **角色 IP 声明**:昔涟 /《崩坏:星穹铁道》相关角色与世界观的著作权归
14
- HoYoverse / 米哈游所有,不在上述 MIT 授权范围内。本目录仅限本机个人使用,
15
- 不得商用或再分发。
16
-
17
- `lib/worldbook.js` 里的解析与匹配代码是本项目自己写的(没有打包上游的
18
- `parse.mjs` / `match.mjs` / `assemble.mjs`),只读取本目录的数据。
1
+ # 世界书数据来源
2
+
3
+ `data/` 下的五份 md(`_glossary.md` / `Cyrene.md` / `world.md` / `characters.md` / `story.md`,
4
+ 共 61 条)不是本项目原创,来源如下:
5
+
6
+ - 原始整理:[Cyrene-Agent](https://github.com/Playa-Cyrene/Cyrene-Agent)
7
+ —— MIT License, Copyright (c) 2026 Playa
8
+ - 转手仓库:[Cyrene-xl/DSH-Cyrene](https://github.com/Cyrene-xl/DSH-Cyrene) 的 `worldbook/data/`
9
+
10
+ 条目格式(`- 触发词:` / `- 常驻:` / `- 内在价值:` / `- 优先级:` / `- 连带触发词:`)
11
+ 沿用上游定义,未做改动;正文一字未改。
12
+
13
+ **角色 IP 声明**:昔涟 /《崩坏:星穹铁道》相关角色与世界观的著作权归
14
+ HoYoverse / 米哈游所有,不在上述 MIT 授权范围内。本目录仅限本机个人使用,
15
+ 不得商用或再分发。
16
+
17
+ `lib/worldbook.js` 里的解析与匹配代码是本项目自己写的(没有打包上游的
18
+ `parse.mjs` / `match.mjs` / `assemble.mjs`),只读取本目录的数据。