如何使用 javascript 播放任意 MIDI 音符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14301785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I play arbitrary MIDI notes with javascript?
提问by Razor Storm
To clarify: I don't want to generate a MIDI file nor do I want to play a MIDI file, I wish to play MIDI notes on the fly.
澄清一下:我不想生成 MIDI 文件,也不想播放 MIDI 文件,我希望即时播放 MIDI 音符。
I tried using https://github.com/mudcube/MIDI.jsas the MIDI library, and it works somewhat.
我尝试使用https://github.com/mudcube/MIDI.js作为 MIDI 库,它有点工作。
I am able to play notes by calling MIDI.noteOn(0,midiNumber,100);
. However, this plays a note for a couple seconds and then tapers off even if I never call MIDI.noteOff
.
我可以通过调用来弹奏音符MIDI.noteOn(0,midiNumber,100);
。但是,这会播放几秒钟的音符,然后即使我从不调用MIDI.noteOff
.
I don't believe this is how MIDI is intended to work. I wish to be able to call noteOn and have a note play and sustain until noteOff is called.
我不相信这就是 MIDI 的工作方式。我希望能够调用 noteOn 并在调用 noteOff 之前播放和维持音符。
Intended browsers: modern firefox/chrome.
预期浏览器:现代 firefox/chrome。
采纳答案by Zeta
It's a bug your version of MIDI.js:
这是您的 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.
如您所见,playChannel
将加载一个给定的音符并播放它。由于没有 autoloop 属性,它不会重复,因此noteOff
不需要调用。如果您将audio
元素设置为自动循环,您可以自己解决这个问题。
回答by Tirno
Alternatively, http://mohayonao.github.io/timbre.js/provides various sound generators for which noteOn and noteOff can be called.
或者,http://mohayonao.github.io/timbre.js/ 提供了可以调用 noteOn 和 noteOff 的各种声音发生器。
回答by dmcontador
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.
此外,noteOn
并noteOff
取决于实现。modcu.be
在 HTML5 实现中为每个音符播放一个 OGG 文件,它根本不在乎noteOff
。当 OGG 结束时,声音停止。并autoloop
不会帮助在这种情况下,一台钢琴。
回答by user1024
This is right behavour. You need a better samples library to play long sounds.
这是正确的行为。您需要一个更好的样本库来播放长音。
See church organ exampleof endless sound.
this exanple uses WebAudioFontto play more then 1500 instruments and drums. This lib supports ADSR, reverberation and loop points.
这个例子使用WebAudioFont来演奏超过 1500 种乐器和鼓。这个库支持 ADSR、混响和循环点。
回答by user1024
Google for Web MIDI API. It is not implemented in all browsers yet, but there is a polyfill by Chris Wilson at GitHub.
Google for Web MIDI API。它尚未在所有浏览器中实现,但 GitHub 上的 Chris Wilson 有一个 polyfill。