如何用javascript播放任意MIDI音符? [英] How do I play arbitrary MIDI notes with javascript?

查看:797
本文介绍了如何用javascript播放任意MIDI音符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

澄清:我不想生成MIDI文件,也不想播放MIDI文件,我希望能够即时播放MIDI音符。

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.

我尝试使用 https://github.com/mudcube/MIDI。 js 作为MIDI库,它有点工作。

I tried using https://github.com/mudcube/MIDI.js as the MIDI library, and it works somewhat.

我可以通过调用 MIDI.noteOn(0, midiNumber,100); 。然而,即使我从不打电话给 MIDI.noteOff ,这会播放几秒钟的音符然后逐渐消失。

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的工作方式。我希望能够在调用noteOff之前调用noteOn并进行音符播放和延音。

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.

目标浏览器:现代firefox / chrome。

Intended browsers: modern firefox/chrome.

推荐答案

这是你的MIDI.js版本的错误:

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;
};

如你所见 playChannel 将加载一个给出笔记并播放它。由于没有autoloop属性,因此不会重复,因此无需调用 noteOff 。如果将 audio 元素设置为自动循环,则可以自行解决此问题。

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.

这篇关于如何用javascript播放任意MIDI音符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆