Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I play arbitrary MIDI notes with javascript?

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.

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

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.

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.

Intended browsers: modern firefox/chrome.

like image 905
Razor Storm Avatar asked Jan 13 '13 08:01

Razor Storm


2 Answers

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.

like image 133
Zeta Avatar answered Sep 20 '22 16:09

Zeta


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

like image 36
Tirno Avatar answered Sep 19 '22 16:09

Tirno