Files

260 lines
8.5 KiB
JavaScript

/*
* visualizer.js —— 钢琴卷帘(Piano Roll)可视化
* ------------------------------------------------------------------
* 用 Canvas 绘制一条向左滚动的钢琴卷帘:
* - 纵向是音高(自动适配曲目实际音域),横向是时间
* - 播放头固定在左侧 28% 处,音符从右向左滑过
* - 左侧画出钢琴键盘,正在发声的音高会高亮
* - 音符颜色按轨道区分,透明度反映力度
*/
(function (root, factory) {
if (typeof module === 'object' && module.exports) module.exports = factory();
else root.MidiVisual = factory();
})(typeof self !== 'undefined' ? self : this, function () {
'use strict';
var PALETTE = [
'#5ad2f4', '#7be495', '#ffd166', '#ff8fa3',
'#c792ea', '#4fd1c5', '#f4978e', '#9bb1ff',
'#e6c384', '#8ee6e6', '#ffb86c', '#b0b8ff'
];
var BLACK = [false, true, false, true, false, false, true, false, true, false, true, false];
function PianoRoll(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.song = null;
this.seconds = 6; // 可视时间窗长度(秒)
this.minNote = 48;
this.maxNote = 84;
this.keysWidth = 44; // 左侧键盘宽度
this.playing = false;
this.resize();
}
PianoRoll.prototype.resize = function () {
var dpr = window.devicePixelRatio || 1;
var rect = this.canvas.getBoundingClientRect();
var w = Math.max(300, Math.round(rect.width || 300));
var h = Math.max(140, Math.round(rect.height || 320));
this.w = w;
this.h = h;
this.canvas.width = Math.round(w * dpr);
this.canvas.height = Math.round(h * dpr);
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
this.ctx.textBaseline = 'middle';
};
PianoRoll.prototype.setSong = function (song) {
this.song = song;
if (song && song.notes && song.notes.length) {
var lo = 127, hi = 0;
for (var i = 0; i < song.notes.length; i++) {
var m = song.notes[i].midi;
if (m < lo) lo = m;
if (m > hi) hi = m;
}
this.minNote = Math.max(0, lo - 1);
this.maxNote = Math.min(127, hi + 1);
}
};
PianoRoll.prototype.render = function (time, audibleFn) {
var ctx = this.ctx;
var W = this.w, H = this.h;
ctx.clearRect(0, 0, W, H);
// 背景
ctx.fillStyle = '#0b0e13';
ctx.fillRect(0, 0, W, H);
var song = this.song;
if (!song || !song.notes.length) {
ctx.fillStyle = 'rgba(255,255,255,0.06)';
ctx.font = '13px system-ui, sans-serif';
ctx.textAlign = 'center';
ctx.fillText('暂无曲目 · 载入一个 .mid 文件试试', W / 2, H / 2);
ctx.textAlign = 'left';
return;
}
var kb = this.keysWidth;
var rulerH = 16;
var areaTop = rulerH + 4;
var areaH = H - areaTop - 4;
var rows = this.maxNote - this.minNote + 1;
var rowH = areaH / rows;
var x0 = kb + (W - kb) * 0.28; // 播放头位置
var pxPerSec = (W - kb) / this.seconds;
var past = 0.28 * this.seconds; // 播放头左侧可见的秒数
/* --- 音高行背景 --- */
for (var n = this.minNote; n <= this.maxNote; n++) {
var y = areaTop + (this.maxNote - n) * rowH;
if (BLACK[((n % 12) + 12) % 12]) {
ctx.fillStyle = 'rgba(255,255,255,0.032)';
ctx.fillRect(kb, y, W - kb, rowH);
}
if (n % 12 === 0) { // 每个 C 画一条分界
ctx.fillStyle = 'rgba(255,255,255,0.10)';
ctx.fillRect(kb, y, W - kb, 1);
}
}
/* --- 时间刻度 --- */
var tStart = time - past;
var tEnd = time + (this.seconds - past);
var stepSec = this.seconds > 10 ? 2 : 1;
ctx.font = '10px ui-monospace, monospace';
for (var s = Math.floor(tStart / stepSec) * stepSec; s <= tEnd; s += stepSec) {
var gx = x0 + (s - time) * pxPerSec;
if (gx < kb) continue;
ctx.fillStyle = 'rgba(255,255,255,0.07)';
ctx.fillRect(gx, areaTop, 1, areaH);
ctx.fillStyle = 'rgba(255,255,255,0.28)';
ctx.fillText(formatTime(Math.max(0, s)), gx + 3, rulerH / 2 + 1);
ctx.fillStyle = 'rgba(255,255,255,0.05)';
ctx.fillRect(gx, 0, 1, rulerH);
}
/* --- 音符 --- */
var active = null;
var notes = song.notes;
var minStart = time - past - 0.5;
for (var i = 0; i < notes.length; i++) {
var nn = notes[i];
if (nn.start + nn.duration < minStart) continue;
if (nn.start > tEnd + 0.5) break; // notes 已按起始时间排序
if (audibleFn && !audibleFn(nn.track)) continue;
var x = x0 + (nn.start - time) * pxPerSec;
var w = Math.max(2.5, nn.duration * pxPerSec);
var left = Math.max(x, kb);
var right = Math.min(x + w, W);
if (right <= left) continue;
var yy = areaTop + (this.maxNote - nn.midi) * rowH;
var hh = Math.max(2, rowH - 1);
var isActive = (time >= nn.start && time <= nn.start + nn.duration);
var col = PALETTE[nn.track % PALETTE.length];
var alpha = 0.42 + 0.55 * (nn.velocity / 127);
if (isActive) {
if (!active) active = {};
active[nn.midi] = true;
}
ctx.globalAlpha = isActive ? Math.min(1, alpha + 0.25) : alpha;
ctx.fillStyle = col;
roundRect(ctx, left, yy + 0.5, right - left, hh, Math.min(3, hh / 2));
ctx.fill();
if (isActive) {
ctx.globalAlpha = 0.9;
ctx.strokeStyle = 'rgba(255,255,255,0.85)';
ctx.lineWidth = 1;
roundRect(ctx, left + 0.5, yy + 1, right - left - 1, hh - 1, Math.min(3, hh / 2));
ctx.stroke();
}
ctx.globalAlpha = 1;
}
/* --- 左侧键盘 --- */
ctx.fillStyle = '#11151c';
ctx.fillRect(0, areaTop, kb, areaH);
for (var k = this.minNote; k <= this.maxNote; k++) {
var ky = areaTop + (this.maxNote - k) * rowH;
var black = BLACK[((k % 12) + 12) % 12];
ctx.fillStyle = black ? '#1b2028' : '#e8edf4';
ctx.fillRect(0, ky + 0.5, kb - 6, Math.max(1, rowH - 1));
if (active && active[k]) {
ctx.fillStyle = '#5ad2f4';
ctx.fillRect(0, ky + 0.5, kb - 6, Math.max(1, rowH - 1));
}
if (rowH > 9 && k % 12 === 0) {
ctx.fillStyle = 'rgba(0,0,0,0.55)';
ctx.font = '8px ui-monospace, monospace';
ctx.fillText('C' + (Math.floor(k / 12) - 1), 3, ky + rowH / 2);
}
}
ctx.fillStyle = 'rgba(255,255,255,0.12)';
ctx.fillRect(kb - 1, areaTop, 1, areaH);
/* --- 播放头 --- */
var grad = ctx.createLinearGradient(x0 - 26, 0, x0, 0);
grad.addColorStop(0, 'rgba(90,210,244,0)');
grad.addColorStop(1, 'rgba(90,210,244,0.16)');
ctx.fillStyle = grad;
ctx.fillRect(x0 - 26, areaTop, 26, areaH);
ctx.fillStyle = this.playing ? '#5ad2f4' : 'rgba(90,210,244,0.45)';
ctx.fillRect(x0, 0, 2, H);
ctx.fillStyle = this.playing ? '#5ad2f4' : 'rgba(90,210,244,0.5)';
ctx.beginPath();
ctx.moveTo(x0 - 5, 0);
ctx.lineTo(x0 + 7, 0);
ctx.lineTo(x0 + 1, 7);
ctx.closePath();
ctx.fill();
};
function roundRect(ctx, x, y, w, h, r) {
if (w <= 0 || h <= 0) return;
var rr = Math.min(r, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + rr, y);
ctx.lineTo(x + w - rr, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + rr);
ctx.lineTo(x + w, y + h - rr);
ctx.quadraticCurveTo(x + w, y + h, x + w - rr, y + h);
ctx.lineTo(x + rr, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - rr);
ctx.lineTo(x, y + rr);
ctx.quadraticCurveTo(x, y, x + rr, y);
ctx.closePath();
}
function formatTime(sec) {
var s = Math.max(0, sec);
var m = Math.floor(s / 60);
var r = s - m * 60;
return m + ':' + (r < 10 ? '0' : '') + r.toFixed(0);
}
/* 简易电平表 */
function drawMeter(canvas, analyser) {
var ctx = canvas.getContext('2d');
var W = canvas.width, H = canvas.height;
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = 'rgba(255,255,255,0.06)';
ctx.fillRect(0, 0, W, H);
if (!analyser) return;
var data = new Uint8Array(analyser.fftSize);
analyser.getByteTimeDomainData(data);
var peak = 0;
for (var i = 0; i < data.length; i++) {
var v = Math.abs(data[i] - 128) / 128;
if (v > peak) peak = v;
}
var bars = 20;
var lit = Math.round(peak * bars);
var bw = W / bars;
for (var b = 0; b < bars; b++) {
var on = b < lit;
var hue = b < bars * 0.7 ? '90,210,244' : '255,143,163';
ctx.fillStyle = on ? 'rgba(' + hue + ',0.95)' : 'rgba(255,255,255,0.08)';
ctx.fillRect(b * bw + 1, 2, bw - 2, H - 4);
}
}
return {
PianoRoll: PianoRoll,
drawMeter: drawMeter,
PALETTE: PALETTE,
formatTime: formatTime
};
});