dsh-game-material-master 0.7.0 → 0.9.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.
- package/README.en.md +173 -16
- package/README.md +136 -17
- package/lib/client.js +4145 -259
- package/lib/comfy.js +769 -0
- package/lib/config.js +83 -2
- package/lib/directions.js +9 -2
- package/lib/index.js +677 -26
- package/lib/links.js +152 -10
- package/lib/mapdoc.js +178 -14
- package/lib/mapexport.js +30 -2
- package/lib/mapgen.js +643 -44
- package/lib/maprender.js +155 -13
- package/lib/mapsamplegen.js +139 -12
- package/lib/maptiles.js +1014 -1
- package/lib/media.js +106 -4
- package/lib/midifile.js +390 -0
- package/lib/midipreview.js +326 -0
- package/lib/minimax.js +2 -2
- package/lib/musicapi.js +883 -0
- package/lib/musicaudio.js +249 -0
- package/lib/musicgen.js +2215 -0
- package/lib/musicguide.js +287 -0
- package/lib/musicmodels.js +200 -0
- package/lib/musicscore.js +931 -0
- package/lib/musicscoreguide.js +496 -0
- package/lib/pipeline.js +102 -51
- package/lib/riggen.js +7 -1
- package/lib/seqgen.js +31 -23
- package/lib/sfxapi.js +391 -0
- package/lib/sfxgen.js +819 -0
- package/lib/store.js +81 -0
- package/lib/tilegen.js +3 -2
- package/lib/tools.js +621 -37
- package/lib/videogen.js +150 -0
- package/lib/wire.js +366 -10
- package/package.json +20 -6
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 谱曲模式的内置试听合成器(FluidSynth / SoundFont 不可用时的兜底)。
|
|
3
|
+
*
|
|
4
|
+
* 目标不是「好听」,而是**任何机器上都能听出曲式、节奏、和声与力度变化**:
|
|
5
|
+
* · 按 General MIDI 的 program family 选波形与包络;
|
|
6
|
+
* · 通道 9 按 GM 鼓映射合成 kick / snare / hihat / tom / crash / ride;
|
|
7
|
+
* · 读取 CC7 音量、CC10 声像;
|
|
8
|
+
* · 峰值归一到 -1 dBFS,不做压缩(保留力度弧)。
|
|
9
|
+
*
|
|
10
|
+
* 合成是确定性的:噪声用固定种子,同一份 MIDI 重复渲染结果一致。
|
|
11
|
+
* 超过 `maxSeconds` 的曲子只渲染前段并标记 `truncated`,避免一次吃掉几百 MB。
|
|
12
|
+
*/
|
|
13
|
+
import { writeWavPcm16 } from "./midifile.js";
|
|
14
|
+
const DEFAULT_SAMPLE_RATE = 44100;
|
|
15
|
+
const DEFAULT_MAX_SECONDS = 480;
|
|
16
|
+
const BLOCK_FRAMES = 4096;
|
|
17
|
+
function clamp(value, min, max) {
|
|
18
|
+
return value < min ? min : value > max ? max : value;
|
|
19
|
+
}
|
|
20
|
+
/** 确定性 PRNG(mulberry32):同一个 seed 每次渲染出同样的噪声。 */
|
|
21
|
+
function mulberry32(seed) {
|
|
22
|
+
let a = seed >>> 0;
|
|
23
|
+
return () => {
|
|
24
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
25
|
+
let t = a;
|
|
26
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
27
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
28
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function profileFor(program) {
|
|
32
|
+
const p = ((program % 128) + 128) % 128;
|
|
33
|
+
if (p < 8)
|
|
34
|
+
return { attack: 0.004, decay: 0.9, sustain: 0.12, release: 0.35, gain: 0.9, pluck: true };
|
|
35
|
+
if (p < 16)
|
|
36
|
+
return { attack: 0.002, decay: 0.45, sustain: 0.05, release: 0.5, gain: 0.8, pluck: true };
|
|
37
|
+
if (p < 24)
|
|
38
|
+
return { attack: 0.03, decay: 0.1, sustain: 0.8, release: 0.25, gain: 0.55, pluck: false };
|
|
39
|
+
if (p < 32)
|
|
40
|
+
return { attack: 0.006, decay: 0.5, sustain: 0.2, release: 0.25, gain: 0.85, pluck: true };
|
|
41
|
+
if (p < 40)
|
|
42
|
+
return { attack: 0.01, decay: 0.2, sustain: 0.7, release: 0.2, gain: 0.95, pluck: false };
|
|
43
|
+
if (p < 48)
|
|
44
|
+
return { attack: 0.08, decay: 0.2, sustain: 0.75, release: 0.5, gain: 0.55, pluck: false };
|
|
45
|
+
if (p < 56)
|
|
46
|
+
return { attack: 0.12, decay: 0.2, sustain: 0.8, release: 0.6, gain: 0.5, pluck: false };
|
|
47
|
+
if (p < 64)
|
|
48
|
+
return { attack: 0.04, decay: 0.2, sustain: 0.75, release: 0.35, gain: 0.6, pluck: false };
|
|
49
|
+
if (p < 72)
|
|
50
|
+
return { attack: 0.03, decay: 0.2, sustain: 0.8, release: 0.3, gain: 0.55, pluck: false };
|
|
51
|
+
if (p < 80)
|
|
52
|
+
return { attack: 0.05, decay: 0.15, sustain: 0.8, release: 0.4, gain: 0.6, pluck: false };
|
|
53
|
+
if (p < 88)
|
|
54
|
+
return { attack: 0.01, decay: 0.25, sustain: 0.6, release: 0.3, gain: 0.55, pluck: false };
|
|
55
|
+
if (p < 96)
|
|
56
|
+
return { attack: 0.25, decay: 0.3, sustain: 0.8, release: 0.8, gain: 0.45, pluck: false };
|
|
57
|
+
if (p < 104)
|
|
58
|
+
return { attack: 0.01, decay: 0.4, sustain: 0.2, release: 0.4, gain: 0.6, pluck: true };
|
|
59
|
+
if (p < 112)
|
|
60
|
+
return { attack: 0.02, decay: 0.35, sustain: 0.2, release: 0.35, gain: 0.6, pluck: true };
|
|
61
|
+
if (p < 120)
|
|
62
|
+
return { attack: 0.005, decay: 0.2, sustain: 0.1, release: 0.2, gain: 0.55, pluck: true };
|
|
63
|
+
return { attack: 0.005, decay: 0.3, sustain: 0.2, release: 0.3, gain: 0.45, pluck: true };
|
|
64
|
+
}
|
|
65
|
+
/** program family 的波形。phase 是弧度,freq 只在需要频率相关的音色里用。 */
|
|
66
|
+
function waveFor(program, phase) {
|
|
67
|
+
const p = ((program % 128) + 128) % 128;
|
|
68
|
+
if (p < 8) {
|
|
69
|
+
// 钢琴:基音 + 二三次谐波,高次衰减靠包络
|
|
70
|
+
return Math.sin(phase) + 0.34 * Math.sin(phase * 2) + 0.14 * Math.sin(phase * 3);
|
|
71
|
+
}
|
|
72
|
+
if (p < 16) {
|
|
73
|
+
// 色彩打击:非整数谐波,钟琴感
|
|
74
|
+
return Math.sin(phase) + 0.45 * Math.sin(phase * 3.01) + 0.2 * Math.sin(phase * 4.98);
|
|
75
|
+
}
|
|
76
|
+
if (p < 24) {
|
|
77
|
+
// 风琴:加法合成
|
|
78
|
+
return Math.sin(phase) + 0.5 * Math.sin(phase * 2) + 0.28 * Math.sin(phase * 3) + 0.16 * Math.sin(phase * 4);
|
|
79
|
+
}
|
|
80
|
+
if (p < 32) {
|
|
81
|
+
// 吉他:锯齿感 + 拨弦瞬态
|
|
82
|
+
return 0.55 * Math.sin(phase) + 0.3 * Math.sin(phase * 2) + 0.12 * Math.sin(phase * 3);
|
|
83
|
+
}
|
|
84
|
+
if (p < 40) {
|
|
85
|
+
// 贝斯:基音 + 少量二次
|
|
86
|
+
return Math.sin(phase) + 0.25 * Math.sin(phase * 2);
|
|
87
|
+
}
|
|
88
|
+
if (p < 48) {
|
|
89
|
+
// 弦乐:两个微微失谐的锯齿
|
|
90
|
+
return 0.42 * Math.sin(phase) + 0.3 * Math.sin(phase * 2.01) + 0.16 * Math.sin(phase * 3.02) + 0.1 * Math.sin(phase * 4.03);
|
|
91
|
+
}
|
|
92
|
+
if (p < 56) {
|
|
93
|
+
// 合奏:更宽的失谐
|
|
94
|
+
return 0.3 * Math.sin(phase) + 0.26 * Math.sin(phase * 2.02) + 0.2 * Math.sin(phase * 3.03) + 0.12 * Math.sin(phase * 4.04) + 0.08 * Math.sin(phase * 5.05);
|
|
95
|
+
}
|
|
96
|
+
if (p < 64) {
|
|
97
|
+
// 铜管:锯齿 + 二次
|
|
98
|
+
return 0.5 * Math.sin(phase) + 0.3 * Math.sin(phase * 2) + 0.16 * Math.sin(phase * 3) + 0.08 * Math.sin(phase * 4);
|
|
99
|
+
}
|
|
100
|
+
if (p < 72) {
|
|
101
|
+
// 簧片:方波感
|
|
102
|
+
return Math.sin(phase) + 0.38 * Math.sin(phase * 3) + 0.18 * Math.sin(phase * 5);
|
|
103
|
+
}
|
|
104
|
+
if (p < 80) {
|
|
105
|
+
// 管乐:柔和基音 + 二次
|
|
106
|
+
return Math.sin(phase) + 0.18 * Math.sin(phase * 2) + 0.06 * Math.sin(phase * 3);
|
|
107
|
+
}
|
|
108
|
+
if (p < 88) {
|
|
109
|
+
// 合成主音:锯齿
|
|
110
|
+
return 0.5 * Math.sin(phase) + 0.28 * Math.sin(phase * 2) + 0.14 * Math.sin(phase * 3) + 0.07 * Math.sin(phase * 4);
|
|
111
|
+
}
|
|
112
|
+
if (p < 96) {
|
|
113
|
+
// 合成垫:慢起音 + 三角感
|
|
114
|
+
return 0.45 * Math.sin(phase) + 0.2 * Math.sin(phase * 2) + 0.08 * Math.sin(phase * 3);
|
|
115
|
+
}
|
|
116
|
+
if (p < 104) {
|
|
117
|
+
return Math.sin(phase) + 0.3 * Math.sin(phase * 2.01);
|
|
118
|
+
}
|
|
119
|
+
if (p < 112) {
|
|
120
|
+
return 0.6 * Math.sin(phase) + 0.25 * Math.sin(phase * 2) + 0.12 * Math.sin(phase * 3.01);
|
|
121
|
+
}
|
|
122
|
+
if (p < 120) {
|
|
123
|
+
return 0.7 * Math.sin(phase) + 0.2 * Math.sin(phase * 2);
|
|
124
|
+
}
|
|
125
|
+
return 0.5 * Math.sin(phase) + 0.25 * Math.sin(phase * 2.5);
|
|
126
|
+
}
|
|
127
|
+
/** 包络:attack → decay 到 sustain → note 结束后的 release。 */
|
|
128
|
+
function envelopeAt(profile, elapsed, noteDuration) {
|
|
129
|
+
if (elapsed < 0)
|
|
130
|
+
return 0;
|
|
131
|
+
if (elapsed < noteDuration) {
|
|
132
|
+
const attack = Math.max(0.001, profile.attack);
|
|
133
|
+
if (elapsed < attack)
|
|
134
|
+
return elapsed / attack;
|
|
135
|
+
if (profile.pluck) {
|
|
136
|
+
const decay = Math.max(0.02, profile.decay);
|
|
137
|
+
const t = elapsed - attack;
|
|
138
|
+
return profile.sustain + (1 - profile.sustain) * Math.exp(-t / decay);
|
|
139
|
+
}
|
|
140
|
+
const decay = Math.max(0.02, profile.decay);
|
|
141
|
+
const t = elapsed - attack;
|
|
142
|
+
return profile.sustain + (1 - profile.sustain) * Math.exp(-t / decay);
|
|
143
|
+
}
|
|
144
|
+
const release = Math.max(0.03, profile.release);
|
|
145
|
+
const atEnd = envelopeAt({ ...profile, release }, noteDuration - 1e-6, noteDuration);
|
|
146
|
+
return atEnd * Math.exp(-(elapsed - noteDuration) / release);
|
|
147
|
+
}
|
|
148
|
+
function noteFrequency(note) {
|
|
149
|
+
return 440 * Math.pow(2, (note - 69) / 12);
|
|
150
|
+
}
|
|
151
|
+
function panGains(pan) {
|
|
152
|
+
const clamped = clamp(pan, -1, 1);
|
|
153
|
+
// 等功率声像
|
|
154
|
+
const angle = ((clamped + 1) / 2) * (Math.PI / 2);
|
|
155
|
+
return { left: Math.cos(angle), right: Math.sin(angle) };
|
|
156
|
+
}
|
|
157
|
+
/** GM 鼓:返回一个 0..1 的包络值;不同 note 用不同合成方式。 */
|
|
158
|
+
function drumSample(note, elapsed, noise) {
|
|
159
|
+
if (elapsed < 0)
|
|
160
|
+
return 0;
|
|
161
|
+
const n = note;
|
|
162
|
+
if (n === 35 || n === 36) {
|
|
163
|
+
// kick:音高从 120Hz 快速下探到 45Hz
|
|
164
|
+
const f = 45 + 75 * Math.exp(-elapsed / 0.035);
|
|
165
|
+
const env = Math.exp(-elapsed / 0.22);
|
|
166
|
+
return Math.sin(2 * Math.PI * f * elapsed) * env * 1.1;
|
|
167
|
+
}
|
|
168
|
+
if (n === 38 || n === 40) {
|
|
169
|
+
// snare:噪声 + 180Hz 鼓身
|
|
170
|
+
const env = Math.exp(-elapsed / 0.13);
|
|
171
|
+
return (noise() * 0.7 + Math.sin(2 * Math.PI * 180 * elapsed) * 0.35) * env;
|
|
172
|
+
}
|
|
173
|
+
if (n === 39) {
|
|
174
|
+
// clap:三连击噪声
|
|
175
|
+
const burst = (t) => (t >= 0 && t < 0.05 ? Math.exp(-t / 0.018) : 0);
|
|
176
|
+
const env = Math.max(burst(elapsed), burst(elapsed - 0.035), burst(elapsed - 0.07)) + Math.exp(-elapsed / 0.12) * 0.25;
|
|
177
|
+
return noise() * env;
|
|
178
|
+
}
|
|
179
|
+
if (n === 42 || n === 44) {
|
|
180
|
+
return noise() * Math.exp(-elapsed / 0.035) * 0.55;
|
|
181
|
+
}
|
|
182
|
+
if (n === 46) {
|
|
183
|
+
return noise() * Math.exp(-elapsed / 0.28) * 0.5;
|
|
184
|
+
}
|
|
185
|
+
if (n === 49 || n === 57) {
|
|
186
|
+
return noise() * Math.exp(-elapsed / 0.9) * 0.42;
|
|
187
|
+
}
|
|
188
|
+
if (n === 51 || n === 59) {
|
|
189
|
+
return (noise() * 0.35 + Math.sin(2 * Math.PI * 6000 * elapsed) * 0.15) * Math.exp(-elapsed / 0.45);
|
|
190
|
+
}
|
|
191
|
+
if (n >= 41 && n <= 50) {
|
|
192
|
+
const base = 90 + (n - 41) * 18;
|
|
193
|
+
const f = base + 60 * Math.exp(-elapsed / 0.08);
|
|
194
|
+
return Math.sin(2 * Math.PI * f * elapsed) * Math.exp(-elapsed / 0.3) * 0.8;
|
|
195
|
+
}
|
|
196
|
+
if (n === 37)
|
|
197
|
+
return Math.sin(2 * Math.PI * 800 * elapsed) * Math.exp(-elapsed / 0.08) * 0.6; // side stick
|
|
198
|
+
if (n === 56)
|
|
199
|
+
return Math.sin(2 * Math.PI * 800 * elapsed) * Math.exp(-elapsed / 0.18) * 0.5; // cowbell
|
|
200
|
+
if (n === 54 || n === 69 || n === 70)
|
|
201
|
+
return noise() * Math.exp(-elapsed / 0.12) * 0.45;
|
|
202
|
+
// 其它鼓:短噪声,保证听得到
|
|
203
|
+
return noise() * Math.exp(-elapsed / 0.1) * 0.4;
|
|
204
|
+
}
|
|
205
|
+
function noteGain(note) {
|
|
206
|
+
const velocity = clamp(note.velocity / 127, 0, 1);
|
|
207
|
+
const volume = clamp(note.volume / 127, 0, 1);
|
|
208
|
+
const profile = profileFor(note.program);
|
|
209
|
+
return profile.gain * (0.25 + 0.75 * velocity) * (0.35 + 0.65 * volume);
|
|
210
|
+
}
|
|
211
|
+
function notePan(note) {
|
|
212
|
+
if (note.channel === 9)
|
|
213
|
+
return 0;
|
|
214
|
+
return clamp((note.pan - 64) / 64, -1, 1);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 把解析后的 MIDI 渲染成 WAV。
|
|
218
|
+
*
|
|
219
|
+
* 返回的 `peak` 是归一化前的最大绝对采样值,便于调用方诊断「是不是整首太轻」。
|
|
220
|
+
*/
|
|
221
|
+
export function renderMidiToWav(parsed, options = {}) {
|
|
222
|
+
const sampleRate = Math.max(8000, Math.min(96000, Math.round(options.sampleRate ?? DEFAULT_SAMPLE_RATE)));
|
|
223
|
+
const maxSeconds = Math.max(1, Math.min(900, options.maxSeconds ?? DEFAULT_MAX_SECONDS));
|
|
224
|
+
const sourceDuration = Math.max(0, parsed.meta.duration);
|
|
225
|
+
const truncated = sourceDuration > maxSeconds;
|
|
226
|
+
const duration = Math.min(sourceDuration + 0.8, maxSeconds);
|
|
227
|
+
const totalFrames = Math.max(1, Math.ceil(duration * sampleRate));
|
|
228
|
+
const notes = parsed.notes.filter((note) => note.start < duration && note.velocity > 0);
|
|
229
|
+
if (notes.length > 80_000)
|
|
230
|
+
throw new Error(`音符太多(${notes.length}),内置试听合成器拒绝渲染;请安装 FluidSynth + SoundFont。`);
|
|
231
|
+
const out = Buffer.alloc(totalFrames * 4);
|
|
232
|
+
const mixL = new Float32Array(BLOCK_FRAMES);
|
|
233
|
+
const mixR = new Float32Array(BLOCK_FRAMES);
|
|
234
|
+
// 每个音符一个固定噪声种子,保证同一份 MIDI 每次渲染一致。
|
|
235
|
+
const noiseSeeds = notes.map((note, index) => {
|
|
236
|
+
let seed = (note.channel + 1) * 73856093 ^ (note.note + 1) * 19349663 ^ Math.round(note.start * 1000) * 83492791 ^ index;
|
|
237
|
+
return seed >>> 0;
|
|
238
|
+
});
|
|
239
|
+
let peak = 0;
|
|
240
|
+
for (let blockStart = 0; blockStart < totalFrames; blockStart += BLOCK_FRAMES) {
|
|
241
|
+
const blockLength = Math.min(BLOCK_FRAMES, totalFrames - blockStart);
|
|
242
|
+
mixL.fill(0, 0, blockLength);
|
|
243
|
+
mixR.fill(0, 0, blockLength);
|
|
244
|
+
const blockTime = blockStart / sampleRate;
|
|
245
|
+
const blockEnd = (blockStart + blockLength) / sampleRate;
|
|
246
|
+
for (let index = 0; index < notes.length; index++) {
|
|
247
|
+
const note = notes[index];
|
|
248
|
+
const profile = profileFor(note.program);
|
|
249
|
+
const releaseEnd = note.end + profile.release + 0.05;
|
|
250
|
+
if (releaseEnd <= blockTime)
|
|
251
|
+
continue;
|
|
252
|
+
if (note.start >= blockEnd)
|
|
253
|
+
break; // notes 按 start 排序
|
|
254
|
+
const fromFrame = Math.max(blockStart, Math.floor(note.start * sampleRate));
|
|
255
|
+
const toFrame = Math.min(blockStart + blockLength, Math.ceil(releaseEnd * sampleRate));
|
|
256
|
+
if (toFrame <= fromFrame)
|
|
257
|
+
continue;
|
|
258
|
+
const pan = notePan(note);
|
|
259
|
+
const gains = panGains(pan);
|
|
260
|
+
const baseGain = noteGain(note) * 0.16;
|
|
261
|
+
const frequency = noteFrequency(note.note);
|
|
262
|
+
const isDrum = note.channel === 9;
|
|
263
|
+
const noise = isDrum ? mulberry32(noiseSeeds[index]) : null;
|
|
264
|
+
const noteDuration = note.end - note.start;
|
|
265
|
+
// release 段的起点包络只算一次,避免每个采样递归。
|
|
266
|
+
const atEnd = isDrum ? 1 : envelopeAt(profile, Math.max(0, noteDuration - 1e-6), noteDuration);
|
|
267
|
+
for (let frame = fromFrame; frame < toFrame; frame++) {
|
|
268
|
+
const elapsed = frame / sampleRate - note.start;
|
|
269
|
+
let sample;
|
|
270
|
+
if (isDrum) {
|
|
271
|
+
sample = drumSample(note.note, elapsed, noise) * baseGain * 1.6;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
let env;
|
|
275
|
+
if (elapsed < noteDuration) {
|
|
276
|
+
env = envelopeAt(profile, elapsed, noteDuration);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const release = Math.max(0.03, profile.release);
|
|
280
|
+
env = atEnd * Math.exp(-(elapsed - noteDuration) / release);
|
|
281
|
+
}
|
|
282
|
+
if (env <= 0.00001)
|
|
283
|
+
continue;
|
|
284
|
+
const phase = 2 * Math.PI * frequency * Math.max(0, elapsed);
|
|
285
|
+
sample = waveFor(note.program, phase) * env * baseGain;
|
|
286
|
+
}
|
|
287
|
+
if (!Number.isFinite(sample) || sample === 0)
|
|
288
|
+
continue;
|
|
289
|
+
const offset = frame - blockStart;
|
|
290
|
+
mixL[offset] += sample * gains.left;
|
|
291
|
+
mixR[offset] += sample * gains.right;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
for (let i = 0; i < blockLength; i++) {
|
|
295
|
+
// 软限幅:多声部叠加时不至于硬削成方波
|
|
296
|
+
const left = Math.tanh(mixL[i] * 1.15);
|
|
297
|
+
const right = Math.tanh(mixR[i] * 1.15);
|
|
298
|
+
const absolute = Math.max(Math.abs(left), Math.abs(right));
|
|
299
|
+
if (absolute > peak)
|
|
300
|
+
peak = absolute;
|
|
301
|
+
const offset = (blockStart + i) * 4;
|
|
302
|
+
out.writeInt16LE(Math.round(clamp(left, -1, 1) * 32767), offset);
|
|
303
|
+
out.writeInt16LE(Math.round(clamp(right, -1, 1) * 32767), offset + 2);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// 归一化到 -1 dBFS:太响压下来,太轻提上去(最多 8 倍,避免把底噪拉爆)。
|
|
307
|
+
if (peak > 0) {
|
|
308
|
+
const target = 0.89;
|
|
309
|
+
const desired = target / Math.max(peak, 1e-6);
|
|
310
|
+
const scale = desired < 1 ? desired : Math.min(desired, 8);
|
|
311
|
+
if (Math.abs(scale - 1) > 0.01) {
|
|
312
|
+
for (let offset = 0; offset < out.length; offset += 2) {
|
|
313
|
+
const value = out.readInt16LE(offset) * scale;
|
|
314
|
+
out.writeInt16LE(Math.round(clamp(value, -32768, 32767)), offset);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return {
|
|
319
|
+
wav: writeWavPcm16(new Int16Array(out.buffer, out.byteOffset, out.length / 2), sampleRate, 2),
|
|
320
|
+
sampleRate,
|
|
321
|
+
duration,
|
|
322
|
+
peak,
|
|
323
|
+
truncated,
|
|
324
|
+
notes: notes.length
|
|
325
|
+
};
|
|
326
|
+
}
|
package/lib/minimax.js
CHANGED
|
@@ -359,9 +359,9 @@ export async function retrieveFile(input) {
|
|
|
359
359
|
return url;
|
|
360
360
|
}
|
|
361
361
|
/** 把视频下载到本地。 */
|
|
362
|
-
export async function downloadVideo(url, timeoutMs) {
|
|
362
|
+
export async function downloadVideo(url, timeoutMs, headers) {
|
|
363
363
|
const timeout = AbortSignal.timeout(Math.max(30000, timeoutMs));
|
|
364
|
-
const response = await fetch(url, { signal: timeout });
|
|
364
|
+
const response = await fetch(url, { signal: timeout, headers });
|
|
365
365
|
if (!response.ok)
|
|
366
366
|
throw new MiniMaxError(`下载视频失败(HTTP ${response.status})`);
|
|
367
367
|
return Buffer.from(await response.arrayBuffer());
|