mIdi to bytecode generator
bytecodegenerator c1b46fc0 18.09.25 Thu 01:43:38
№
9217
1
photo_2025-09-18_00-42-48.jpg (565.32KB, 1014x919)
%3Ca%20href%3D%22%2Fbtb%2Fsrc%2F1758149018774-0.jpg%22%20onclick%3D%22return%20expandFile%28event%2C%2092170%29%3B%22%3E%3Cimg%20src%3D%22%2Fbtb%2Fsrc%2F1758149018774-0.jpg%22%20width%3D%221014%22%20style%3D%22max-width%3A%20100%25%3B%20height%3A%20auto%3B%22%3E%3C%2Fa%3E
so i was just interested can i turn midi into a bytecode, and the answer is yes but the final code is too big and idk how to optimize it
const fs = require('fs');
const { Midi } = require('@tonejs/midi');
const midiData = fs.readFileSync('song.mid');
const midi = new Midi(midiData);
const sampleRate = 44100;
let notesByteCode = [];
function midiToFrequency(n) {
return 440 * Math.pow(2, (n - 69) / 12);
}
function getADSRCode(startT, endT) {
const attack = 50;
const release = 50;
return `(t<${startT}+${attack}?((t-${startT})/${attack}):t>${endT}-${release}?(${endT}-t)/${release}:1)`;
}
function getByteCode(note) {
const startT = Math.round(note.time * sampleRate);
const endT = Math.round((note.time + note.duration) * sampleRate);
const freq = midiToFrequency(note.midi);
const velocity = note.velocity || 1;
const adsr = getADSRCode(startT, endT);
return `(t>=${startT}&&t<${endT}?(${velocity}*${adsr}*Math.sin(2*Math.PI*${freq}*t/${sampleRate})*127 + 128):0)`;
}
midi.tracks.forEach(track => {
track.notes.forEach(note => {
notesByteCode.push(getByteCode(note));
});
});
const result = `Math.max(${notesByteCode.join(',')})`;
fs.writeFileSync('result.txt', result); im curios if someone can fix it for the result being more compact but it can run mario! Here's the link:
[
Click ]
Message edited: 28.09.25 Sun 01:35:11