newlandsvalley / purescript-midi

MIDI and Web MIDI support
MIT License
10 stars 1 forks source link

Parsing messages from node-midi #11

Open f-f opened 2 years ago

f-f commented 2 years ago

Hi! I'm writing a library to talk with node-midi, and I'd like to integrate it with this library, i.e. possibly reuse the datatypes and the parser, etc.

I'm opening this issue because I'm not sure how to make the parser work on the events that I get from the Node library, which are JS arrays. E.g. a NoteOn would be something like [144, 89, 127].

Is this possible right now, or would it require a patch?

newlandsvalley commented 2 years ago

Hi, @f-f . I'd not come across node-midi before and nor had I heard of RtMidi on which it's based. My parser is based on General MIDI 1.0. I find this spec rather verbose and awkward to work with, but the main point is that it is biased towards entire MIDI recordings. Specifically, the whole melody is captured in the eventual file structure and individual MIDI messages such as NoteOn are timestamped, which allows a player to emit them at the appropriate time.

From a brief reading of the rtMidi Tutorial it seems that the real-time nature means that messages are issued immediately with no need for a timestamp. Also, General MIDI allows up to 10 channels to be in use concurrently, whereas I'm guessing, with rtMidi you choose a default channel (is this an rtMidi Port?). Anyway, where it defines a NoteOn as [144, 89, 127] (i.e. [MessageType, Note, Velocity] I use NoteOn Channel Note Velocity. I presume the other messages which I enumerate in the Event type do something similar.

Whether all this might be ergonomic for you to reuse is difficult to say. Many of the data types would not be useful to you but Event might help. The parser itself I imagine would be useful if you had a need to parse a MIDI recording or a Web-Midi event stream and then generate rtMidi output.

Another consideration may be that purescript-midi was written before the advent of MIDI 2.0.

It may be possible to use parseMidiEvent to parse individual messages if you don't mind building the Event type as it stands. It would be an interesting experiment to see if this works,