How do I play arbitrary MIDI notes with javascript?

限于喜欢 提交于 2019-12-03 02:08:31

It's a bug your version of MIDI.js:

var playChannel = function (id) {
    var note = notes[id];
    if (!note) return;
    var nid = (channel_nid + 1) % channels.length;
    var time = (new Date()).getTime();
    var audio = channels[nid];
    channel_map[note.id] = audio;
    audio.src = MIDI.Soundfont[note.id];
    audio.volume = volume;
    audio.play();
    channel_nid = nid;
};

As you can see playChannel will load a given note and play it. Since there is no autoloop attribute it won't repeat, so the call of noteOff isn't necessary. You could fix this yourself if you set the audio element to auto-loop.

Alternatively, http://mohayonao.github.io/timbre.js/ provides various sound generators for which noteOn and noteOff can be called.

Different instruments behave differently. A piano has an "intrinsic" note off, but an organ doesn't.

Also, noteOn and noteOff depend on the implementation. modcu.be in the HTML5 implementation plays a OGG file for each note, and it doesn't care of noteOff at all. When the OGG ends, the sound stops. And autoloop wouldn't help for a piano in this case.

Google for Web MIDI API. It is not implemented in all browsers yet, but there is a polyfill by Chris Wilson at GitHub.

This is right behavour. You need a better samples library to play long sounds.

See church organ example of endless sound.

this exanple uses WebAudioFont to play more then 1500 instruments and drums. This lib supports ADSR, reverberation and loop points.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!