-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidiToBeeps.js
97 lines (81 loc) · 2.95 KB
/
midiToBeeps.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const fs = require('fs');
const Midi = require('midi-file');
const process = require('process');
// Mapping of MIDI note numbers to frequencies
const noteFrequencies = require('./noteFrequencies').noteFrequencies;
/**
* Calculates the duration in milliseconds for a given delta time and tempo.
* @param {number} deltaTime The delta time from the MIDI event.
* @param {number} tempo The tempo in BPM.
* @returns {number} The duration in milliseconds.
*/
function calculateDuration(deltaTime, tempo) {
const noteLengthInSixteenths = (deltaTime/24)*2;
return Math.round((noteLengthInSixteenths*tempo)*0.520833333);
}
/**
* Converts a MIDI file to a custom format.
* @param {string} midiFilePath Path to the MIDI file.
* @param {string} outputFilePath Path to the output file.
* @param {number} tempo The tempo in BPM.
*/
function convertMidiToCustomFormat(midiFilePath, outputFilePath, tempo) {
// Read MIDI file
const midiData = fs.readFileSync(midiFilePath);
const midi = Midi.parseMidi(midiData);
const notes = [];
// Convert MIDI events to custom format
const customFormatLines = midi.tracks.flatMap((events) => {
let totalDelta = 0;
events.forEach((event,i) => {
totalDelta += event.deltaTime;
if (!["noteOn","noteOff"].includes(event.type)) return;
const noteData = noteFrequencies[event.noteNumber] || {name:"UNKNOWN",frequency:0};
switch (event.type) {
case "noteOn":
const note = {
frequency: noteData.frequency,
name: noteData.name,
start: totalDelta,
duration: false, // new note, we don't know the duration yet
isOff: false, // once the note gets turned off, its duration will be calculated
}
notes.push(note);
break;
case "noteOff":
for (let i=0;i<notes.length;i++) {
const note = notes[i];
if (note.frequency !== noteData.frequency) continue;
if (note.isOff !== false || note.duration !== false) continue;
note.isOff = true;
note.duration = totalDelta-note.start;
break;
}
break;
}
});
const output = [];
let lastPosition = 0;
notes.forEach((note,i) => {
const pause = note.start - lastPosition;
if (pause > 0) {
output.push(`"P:${calculateDuration(pause,tempo)}",`);
}
output.push(`"T:${note.frequency},${calculateDuration(note.duration,tempo)}",`);
lastPosition += note.duration + pause;
});
// console.log(output); // debug output
return output;
});
// Write to the output file
fs.writeFileSync(outputFilePath, customFormatLines.join('\n'));
}
// Process command-line arguments
const [midiFilePath, outputFilePath, tempo] = process.argv.slice(2);
// Validate input
if (!midiFilePath || !outputFilePath) {
console.log("Usage: node script.js <MIDI file path> <output file path> <tempo>");
process.exit(1);
}
// Convert the MIDI file
convertMidiToCustomFormat(midiFilePath, outputFilePath, parseInt(tempo) || 120);