easymidi

repository·master·Indexed 41 days ago

https://github.com/dinchak/node-easymidi

A simple, event-based MIDI messaging wrapper for node-midi in Node.js. Version 3.2.0 allows developers to interface with MIDI hardware or create virtual MIDI devices for software-based routing. It provides Input and Output classes to send and receive MIDI messages, including support for noteon, noteoff, cc, sysex, and other standard MIDI message types.

Tokens
2.6K
Snippets
12
Records
12
Agent score
2%

What's inside easymidi

  1. Create virtual MIDI devices

    master

    You can create virtual MIDI inputs and outputs by passing true as the second argument to the Input or Output constructors. This is useful for software-only MIDI routing.

    const virtualInput = new easymidi.Input('Virtual input name', true);
    const virtualOutput = new easymidi.Output('Virtual output name', true);
  2. Send a System Exclusive (sysex) message

    master

    To send a sysex message, pass a variable-length array of bytes to .send(). The array must start with 0xf0 (240) and end with 0xf7 (247), otherwise an error will be thrown.

    output.send('sysex', [240, 126, 1, 6, 1, 247]);
  3. Listen to MIDI input events

    master

    To receive MIDI messages, create a new easymidi.Input instance with the exact name of the MIDI input device. The Input object is an EventEmitter, allowing you to listen for specific message types like noteon.

    const easymidi = require('easymidi');
    const input = new easymidi.Input('MIDI Input Name');
    
    input.on('noteon', function (msg) {
      // msg contains { note, velocity, channel }
    });
  4. Get lists of available MIDI devices

    master

    Use easymidi.getInputs() and easymidi.getOutputs() to retrieve arrays of strings containing the names of all currently available MIDI input and output devices.

    const inputs = easymidi.getInputs();
    const outputs = easymidi.getOutputs();
  5. Close MIDI devices

    master

    To release resources and stop interfacing with a device, call the .close() method on your Input or Output instance.

    const input = new easymidi.Input('My input', true);
    input.close();
    
    const output = new easymidi.Output('My output', true);
    output.close();
  6. Send MIDI messages to an output

    master

    To send MIDI messages, create a new easymidi.Output instance with the exact name of the MIDI output device. Use the .send(type, params) method, where type is the MIDI message type and params is an object containing the message data.

    const easymidi = require('easymidi');
    const output = new easymidi.Output('MIDI Output Name');
    
    output.send('noteon', {
      note: 64,
      velocity: 127,
      channel: 3
    });
  7. Reference MIDI message types and parameters

    master

    The following table defines the supported MIDI message types and the structure of the parameters passed to .send() or received in event listeners.

    | Type               | Parameter          | Parameter        | Parameter        |
    |--------------------|--------------------|------------------|------------------|
    | noteon             | note [0-127]       | velocity [0-127] | channel [0-15] |
    | noteoff            | note [0-127]       | velocity [0-127] | channel [0-15] |
    | poly aftertouch    | note [0-127]       | velocity [0-127] | channel [0-15] |
    | cc                 | controller [0-127] | value [0-127]    | channel [0-15] |
    | program            | number [0-127]     |                  | channel [0-15] |
    | channel aftertouch | pressure [0-127]   |                  | channel [0-15] |
    | pitch              | value [0-16384]   |                  | channel [0-15] |
    | position           | value [0-16384]   |                  |                |
    | mtc                | type [0-7]         | value [0-15]     |                |
    | select             | song [0-127]       |                  |                |
    | clock              |                    |                  |                |
    | start              |                    |                  |                |
    | continue           |                    |                  |                |
    | stop               |                    |                  |                |
    | activesense        |                    |                  |                |
    | reset              |                    |                  |                |
    | sysex              | bytes (variable length array) |             |                |
  8. List available MIDI inputs and outputs

    master

    Use getInputs() and getOutputs() to retrieve an array of available MIDI port names. Note that if multiple ports have the same name, the library appends a counter to ensure uniqueness (e.g., PortName, PortName1, PortName2).

    const { getInputs, getOutputs } = require('@julusian/midi');
    
    const inputs = getInputs();
    console.log('Available Inputs:', inputs);
    
    const outputs = getOutputs();
    console.log('Available Outputs:', outputs);
  9. Use the Input class to receive MIDI messages

    master

    The Input class allows you to listen to MIDI messages from an existing hardware port or a virtual port. It extends EventEmitter, so you can listen for specific message types (like noteon, cc, sysex) or a generic message event.

    To open a physical port, pass the exact name of the port as found in getInputs(). To create a virtual port, pass true as the second argument.

    Supported Events:

    • Specific message types: noteoff, noteon, poly aftertouch, cc, program, channel aftertouch, pitch, sysex, mtc, position, select, tune, clock, start, continue, stop, activesense, reset.
    • message: Emits the parsed message object.
    • smpte: Emits when MIDI Time Code (mtc) is parsed and a full SMPTE frame is reconstructed.
    const { Input, getInputs } = require('@julusian/midi');
    
    // List available inputs to find the correct name
    console.log(getInputs());
    
    // Open a physical input port
    const input = new Input('Your MIDI Device Name');
    
    input.on('noteon', (msg) => {
      console.log(`Note On: ${msg.note}, Velocity: ${msg.velocity}, Channel: ${msg.channel}`);
    });
    
    input.on('message', (msg) => {
      console.log('Generic message received:', msg);
    });
    
    // Open a virtual input port
    const virtualInput = new Input('My Virtual Port', true);
  10. Use the Output class to send MIDI messages

    master

    The Output class allows you to send MIDI messages to a physical or virtual MIDI port.

    Message Types and Arguments:

    • noteon / noteoff: { note, velocity, channel }
    • cc: { controller, value, channel }
    • poly aftertouch: { note, pressure, channel }
    • channel aftertouch: { pressure, channel }
    • program: { number, channel }
    • pitch / position: { value, channel } (where value is 0-16383)
    • mtc: { type, value }
    • select: { song }
    • sysex: { bytes } (must be an array starting with 0xf0 and ending with 0xf7)

    To open a physical port, pass the name from getOutputs(). To create a virtual port, pass true as the second argument.

    const { Output, getOutputs } = require('@julusian/midi');
    
    // Open a physical output port
    const output = new Output('Your MIDI Output Name');
    
    // Send a Note On message
    output.send('noteon', { note: 60, velocity: 127, channel: 0 });
    
    // Send a CC message
    output.send('cc', { controller: 7, value: 100, channel: 0 });
    
    // Send a Sysex message
    output.send('sysex', { bytes: [0xf0, 0x01, 0x02, 0xf7] });