From f6792cf8143805a9a16bb3248afb6aa1bf029fb9 Mon Sep 17 00:00:00 2001 From: Marc Rominger Date: Mon, 24 Jan 2022 18:49:47 +0100 Subject: [PATCH] simplified usage --- README.md | 33 ++++++++++++++++++++----------- example/src/app/home/home.page.ts | 2 +- package.json | 2 +- src/definitions.ts | 4 ++-- src/web.ts | 7 ++++++- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 32b0de9..c91a5d6 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # 🎹 capacitor-midi -Grants access to midi devices via native libraries or WebMIDI. +Grants access to midi devices via native libraries or WebMIDI. -❗ Currently NO iOS support. Because I don't have access to a mac. I hope this changes soon. ❗ +❗Currently NO iOS support. Because I don't have access to a Mac. Contributors Welcome❗ ## 🔌 Install @@ -12,17 +12,18 @@ npx cap sync ``` ## 🎼 Usage + ### Subscribe to MIDI events after a device is opened + ```typescript const options: DeviceOptions = { deviceNumber: 0 // Choose device from listMIDIDevices() } await MIDI.openDevice(options) + .catch((e) => console.error(e)) - -MIDI.addListener('MIDIEventReceived', - (message: MidiMessage) => console.log(message)); +MIDI.addDeviceListener((message: MidiMessage) => console.log(message)); interface MidiMessage { type: string; // NoteOn, NoteOff, UNKNOWN - XXX @@ -37,7 +38,7 @@ interface MidiMessage { * [`listMIDIDevices()`](#listmididevices) * [`openDevice(...)`](#opendevice) -* [`addListener(...)`](#addlistener) +* [`addDeviceListener(...)`](#adddevicelistener) * [Interfaces](#interfaces) @@ -69,16 +70,15 @@ openDevice(options: DeviceOptions) => Promise -------------------- -### addListener(...) +### addDeviceListener(...) ```typescript -addListener(eventName: string, listenerFunc: ListenerCallback) => PluginListenerHandle +addDeviceListener(callback: (message: MidiMessage) => any) => PluginListenerHandle ``` -| Param | Type | -| ------------------ | ----------------------------------------------- | -| **`eventName`** | string | -| **`listenerFunc`** | (err: any, ...args: {}) => void | +| Param | Type | +| -------------- | ------------------------------------------------------------------------ | +| **`callback`** | (message: MidiMessage) => any | **Returns:** PluginListenerHandle @@ -101,4 +101,13 @@ addListener(eventName: string, listenerFunc: ListenerCallback) => PluginListener | ------------ | ----------------------------------------- | | **`remove`** | () => Promise<void> | + +#### MidiMessage + +| Prop | Type | +| -------------- | ------------------- | +| **`type`** | string | +| **`note`** | number | +| **`velocity`** | number | + diff --git a/example/src/app/home/home.page.ts b/example/src/app/home/home.page.ts index 7931742..0ab38ed 100644 --- a/example/src/app/home/home.page.ts +++ b/example/src/app/home/home.page.ts @@ -17,7 +17,7 @@ export class HomePage { this.devices = devices.value; }); - MIDI.addListener('MIDIEventReceived', (message: MidiMessage) => { + MIDI.addDeviceListener((message: MidiMessage) => { this.messages.push(message); cd.detectChanges(); }); diff --git a/package.json b/package.json index 84fa20a..7de6f35 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "capacitor-midi", - "version": "0.0.5", + "version": "0.0.6", "description": "Grants access to midi devices via native libraries or WebMIDI.", "main": "dist/plugin.cjs.js", "module": "dist/esm/index.js", diff --git a/src/definitions.ts b/src/definitions.ts index f6c49e6..500d8e4 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -1,11 +1,11 @@ -import type {ListenerCallback, PluginListenerHandle} from "@capacitor/core"; +import type {PluginListenerHandle} from "@capacitor/core"; export interface MIDIPlugin { listMIDIDevices(): Promise<{ value: string[] }> openDevice(options: DeviceOptions): Promise - addListener(eventName: string, listenerFunc: ListenerCallback): PluginListenerHandle + addDeviceListener(callback: ((message: MidiMessage) => any)): PluginListenerHandle } export interface MidiMessage { diff --git a/src/web.ts b/src/web.ts index 83b893a..b6ee61e 100644 --- a/src/web.ts +++ b/src/web.ts @@ -1,7 +1,8 @@ +import type {PluginListenerHandle} from '@capacitor/core'; import {WebPlugin} from '@capacitor/core'; import {WebMIDIHandler} from "./WebMIDIHandler"; -import type {DeviceOptions, MIDIPlugin} from './definitions'; +import type {DeviceOptions, MidiMessage, MIDIPlugin} from './definitions'; export class MIDIPluginWeb extends WebPlugin implements MIDIPlugin { async listMIDIDevices(): Promise<{ value: string[] }> { @@ -41,4 +42,8 @@ export class MIDIPluginWeb extends WebPlugin implements MIDIPlugin { return } + addDeviceListener(callback: (message: MidiMessage) => any): PluginListenerHandle { + return this.addListener('MIDIEventReceived', callback); + } + }