我试图基本上是异步播放钢琴midi文件(当按下一个键时我想在整个midi文件音符中前进,基本上要按照音符进行播放。
我设法以这种方式播放midi文件,但解析后的midi文件正在关闭(静音),我不知道这是因为缺少仪器还是因为它无法解析和弦。这是我正在使用的MIDI Parser。
var parser = new MidiParser();
const fileReader = new FileReader();
var counter = 0;
var midiFileInput = document.getElementById('midiFileInput');
var parsedMidi;
const onLoadFile = (loadEvent) => {};
const file = fileReader.result;
const onMidiFileChange = (inputEvent) => {
fileReader.readAsArrayBuffer(inputEvent.target.files[0]);
}
midiFileInput.addEventListener('change', onMidiFileChange);
window.onload = function () {
MIDI.loadPlugin({
soundfontUrl: "./soundfont/",
instrument: "acoustic_grand_piano",
onprogress: function(state, progress) {
console.log(state, progress);
}
});
};
fileReader.onload = function (file) {
var uint8Midi = new Uint8Array(file.target.result);
parsedMidi = parser.parseUint8(uint8Midi);
console.log(parsedMidi.note)
// Use parsed midi
document.addEventListener("keypress", function(event) {
if (event.keyCode != 1) {
MIDI.setVolume(0, 127);
console.log(parsedMidi[counter].note, counter);
counter = counter +1;
var velocity = 127; // how hard the note hits
MIDI.setVolume(0, 127);
MIDI.noteOn(parsedMidi[counter].channel, parsedMidi[counter].note, parsedMidi[counter].velocity, parsedMidi[counter].delay);
MIDI.noteOff(parsedMidi[counter].channel, parsedMidi[counter].note, parsedMidi[counter].velocity, parsedMidi[counter].delay + 0.75);
}
})
return parsedMidi
}