add global try-catch
This commit is contained in:
parent
27e755cbc2
commit
824fd815af
1 changed files with 354 additions and 349 deletions
255
src/index.ts
255
src/index.ts
|
|
@ -2,11 +2,12 @@ import * as midi from 'easymidi';
|
|||
import osc from 'osc';
|
||||
|
||||
|
||||
console.log('inputs', midi.getInputs())
|
||||
console.log('outputs', midi.getOutputs())
|
||||
try {
|
||||
console.log('inputs', midi.getInputs())
|
||||
console.log('outputs', midi.getOutputs())
|
||||
|
||||
|
||||
function mapNumber(value: number, fromMin: number, fromMax: number, toMin: number, toMax: number): number {
|
||||
function mapNumber(value: number, fromMin: number, fromMax: number, toMin: number, toMax: number): number {
|
||||
if (value <= fromMin) return toMin;
|
||||
if (value >= fromMax) return toMax;
|
||||
|
||||
|
|
@ -15,34 +16,34 @@ function mapNumber(value: number, fromMin: number, fromMax: number, toMin: numbe
|
|||
|
||||
const mapped = (value / absFromMax) * absToMax;
|
||||
return mapped + toMin;
|
||||
}
|
||||
}
|
||||
|
||||
enum MessageType {
|
||||
enum MessageType {
|
||||
NoteOn = 'noteon',
|
||||
NoteOff = 'noteoff',
|
||||
Pitch = 'pitch'
|
||||
}
|
||||
}
|
||||
|
||||
type DataLookup = {
|
||||
type DataLookup = {
|
||||
[MessageType.NoteOn]: midi.Note,
|
||||
[MessageType.NoteOff]: midi.Note,
|
||||
[MessageType.Pitch]: midi.Pitch
|
||||
}
|
||||
}
|
||||
|
||||
const defaultData = {
|
||||
const defaultData = {
|
||||
[MessageType.NoteOn]: { channel: 0, note: 0, velocity: 0 },
|
||||
[MessageType.NoteOff]: { channel: 0, note: 0, velocity: 0 },
|
||||
[MessageType.Pitch]: { channel: 0, value: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
interface IMidiControlOptions {
|
||||
interface IMidiControlOptions {
|
||||
feedbackInput: boolean,
|
||||
noPageFeedback: boolean,
|
||||
noStoreInput: boolean
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class MidiControl<T extends MessageType> {
|
||||
class MidiControl<T extends MessageType> {
|
||||
public values: { [page: number]: DataLookup[T] } = [];
|
||||
private outputs: { [page: number]: (data: any) => void } = [];
|
||||
private page: number = 0;
|
||||
|
|
@ -120,9 +121,9 @@ class MidiControl<T extends MessageType> {
|
|||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MidiDevice {
|
||||
class MidiDevice {
|
||||
public input: midi.Input;
|
||||
public output: midi.Output;
|
||||
private _page: number = 0;
|
||||
|
|
@ -150,14 +151,14 @@ class MidiDevice {
|
|||
public get page(): number {
|
||||
return this._page;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface IOSCEvent {
|
||||
interface IOSCEvent {
|
||||
address: string;
|
||||
handler: (message: osc.ResponseMessage<osc.MessageArg>) => void;
|
||||
}
|
||||
}
|
||||
|
||||
class OSCDevice {
|
||||
class OSCDevice {
|
||||
private port: osc.UDPPort;
|
||||
private listeners: Array<IOSCEvent> = [];
|
||||
|
||||
|
|
@ -221,132 +222,132 @@ class OSCDevice {
|
|||
});
|
||||
return newLength - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const deviceName = midi.getInputs().find(i => i.includes('Platform X'));
|
||||
if (!deviceName) {
|
||||
const deviceName = midi.getInputs().find(i => i.includes('Platform X'));
|
||||
if (!deviceName) {
|
||||
console.log('midi device not found')
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
const mdev = new MidiDevice(deviceName);
|
||||
const odev = new OSCDevice('0.0.0.0', '192.168.0.47', 10024);
|
||||
const mdev = new MidiDevice(deviceName);
|
||||
const odev = new OSCDevice('0.0.0.0', '192.168.0.47', 10024);
|
||||
|
||||
// const client = new osc.OSCClient('192.168.0.26', 9889);
|
||||
// const client = new osc.OSCClient('192.168.0.26', 9889);
|
||||
|
||||
// DEBUG OUT
|
||||
//['noteon', 'pitch'].map((v) => mdev.input.addListener(v, (data) => { console.log('data', data) }))
|
||||
// DEBUG OUT
|
||||
//['noteon', 'pitch'].map((v) => mdev.input.addListener(v, (data) => { console.log('data', data) }))
|
||||
|
||||
|
||||
// MM MM MMMM MMMMMM MMMMMM MMMMMM MM MM MMMM MMMM
|
||||
// MMMM MMMM MM MM MM MM MM MM MM MMMM MM MM MM MM MM
|
||||
// MM MM MM MMMMMMMM MM MM MM MM MM MM MMMM MM MM
|
||||
// MM MM MM MM MMMMMM MMMMMM MM MM MM MM MMMM MM
|
||||
// MM MM MM MM MM MM MM MM MM MM MM MM MM
|
||||
// MM MM MM MM MM MM MMMMMM MM MM MMMM MMMM
|
||||
// MM MM MMMM MMMMMM MMMMMM MMMMMM MM MM MMMM MMMM
|
||||
// MMMM MMMM MM MM MM MM MM MM MM MMMM MM MM MM MM MM
|
||||
// MM MM MM MMMMMMMM MM MM MM MM MM MM MMMM MM MM
|
||||
// MM MM MM MM MMMMMM MMMMMM MM MM MM MM MMMM MM
|
||||
// MM MM MM MM MM MM MM MM MM MM MM MM MM
|
||||
// MM MM MM MM MM MM MMMMMM MM MM MMMM MMMM
|
||||
|
||||
const PAGE_MAIN = 0;
|
||||
const PAGE_HP = 1;
|
||||
const PAGE_PC = 2;
|
||||
const PAGE_MAIN = 0;
|
||||
const PAGE_HP = 1;
|
||||
const PAGE_PC = 2;
|
||||
|
||||
const ALL_PAGES = [PAGE_MAIN, PAGE_HP, PAGE_PC];
|
||||
const ALL_PAGES = [PAGE_MAIN, PAGE_HP, PAGE_PC];
|
||||
|
||||
const OSC_LEVEL_0 = 0.75;
|
||||
const OSC_LEVEL_0 = 0.75;
|
||||
|
||||
|
||||
// TURN OFF ALL LEDS
|
||||
const feedbackOff = () => {
|
||||
// TURN OFF ALL LEDS
|
||||
const feedbackOff = () => {
|
||||
for (const control of mdev.controls) {
|
||||
if (control.type === MessageType.NoteOn) {
|
||||
control.handleFeedback(ALL_PAGES, { velocity: 0 }, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
const offButton = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 39 }, { noPageFeedback: true })
|
||||
offButton.addOutput(ALL_PAGES, (d) => feedbackOff());
|
||||
odev.addListener('/offleds', () => feedbackOff());
|
||||
}
|
||||
const offButton = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 39 }, { noPageFeedback: true })
|
||||
offButton.addOutput(ALL_PAGES, (d) => feedbackOff());
|
||||
odev.addListener('/offleds', () => feedbackOff());
|
||||
|
||||
// PAGE SWITCHING
|
||||
const pageFeedback = () => {
|
||||
// PAGE SWITCHING
|
||||
const pageFeedback = () => {
|
||||
const page = mdev.page;
|
||||
buttonSel1.handleFeedback(ALL_PAGES, { velocity: page === PAGE_MAIN ? 127 : 0 })
|
||||
buttonSel2.handleFeedback(ALL_PAGES, { velocity: page === PAGE_HP ? 127 : 0 })
|
||||
buttonSel3.handleFeedback(ALL_PAGES, { velocity: page === PAGE_PC ? 127 : 0 })
|
||||
}
|
||||
}
|
||||
|
||||
const buttonSel1 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 8 }, { noPageFeedback: true })
|
||||
buttonSel1.addOutput(ALL_PAGES, (d) => {
|
||||
const buttonSel1 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 8 }, { noPageFeedback: true })
|
||||
buttonSel1.addOutput(ALL_PAGES, (d) => {
|
||||
if (d.velocity === 127) mdev.setPage(PAGE_MAIN)
|
||||
pageFeedback();
|
||||
});
|
||||
});
|
||||
|
||||
const buttonSel2 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 9 }, { noPageFeedback: true })
|
||||
buttonSel2.addOutput(ALL_PAGES, (d) => {
|
||||
const buttonSel2 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 9 }, { noPageFeedback: true })
|
||||
buttonSel2.addOutput(ALL_PAGES, (d) => {
|
||||
if (d.velocity === 127) mdev.setPage(PAGE_HP)
|
||||
pageFeedback();
|
||||
});
|
||||
});
|
||||
|
||||
const buttonSel3 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 10 }, { noPageFeedback: true })
|
||||
buttonSel3.addOutput(ALL_PAGES, (d) => {
|
||||
const buttonSel3 = mdev.addControl(MessageType.NoteOn, { channel: 0, note: 10 }, { noPageFeedback: true })
|
||||
buttonSel3.addOutput(ALL_PAGES, (d) => {
|
||||
if (d.velocity === 127) mdev.setPage(PAGE_PC)
|
||||
pageFeedback();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// FADERS
|
||||
const faders = [0, 1, 2, 3, 4, 5, 6, 7].map((f) => mdev.addControl(MessageType.Pitch, { channel: f as midi.Channel }, { feedbackInput: true }));
|
||||
// FADERS
|
||||
const faders = [0, 1, 2, 3, 4, 5, 6, 7].map((f) => mdev.addControl(MessageType.Pitch, { channel: f as midi.Channel }, { feedbackInput: true }));
|
||||
|
||||
const setLevel = (addr: string, value: number, max: number = 1) => odev.sendFloat(addr, mapNumber(value, 0, 16383, 0, max));
|
||||
const levelFeedback = (fader: number, page: number, value: number, max: number = 1) => {
|
||||
const setLevel = (addr: string, value: number, max: number = 1) => odev.sendFloat(addr, mapNumber(value, 0, 16383, 0, max));
|
||||
const levelFeedback = (fader: number, page: number, value: number, max: number = 1) => {
|
||||
faders[fader]?.handleFeedback(page, { value: mapNumber(value, 0, max, 0, 16383) });
|
||||
}
|
||||
}
|
||||
|
||||
const fader2way = (fader: number, page: number, addr: string, max: number = 1) => {
|
||||
const fader2way = (fader: number, page: number, addr: string, max: number = 1) => {
|
||||
faders[fader]?.addOutput(page, d => setLevel(addr, d.value, max));
|
||||
odev.addListener(addr, d => { levelFeedback(fader, page, (d.args as any)[0], max) });
|
||||
|
||||
odev.sendNull(addr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// PAGE 1: Main
|
||||
fader2way(0, PAGE_MAIN, '/dca/1/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_MAIN, '/rtn/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(2, PAGE_MAIN, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(3, PAGE_MAIN, '/ch/01/mix/fader')
|
||||
fader2way(4, PAGE_MAIN, '/ch/03/mix/fader')
|
||||
fader2way(5, PAGE_MAIN, '/ch/05/mix/fader')
|
||||
fader2way(6, PAGE_MAIN, '/ch/07/mix/fader')
|
||||
fader2way(7, PAGE_MAIN, '/ch/09/mix/fader')
|
||||
// PAGE 1: Main
|
||||
fader2way(0, PAGE_MAIN, '/dca/1/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_MAIN, '/rtn/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(2, PAGE_MAIN, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(3, PAGE_MAIN, '/ch/01/mix/fader')
|
||||
fader2way(4, PAGE_MAIN, '/ch/03/mix/fader')
|
||||
fader2way(5, PAGE_MAIN, '/ch/05/mix/fader')
|
||||
fader2way(6, PAGE_MAIN, '/ch/07/mix/fader')
|
||||
fader2way(7, PAGE_MAIN, '/ch/09/mix/fader')
|
||||
|
||||
|
||||
// PAGE 2: Headphones
|
||||
fader2way(0, PAGE_HP, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_HP, '/ch/15/mix/01/level')
|
||||
fader2way(2, PAGE_HP, '/ch/16/mix/01/level')
|
||||
fader2way(3, PAGE_HP, '/ch/01/mix/01/level')
|
||||
fader2way(4, PAGE_HP, '/ch/03/mix/01/level')
|
||||
fader2way(5, PAGE_HP, '/ch/05/mix/01/level')
|
||||
fader2way(6, PAGE_HP, '/ch/07/mix/01/level')
|
||||
fader2way(7, PAGE_HP, '/ch/09/mix/01/level')
|
||||
// PAGE 2: Headphones
|
||||
fader2way(0, PAGE_HP, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_HP, '/ch/15/mix/01/level')
|
||||
fader2way(2, PAGE_HP, '/ch/16/mix/01/level')
|
||||
fader2way(3, PAGE_HP, '/ch/01/mix/01/level')
|
||||
fader2way(4, PAGE_HP, '/ch/03/mix/01/level')
|
||||
fader2way(5, PAGE_HP, '/ch/05/mix/01/level')
|
||||
fader2way(6, PAGE_HP, '/ch/07/mix/01/level')
|
||||
fader2way(7, PAGE_HP, '/ch/09/mix/01/level')
|
||||
|
||||
|
||||
// PAGE 3: PC
|
||||
fader2way(0, PAGE_PC, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_PC, '/ch/15/mix/03/level')
|
||||
fader2way(2, PAGE_PC, '/ch/16/mix/03/level')
|
||||
fader2way(3, PAGE_PC, '/ch/01/mix/03/level')
|
||||
fader2way(4, PAGE_PC, '/ch/03/mix/03/level')
|
||||
fader2way(5, PAGE_PC, '/ch/05/mix/03/level')
|
||||
fader2way(6, PAGE_PC, '/ch/07/mix/03/level')
|
||||
fader2way(7, PAGE_PC, '/ch/09/mix/03/level')
|
||||
// PAGE 3: PC
|
||||
fader2way(0, PAGE_PC, '/bus/1/mix/fader', OSC_LEVEL_0)
|
||||
fader2way(1, PAGE_PC, '/ch/15/mix/03/level')
|
||||
fader2way(2, PAGE_PC, '/ch/16/mix/03/level')
|
||||
fader2way(3, PAGE_PC, '/ch/01/mix/03/level')
|
||||
fader2way(4, PAGE_PC, '/ch/03/mix/03/level')
|
||||
fader2way(5, PAGE_PC, '/ch/05/mix/03/level')
|
||||
fader2way(6, PAGE_PC, '/ch/07/mix/03/level')
|
||||
fader2way(7, PAGE_PC, '/ch/09/mix/03/level')
|
||||
|
||||
|
||||
const button = (note: number) => mdev.addControl(MessageType.NoteOn, { channel: 0, note }, { feedbackInput: false, noStoreInput: true });
|
||||
const mutes = [16, 17, 18, 19, 20, 21, 22, 23].map(n => button(n));
|
||||
const button = (note: number) => mdev.addControl(MessageType.NoteOn, { channel: 0, note }, { feedbackInput: false, noStoreInput: true });
|
||||
const mutes = [16, 17, 18, 19, 20, 21, 22, 23].map(n => button(n));
|
||||
|
||||
// MUTE BUTTONS
|
||||
const buttonToggle = (control: MidiControl<MessageType.NoteOn> | undefined, page: number, addr: string) => {
|
||||
// MUTE BUTTONS
|
||||
const buttonToggle = (control: MidiControl<MessageType.NoteOn> | undefined, page: number, addr: string) => {
|
||||
if (!control) return;
|
||||
|
||||
control.addOutput(page, (d) => {
|
||||
|
|
@ -366,34 +367,38 @@ const buttonToggle = (control: MidiControl<MessageType.NoteOn> | undefined, page
|
|||
})
|
||||
|
||||
odev.sendNull(addr);
|
||||
}
|
||||
|
||||
// PAGE 1: Main
|
||||
buttonToggle(mutes[0], PAGE_MAIN, '/dca/1/on')
|
||||
buttonToggle(mutes[1], PAGE_MAIN, '/rtn/1/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_MAIN, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_MAIN, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_MAIN, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_MAIN, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_MAIN, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_MAIN, '/ch/09/mix/on')
|
||||
|
||||
// PAGE 2: Headphones
|
||||
buttonToggle(mutes[0], PAGE_HP, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[1], PAGE_HP, '/ch/15/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_HP, '/ch/16/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_HP, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_HP, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_HP, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_HP, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_HP, '/ch/09/mix/on')
|
||||
|
||||
// PAGE 3: PC
|
||||
buttonToggle(mutes[0], PAGE_PC, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[1], PAGE_PC, '/ch/15/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_PC, '/ch/16/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_PC, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_PC, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_PC, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_PC, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_PC, '/ch/09/mix/on')
|
||||
|
||||
} catch (e) {
|
||||
console.error('Uncaught error', e)
|
||||
}
|
||||
|
||||
// PAGE 1: Main
|
||||
buttonToggle(mutes[0], PAGE_MAIN, '/dca/1/on')
|
||||
buttonToggle(mutes[1], PAGE_MAIN, '/rtn/1/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_MAIN, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_MAIN, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_MAIN, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_MAIN, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_MAIN, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_MAIN, '/ch/09/mix/on')
|
||||
|
||||
// PAGE 2: Headphones
|
||||
buttonToggle(mutes[0], PAGE_HP, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[1], PAGE_HP, '/ch/15/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_HP, '/ch/16/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_HP, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_HP, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_HP, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_HP, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_HP, '/ch/09/mix/on')
|
||||
|
||||
// PAGE 3: PC
|
||||
buttonToggle(mutes[0], PAGE_PC, '/bus/1/mix/on')
|
||||
buttonToggle(mutes[1], PAGE_PC, '/ch/15/mix/on')
|
||||
buttonToggle(mutes[2], PAGE_PC, '/ch/16/mix/on')
|
||||
buttonToggle(mutes[3], PAGE_PC, '/ch/01/mix/on')
|
||||
buttonToggle(mutes[4], PAGE_PC, '/ch/03/mix/on')
|
||||
buttonToggle(mutes[5], PAGE_PC, '/ch/05/mix/on')
|
||||
buttonToggle(mutes[6], PAGE_PC, '/ch/07/mix/on')
|
||||
buttonToggle(mutes[7], PAGE_PC, '/ch/09/mix/on')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue