Midi Clef Karaoke Player Apr 2026

this.initEventListeners(); this.initAudio();

parseMIDIData() this.notes = []; this.lyrics = []; this.midiData.tracks.forEach((track, trackIndex) => let currentTime = 0; let currentLyric = ''; track.forEach(event => (event.type === 'noteOn' && event.velocity === 0)) const matchingNote = [...this.notes].reverse().find(n => n.pitch === event.noteNumber && n.duration === 0); if (matchingNote) matchingNote.duration = currentTime - matchingNote.startTime; // Parse lyrics (text events) if (event.type === 'text' && event.text) currentLyric = event.text; this.lyrics.push( text: currentLyric, time: currentTime ); ); ); // Sort notes by start time this.notes.sort((a, b) => a.startTime - b.startTime); this.lyrics.sort((a, b) => a.time - b.time); console.log(`Loaded $this.notes.length notes, $this.lyrics.length lyrics`);

.clef-indicator font-size: 18px; font-weight: bold; margin-bottom: 10px; text-align: center; color: #ffd700; </style> </head> <body> <div class="container"> <div class="player"> <div class="controls"> <input type="file" id="midiFileInput" accept=".mid,.midi"> <button id="playBtn">β–Ά Play</button> <button id="pauseBtn">⏸ Pause</button> <button id="stopBtn">⏹ Stop</button> </div> <div class="clef-indicator" id="clefIndicator">Clef: --</div> <div class="staff-container"> <canvas id="staffCanvas" width="900" height="300"></canvas> </div> <div class="lyrics" id="lyricsDisplay">🎡 Load a MIDI file to start 🎡</div> <div class="info"> 🎹 Supports karaoke MIDI files with lyrics | Clef auto-detected | Scrolling notes </div> </div> </div>

.controls display: flex; gap: 15px; margin-bottom: 20px; flex-wrap: wrap; justify-content: center; midi clef karaoke player

input[type="file"] background: #533483; color: white; padding: 12px 24px; border-radius: 50px; cursor: pointer; border: none; font-size: 16px;

async initAudio() window.webkitAudioContext)(); await MIDI.loadPlugin( soundfontUrl: "https://cdn.jsdelivr.net/npm/midijs-soundfonts@1.0.0/FluidR3_GM/", instrument: "acoustic_grand_piano", onprogress: (state, progress) => console.log('Loading soundfont:', progress), onsuccess: () => console.log('Soundfont loaded') );

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; 'π„ž Treble' : '𝄒 Bass'`;

.container background: rgba(255,255,255,0.1); backdrop-filter: blur(10px); border-radius: 30px; padding: 25px; box-shadow: 0 20px 40px rgba(0,0,0,0.3); border: 1px solid rgba(255,255,255,0.2);

button:active transform: translateY(0);

detectClef() if (!this.notes.length) return; // Calculate average pitch const avgPitch = this.notes.reduce((sum, n) => sum + n.pitch, 0) / this.notes.length; // MIDI note 60 = middle C // Treble clef typically for notes > 60, Bass clef for notes < 60 if (avgPitch > 62) this.clef = 'treble'; else if (avgPitch < 58) this.clef = 'bass'; else // Mixed - check range const highNotes = this.notes.filter(n => n.pitch > 64).length; const lowNotes = this.notes.filter(n => n.pitch < 56).length; this.clef = highNotes > lowNotes ? 'treble' : 'bass'; document.getElementById('clefIndicator').innerHTML = `Clef: $this.clef === 'treble' ? 'π„ž Treble' : '𝄒 Bass'`; 'π„ž Treble' : '𝄒 Bass'`

.lyrics background: rgba(0,0,0,0.7); color: #ffd700; padding: 15px; border-radius: 15px; text-align: center; font-size: 20px; font-weight: bold; margin-top: 15px; font-family: monospace;

play() !this.notes.length) return; if (this.audioContext.state === 'suspended') this.audioContext.resume(); if (!this.isPlaying) this.startTime = performance.now() - (this.currentPauseTime * 1000); this.isPlaying = true; this.playMIDINotes(); this.drawStaff(); // Update lyrics in real-time const lyricInterval = setInterval(() => if (!this.isPlaying) clearInterval(lyricInterval); this.updateLyrics(); , 100);

updateLyrics() const currentTime = this.isPlaying ? (performance.now() - this.startTime) / 1000 : this.currentPauseTime; const currentLyric = this.lyrics.filter(l => l.time <= currentTime).pop(); if (currentLyric) document.getElementById('lyricsDisplay').innerHTML = `🎀 $currentLyric.text 🎀`;

button:hover background: #ff6b6b; transform: translateY(-2px); box-shadow: 0 6px 12px rgba(0,0,0,0.3);

<script src="https://cdn.jsdelivr.net/npm/midijs@0.2.1/build/MIDI.js"></script> <script src="https://cdn.socket.io/4.5.0/socket.io.min.js"></script> <script> // Main implementation class MIDIClefKaraokePlayer constructor() this.midiData = null; this.audioContext = null; this.isPlaying = false; this.startTime = 0; this.currentPauseTime = 0; this.notes = []; this.lyrics = []; this.clef = 'treble'; // treble or bass this.canvas = document.getElementById('staffCanvas'); this.ctx = this.canvas.getContext('2d'); this.animationId = null; this.scrollOffset = 0;