0

我正在尝试使用WebMidi.js在浏览器中播放音符。我正在使用下面的代码,该代码主要取自快速入门指南。

<body>
  <script src="node_modules/webmidi/webmidi.min.js"></script>
  <script defer>
    WebMidi.enable(function(err) {
      if (err) {
        console.log("WebMidi could not be enabled.", err);
      } else {
        console.log("WebMidi enabled!");
        console.log("outputs:", WebMidi.outputs);
      }
    });
  </script>
</body>

当我加载页面时,它说 WebMidi 已启用,但输出数组为空。我怎样才能得到包来检测我的输出?我在 Mac 上使用 Chrome——我的软件是最新的。

4

1 回答 1

2

要使用 MIDI 输出来播放声音,您需要有一个运行虚拟 MIDI 端口的硬件或软件 MIDI 合成器。您可能想改用 AudioContext :

// patch up prefixes
window.AudioContext=window.AudioContext||window.webkitAudioContext;

context = new AudioContext();
if (navigator.requestMIDIAccess)
  navigator.requestMIDIAccess().then( onMIDIInit, onMIDIReject );
else
  alert("No MIDI support present in your browser.  You're gonna have a bad time.")

// set up the basic oscillator chain, muted to begin with.
oscillator = context.createOscillator();
oscillator.frequency.setValueAtTime(110, 0);
envelope = context.createGain();
oscillator.connect(envelope);
envelope.connect(context.destination);
envelope.gain.value = 0.0;  // Mute the sound
oscillator.start(0);  // Go ahead and start up the oscillator

要生成更复杂的声音,请查看 SoundJS 或其他声音库。

于 2018-08-30T08:01:30.050 回答