@media (max-width: 700px) .container padding: 20px; .action-bar justify-content: center; </style> </head> <body> <div class="container"> <h1>🎹 MIDI to Sheet Music</h1> <div class="sub">Upload any .mid file → instant piano roll & standard notation → save as PDF (under $30 stack)</div>
h1 font-size: 1.9rem; font-weight: 600; margin: 0 0 6px 0; background: linear-gradient(135deg, #1F6E8C, #2C3E50); background-clip: text; -webkit-background-clip: text; color: transparent;
// Parse MIDI file from ArrayBuffer async function parseMidiFromBuffer(buffer) try // MidiFile library expects Uint8Array or ArrayBuffer const midiFile = new MidiFile(buffer); // ensure parsing await midiFile.parse(); return midiFile; catch (err) console.error(err); throw new Error("Invalid MIDI structure or unsupported format");
.btn-primary background: #2c7da0; .btn-primary:hover background: #1f5e7a; transform: scale(0.97); midi to thirty dollar website
#notationCanvas background: #fffdf8; box-shadow: 0 2px 8px rgba(0,0,0,0.03); width: 100%; height: auto; border-radius: 16px;
// Event Listeners selectBtn.addEventListener('click', () => fileInput.click()); fileInput.addEventListener('change', (e) => if (e.target.files.length) loadMidiFile(e.target.files[0]); ); dropZone.addEventListener('dragover', (e) => e.preventDefault(); dropZone.style.borderColor = '#2c7da0'; ); dropZone.addEventListener('dragleave', () => dropZone.style.borderColor = '#bdd3e8'; ); dropZone.addEventListener('drop', (e) => files[0].name.endsWith('.midi'))) loadMidiFile(files[0]); else setStatus("Drop a .mid file", true); ); resetBtn.addEventListener('click', () => fileInput.value = ''; controlsSection.style.display = 'none'; setStatus("Ready — upload a MIDI file"); currentMidiData = null; parsedMidi = null; ); downloadBtn.addEventListener('click', exportAsPDF);
<div id="controlsSection" style="display: none;"> <div class="action-bar"> <button class="btn btn-secondary" id="downloadPdfBtn">📄 Export as PDF (score)</button> <button class="btn btn-secondary" id="resetBtn">⟳ Load another MIDI</button> </div> <div class="sheet-preview"> <div style="display: flex; justify-content: space-between; align-items: baseline; flex-wrap: wrap;"> <h2 style="font-weight: 500; margin: 0 0 12px 0;">🎼 Standard Notation (VexFlow)</h2> <span id="trackInfo" style="font-size:0.75rem; background:#eef2f8; padding:4px 12px; border-radius:20px;"></span> </div> <div id="vexflowContainer" style="overflow-x: auto; padding: 8px 0;"> <canvas id="notationCanvas" width="800" height="200" style="width:100%; height:auto; max-width:100%;"></canvas> </div> <div class="piano-roll"> <h3>🎹 Piano Roll Preview (first 2 tracks / 4 bars)</h3> <canvas id="pianoCanvas" width="900" height="280" style="width:100%; height:auto; background:#11181f; border-radius:12px;"></canvas> <div class="status" id="midiStatus">Ready — upload a MIDI file</div> </div> </div> </div> <footer> ⚡ 100% client-side • No server costs • Works offline • Ideal for $30 budget websites </footer> </div> @media (max-width: 700px)
// VexFlow rendering async function renderNotation(eventsData, ticksPerQuarter, canvasElem) if (!eventsData.events.length) const ctx = canvasElem.getContext('2d'); ctx.clearRect(0, 0, canvasElem.width, canvasElem.height); ctx.fillStyle = "#6c7a89"; ctx.font = "14px Inter"; ctx.fillText("No melodic content to render (empty track)", 20, 70); return; const VF = VexFlow; const width = canvasElem.width; const ctx = canvasElem.getContext('2d'); ctx.clearRect(0, 0, width, 200); // Create a stave with 4 measures const stave = new VF.Stave(10, 20, width - 40); stave.addClef("treble").addTimeSignature("4/4"); stave.setContext(ctx).draw(); // Build voice from events let vexNotes = []; for (let ev of eventsData.events.slice(0, 32)) // limit notes per line vexNotes.push(new VF.StaveNote( keys: ev.keys, duration: ev.duration )); if (vexNotes.length === 0) return; const voice = new VF.Voice( num_beats: 4, beat_value: 4 ); voice.addTickables(vexNotes); new VF.Formatter().joinVoices([voice]).formatToStave([voice], stave.getWidth() - 20); voice.draw(ctx, stave);
.upload-area background: #f8fafd; border: 2px dashed #bdd3e8; border-radius: 28px; padding: 36px 24px; text-align: center; cursor: pointer; transition: 0.2s; margin-bottom: 32px;
// PDF export using html2canvas: capture notation canvas + piano roll + status async function exportAsPDF() if (!parsedMidi) setStatus("No MIDI loaded", true); return; setStatus("Generating PDF preview..."); const elementToCapture = document.querySelector('.sheet-preview'); if (!elementToCapture) return; try const canvas = await html2canvas(elementToCapture, scale: 2, backgroundColor: '#ffffff' ); const imgData = canvas.toDataURL('image/png'); const link = document.createElement('a'); const timestamp = new Date().toISOString().slice(0,19).replace(/:/g, '-'); link.download = `midi_sheet_$timestamp.png`; link.href = imgData; link.click(); setStatus("PDF (PNG) saved! For real PDF, use 'Save as PDF' from browser print dialog. But high-res PNG ready."); // Alternative: open print dialog for true PDF (we give user note) setTimeout(() => if(confirm("Want to open print dialog to generate real PDF? (Recommended for vector quality)")) window.print(); , 200); catch(e) setStatus("Export failed: " + e.message, true); (Recommended for vector quality)")) window
.upload-area:hover border-color: #2c7da0; background: #f0f6fe;
// Build simplified events for VexFlow: quantized to quarter notes, limit bars function buildVexFlowNotation(notes, ticksPerQuarter, maxMeasures = 4) if (!notes.length) return []; const ticksPerMeasure = ticksPerQuarter * 4; // 4/4 time sig default const maxTickLimit = ticksPerMeasure * maxMeasures; // filter notes within first N bars const filtered = notes.filter(n => n.startTick < maxTickLimit); // group by startTick and convert to VexFlow stave notes // we'll create an array of objects: keys: [pitchName], duration: string, startTick const pitchToNoteName = (pitch) => const notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']; let octave = Math.floor(pitch / 12) - 1; let noteIndex = pitch % 12; return notes[noteIndex] + '/' + octave; ;