@tremolo-ui/dom 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/index.cjs +106 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +59 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
- package/src/index.ts +14 -0
- package/src/midi/access.ts +79 -0
- package/src/midi/input.ts +43 -0
- package/src/midi/message.ts +30 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 mimoz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/midi/access.ts
|
|
3
|
+
/** @private */
|
|
4
|
+
const PERMISSION_DENIED = "PERMISSION_DENIED";
|
|
5
|
+
/** @private */
|
|
6
|
+
const NOT_SUPPORTED = "NOT_SUPPORTED";
|
|
7
|
+
const INITIAL_STATE = {
|
|
8
|
+
midiAccess: null,
|
|
9
|
+
error: null
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Request MIDI access in the browser.
|
|
13
|
+
*
|
|
14
|
+
* The returned instance holds the state and notifies subscribers when it changes,
|
|
15
|
+
* so it can be consumed from any framework.
|
|
16
|
+
*/
|
|
17
|
+
function createMIDIAccess() {
|
|
18
|
+
let state = INITIAL_STATE;
|
|
19
|
+
let destroyed = false;
|
|
20
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
21
|
+
function setState(next) {
|
|
22
|
+
state = next;
|
|
23
|
+
for (const listener of listeners) listener();
|
|
24
|
+
}
|
|
25
|
+
function request() {
|
|
26
|
+
if (destroyed) return;
|
|
27
|
+
if (typeof navigator === "undefined" || !navigator.requestMIDIAccess) {
|
|
28
|
+
setState({
|
|
29
|
+
...state,
|
|
30
|
+
error: NOT_SUPPORTED
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
navigator.requestMIDIAccess().then((access) => {
|
|
35
|
+
if (destroyed) return;
|
|
36
|
+
setState({
|
|
37
|
+
midiAccess: access,
|
|
38
|
+
error: null
|
|
39
|
+
});
|
|
40
|
+
}).catch(() => {
|
|
41
|
+
if (destroyed) return;
|
|
42
|
+
setState({
|
|
43
|
+
...state,
|
|
44
|
+
error: PERMISSION_DENIED
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
request,
|
|
50
|
+
getState: () => state,
|
|
51
|
+
getServerState: () => INITIAL_STATE,
|
|
52
|
+
subscribe: (listener) => {
|
|
53
|
+
listeners.add(listener);
|
|
54
|
+
return () => {
|
|
55
|
+
listeners.delete(listener);
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
destroy: () => {
|
|
59
|
+
destroyed = true;
|
|
60
|
+
listeners.clear();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/midi/message.ts
|
|
66
|
+
/**
|
|
67
|
+
* Listen to raw `midimessage` events on every input of a MIDIAccess.
|
|
68
|
+
*
|
|
69
|
+
* Use this when you need more detail than {@link createMIDIInput} provides.
|
|
70
|
+
*/
|
|
71
|
+
function createMIDIMessage(midiAccess, onMIDIMessage) {
|
|
72
|
+
if (!midiAccess) return { destroy: () => {} };
|
|
73
|
+
const inputs = [...midiAccess.inputs.values()];
|
|
74
|
+
for (const input of inputs) input.addEventListener("midimessage", onMIDIMessage);
|
|
75
|
+
return { destroy: () => {
|
|
76
|
+
for (const input of inputs) input.removeEventListener("midimessage", onMIDIMessage);
|
|
77
|
+
} };
|
|
78
|
+
}
|
|
79
|
+
//#endregion
|
|
80
|
+
//#region src/midi/input.ts
|
|
81
|
+
const MIDI_EVENT_TO_NUMBER = {
|
|
82
|
+
NOTE_ON: 144,
|
|
83
|
+
NOTE_OFF: 128,
|
|
84
|
+
PITCH_BEND: 224
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.
|
|
88
|
+
* Internally uses {@link createMIDIMessage}.
|
|
89
|
+
*/
|
|
90
|
+
function createMIDIInput(midiAccess, { onNoteOnEvent, onNoteOffEvent, onPitchBendEvent }) {
|
|
91
|
+
return createMIDIMessage(midiAccess, (event) => {
|
|
92
|
+
if (!event.data) return;
|
|
93
|
+
const kind = event.data[0] & 240;
|
|
94
|
+
if (kind == MIDI_EVENT_TO_NUMBER.NOTE_OFF || kind == MIDI_EVENT_TO_NUMBER.NOTE_ON && event.data[2] == 0) onNoteOffEvent?.(event.data[1]);
|
|
95
|
+
else if (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON) onNoteOnEvent?.(event.data[1], event.data[2]);
|
|
96
|
+
else if (kind == MIDI_EVENT_TO_NUMBER.PITCH_BEND) onPitchBendEvent?.(event.data[1], event.data[2]);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.NOT_SUPPORTED = NOT_SUPPORTED;
|
|
101
|
+
exports.PERMISSION_DENIED = PERMISSION_DENIED;
|
|
102
|
+
exports.createMIDIAccess = createMIDIAccess;
|
|
103
|
+
exports.createMIDIInput = createMIDIInput;
|
|
104
|
+
exports.createMIDIMessage = createMIDIMessage;
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/midi/access.ts","../src/midi/message.ts","../src/midi/input.ts"],"sourcesContent":["/** @private */\nexport const PERMISSION_DENIED = 'PERMISSION_DENIED'\n/** @private */\nexport const NOT_SUPPORTED = 'NOT_SUPPORTED'\n\n/** @private */\nexport type MIDIAccessError = typeof PERMISSION_DENIED | typeof NOT_SUPPORTED\n\nexport type MIDIAccessState = {\n readonly midiAccess: MIDIAccess | null\n readonly error: MIDIAccessError | null\n}\n\nconst INITIAL_STATE: MIDIAccessState = {\n midiAccess: null,\n error: null,\n}\n\nexport interface MIDIAccessInstance {\n /** Request MIDI access. Safe to call more than once. */\n request: () => void\n getState: () => MIDIAccessState\n /** Snapshot for server side rendering. Always the initial state. */\n getServerState: () => MIDIAccessState\n /** @returns unsubscribe function */\n subscribe: (listener: () => void) => () => void\n destroy: () => void\n}\n\n/**\n * Request MIDI access in the browser.\n *\n * The returned instance holds the state and notifies subscribers when it changes,\n * so it can be consumed from any framework.\n */\nexport function createMIDIAccess(): MIDIAccessInstance {\n let state = INITIAL_STATE\n let destroyed = false\n const listeners = new Set<() => void>()\n\n function setState(next: MIDIAccessState) {\n state = next\n for (const listener of listeners) listener()\n }\n\n function request() {\n if (destroyed) return\n if (typeof navigator === 'undefined' || !navigator.requestMIDIAccess) {\n setState({ ...state, error: NOT_SUPPORTED })\n return\n }\n navigator\n .requestMIDIAccess()\n .then((access) => {\n if (destroyed) return\n setState({ midiAccess: access, error: null })\n })\n .catch(() => {\n if (destroyed) return\n setState({ ...state, error: PERMISSION_DENIED })\n })\n }\n\n return {\n request,\n getState: () => state,\n getServerState: () => INITIAL_STATE,\n subscribe: (listener) => {\n listeners.add(listener)\n return () => {\n listeners.delete(listener)\n }\n },\n destroy: () => {\n destroyed = true\n listeners.clear()\n },\n }\n}\n","export interface MIDIMessageInstance {\n destroy: () => void\n}\n\n/**\n * Listen to raw `midimessage` events on every input of a MIDIAccess.\n *\n * Use this when you need more detail than {@link createMIDIInput} provides.\n */\nexport function createMIDIMessage(\n midiAccess: MIDIAccess | null,\n onMIDIMessage: (event: MIDIMessageEvent) => void,\n): MIDIMessageInstance {\n if (!midiAccess) {\n return { destroy: () => {} }\n }\n\n const inputs = [...midiAccess.inputs.values()]\n for (const input of inputs) {\n input.addEventListener('midimessage', onMIDIMessage)\n }\n\n return {\n destroy: () => {\n for (const input of inputs) {\n input.removeEventListener('midimessage', onMIDIMessage)\n }\n },\n }\n}\n","import { createMIDIMessage, type MIDIMessageInstance } from './message'\n\nconst MIDI_EVENT_TO_NUMBER = {\n NOTE_ON: 0x90,\n NOTE_OFF: 0x80,\n PITCH_BEND: 0xe0,\n}\n\nexport type MIDIInputHandlers = {\n onNoteOnEvent?: (note: number, velocity: number) => void\n onNoteOffEvent?: (note: number) => void\n onPitchBendEvent?: (msb: number, lsb: number) => void\n}\n\nexport type MIDIInputInstance = MIDIMessageInstance\n\n/**\n * Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.\n * Internally uses {@link createMIDIMessage}.\n */\nexport function createMIDIInput(\n midiAccess: MIDIAccess | null,\n { onNoteOnEvent, onNoteOffEvent, onPitchBendEvent }: MIDIInputHandlers,\n): MIDIInputInstance {\n return createMIDIMessage(midiAccess, (event) => {\n if (!event.data) return\n // event.data[0] ... command\n // event.data[1] ... note, MSB (Most Significant Byte)\n // event.data[2] ... velocity, LSB (Least Significant Byte)\n const kind = event.data[0] & 0xf0\n\n if (\n kind == MIDI_EVENT_TO_NUMBER.NOTE_OFF ||\n (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON && event.data[2] == 0)\n ) {\n onNoteOffEvent?.(event.data[1])\n } else if (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON) {\n onNoteOnEvent?.(event.data[1], event.data[2])\n } else if (kind == MIDI_EVENT_TO_NUMBER.PITCH_BEND) {\n onPitchBendEvent?.(event.data[1], event.data[2])\n }\n })\n}\n"],"mappings":";;;AACA,MAAa,oBAAoB;;AAEjC,MAAa,gBAAgB;AAU7B,MAAM,gBAAiC;CACrC,YAAY;CACZ,OAAO;CACR;;;;;;;AAmBD,SAAgB,mBAAuC;CACrD,IAAI,QAAQ;CACZ,IAAI,YAAY;CAChB,MAAM,4BAAY,IAAI,KAAiB;CAEvC,SAAS,SAAS,MAAuB;AACvC,UAAQ;AACR,OAAK,MAAM,YAAY,UAAW,WAAU;;CAG9C,SAAS,UAAU;AACjB,MAAI,UAAW;AACf,MAAI,OAAO,cAAc,eAAe,CAAC,UAAU,mBAAmB;AACpE,YAAS;IAAE,GAAG;IAAO,OAAO;IAAe,CAAC;AAC5C;;AAEF,YACG,mBAAmB,CACnB,MAAM,WAAW;AAChB,OAAI,UAAW;AACf,YAAS;IAAE,YAAY;IAAQ,OAAO;IAAM,CAAC;IAC7C,CACD,YAAY;AACX,OAAI,UAAW;AACf,YAAS;IAAE,GAAG;IAAO,OAAO;IAAmB,CAAC;IAChD;;AAGN,QAAO;EACL;EACA,gBAAgB;EAChB,sBAAsB;EACtB,YAAY,aAAa;AACvB,aAAU,IAAI,SAAS;AACvB,gBAAa;AACX,cAAU,OAAO,SAAS;;;EAG9B,eAAe;AACb,eAAY;AACZ,aAAU,OAAO;;EAEpB;;;;;;;;;ACpEH,SAAgB,kBACd,YACA,eACqB;AACrB,KAAI,CAAC,WACH,QAAO,EAAE,eAAe,IAAI;CAG9B,MAAM,SAAS,CAAC,GAAG,WAAW,OAAO,QAAQ,CAAC;AAC9C,MAAK,MAAM,SAAS,OAClB,OAAM,iBAAiB,eAAe,cAAc;AAGtD,QAAO,EACL,eAAe;AACb,OAAK,MAAM,SAAS,OAClB,OAAM,oBAAoB,eAAe,cAAc;IAG5D;;;;AC1BH,MAAM,uBAAuB;CAC3B,SAAS;CACT,UAAU;CACV,YAAY;CACb;;;;;AAcD,SAAgB,gBACd,YACA,EAAE,eAAe,gBAAgB,oBACd;AACnB,QAAO,kBAAkB,aAAa,UAAU;AAC9C,MAAI,CAAC,MAAM,KAAM;EAIjB,MAAM,OAAO,MAAM,KAAK,KAAK;AAE7B,MACE,QAAQ,qBAAqB,YAC5B,QAAQ,qBAAqB,WAAW,MAAM,KAAK,MAAM,EAE1D,kBAAiB,MAAM,KAAK,GAAG;WACtB,QAAQ,qBAAqB,QACtC,iBAAgB,MAAM,KAAK,IAAI,MAAM,KAAK,GAAG;WACpC,QAAQ,qBAAqB,WACtC,oBAAmB,MAAM,KAAK,IAAI,MAAM,KAAK,GAAG;GAElD"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/midi/access.d.ts
|
|
2
|
+
/** @private */
|
|
3
|
+
declare const PERMISSION_DENIED = "PERMISSION_DENIED";
|
|
4
|
+
/** @private */
|
|
5
|
+
declare const NOT_SUPPORTED = "NOT_SUPPORTED";
|
|
6
|
+
/** @private */
|
|
7
|
+
type MIDIAccessError = typeof PERMISSION_DENIED | typeof NOT_SUPPORTED;
|
|
8
|
+
type MIDIAccessState = {
|
|
9
|
+
readonly midiAccess: MIDIAccess | null;
|
|
10
|
+
readonly error: MIDIAccessError | null;
|
|
11
|
+
};
|
|
12
|
+
interface MIDIAccessInstance {
|
|
13
|
+
/** Request MIDI access. Safe to call more than once. */
|
|
14
|
+
request: () => void;
|
|
15
|
+
getState: () => MIDIAccessState;
|
|
16
|
+
/** Snapshot for server side rendering. Always the initial state. */
|
|
17
|
+
getServerState: () => MIDIAccessState;
|
|
18
|
+
/** @returns unsubscribe function */
|
|
19
|
+
subscribe: (listener: () => void) => () => void;
|
|
20
|
+
destroy: () => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Request MIDI access in the browser.
|
|
24
|
+
*
|
|
25
|
+
* The returned instance holds the state and notifies subscribers when it changes,
|
|
26
|
+
* so it can be consumed from any framework.
|
|
27
|
+
*/
|
|
28
|
+
declare function createMIDIAccess(): MIDIAccessInstance;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/midi/message.d.ts
|
|
31
|
+
interface MIDIMessageInstance {
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Listen to raw `midimessage` events on every input of a MIDIAccess.
|
|
36
|
+
*
|
|
37
|
+
* Use this when you need more detail than {@link createMIDIInput} provides.
|
|
38
|
+
*/
|
|
39
|
+
declare function createMIDIMessage(midiAccess: MIDIAccess | null, onMIDIMessage: (event: MIDIMessageEvent) => void): MIDIMessageInstance;
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/midi/input.d.ts
|
|
42
|
+
type MIDIInputHandlers = {
|
|
43
|
+
onNoteOnEvent?: (note: number, velocity: number) => void;
|
|
44
|
+
onNoteOffEvent?: (note: number) => void;
|
|
45
|
+
onPitchBendEvent?: (msb: number, lsb: number) => void;
|
|
46
|
+
};
|
|
47
|
+
type MIDIInputInstance = MIDIMessageInstance;
|
|
48
|
+
/**
|
|
49
|
+
* Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.
|
|
50
|
+
* Internally uses {@link createMIDIMessage}.
|
|
51
|
+
*/
|
|
52
|
+
declare function createMIDIInput(midiAccess: MIDIAccess | null, {
|
|
53
|
+
onNoteOnEvent,
|
|
54
|
+
onNoteOffEvent,
|
|
55
|
+
onPitchBendEvent
|
|
56
|
+
}: MIDIInputHandlers): MIDIInputInstance;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { type MIDIAccessError, type MIDIAccessInstance, type MIDIAccessState, type MIDIInputHandlers, type MIDIInputInstance, type MIDIMessageInstance, NOT_SUPPORTED, PERMISSION_DENIED, createMIDIAccess, createMIDIInput, createMIDIMessage };
|
|
59
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/midi/access.ts","../src/midi/message.ts","../src/midi/input.ts"],"mappings":";;cACa,iBAAA;;cAEA,aAAA;;KAGD,eAAA,UAAyB,iBAAA,UAA2B,aAAA;AAAA,KAEpD,eAAA;EAAA,SACD,UAAA,EAAY,UAAA;EAAA,SACZ,KAAA,EAAO,eAAA;AAAA;AAAA,UAQD,kBAAA;EAZL;EAcV,OAAA;EACA,QAAA,QAAgB,eAAA;EAfmB;EAiBnC,cAAA,QAAsB,eAAA;EAfZ;EAiBV,SAAA,GAAY,QAAA;EACZ,OAAA;AAAA;;;;;;;iBASc,gBAAA,CAAA,GAAoB,kBAAA;;;UCnCnB,mBAAA;EACf,OAAA;AAAA;;;;ADEF;;iBCMgB,iBAAA,CACd,UAAA,EAAY,UAAA,SACZ,aAAA,GAAgB,KAAA,EAAO,gBAAA,YACtB,mBAAA;;;KCJS,iBAAA;EACV,aAAA,IAAiB,IAAA,UAAc,QAAA;EAC/B,cAAA,IAAkB,IAAA;EAClB,gBAAA,IAAoB,GAAA,UAAa,GAAA;AAAA;AAAA,KAGvB,iBAAA,GAAoB,mBAAA;;;;;iBAMhB,eAAA,CACd,UAAA,EAAY,UAAA;EACV,aAAA;EAAe,cAAA;EAAgB;AAAA,GAAoB,iBAAA,GACpD,iBAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//#region src/midi/access.d.ts
|
|
2
|
+
/** @private */
|
|
3
|
+
declare const PERMISSION_DENIED = "PERMISSION_DENIED";
|
|
4
|
+
/** @private */
|
|
5
|
+
declare const NOT_SUPPORTED = "NOT_SUPPORTED";
|
|
6
|
+
/** @private */
|
|
7
|
+
type MIDIAccessError = typeof PERMISSION_DENIED | typeof NOT_SUPPORTED;
|
|
8
|
+
type MIDIAccessState = {
|
|
9
|
+
readonly midiAccess: MIDIAccess | null;
|
|
10
|
+
readonly error: MIDIAccessError | null;
|
|
11
|
+
};
|
|
12
|
+
interface MIDIAccessInstance {
|
|
13
|
+
/** Request MIDI access. Safe to call more than once. */
|
|
14
|
+
request: () => void;
|
|
15
|
+
getState: () => MIDIAccessState;
|
|
16
|
+
/** Snapshot for server side rendering. Always the initial state. */
|
|
17
|
+
getServerState: () => MIDIAccessState;
|
|
18
|
+
/** @returns unsubscribe function */
|
|
19
|
+
subscribe: (listener: () => void) => () => void;
|
|
20
|
+
destroy: () => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Request MIDI access in the browser.
|
|
24
|
+
*
|
|
25
|
+
* The returned instance holds the state and notifies subscribers when it changes,
|
|
26
|
+
* so it can be consumed from any framework.
|
|
27
|
+
*/
|
|
28
|
+
declare function createMIDIAccess(): MIDIAccessInstance;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/midi/message.d.ts
|
|
31
|
+
interface MIDIMessageInstance {
|
|
32
|
+
destroy: () => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Listen to raw `midimessage` events on every input of a MIDIAccess.
|
|
36
|
+
*
|
|
37
|
+
* Use this when you need more detail than {@link createMIDIInput} provides.
|
|
38
|
+
*/
|
|
39
|
+
declare function createMIDIMessage(midiAccess: MIDIAccess | null, onMIDIMessage: (event: MIDIMessageEvent) => void): MIDIMessageInstance;
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/midi/input.d.ts
|
|
42
|
+
type MIDIInputHandlers = {
|
|
43
|
+
onNoteOnEvent?: (note: number, velocity: number) => void;
|
|
44
|
+
onNoteOffEvent?: (note: number) => void;
|
|
45
|
+
onPitchBendEvent?: (msb: number, lsb: number) => void;
|
|
46
|
+
};
|
|
47
|
+
type MIDIInputInstance = MIDIMessageInstance;
|
|
48
|
+
/**
|
|
49
|
+
* Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.
|
|
50
|
+
* Internally uses {@link createMIDIMessage}.
|
|
51
|
+
*/
|
|
52
|
+
declare function createMIDIInput(midiAccess: MIDIAccess | null, {
|
|
53
|
+
onNoteOnEvent,
|
|
54
|
+
onNoteOffEvent,
|
|
55
|
+
onPitchBendEvent
|
|
56
|
+
}: MIDIInputHandlers): MIDIInputInstance;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { type MIDIAccessError, type MIDIAccessInstance, type MIDIAccessState, type MIDIInputHandlers, type MIDIInputInstance, type MIDIMessageInstance, NOT_SUPPORTED, PERMISSION_DENIED, createMIDIAccess, createMIDIInput, createMIDIMessage };
|
|
59
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/midi/access.ts","../src/midi/message.ts","../src/midi/input.ts"],"mappings":";;cACa,iBAAA;;cAEA,aAAA;;KAGD,eAAA,UAAyB,iBAAA,UAA2B,aAAA;AAAA,KAEpD,eAAA;EAAA,SACD,UAAA,EAAY,UAAA;EAAA,SACZ,KAAA,EAAO,eAAA;AAAA;AAAA,UAQD,kBAAA;EAZL;EAcV,OAAA;EACA,QAAA,QAAgB,eAAA;EAfmB;EAiBnC,cAAA,QAAsB,eAAA;EAfZ;EAiBV,SAAA,GAAY,QAAA;EACZ,OAAA;AAAA;;;;;;;iBASc,gBAAA,CAAA,GAAoB,kBAAA;;;UCnCnB,mBAAA;EACf,OAAA;AAAA;;;;ADEF;;iBCMgB,iBAAA,CACd,UAAA,EAAY,UAAA,SACZ,aAAA,GAAgB,KAAA,EAAO,gBAAA,YACtB,mBAAA;;;KCJS,iBAAA;EACV,aAAA,IAAiB,IAAA,UAAc,QAAA;EAC/B,cAAA,IAAkB,IAAA;EAClB,gBAAA,IAAoB,GAAA,UAAa,GAAA;AAAA;AAAA,KAGvB,iBAAA,GAAoB,mBAAA;;;;;iBAMhB,eAAA,CACd,UAAA,EAAY,UAAA;EACV,aAAA;EAAe,cAAA;EAAgB;AAAA,GAAoB,iBAAA,GACpD,iBAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
//#region src/midi/access.ts
|
|
2
|
+
/** @private */
|
|
3
|
+
const PERMISSION_DENIED = "PERMISSION_DENIED";
|
|
4
|
+
/** @private */
|
|
5
|
+
const NOT_SUPPORTED = "NOT_SUPPORTED";
|
|
6
|
+
const INITIAL_STATE = {
|
|
7
|
+
midiAccess: null,
|
|
8
|
+
error: null
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Request MIDI access in the browser.
|
|
12
|
+
*
|
|
13
|
+
* The returned instance holds the state and notifies subscribers when it changes,
|
|
14
|
+
* so it can be consumed from any framework.
|
|
15
|
+
*/
|
|
16
|
+
function createMIDIAccess() {
|
|
17
|
+
let state = INITIAL_STATE;
|
|
18
|
+
let destroyed = false;
|
|
19
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
20
|
+
function setState(next) {
|
|
21
|
+
state = next;
|
|
22
|
+
for (const listener of listeners) listener();
|
|
23
|
+
}
|
|
24
|
+
function request() {
|
|
25
|
+
if (destroyed) return;
|
|
26
|
+
if (typeof navigator === "undefined" || !navigator.requestMIDIAccess) {
|
|
27
|
+
setState({
|
|
28
|
+
...state,
|
|
29
|
+
error: NOT_SUPPORTED
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
navigator.requestMIDIAccess().then((access) => {
|
|
34
|
+
if (destroyed) return;
|
|
35
|
+
setState({
|
|
36
|
+
midiAccess: access,
|
|
37
|
+
error: null
|
|
38
|
+
});
|
|
39
|
+
}).catch(() => {
|
|
40
|
+
if (destroyed) return;
|
|
41
|
+
setState({
|
|
42
|
+
...state,
|
|
43
|
+
error: PERMISSION_DENIED
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
request,
|
|
49
|
+
getState: () => state,
|
|
50
|
+
getServerState: () => INITIAL_STATE,
|
|
51
|
+
subscribe: (listener) => {
|
|
52
|
+
listeners.add(listener);
|
|
53
|
+
return () => {
|
|
54
|
+
listeners.delete(listener);
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
destroy: () => {
|
|
58
|
+
destroyed = true;
|
|
59
|
+
listeners.clear();
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/midi/message.ts
|
|
65
|
+
/**
|
|
66
|
+
* Listen to raw `midimessage` events on every input of a MIDIAccess.
|
|
67
|
+
*
|
|
68
|
+
* Use this when you need more detail than {@link createMIDIInput} provides.
|
|
69
|
+
*/
|
|
70
|
+
function createMIDIMessage(midiAccess, onMIDIMessage) {
|
|
71
|
+
if (!midiAccess) return { destroy: () => {} };
|
|
72
|
+
const inputs = [...midiAccess.inputs.values()];
|
|
73
|
+
for (const input of inputs) input.addEventListener("midimessage", onMIDIMessage);
|
|
74
|
+
return { destroy: () => {
|
|
75
|
+
for (const input of inputs) input.removeEventListener("midimessage", onMIDIMessage);
|
|
76
|
+
} };
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/midi/input.ts
|
|
80
|
+
const MIDI_EVENT_TO_NUMBER = {
|
|
81
|
+
NOTE_ON: 144,
|
|
82
|
+
NOTE_OFF: 128,
|
|
83
|
+
PITCH_BEND: 224
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.
|
|
87
|
+
* Internally uses {@link createMIDIMessage}.
|
|
88
|
+
*/
|
|
89
|
+
function createMIDIInput(midiAccess, { onNoteOnEvent, onNoteOffEvent, onPitchBendEvent }) {
|
|
90
|
+
return createMIDIMessage(midiAccess, (event) => {
|
|
91
|
+
if (!event.data) return;
|
|
92
|
+
const kind = event.data[0] & 240;
|
|
93
|
+
if (kind == MIDI_EVENT_TO_NUMBER.NOTE_OFF || kind == MIDI_EVENT_TO_NUMBER.NOTE_ON && event.data[2] == 0) onNoteOffEvent?.(event.data[1]);
|
|
94
|
+
else if (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON) onNoteOnEvent?.(event.data[1], event.data[2]);
|
|
95
|
+
else if (kind == MIDI_EVENT_TO_NUMBER.PITCH_BEND) onPitchBendEvent?.(event.data[1], event.data[2]);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { NOT_SUPPORTED, PERMISSION_DENIED, createMIDIAccess, createMIDIInput, createMIDIMessage };
|
|
100
|
+
|
|
101
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/midi/access.ts","../src/midi/message.ts","../src/midi/input.ts"],"sourcesContent":["/** @private */\nexport const PERMISSION_DENIED = 'PERMISSION_DENIED'\n/** @private */\nexport const NOT_SUPPORTED = 'NOT_SUPPORTED'\n\n/** @private */\nexport type MIDIAccessError = typeof PERMISSION_DENIED | typeof NOT_SUPPORTED\n\nexport type MIDIAccessState = {\n readonly midiAccess: MIDIAccess | null\n readonly error: MIDIAccessError | null\n}\n\nconst INITIAL_STATE: MIDIAccessState = {\n midiAccess: null,\n error: null,\n}\n\nexport interface MIDIAccessInstance {\n /** Request MIDI access. Safe to call more than once. */\n request: () => void\n getState: () => MIDIAccessState\n /** Snapshot for server side rendering. Always the initial state. */\n getServerState: () => MIDIAccessState\n /** @returns unsubscribe function */\n subscribe: (listener: () => void) => () => void\n destroy: () => void\n}\n\n/**\n * Request MIDI access in the browser.\n *\n * The returned instance holds the state and notifies subscribers when it changes,\n * so it can be consumed from any framework.\n */\nexport function createMIDIAccess(): MIDIAccessInstance {\n let state = INITIAL_STATE\n let destroyed = false\n const listeners = new Set<() => void>()\n\n function setState(next: MIDIAccessState) {\n state = next\n for (const listener of listeners) listener()\n }\n\n function request() {\n if (destroyed) return\n if (typeof navigator === 'undefined' || !navigator.requestMIDIAccess) {\n setState({ ...state, error: NOT_SUPPORTED })\n return\n }\n navigator\n .requestMIDIAccess()\n .then((access) => {\n if (destroyed) return\n setState({ midiAccess: access, error: null })\n })\n .catch(() => {\n if (destroyed) return\n setState({ ...state, error: PERMISSION_DENIED })\n })\n }\n\n return {\n request,\n getState: () => state,\n getServerState: () => INITIAL_STATE,\n subscribe: (listener) => {\n listeners.add(listener)\n return () => {\n listeners.delete(listener)\n }\n },\n destroy: () => {\n destroyed = true\n listeners.clear()\n },\n }\n}\n","export interface MIDIMessageInstance {\n destroy: () => void\n}\n\n/**\n * Listen to raw `midimessage` events on every input of a MIDIAccess.\n *\n * Use this when you need more detail than {@link createMIDIInput} provides.\n */\nexport function createMIDIMessage(\n midiAccess: MIDIAccess | null,\n onMIDIMessage: (event: MIDIMessageEvent) => void,\n): MIDIMessageInstance {\n if (!midiAccess) {\n return { destroy: () => {} }\n }\n\n const inputs = [...midiAccess.inputs.values()]\n for (const input of inputs) {\n input.addEventListener('midimessage', onMIDIMessage)\n }\n\n return {\n destroy: () => {\n for (const input of inputs) {\n input.removeEventListener('midimessage', onMIDIMessage)\n }\n },\n }\n}\n","import { createMIDIMessage, type MIDIMessageInstance } from './message'\n\nconst MIDI_EVENT_TO_NUMBER = {\n NOTE_ON: 0x90,\n NOTE_OFF: 0x80,\n PITCH_BEND: 0xe0,\n}\n\nexport type MIDIInputHandlers = {\n onNoteOnEvent?: (note: number, velocity: number) => void\n onNoteOffEvent?: (note: number) => void\n onPitchBendEvent?: (msb: number, lsb: number) => void\n}\n\nexport type MIDIInputInstance = MIDIMessageInstance\n\n/**\n * Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.\n * Internally uses {@link createMIDIMessage}.\n */\nexport function createMIDIInput(\n midiAccess: MIDIAccess | null,\n { onNoteOnEvent, onNoteOffEvent, onPitchBendEvent }: MIDIInputHandlers,\n): MIDIInputInstance {\n return createMIDIMessage(midiAccess, (event) => {\n if (!event.data) return\n // event.data[0] ... command\n // event.data[1] ... note, MSB (Most Significant Byte)\n // event.data[2] ... velocity, LSB (Least Significant Byte)\n const kind = event.data[0] & 0xf0\n\n if (\n kind == MIDI_EVENT_TO_NUMBER.NOTE_OFF ||\n (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON && event.data[2] == 0)\n ) {\n onNoteOffEvent?.(event.data[1])\n } else if (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON) {\n onNoteOnEvent?.(event.data[1], event.data[2])\n } else if (kind == MIDI_EVENT_TO_NUMBER.PITCH_BEND) {\n onPitchBendEvent?.(event.data[1], event.data[2])\n }\n })\n}\n"],"mappings":";;AACA,MAAa,oBAAoB;;AAEjC,MAAa,gBAAgB;AAU7B,MAAM,gBAAiC;CACrC,YAAY;CACZ,OAAO;CACR;;;;;;;AAmBD,SAAgB,mBAAuC;CACrD,IAAI,QAAQ;CACZ,IAAI,YAAY;CAChB,MAAM,4BAAY,IAAI,KAAiB;CAEvC,SAAS,SAAS,MAAuB;AACvC,UAAQ;AACR,OAAK,MAAM,YAAY,UAAW,WAAU;;CAG9C,SAAS,UAAU;AACjB,MAAI,UAAW;AACf,MAAI,OAAO,cAAc,eAAe,CAAC,UAAU,mBAAmB;AACpE,YAAS;IAAE,GAAG;IAAO,OAAO;IAAe,CAAC;AAC5C;;AAEF,YACG,mBAAmB,CACnB,MAAM,WAAW;AAChB,OAAI,UAAW;AACf,YAAS;IAAE,YAAY;IAAQ,OAAO;IAAM,CAAC;IAC7C,CACD,YAAY;AACX,OAAI,UAAW;AACf,YAAS;IAAE,GAAG;IAAO,OAAO;IAAmB,CAAC;IAChD;;AAGN,QAAO;EACL;EACA,gBAAgB;EAChB,sBAAsB;EACtB,YAAY,aAAa;AACvB,aAAU,IAAI,SAAS;AACvB,gBAAa;AACX,cAAU,OAAO,SAAS;;;EAG9B,eAAe;AACb,eAAY;AACZ,aAAU,OAAO;;EAEpB;;;;;;;;;ACpEH,SAAgB,kBACd,YACA,eACqB;AACrB,KAAI,CAAC,WACH,QAAO,EAAE,eAAe,IAAI;CAG9B,MAAM,SAAS,CAAC,GAAG,WAAW,OAAO,QAAQ,CAAC;AAC9C,MAAK,MAAM,SAAS,OAClB,OAAM,iBAAiB,eAAe,cAAc;AAGtD,QAAO,EACL,eAAe;AACb,OAAK,MAAM,SAAS,OAClB,OAAM,oBAAoB,eAAe,cAAc;IAG5D;;;;AC1BH,MAAM,uBAAuB;CAC3B,SAAS;CACT,UAAU;CACV,YAAY;CACb;;;;;AAcD,SAAgB,gBACd,YACA,EAAE,eAAe,gBAAgB,oBACd;AACnB,QAAO,kBAAkB,aAAa,UAAU;AAC9C,MAAI,CAAC,MAAM,KAAM;EAIjB,MAAM,OAAO,MAAM,KAAK,KAAK;AAE7B,MACE,QAAQ,qBAAqB,YAC5B,QAAQ,qBAAqB,WAAW,MAAM,KAAK,MAAM,EAE1D,kBAAiB,MAAM,KAAK,GAAG;WACtB,QAAQ,qBAAqB,QACtC,iBAAgB,MAAM,KAAK,IAAI,MAAM,KAAK,GAAG;WACpC,QAAQ,qBAAqB,WACtC,oBAAmB,MAAM,KAAK,IAAI,MAAM,KAAK,GAAG;GAElD"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tremolo-ui/dom",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "framework-agnostic DOM layer used in @tremolo-ui/*",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.cts",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "jest",
|
|
12
|
+
"typecheck": "tsc --noEmit",
|
|
13
|
+
"build:package": "tsc && tsdown",
|
|
14
|
+
"watch:package": "tsdown --watch",
|
|
15
|
+
"clean": "npm run clean:build",
|
|
16
|
+
"clean:build": "rimraf ./dist",
|
|
17
|
+
"lint": "eslint --fix --ext .ts,.tsx .",
|
|
18
|
+
"format": "prettier --write ."
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "mimoz",
|
|
29
|
+
"email": "contact.m1m0zzz@gmail.com"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"homepage": "https://github.com/m1m0zzz/tremolo-ui",
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/m1m0zzz/tremolo-ui.git",
|
|
36
|
+
"directory": "packages/dom"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"web-audio",
|
|
40
|
+
"midi",
|
|
41
|
+
"dom"
|
|
42
|
+
],
|
|
43
|
+
"exports": {
|
|
44
|
+
".": {
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/index.d.cts",
|
|
47
|
+
"default": "./dist/index.cjs"
|
|
48
|
+
},
|
|
49
|
+
"import": {
|
|
50
|
+
"types": "./dist/index.d.ts",
|
|
51
|
+
"default": "./dist/index.js"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createMIDIAccess,
|
|
3
|
+
NOT_SUPPORTED,
|
|
4
|
+
PERMISSION_DENIED,
|
|
5
|
+
type MIDIAccessError,
|
|
6
|
+
type MIDIAccessInstance,
|
|
7
|
+
type MIDIAccessState,
|
|
8
|
+
} from './midi/access'
|
|
9
|
+
export {
|
|
10
|
+
createMIDIInput,
|
|
11
|
+
type MIDIInputHandlers,
|
|
12
|
+
type MIDIInputInstance,
|
|
13
|
+
} from './midi/input'
|
|
14
|
+
export { createMIDIMessage, type MIDIMessageInstance } from './midi/message'
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/** @private */
|
|
2
|
+
export const PERMISSION_DENIED = 'PERMISSION_DENIED'
|
|
3
|
+
/** @private */
|
|
4
|
+
export const NOT_SUPPORTED = 'NOT_SUPPORTED'
|
|
5
|
+
|
|
6
|
+
/** @private */
|
|
7
|
+
export type MIDIAccessError = typeof PERMISSION_DENIED | typeof NOT_SUPPORTED
|
|
8
|
+
|
|
9
|
+
export type MIDIAccessState = {
|
|
10
|
+
readonly midiAccess: MIDIAccess | null
|
|
11
|
+
readonly error: MIDIAccessError | null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const INITIAL_STATE: MIDIAccessState = {
|
|
15
|
+
midiAccess: null,
|
|
16
|
+
error: null,
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface MIDIAccessInstance {
|
|
20
|
+
/** Request MIDI access. Safe to call more than once. */
|
|
21
|
+
request: () => void
|
|
22
|
+
getState: () => MIDIAccessState
|
|
23
|
+
/** Snapshot for server side rendering. Always the initial state. */
|
|
24
|
+
getServerState: () => MIDIAccessState
|
|
25
|
+
/** @returns unsubscribe function */
|
|
26
|
+
subscribe: (listener: () => void) => () => void
|
|
27
|
+
destroy: () => void
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Request MIDI access in the browser.
|
|
32
|
+
*
|
|
33
|
+
* The returned instance holds the state and notifies subscribers when it changes,
|
|
34
|
+
* so it can be consumed from any framework.
|
|
35
|
+
*/
|
|
36
|
+
export function createMIDIAccess(): MIDIAccessInstance {
|
|
37
|
+
let state = INITIAL_STATE
|
|
38
|
+
let destroyed = false
|
|
39
|
+
const listeners = new Set<() => void>()
|
|
40
|
+
|
|
41
|
+
function setState(next: MIDIAccessState) {
|
|
42
|
+
state = next
|
|
43
|
+
for (const listener of listeners) listener()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function request() {
|
|
47
|
+
if (destroyed) return
|
|
48
|
+
if (typeof navigator === 'undefined' || !navigator.requestMIDIAccess) {
|
|
49
|
+
setState({ ...state, error: NOT_SUPPORTED })
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
navigator
|
|
53
|
+
.requestMIDIAccess()
|
|
54
|
+
.then((access) => {
|
|
55
|
+
if (destroyed) return
|
|
56
|
+
setState({ midiAccess: access, error: null })
|
|
57
|
+
})
|
|
58
|
+
.catch(() => {
|
|
59
|
+
if (destroyed) return
|
|
60
|
+
setState({ ...state, error: PERMISSION_DENIED })
|
|
61
|
+
})
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
request,
|
|
66
|
+
getState: () => state,
|
|
67
|
+
getServerState: () => INITIAL_STATE,
|
|
68
|
+
subscribe: (listener) => {
|
|
69
|
+
listeners.add(listener)
|
|
70
|
+
return () => {
|
|
71
|
+
listeners.delete(listener)
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
destroy: () => {
|
|
75
|
+
destroyed = true
|
|
76
|
+
listeners.clear()
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { createMIDIMessage, type MIDIMessageInstance } from './message'
|
|
2
|
+
|
|
3
|
+
const MIDI_EVENT_TO_NUMBER = {
|
|
4
|
+
NOTE_ON: 0x90,
|
|
5
|
+
NOTE_OFF: 0x80,
|
|
6
|
+
PITCH_BEND: 0xe0,
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type MIDIInputHandlers = {
|
|
10
|
+
onNoteOnEvent?: (note: number, velocity: number) => void
|
|
11
|
+
onNoteOffEvent?: (note: number) => void
|
|
12
|
+
onPitchBendEvent?: (msb: number, lsb: number) => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type MIDIInputInstance = MIDIMessageInstance
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Handle note on/off and pitch bend events. To be used with {@link createMIDIAccess}.
|
|
19
|
+
* Internally uses {@link createMIDIMessage}.
|
|
20
|
+
*/
|
|
21
|
+
export function createMIDIInput(
|
|
22
|
+
midiAccess: MIDIAccess | null,
|
|
23
|
+
{ onNoteOnEvent, onNoteOffEvent, onPitchBendEvent }: MIDIInputHandlers,
|
|
24
|
+
): MIDIInputInstance {
|
|
25
|
+
return createMIDIMessage(midiAccess, (event) => {
|
|
26
|
+
if (!event.data) return
|
|
27
|
+
// event.data[0] ... command
|
|
28
|
+
// event.data[1] ... note, MSB (Most Significant Byte)
|
|
29
|
+
// event.data[2] ... velocity, LSB (Least Significant Byte)
|
|
30
|
+
const kind = event.data[0] & 0xf0
|
|
31
|
+
|
|
32
|
+
if (
|
|
33
|
+
kind == MIDI_EVENT_TO_NUMBER.NOTE_OFF ||
|
|
34
|
+
(kind == MIDI_EVENT_TO_NUMBER.NOTE_ON && event.data[2] == 0)
|
|
35
|
+
) {
|
|
36
|
+
onNoteOffEvent?.(event.data[1])
|
|
37
|
+
} else if (kind == MIDI_EVENT_TO_NUMBER.NOTE_ON) {
|
|
38
|
+
onNoteOnEvent?.(event.data[1], event.data[2])
|
|
39
|
+
} else if (kind == MIDI_EVENT_TO_NUMBER.PITCH_BEND) {
|
|
40
|
+
onPitchBendEvent?.(event.data[1], event.data[2])
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface MIDIMessageInstance {
|
|
2
|
+
destroy: () => void
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Listen to raw `midimessage` events on every input of a MIDIAccess.
|
|
7
|
+
*
|
|
8
|
+
* Use this when you need more detail than {@link createMIDIInput} provides.
|
|
9
|
+
*/
|
|
10
|
+
export function createMIDIMessage(
|
|
11
|
+
midiAccess: MIDIAccess | null,
|
|
12
|
+
onMIDIMessage: (event: MIDIMessageEvent) => void,
|
|
13
|
+
): MIDIMessageInstance {
|
|
14
|
+
if (!midiAccess) {
|
|
15
|
+
return { destroy: () => {} }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const inputs = [...midiAccess.inputs.values()]
|
|
19
|
+
for (const input of inputs) {
|
|
20
|
+
input.addEventListener('midimessage', onMIDIMessage)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
destroy: () => {
|
|
25
|
+
for (const input of inputs) {
|
|
26
|
+
input.removeEventListener('midimessage', onMIDIMessage)
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
}
|