This commit is contained in:
ChomeNS 2022-11-20 09:09:36 +07:00
parent 44f3be1c18
commit 3f0c366cf9

View file

@ -1,36 +1,38 @@
const { Midi } = require('@tonejs/midi')
const { convertNote, convertPercussionNote } = require('./convert_note.js')
/* eslint-disable max-len */
/* eslint-disable require-jsdoc */
const {Midi} = require('@tonejs/midi');
const {convertNote, convertPercussionNote} = require('./convert_note.js');
function convertMidi (midi) {
if (!(midi instanceof Midi)) throw new TypeError("midi must be an instance of require('@tonejs/midi').Midi")
function convertMidi(midi) {
if (!(midi instanceof Midi)) throw new TypeError('midi must be an instance of require(\'@tonejs/midi\').Midi');
let noteList = []
let noteList = [];
for (const track of midi.tracks) {
for (const note of track.notes) {
const mcNote = (track.instrument.percussion ? convertPercussionNote : convertNote)(track, note)
const mcNote = (track.instrument.percussion ? convertPercussionNote : convertNote)(track, note);
if (mcNote != null) {
noteList.push(mcNote)
noteList.push(mcNote);
}
}
}
noteList = noteList.sort((a, b) => a.time - b.time)
noteList = noteList.sort((a, b) => a.time - b.time);
// It might be better to move some of this code to the converting loop (for performance reasons)
let maxVolume = 0.001
let maxVolume = 0.001;
for (const note of noteList) {
if (note.volume > maxVolume) maxVolume = note.volume
if (note.volume > maxVolume) maxVolume = note.volume;
}
for (const note of noteList) {
note.volume /= maxVolume
note.volume /= maxVolume;
}
let songLength = 0
let songLength = 0;
for (const note of noteList) {
if (note.time > songLength) songLength = note.time
if (note.time > songLength) songLength = note.time;
}
return { name: midi.header.name, notes: noteList, loop: false, loopPosition: 0, length: songLength }
return {name: midi.header.name, notes: noteList, loop: false, loopPosition: 0, length: songLength};
}
module.exports = { convertMidi, convertNote, convertPercussionNote }
module.exports = {convertMidi, convertNote, convertPercussionNote};