纸月三音铃
2.5秒原创音效,可试听、可下载 WAV。三音铃声,适合窗口亮起、放下小灯或新信息出现的落点。
使用
- 在本页首屏试听,按需要下载 WAV 放入自己的剪辑工程。
- 要调整音符时,复制下方参数与源码,在本地重建。
- 小音量回听,检查起止、接缝与叠加后的音量。
重建
将专属参数保存为 paper-moon-bell.json,完整源码保存为 synthesis.mjs。在 Node.js 本地环境执行:
node synthesis.mjs paper-moon-bell.json paper-moon-bell.wav得到 paper-moon-bell.wav,原始规格为24000 Hz、16 bit、单声道。三个音依次进入,按画面动作对齐起音。
调整时先修改本曲参数,再试听重建结果;叠加后出现削波时调低增益。源码使用 Node.js 内置模块和数学振荡器合成。
参考:TokenX《纸月三音铃》;Node.js 文件系统与 Buffer 文档。
完整交付源:paper-moon-bell.json
JSON25 行
{
"title": "纸月三音铃",
"durationSeconds": 2.5,
"voice": "bell",
"events": [
{
"start": 0,
"duration": 1.8,
"midi": 76,
"gain": 0.25
},
{
"start": 0.28,
"duration": 1.9,
"midi": 79,
"gain": 0.21
},
{
"start": 0.64,
"duration": 1.86,
"midi": 83,
"gain": 0.17
}
]
}完整交付源:synthesis.mjs
JavaScript45 行
/** TokenX original tonal material, from the supplied score; no samples or network requests. */
import { readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
export function synthesize(score) {
const rate = 24000;
if (!Number.isFinite(score.durationSeconds) || score.durationSeconds < 1 || score.durationSeconds > 30) throw new Error("时长必须为1–30秒");
if (!Array.isArray(score.events) || !score.events.length || score.events.length > 100) throw new Error("必须提供1–100个原创音符事件");
const frames = Math.round(score.durationSeconds * rate);
const pcm = new Float64Array(frames);
for (const event of score.events) {
const { start, duration, midi, gain } = event;
if (![start, duration, midi, gain].every(Number.isFinite) || start < 0 || duration <= 0 || start + duration > score.durationSeconds + 0.00001 || midi < 36 || midi > 84 || gain < 0 || gain > 0.35) throw new Error("音符参数超出此制作源的范围");
const hz = 440 * 2 ** ((midi - 69) / 12);
const first = Math.round(start * rate);
const length = Math.round(duration * rate);
const attack = Math.min(Math.round(0.012 * rate), length / 4);
const release = Math.min(Math.round(0.10 * rate), length / 3);
for (let i = 0; i < length && first + i < frames; i++) {
const t = i / rate;
const edge = Math.min(1, i / attack, (length - 1 - i) / release);
const decay = score.voice === "bell" ? (1 - i / length) ** 2 : 1;
const fundamental = Math.sin(2 * Math.PI * hz * t);
const second = 0.18 * Math.sin(2 * Math.PI * hz * 2 * t);
const third = 0.07 * Math.sin(2 * Math.PI * hz * 3 * t);
pcm[first + i] += gain * edge * decay * (fundamental + second + third) / 1.25;
}
}
const wav = Buffer.alloc(44 + frames * 2);
wav.write("RIFF", 0); wav.writeUInt32LE(wav.length - 8, 4); wav.write("WAVEfmt ", 8);
wav.writeUInt32LE(16, 16); wav.writeUInt16LE(1, 20); wav.writeUInt16LE(1, 22);
wav.writeUInt32LE(rate, 24); wav.writeUInt32LE(rate * 2, 28); wav.writeUInt16LE(2, 32); wav.writeUInt16LE(16, 34);
wav.write("data", 36); wav.writeUInt32LE(frames * 2, 40);
for (let i = 0; i < frames; i++) {
if (Math.abs(pcm[i]) >= 0.98) throw new Error("合成叠加过大,请降低事件gain");
wav.writeInt16LE(Math.round(pcm[i] * 32767), 44 + i * 2);
}
return wav;
}
if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
if (process.argv.length !== 4) throw new Error("用法:node synthesis.mjs score.json output.wav");
writeFileSync(process.argv[3], synthesize(JSON.parse(readFileSync(process.argv[2], "utf8"))));
}