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,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 音乐编辑的本地音频层(模块七)。
|
|
3
|
+
*
|
|
4
|
+
* 只做四件事:探测音频、按参数渲染、算波形峰值、给出可读错误。
|
|
5
|
+
* 所有 ffmpeg / ffprobe 调用都走 `media.ts` 的统一入口 —— 那边会处理
|
|
6
|
+
* FFMPEG_PATH、Windows 注册表与常见安装位置,绕过它就是「自检全绿、真机找不到 ffmpeg」。
|
|
7
|
+
*/
|
|
8
|
+
import { copyFile, mkdir, stat } from "node:fs/promises";
|
|
9
|
+
import { dirname, extname } from "node:path";
|
|
10
|
+
import { runFfmpeg, runFfprobe, MediaError } from "./media.js";
|
|
11
|
+
import { MUSIC_EDIT_BITRATES, MUSIC_EDIT_FORMATS } from "./musicmodels.js";
|
|
12
|
+
function clamp(value, fallback, min, max) {
|
|
13
|
+
const n = typeof value === "number" ? value : Number.parseFloat(String(value ?? ""));
|
|
14
|
+
if (!Number.isFinite(n))
|
|
15
|
+
return fallback;
|
|
16
|
+
return Math.min(max, Math.max(min, n));
|
|
17
|
+
}
|
|
18
|
+
function round2(value) {
|
|
19
|
+
return Math.round(value * 100) / 100;
|
|
20
|
+
}
|
|
21
|
+
/** 探测音频。duration 优先取 format.duration,缺失时退回第一条音频流的 duration。 */
|
|
22
|
+
export async function probeAudio(file) {
|
|
23
|
+
const { stdout } = await runFfprobe(["-v", "error", "-show_format", "-show_streams", "-of", "json", file], 60000);
|
|
24
|
+
let payload;
|
|
25
|
+
try {
|
|
26
|
+
payload = JSON.parse(stdout.toString("utf8"));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
throw new MediaError(`无法解析 ffprobe 输出:${file}`);
|
|
30
|
+
}
|
|
31
|
+
const streams = Array.isArray(payload?.streams) ? payload.streams : [];
|
|
32
|
+
const audio = streams.find((stream) => stream?.codec_type === "audio");
|
|
33
|
+
if (audio === undefined)
|
|
34
|
+
throw new MediaError(`这个文件里没有音频流:${file}`);
|
|
35
|
+
const formatDuration = Number.parseFloat(String(payload?.format?.duration ?? ""));
|
|
36
|
+
const streamDuration = Number.parseFloat(String(audio?.duration ?? ""));
|
|
37
|
+
const duration = Number.isFinite(formatDuration) && formatDuration > 0 ? formatDuration : streamDuration;
|
|
38
|
+
if (!Number.isFinite(duration) || duration <= 0)
|
|
39
|
+
throw new MediaError(`无法读取音频时长:${file}`);
|
|
40
|
+
let size = Number.parseInt(String(payload?.format?.size ?? ""), 10);
|
|
41
|
+
if (!Number.isFinite(size) || size <= 0) {
|
|
42
|
+
try {
|
|
43
|
+
size = (await stat(file)).size;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
size = 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
duration,
|
|
51
|
+
size,
|
|
52
|
+
codec: typeof audio?.codec_name === "string" ? audio.codec_name : undefined,
|
|
53
|
+
sampleRate: Number.isFinite(Number(audio?.sample_rate)) ? Number(audio.sample_rate) : undefined,
|
|
54
|
+
channels: Number.isFinite(Number(audio?.channels)) ? Number(audio.channels) : undefined,
|
|
55
|
+
bitRate: Number.isFinite(Number(payload?.format?.bit_rate)) ? Number(payload.format.bit_rate) : undefined
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 规范化编辑参数。
|
|
60
|
+
*
|
|
61
|
+
* 规则:
|
|
62
|
+
* - start/end 相对源文件;end 缺省 = 全长;夹到 [0,duration],end<=start 报错。
|
|
63
|
+
* - 淡入 + 淡出超过段长时**按比例一起缩**(只缩一个会让另一个仍越界)。
|
|
64
|
+
* - normalize 默认关;lufs 默认 -14、truePeak 默认 -1(dBTP)。
|
|
65
|
+
*/
|
|
66
|
+
export function normalizeEditOps(raw, duration) {
|
|
67
|
+
if (!Number.isFinite(duration) || duration <= 0)
|
|
68
|
+
throw new MediaError("源音频时长无效");
|
|
69
|
+
const start = clamp(raw.start, 0, 0, Math.max(0, duration - 0.05));
|
|
70
|
+
const endRaw = raw.end === undefined || raw.end === null || String(raw.end) === "" ? duration : Number(raw.end);
|
|
71
|
+
if (!Number.isFinite(endRaw))
|
|
72
|
+
throw new MediaError("裁剪终点必须是数字");
|
|
73
|
+
const end = clamp(endRaw, duration, 0, duration);
|
|
74
|
+
if (end <= start)
|
|
75
|
+
throw new MediaError(`裁剪终点必须大于起点(${round2(start)}s → ${round2(end)}s)`);
|
|
76
|
+
const segment = end - start;
|
|
77
|
+
let fadeIn = clamp(raw.fadeIn, 0, 0, 30);
|
|
78
|
+
let fadeOut = clamp(raw.fadeOut, 0, 0, 30);
|
|
79
|
+
if (fadeIn + fadeOut > segment) {
|
|
80
|
+
const scale = segment / (fadeIn + fadeOut);
|
|
81
|
+
fadeIn = Math.floor(fadeIn * scale * 100) / 100;
|
|
82
|
+
fadeOut = Math.floor(fadeOut * scale * 100) / 100;
|
|
83
|
+
}
|
|
84
|
+
fadeIn = Math.min(fadeIn, segment);
|
|
85
|
+
fadeOut = Math.min(fadeOut, segment);
|
|
86
|
+
const format = MUSIC_EDIT_FORMATS.includes(String(raw.format))
|
|
87
|
+
? String(raw.format)
|
|
88
|
+
: "mp3";
|
|
89
|
+
const bitrateRaw = Math.round(clamp(raw.bitrate, 192, 32, 512));
|
|
90
|
+
const bitrate = MUSIC_EDIT_BITRATES.includes(bitrateRaw) ? bitrateRaw : 192;
|
|
91
|
+
const channels = raw.channels === 1 || raw.channels === 2 ? raw.channels : undefined;
|
|
92
|
+
const sampleRateRaw = raw.sampleRate === undefined ? undefined : Math.round(clamp(raw.sampleRate, 44100, 8000, 96000));
|
|
93
|
+
const sampleRate = sampleRateRaw === undefined ? undefined : [22050, 44100, 48000, 96000].includes(sampleRateRaw) ? sampleRateRaw : 44100;
|
|
94
|
+
return {
|
|
95
|
+
start: round2(start),
|
|
96
|
+
end: round2(end),
|
|
97
|
+
gainDb: round2(clamp(raw.gainDb, 0, -24, 24)),
|
|
98
|
+
fadeIn: round2(fadeIn),
|
|
99
|
+
fadeOut: round2(fadeOut),
|
|
100
|
+
normalize: raw.normalize === true,
|
|
101
|
+
lufs: round2(clamp(raw.lufs, -14, -40, -5)),
|
|
102
|
+
truePeak: round2(clamp(raw.truePeak, -1, -9, 0)),
|
|
103
|
+
format,
|
|
104
|
+
bitrate,
|
|
105
|
+
channels,
|
|
106
|
+
sampleRate
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function formatFilter(ops) {
|
|
110
|
+
const chain = [];
|
|
111
|
+
if (ops.start > 0 || ops.end < Number.MAX_SAFE_INTEGER) {
|
|
112
|
+
chain.push(`atrim=start=${ops.start.toFixed(3)}:end=${ops.end.toFixed(3)}`);
|
|
113
|
+
chain.push("asetpts=PTS-STARTPTS");
|
|
114
|
+
}
|
|
115
|
+
if (ops.gainDb !== 0)
|
|
116
|
+
chain.push(`volume=${ops.gainDb.toFixed(2)}dB`);
|
|
117
|
+
if (ops.fadeIn > 0)
|
|
118
|
+
chain.push(`afade=t=in:st=0:d=${ops.fadeIn.toFixed(3)}`);
|
|
119
|
+
if (ops.fadeOut > 0) {
|
|
120
|
+
const segment = ops.end - ops.start;
|
|
121
|
+
const at = Math.max(0, segment - ops.fadeOut);
|
|
122
|
+
chain.push(`afade=t=out:st=${at.toFixed(3)}:d=${ops.fadeOut.toFixed(3)}`);
|
|
123
|
+
}
|
|
124
|
+
if (ops.normalize)
|
|
125
|
+
chain.push(`loudnorm=I=${ops.lufs.toFixed(1)}:TP=${ops.truePeak.toFixed(1)}:LRA=11`);
|
|
126
|
+
return chain.join(",");
|
|
127
|
+
}
|
|
128
|
+
function encoderArgs(ops) {
|
|
129
|
+
const args = [];
|
|
130
|
+
if (ops.format === "mp3")
|
|
131
|
+
args.push("-c:a", "libmp3lame", "-b:a", `${ops.bitrate}k`);
|
|
132
|
+
else if (ops.format === "wav")
|
|
133
|
+
args.push("-c:a", "pcm_s16le");
|
|
134
|
+
else
|
|
135
|
+
args.push("-c:a", "aac", "-b:a", `${ops.bitrate}k`, "-movflags", "+faststart");
|
|
136
|
+
if (ops.sampleRate !== undefined)
|
|
137
|
+
args.push("-ar", String(ops.sampleRate));
|
|
138
|
+
if (ops.channels !== undefined)
|
|
139
|
+
args.push("-ac", String(ops.channels));
|
|
140
|
+
return args;
|
|
141
|
+
}
|
|
142
|
+
/** 编辑结果的目标路径(调用方给目录与主名,扩展名由 format 决定)。 */
|
|
143
|
+
export function editTargetPath(dir, stem, format) {
|
|
144
|
+
return `${dir}/${stem}.${format}`;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 渲染一次编辑。
|
|
148
|
+
*
|
|
149
|
+
* `force` 语义由上层(musicgen)处理;这里只负责「源 → 目标」。
|
|
150
|
+
* 没开 normalize 时不会碰 loudnorm;开了但 ffmpeg 失败,会**去掉 normalize 重试一次**,
|
|
151
|
+
* 并在 warnings 里说明——不让一条响度滤镜把整次编辑废掉。
|
|
152
|
+
*/
|
|
153
|
+
export async function renderAudioEdit(input) {
|
|
154
|
+
const probe = await probeAudio(input.source);
|
|
155
|
+
const ops = normalizeEditOps(input.ops, probe.duration);
|
|
156
|
+
const warnings = [];
|
|
157
|
+
await mkdir(dirname(input.dest), { recursive: true });
|
|
158
|
+
const sourceExt = extname(input.source).toLowerCase().replace(/^\./u, "");
|
|
159
|
+
const noOp = ops.start <= 0 &&
|
|
160
|
+
Math.abs(ops.end - probe.duration) < 0.01 &&
|
|
161
|
+
ops.gainDb === 0 &&
|
|
162
|
+
ops.fadeIn === 0 &&
|
|
163
|
+
ops.fadeOut === 0 &&
|
|
164
|
+
!ops.normalize &&
|
|
165
|
+
ops.channels === undefined &&
|
|
166
|
+
ops.sampleRate === undefined;
|
|
167
|
+
if (noOp && sourceExt === ops.format) {
|
|
168
|
+
await copyFile(input.source, input.dest);
|
|
169
|
+
const size = (await stat(input.dest)).size;
|
|
170
|
+
return { file: input.dest, duration: ops.end - ops.start, size, format: ops.format, ops, warnings };
|
|
171
|
+
}
|
|
172
|
+
const baseArgs = [
|
|
173
|
+
"-hide_banner",
|
|
174
|
+
"-loglevel", "error",
|
|
175
|
+
"-nostdin",
|
|
176
|
+
"-y",
|
|
177
|
+
"-i", input.source,
|
|
178
|
+
"-map", "0:a:0",
|
|
179
|
+
"-vn", "-sn", "-dn",
|
|
180
|
+
"-map_metadata", "-1"
|
|
181
|
+
];
|
|
182
|
+
const filter = formatFilter(ops);
|
|
183
|
+
const runOnce = async (withNormalize) => {
|
|
184
|
+
const effective = withNormalize ? ops : { ...ops, normalize: false };
|
|
185
|
+
const chain = formatFilter(effective);
|
|
186
|
+
const args = [...baseArgs];
|
|
187
|
+
if (chain !== "")
|
|
188
|
+
args.push("-af", chain);
|
|
189
|
+
args.push(...encoderArgs(ops), input.dest);
|
|
190
|
+
await runFfmpeg(args, input.timeoutMs ?? 600000);
|
|
191
|
+
};
|
|
192
|
+
try {
|
|
193
|
+
await runOnce(ops.normalize);
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
if (!ops.normalize) {
|
|
197
|
+
throw error instanceof MediaError
|
|
198
|
+
? error
|
|
199
|
+
: new MediaError(`音频渲染失败:${error instanceof Error ? error.message : String(error)}`);
|
|
200
|
+
}
|
|
201
|
+
warnings.push(`响度标准化失败,已改为不做标准化重试一次:${error instanceof Error ? error.message : String(error)}`);
|
|
202
|
+
await runOnce(false);
|
|
203
|
+
}
|
|
204
|
+
let rendered;
|
|
205
|
+
try {
|
|
206
|
+
rendered = await probeAudio(input.dest);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
throw new MediaError(`渲染完成但读不出输出音频:${error instanceof Error ? error.message : String(error)}`);
|
|
210
|
+
}
|
|
211
|
+
const size = (await stat(input.dest)).size;
|
|
212
|
+
return {
|
|
213
|
+
file: input.dest,
|
|
214
|
+
duration: rendered.duration > 0 ? rendered.duration : ops.end - ops.start,
|
|
215
|
+
size,
|
|
216
|
+
format: ops.format,
|
|
217
|
+
ops,
|
|
218
|
+
warnings
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/** 波形峰值:解码成 4kHz 单声道 s16,再按桶取绝对值最大。 */
|
|
222
|
+
export async function audioPeaks(file, buckets = 800) {
|
|
223
|
+
const count = Math.round(clamp(buckets, 800, 64, 2000));
|
|
224
|
+
const probe = await probeAudio(file);
|
|
225
|
+
const { stdout } = await runFfmpeg(["-hide_banner", "-loglevel", "error", "-nostdin", "-i", file, "-ac", "1", "-ar", "4000", "-f", "s16le", "-"], 180000);
|
|
226
|
+
const samples = Math.floor(stdout.length / 2);
|
|
227
|
+
if (samples <= 0)
|
|
228
|
+
throw new MediaError(`无法解码波形:${file}`);
|
|
229
|
+
const peaks = new Array(count).fill(0);
|
|
230
|
+
for (let i = 0; i < samples; i++) {
|
|
231
|
+
const value = Math.abs(stdout.readInt16LE(i * 2)) / 32768;
|
|
232
|
+
const bucket = Math.min(count - 1, Math.floor((i / samples) * count));
|
|
233
|
+
if (value > peaks[bucket])
|
|
234
|
+
peaks[bucket] = value;
|
|
235
|
+
}
|
|
236
|
+
// 归一化到 0..1(有些音频峰值只到 0.3,不归一化会看起来像一条平线)。
|
|
237
|
+
const max = Math.max(...peaks);
|
|
238
|
+
const normalized = max > 0 ? peaks.map((value) => Math.round((value / max) * 1000) / 1000) : peaks;
|
|
239
|
+
return { peaks: normalized, buckets: count, duration: probe.duration };
|
|
240
|
+
}
|
|
241
|
+
/** 把秒数格式化成 m:ss.xx(界面与日志共用)。 */
|
|
242
|
+
export function formatSeconds(seconds) {
|
|
243
|
+
const value = Math.max(0, Number(seconds) || 0);
|
|
244
|
+
const minutes = Math.floor(value / 60);
|
|
245
|
+
const rest = value - minutes * 60;
|
|
246
|
+
const whole = Math.floor(rest);
|
|
247
|
+
const hundredths = Math.round((rest - whole) * 100);
|
|
248
|
+
return `${minutes}:${String(whole).padStart(2, "0")}.${String(Math.min(99, hundredths)).padStart(2, "0")}`;
|
|
249
|
+
}
|