Midi Clef Karaoke Player -
midiToStaffY(midiNote) // Middle C (MIDI 60) position depends on clef const staffTop = 50; const lineSpacing = 25; let linesFromC; if (this.clef === 'treble') // In treble clef, middle C is below the staff (1 ledger line) // E4 (MIDI 64) is bottom line linesFromC = midiNote - 60; // each step = half line const y = staffTop + (4 * lineSpacing) - (linesFromC * lineSpacing / 2); return y; else // Bass clef: middle C is above staff // C3 (MIDI 48) is top line? Let's adjust const bassRef = 48; // C3 linesFromC = midiNote - bassRef; const y = staffTop + (1 * lineSpacing) - (linesFromC * lineSpacing / 2); return y;
async loadMIDI(event) const file = event.target.files[0]; if (!file) return; midi clef karaoke player
canvas display: block; margin: 0 auto; background: #fff9e8; border-radius: 10px; cursor: pointer; const lineSpacing = 25
.controls display: flex; gap: 15px; margin-bottom: 20px; flex-wrap: wrap; justify-content: center; async loadMIDI(event) const file = event.target.files[0]
drawStaff() this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); const staffTop = 50; const staffBottom = 250; const lineSpacing = 25; // Draw 5 staff lines this.ctx.beginPath(); this.ctx.strokeStyle = '#333'; this.ctx.lineWidth = 1.5; for (let i = 0; i < 5; i++) const y = staffTop + (i * lineSpacing); this.ctx.moveTo(50, y); this.ctx.lineTo(this.canvas.width - 50, y); this.ctx.stroke(); // Draw clef this.ctx.font = 'bold 60px "Segoe UI", "Arial"'; this.ctx.fillStyle = '#e94560'; if (this.clef === 'treble') this.ctx.fillText('𝄞', 20, staffTop + 100); else this.ctx.fillText('𝄢', 20, staffTop + 100); // Draw notes based on current scroll offset const currentTime = this.isPlaying ? (performance.now() - this.startTime) / 1000 : this.currentPauseTime; const visibleTimeRange = 4; // seconds visible const pixelsPerSecond = (this.canvas.width - 100) / visibleTimeRange; this.notes.forEach(note => const noteTime = note.startTime; const x = 80 + (noteTime - currentTime) * pixelsPerSecond; if (x > 30 && x < this.canvas.width - 30) const midiPitch = note.pitch; const staffPosition = this.midiToStaffY(midiPitch); if (staffPosition !== null) this.drawNote(x, staffPosition, note.duration * pixelsPerSecond); ); // Draw current time marker this.ctx.beginPath(); this.ctx.strokeStyle = '#ff0000'; this.ctx.lineWidth = 2; this.ctx.moveTo(80, 20); this.ctx.lineTo(80, this.canvas.height - 20); this.ctx.stroke(); if (this.isPlaying) this.animationId = requestAnimationFrame(() => this.drawStaff());
