Skip to content

Commit

Permalink
simplified usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dante1349 committed Jan 24, 2022
1 parent c21ad43 commit f6792cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -37,7 +38,7 @@ interface MidiMessage {

* [`listMIDIDevices()`](#listmididevices)
* [`openDevice(...)`](#opendevice)
* [`addListener(...)`](#addlistener)
* [`addDeviceListener(...)`](#adddevicelistener)
* [Interfaces](#interfaces)

</docgen-index>
Expand Down Expand Up @@ -69,16 +70,15 @@ openDevice(options: DeviceOptions) => Promise<void>
--------------------


### addListener(...)
### addDeviceListener(...)

```typescript
addListener(eventName: string, listenerFunc: ListenerCallback) => PluginListenerHandle
addDeviceListener(callback: (message: MidiMessage) => any) => PluginListenerHandle
```

| Param | Type |
| ------------------ | ----------------------------------------------- |
| **`eventName`** | <code>string</code> |
| **`listenerFunc`** | <code>(err: any, ...args: {}) =&gt; void</code> |
| Param | Type |
| -------------- | ------------------------------------------------------------------------ |
| **`callback`** | <code>(message: <a href="#midimessage">MidiMessage</a>) =&gt; any</code> |

**Returns:** <code><a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

Expand All @@ -101,4 +101,13 @@ addListener(eventName: string, listenerFunc: ListenerCallback) => PluginListener
| ------------ | ----------------------------------------- |
| **`remove`** | <code>() =&gt; Promise&lt;void&gt;</code> |


#### MidiMessage

| Prop | Type |
| -------------- | ------------------- |
| **`type`** | <code>string</code> |
| **`note`** | <code>number</code> |
| **`velocity`** | <code>number</code> |

</docgen-api>
2 changes: 1 addition & 1 deletion example/src/app/home/home.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -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<void>

addListener(eventName: string, listenerFunc: ListenerCallback): PluginListenerHandle
addDeviceListener(callback: ((message: MidiMessage) => any)): PluginListenerHandle
}

export interface MidiMessage {
Expand Down
7 changes: 6 additions & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
@@ -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[] }> {
Expand Down Expand Up @@ -41,4 +42,8 @@ export class MIDIPluginWeb extends WebPlugin implements MIDIPlugin {
return
}

addDeviceListener(callback: (message: MidiMessage) => any): PluginListenerHandle {
return this.addListener('MIDIEventReceived', callback);
}

}

0 comments on commit f6792cf

Please sign in to comment.