@mohayonao/midi-device

isomorphic abstract class for MIDI device

Stats

StarsIssuesVersionUpdatedCreatedSize
@mohayonao/midi-device
1110.5.04 years ago6 years agoMinified + gzip package size for @mohayonao/midi-device in KB

Readme

MIDI Device

Build Status NPM Version License

isomorphic abstract class for MIDI device

Installation

npm install @mohayonao/midi-device

API

MIDIDevice

  • constructor(deviceName: string)

Class methods

  • requestDeviceNames(): Promise<{ inputs: string[], outputs: string[] }>

Instance attributes

  • deviceName: string

Instance methods

  • open(): Promise<[ input: any, output: any ]>
  • close(): Promise<[ input: any, output: any ]>
  • send(data: number[]): void
  • _onmidimessage(e: MIDIMessageEvent): void

Usage

Create a subclass that is inherited from a base class needed for each target environment.

// CustomMIDIDevice.js
export default {
  extends(MIDIDevice) {
    return class CustomMIDIDevice extends MIDIDevice {
      constructor(deviceName = "Name of Custom Device") {
        super(deviceName);

        this._onmidimessage = (e) => {
          this.emit(e.type, e);
        };
      }
    }
  }
};
Node.js
// NodeCustomMIDIDevice.js
import NodeMIDIDevice from "@mohayonao/midi-device";
import CustomMIDIDevice from "./CustomMIDIDevice";

export default CustomMIDIDevice.extends(NodeMIDIDevice);
Browser
// WebCustomMIDIDevice.js
import WebMIDIDevice from "@mohayonao/midi-device/webmidi";
import CustomMIDIDevice from "./CustomMIDIDevice";

export default CustomMIDIDevice.extends(WebMIDIDevice);
Test

This provides API interfaces that not require a real MIDI device.

import TestMIDIDevice from "@mohayonao/midi-device/test";
import CustomMIDIDevice from "../src/CustomMIDIDevice";

let TestCustomMIDIDevice = CustomMIDIDevice.extends(TestMIDIDevice);

describe("CustomMIDIDevice", () => {
  it("deviceNames", () => {
    return TestCustomMIDIDevice.requestDeviceNames().then(([ inputs, outpus ]) => {
      assert(Array.isArray(inputs));
      assert(inputs.every(x => typeof x === "string"));
      assert(Array.isArray(outputs));
      assert(outputs.every(x => typeof x === "string"));
    });
  });
  it("works", () => {
    let midiDevice = new TestCustomMIDIDevice();
    let onmessage = sinon.spy();

    midiDevice.on("message", onmessage);

    return midiDevice.open().then(([ input, output ]) => {
      input.recv([ 0x00, 0x01, 0x02 ]);

      assert(onmessage.calledOnce);

      let msg = onmessage.args[0][0];
      assert(typeof msg.receivedTime === "number");
      assert.deepEqual(msg.data, new Uint8Array([ 0x00, 0x01, 0x02 ]));

      output.onmessage = sinon.spy();
      output.send([ 0x03, 0x04, 0x05 ]);

      assert(output.onmessage.calledOnce);
      assert.deepEqual(output.onmessage.args[0][0], [ 0x03, 0x04, 0x05 ]);
    });
  });
});

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.