Files
midi-player/js/demo.js
T

223 lines
7.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* demo.js —— 内置示例曲目生成器
* ------------------------------------------------------------------
* 不依赖任何外部音频文件:直接在内存里"手写"一份标准 MIDI 文件字节流,
* 再交给 midi-parser 解析播放。播放器因此开箱即用,同时也顺带验证了
* 解析器对真实 SMF 结构(多轨 + 变速 + 打击乐通道)的处理能力。
*
* 示例曲目:贝多芬《欢乐颂》主题(公有领域),8 小节
* Track 0 : Tempo Map(120 BPM,第 5 小节起 132 BPM)
* Track 1 : 旋律(通道 0,大钢琴)
* Track 2 : 和弦铺底(通道 2,电钢琴)
* Track 3 : 贝斯(通道 1,指弹电贝斯)
* Track 4 : 鼓组(通道 9)
*/
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.DemoSong = factory();
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';
var TPQ = 480; // 每四分音符的 tick 数
var EIGHTH = TPQ / 2; // 一个八分音符 = 240 tick
var BAR = TPQ * 4; // 4/4 拍下一小节 = 1920 tick
/* ------------------------- 基础编码工具 ------------------------- */
/* 变长数量编码 */
function vlq(n) {
var out = [n & 0x7f];
n >>= 7;
while (n > 0) { out.unshift((n & 0x7f) | 0x80); n >>= 7; }
return out;
}
/* ASCII(用于块标识符) */
function ascii(s) {
var a = [];
for (var i = 0; i < s.length; i++) a.push(s.charCodeAt(i) & 0xff);
return a;
}
/* 完整 UTF-8 编码(元事件里的文本,如曲名、轨道名) */
function utf8(s) {
var out = [];
for (var i = 0; i < s.length; i++) {
var c = s.charCodeAt(i);
if (c < 0x80) {
out.push(c);
} else if (c < 0x800) {
out.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f));
} else if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {
var c2 = s.charCodeAt(++i);
var cp = 0x10000 + ((c - 0xd800) << 10) + (c2 - 0xdc00);
out.push(
0xf0 | (cp >> 18),
0x80 | ((cp >> 12) & 0x3f),
0x80 | ((cp >> 6) & 0x3f),
0x80 | (cp & 0x3f)
);
} else {
out.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f));
}
}
return out;
}
function u32(v) { return [(v >>> 24) & 0xff, (v >>> 16) & 0xff, (v >>> 8) & 0xff, v & 0xff]; }
function u16(v) { return [(v >>> 8) & 0xff, v & 0xff]; }
function u24(v) { return [(v >> 16) & 0xff, (v >> 8) & 0xff, v & 0xff]; }
function Track(name) {
this.name = name;
this.events = [];
this._seq = 0;
}
Track.prototype.push = function (tick, bytes) {
this.events.push({ tick: Math.max(0, Math.round(tick)), bytes: bytes, seq: this._seq++ });
return this;
};
Track.prototype.meta = function (tick, type, data) {
return this.push(tick, [0xff, type].concat(vlq(data.length), data));
};
Track.prototype.text = function (tick, type, str) {
return this.meta(tick, type, utf8(str));
};
/* 写一个带时长的音符(自动生成 note-on / note-off 两条事件) */
Track.prototype.note = function (tick, duration, key, velocity, channel) {
this.push(tick, [0x90 | channel, key & 0x7f, velocity & 0x7f]);
this.push(tick + duration, [0x80 | channel, key & 0x7f, 0]);
return this;
};
Track.prototype.program = function (tick, channel, program) {
return this.push(tick, [0xc0 | channel, program & 0x7f]);
};
Track.prototype.tempo = function (tick, usPerQuarter) {
return this.meta(tick, 0x51, u24(usPerQuarter));
};
Track.prototype.toBytes = function () {
var evts = this.events.slice().sort(function (a, b) {
return (a.tick - b.tick) || (a.seq - b.seq);
});
var out = [], prev = 0;
for (var i = 0; i < evts.length; i++) {
var e = evts[i];
out = out.concat(vlq(e.tick - prev), e.bytes);
prev = e.tick;
}
out = out.concat(vlq(0), [0xff, 0x2f, 0x00]); // End of Track
return out;
};
/* ------------------------------ 编曲 ------------------------------ */
/* 旋律:[起始位置(八分音符), 音高, 时值(八分音符)] */
var MELODY = [
[0, 64, 2], [2, 64, 2], [4, 65, 2], [6, 67, 2],
[8, 67, 2], [10, 65, 2], [12, 64, 2], [14, 62, 2],
[16, 60, 2], [18, 60, 2], [20, 62, 2], [22, 64, 2],
[24, 64, 3], [27, 62, 1], [28, 62, 4],
[32, 64, 2], [34, 64, 2], [36, 65, 2], [38, 67, 2],
[40, 67, 2], [42, 65, 2], [44, 64, 2], [46, 62, 2],
[48, 60, 2], [50, 60, 2], [52, 62, 2], [54, 64, 2],
[56, 62, 3], [59, 60, 1], [60, 60, 4]
];
/* 和弦:每小节一个,C - G - C - G ... */
var CHORDS = [
[48, 52, 55], [43, 47, 50], [48, 52, 55], [43, 47, 50],
[48, 52, 55], [43, 47, 50], [48, 52, 55], [43, 47, 50]
];
var BASS_ROOTS = [36, 31, 36, 31, 36, 31, 36, 31]; // C2 / G1
function createDemoArrayBuffer() {
/* ---- Track 0:速度与元信息 ---- */
var t0 = new Track('Tempo Map');
t0.text(0, 0x03, '示例曲目 · 欢乐颂主题');
t0.tempo(0, 500000); // 120 BPM
t0.meta(0, 0x58, [4, 2, 24, 8]); // 4/4 拍
t0.meta(0, 0x59, [0, 0]); // C 大调
t0.tempo(BAR * 4, 454545); // ≈132 BPM
t0.tempo(BAR * 8, 500000); // 回到 120 BPM
/* ---- Track 1:旋律 ---- */
var t1 = new Track('旋律 Melody');
t1.text(0, 0x03, '旋律 Melody');
t1.program(0, 0, 0); // Acoustic Grand Piano
MELODY.forEach(function (n) {
var vel = n[1] >= 64 ? 104 : 92;
t1.note(n[0] * EIGHTH, n[2] * EIGHTH - 20, n[1], vel, 0);
});
/* ---- Track 2:和弦铺底 ---- */
var t2 = new Track('和弦 Chords');
t2.text(0, 0x03, '和弦 Chords');
t2.program(0, 2, 4); // Electric Piano 1
CHORDS.forEach(function (chord, bar) {
chord.forEach(function (key) {
t2.note(bar * BAR + 10, BAR - 40, key, 58, 2);
});
});
[48, 52, 55, 60].forEach(function (key) { // 结尾长和弦
t2.note(BAR * 8, BAR * 2, key, 62, 2);
});
/* ---- Track 3:贝斯 ---- */
var t3 = new Track('贝斯 Bass');
t3.text(0, 0x03, '贝斯 Bass');
t3.program(0, 1, 33); // Electric Bass (finger)
BASS_ROOTS.forEach(function (root, bar) {
var base = bar * BAR;
t3.note(base, TPQ * 2 - 30, root, 88, 1); // 根音
t3.note(base + TPQ * 2, TPQ * 2 - 30, root + 7, 80, 1); // 五度
});
t3.note(BAR * 8, BAR * 2 - 40, 36, 92, 1);
/* ---- Track 4:鼓组(通道 9) ---- */
var t4 = new Track('鼓组 Drums');
t4.text(0, 0x03, '鼓组 Drums');
for (var bar = 0; bar < 8; bar++) {
var b = bar * BAR;
t4.note(b, 60, 36, 108, 9); // 底鼓
t4.note(b + TPQ * 2, 60, 36, 96, 9);
t4.note(b + TPQ, 60, 38, 94, 9); // 军鼓
t4.note(b + TPQ * 3, 60, 38, 94, 9);
for (var i = 0; i < 8; i++) { // 八分音符踩镲
t4.note(b + i * EIGHTH, 40, 42, i % 2 ? 52 : 68, 9);
}
if (bar === 0 || bar === 4) t4.note(b, 200, 49, 100, 9); // 吊镲
}
t4.note(BAR * 8, 400, 49, 104, 9);
t4.note(BAR * 8, 60, 36, 110, 9);
/* ---- 组装完整的 SMF 文件 ---- */
var tracks = [t0, t1, t2, t3, t4];
var body = [];
tracks.forEach(function (t) {
var data = t.toBytes();
body = body.concat(ascii('MTrk'), u32(data.length), data);
});
var header = ascii('MThd').concat(
u32(6),
u16(1), // format 1:多轨同步
u16(tracks.length),
u16(TPQ)
);
var all = header.concat(body);
var buf = new ArrayBuffer(all.length);
var out = new Uint8Array(buf);
for (var k = 0; k < all.length; k++) out[k] = all[k];
return buf;
}
return {
createDemoArrayBuffer: createDemoArrayBuffer,
TPQ: TPQ,
utf8: utf8
};
});