-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiditools.js
54 lines (44 loc) · 1.53 KB
/
miditools.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
/* jshint strict: false */
/* global require , Buffer , module */
var Log = require('./logger.js');
var fs = require('fs');
var parseMidi = require('midi-file').parseMidi;
var writeMidi = require('midi-file').writeMidi;
// It may seem silly to have all three below
// functions, but they exist for clarity
// in code what you are trying to do, not
// what you are actually doing.
function msBpmSwap(one) {
return parseInt(60000 / one * 1000);
}
function microsecondsToBpm(ms) {
return msBpmSwap(ms);
}
function bpmToMicroseconds(bpm) {
return msBpmSwap(bpm);
}
function changeMidiTempo(newtempo, infile, outfile) {
try {
Log.debug("Changing tempo for " + infile + " and putting it to " + outfile);
var input = fs.readFileSync(infile);
var parsed = parseMidi(input);
for (var o in parsed.tracks[0]) {
if (parsed.tracks[0][o].type === "setTempo") {
Log.debug("Current tempo is :: " + microsecondsToBpm(parsed.tracks[0][o].microsecondsPerBeat));
parsed.tracks[0][o].microsecondsPerBeat = bpmToMicroseconds(newtempo);
Log.debug("Tempo changed to :: " + microsecondsToBpm(parsed.tracks[0][o].microsecondsPerBeat));
break;
}
}
var output = writeMidi(parsed);
var outputBuffer = new Buffer.from(output);
fs.writeFileSync(outfile, outputBuffer);
return true;
} catch (e) {
Log.error(e);
return false;
}
}
module.exports = {
changeMidiTempo: changeMidiTempo
};