Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { createLaunchpadCore } from "launchpadcore";

const App = createLaunchpadCore("LaunchpadX");

App.on("onEnabled", (instance, driver) => {
App.on("onConnected", (instance, driver) => {
instance.out.send(driver.textScrolling(15, "Welcome!"))
instance.out.noteOn(0, 11, 25) // Pad 11 to color 25
})
Expand All @@ -67,16 +67,21 @@ App.on("onMidiIn", (data) => {
App.on("onDisabled", () => {
console.log("Shutdown...")
})

App.on("onDisconnected", () => {
console.log("Device disconnected")
})
```

## What's can I do ?

### Events
| Name | Description |
| :---------- | :------------------------- |
| `onConnected` | When connected to Launchpad |
| `onDisabled` | When disabled (exit the program) |
| `onMidiIn` | When new MIDI message received |
| `onConnected` | When connected to Launchpad |
| `onDisabled` | When disabled (exit the program) |
| `onDisconnected` | When the device disconnects |
| `onMidiIn` | When new MIDI message received |


### MIDI methods
Expand Down
20 changes: 13 additions & 7 deletions src/Service/Midi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,33 @@ export default class MidiService {

private _midiIn: string;
private _midiOut: string;
private _onDisconnect?: () => void;

constructor(midiIn: string, midiOut: string) {
constructor(midiIn: string, midiOut: string, onDisconnect?: () => void) {
this._midiIn = midiIn;
this._midiOut = midiOut;
this._onDisconnect = onDisconnect;

this._midiInput = midi()
.openMidiIn(midiIn)
.or(() => this.midiError);
.or(() => this.midiError());
this._midiOutput = midi()
.openMidiOut(midiIn)
.or(() => this.midiError);
.openMidiOut(midiOut)
.or(() => this.midiError());
}

private get midiError() {
throw new midiError('Device not connected.');
private midiError() {
if (this._onDisconnect) {
this._onDisconnect();
}
this.closeAll();
console.error(new midiError('Device not connected.'));
}

private openOutput() {
this._midiOutput = midi()
.openMidiOut(this._midiOut)
.or(() => this.midiError);
.or(() => this.midiError());
}

private closeOutput() {
Expand Down
12 changes: 11 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LaunchpadCore<T extends StringDrivers> {
onMidiIn: [],
onConnected: [],
onDisabled: [],
onDisconnected: [],
};

constructor(driverName: T) {
Expand All @@ -23,7 +24,11 @@ class LaunchpadCore<T extends StringDrivers> {
MidiService.requestWebAccess().catch(() => {});
}

this._instance = new MidiService(this._driver.MidiIn, this._driver.MidiOut);
this._instance = new MidiService(
this._driver.MidiIn,
this._driver.MidiOut,
() => this.onDisconnected(),
);

this.onEnabled();

Expand All @@ -45,6 +50,10 @@ class LaunchpadCore<T extends StringDrivers> {
this._instance.closeAll();
}

private async onDisconnected() {
await this.handleEvent('onDisconnected', this._instance, this._driver);
}

/**
* Events
*/
Expand All @@ -55,6 +64,7 @@ class LaunchpadCore<T extends StringDrivers> {
public on(event: 'onMidiIn', callback: (data: any) => void): void;
public on(event: 'onConnected', callback: (instance: MidiService, driver: DriverMap[T]) => void): void;
public on(event: 'onDisabled', callback: (instance: MidiService, driver: DriverMap[T]) => void): void;
public on(event: 'onDisconnected', callback: (instance: MidiService, driver: DriverMap[T]) => void): void;

public on(event: string, callback: any) {
if (!this.callbacks[event]) throw new Error(`Unknown event name: '${event}'`);
Expand Down