@m4l-jweb/bridge 0.1.0 → 0.3.0
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/package.json +3 -2
- package/src/index.ts +177 -29
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jaime Lopez (alienmind)
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@m4l-jweb/bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "m4l-jweb: the browser-side bridge connecting a device's web UI to Max for Live.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
".": "./src/index.ts"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"src"
|
|
19
|
+
"src",
|
|
20
|
+
"LICENSE"
|
|
20
21
|
],
|
|
21
22
|
"sideEffects": [
|
|
22
23
|
"./src/index.ts"
|
package/src/index.ts
CHANGED
|
@@ -21,15 +21,15 @@
|
|
|
21
21
|
type InletHandler = (...args: unknown[]) => void;
|
|
22
22
|
|
|
23
23
|
interface MaxGlobal {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
bindInlet: (name: string, fn: InletHandler) => void;
|
|
25
|
+
outlet: (...args: unknown[]) => void;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
declare global {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
29
|
+
interface Window {
|
|
30
|
+
max?: MaxGlobal;
|
|
31
|
+
maxSimulate?: (name: string, ...args: unknown[]) => void;
|
|
32
|
+
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
const handlers = new Map<string, InletHandler>();
|
|
@@ -37,24 +37,82 @@ const handlers = new Map<string, InletHandler>();
|
|
|
37
37
|
/** True inside a real [jweb] view; false in the browser dev shim. */
|
|
38
38
|
export const inJweb: boolean = typeof window !== "undefined" && !!window.max;
|
|
39
39
|
|
|
40
|
+
/* ------------------------------------------------------------------ *
|
|
41
|
+
* The message tap
|
|
42
|
+
*
|
|
43
|
+
* The bridge is the ONLY channel between the two halves of a device, which
|
|
44
|
+
* makes it the one place worth observing: tap it and you see the device's
|
|
45
|
+
* entire contract, live, in both directions. The dev harness renders this as a
|
|
46
|
+
* message log (see @m4l-jweb/surface/dev).
|
|
47
|
+
*
|
|
48
|
+
* It stays in the shipped bundle deliberately - an empty listener set costs a
|
|
49
|
+
* branch per message, and a device in Live can be inspected by binding a tap
|
|
50
|
+
* from the console. The harness UI is what gets tree-shaken out, not this.
|
|
51
|
+
* ------------------------------------------------------------------ */
|
|
52
|
+
|
|
53
|
+
/** One message crossing the bridge. `in` = device -> UI, `out` = UI -> device. */
|
|
54
|
+
export interface BridgeMessage {
|
|
55
|
+
direction: "in" | "out";
|
|
56
|
+
selector: string;
|
|
57
|
+
args: unknown[];
|
|
58
|
+
/** performance.now() at the moment it crossed. */
|
|
59
|
+
at: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
type Tap = (m: BridgeMessage) => void;
|
|
63
|
+
const taps = new Set<Tap>();
|
|
64
|
+
|
|
65
|
+
/** Observe every message crossing the bridge. Returns an unsubscribe function. */
|
|
66
|
+
export function tapMessages(fn: Tap): () => void {
|
|
67
|
+
taps.add(fn);
|
|
68
|
+
return () => taps.delete(fn);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function emit(direction: "in" | "out", selector: string, args: unknown[]): void {
|
|
72
|
+
if (!taps.size) return;
|
|
73
|
+
const at = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
74
|
+
for (const t of taps) t({ direction, selector, args, at });
|
|
75
|
+
}
|
|
76
|
+
|
|
40
77
|
/**
|
|
41
78
|
* Handle the Max message `name`. Bind every selector your device receives, and
|
|
42
79
|
* keep the names in one `protocol.ts` so both sides of the bridge agree.
|
|
43
80
|
*/
|
|
44
81
|
export function bindInlet(name: string, fn: InletHandler): void {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
82
|
+
// Wrap rather than register `fn` directly: the tap has to see inbound
|
|
83
|
+
// messages in a real [jweb] too, where Max calls the handler we hand it.
|
|
84
|
+
const tapped: InletHandler = (...args) => {
|
|
85
|
+
emit("in", name, args);
|
|
86
|
+
fn(...args);
|
|
87
|
+
};
|
|
88
|
+
handlers.set(name, tapped);
|
|
89
|
+
if (typeof window !== "undefined" && window.max) {
|
|
90
|
+
window.max.bindInlet(name, tapped);
|
|
91
|
+
}
|
|
49
92
|
}
|
|
50
93
|
|
|
51
94
|
/** Send a Max message: a selector word followed by its arguments. */
|
|
52
95
|
export function outlet(...args: unknown[]): void {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
96
|
+
emit("out", String(args[0]), args.slice(1));
|
|
97
|
+
if (typeof window !== "undefined" && window.max) {
|
|
98
|
+
window.max.outlet(...args);
|
|
99
|
+
} else {
|
|
100
|
+
console.debug("[m4l-jweb:outlet]", ...args);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Deliver a message to the bound handlers as if the device had sent it.
|
|
106
|
+
*
|
|
107
|
+
* This is what `window.maxSimulate` calls, exported so the dev harness can
|
|
108
|
+
* drive a device programmatically (a mock transport emitting `tick`/`tempo`).
|
|
109
|
+
* In a real [jweb] it still works - it does not reach Max, it only fakes an
|
|
110
|
+
* inbound message - so keep it to dev paths.
|
|
111
|
+
*/
|
|
112
|
+
export function simulate(name: string, ...args: unknown[]): void {
|
|
113
|
+
const fn = handlers.get(name);
|
|
114
|
+
if (fn) fn(...args);
|
|
115
|
+
else console.warn(`[m4l-jweb] no handler bound for "${name}"`);
|
|
58
116
|
}
|
|
59
117
|
|
|
60
118
|
/**
|
|
@@ -66,7 +124,101 @@ export function outlet(...args: unknown[]): void {
|
|
|
66
124
|
* binding, and treat the reply as the source of truth.
|
|
67
125
|
*/
|
|
68
126
|
export function uiReady(): void {
|
|
69
|
-
|
|
127
|
+
outlet("ui_ready");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ------------------------------------------------------------------ *
|
|
131
|
+
* The chain contract
|
|
132
|
+
*
|
|
133
|
+
* A chain in @m4l-jweb/build is library code, but until now the selectors for
|
|
134
|
+
* ADDRESSING one were not: every device re-declared `midinote` and `notein` by
|
|
135
|
+
* hand in its own protocol.ts, and a typo produced no error - just a message
|
|
136
|
+
* falling on the floor.
|
|
137
|
+
*
|
|
138
|
+
* These are the selectors the packaged chains own. Spread them into your
|
|
139
|
+
* device's protocol.ts so the name you send and the name the generated [route]
|
|
140
|
+
* matches come from ONE definition:
|
|
141
|
+
*
|
|
142
|
+
* export const IN = { ...CHAIN_IN, mode: "mode", build: "build" } as const;
|
|
143
|
+
* export const OUT = { ...CHAIN_OUT, ui_ready: "ui_ready" } as const;
|
|
144
|
+
* ------------------------------------------------------------------ */
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Selectors the packaged WRAPPER sends to every device, always. Spread these in
|
|
148
|
+
* too - they are not optional, and they are not yours to rename.
|
|
149
|
+
*/
|
|
150
|
+
export const DEVICE_IN = {
|
|
151
|
+
/** wrapper -> UI: the run mode, from the [js] object-box argument. */
|
|
152
|
+
mode: "mode",
|
|
153
|
+
/** wrapper -> UI: the build stamp, for the stale-install check. */
|
|
154
|
+
build: "build",
|
|
155
|
+
/** wrapper -> UI: transport state. args: `<playing 0|1> <beats>`. */
|
|
156
|
+
tick: "tick",
|
|
157
|
+
/** wrapper -> UI: Live's tempo in BPM. args: `<bpm>`. */
|
|
158
|
+
tempo: "tempo",
|
|
159
|
+
} as const;
|
|
160
|
+
|
|
161
|
+
/** Selectors the packaged chains SEND to the UI. Requires the `midiin` chain. */
|
|
162
|
+
export const CHAIN_IN = {
|
|
163
|
+
/** midiin -> UI: `notein <pitch> <velocity>`. Velocity 0 is a note-off. */
|
|
164
|
+
notein: "notein",
|
|
165
|
+
} as const;
|
|
166
|
+
|
|
167
|
+
/** Selectors the packaged chains RECEIVE from the UI. Requires the `midiout` chain. */
|
|
168
|
+
export const CHAIN_OUT = {
|
|
169
|
+
/** UI -> midiout: `midinote <pitch> <vel> <durMs> <chan> <delayMs>`. */
|
|
170
|
+
midinote: "midinote",
|
|
171
|
+
/** UI -> midiout: release every hanging note. */
|
|
172
|
+
flush: "flush",
|
|
173
|
+
} as const;
|
|
174
|
+
|
|
175
|
+
/** A note handed to the `midiout` chain. Max does the placing; you do the timing. */
|
|
176
|
+
export interface Note {
|
|
177
|
+
/** MIDI pitch, 0-127. */
|
|
178
|
+
pitch: number;
|
|
179
|
+
/** 1-127. Velocity 0 would be a note-off, which `makenote` already owns. */
|
|
180
|
+
velocity: number;
|
|
181
|
+
/** How long the note is held, in milliseconds. */
|
|
182
|
+
durationMs: number;
|
|
183
|
+
/** MIDI channel, 1-16. Default 1. */
|
|
184
|
+
channel?: number;
|
|
185
|
+
/** Lookahead: how far in the future to place the note. Default 0 (now). */
|
|
186
|
+
delayMs?: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Send one note to the `midiout` chain.
|
|
191
|
+
*
|
|
192
|
+
* Your app computes WHEN; Max places it. `delayMs` is the whole point: a
|
|
193
|
+
* sequencer decides on a 20 Hz transport tick that a note falls 80 ms from now,
|
|
194
|
+
* sends it with `delayMs: 80`, and [pipe] releases it on the scheduler at the
|
|
195
|
+
* right moment. The note lands with Max's timing, not the browser's.
|
|
196
|
+
*
|
|
197
|
+
* Named fields rather than five positional ints, because
|
|
198
|
+
* `outlet("midinote", 60, 100, 250, 1, 0)` is trivially easy to get subtly wrong
|
|
199
|
+
* and produces no error when you do.
|
|
200
|
+
*/
|
|
201
|
+
export function sendNote(note: Note): void {
|
|
202
|
+
outlet(CHAIN_OUT.midinote, note.pitch, note.velocity, note.durationMs, note.channel ?? 1, note.delayMs ?? 0);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Bind incoming MIDI. Note-offs are filtered out: `makenote` already owns the release. */
|
|
206
|
+
export function onNote(fn: (pitch: number, velocity: number) => void): void {
|
|
207
|
+
bindInlet(CHAIN_IN.notein, (pitch, velocity) => {
|
|
208
|
+
const v = Number(velocity);
|
|
209
|
+
if (v === 0) return;
|
|
210
|
+
fn(Number(pitch), v);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Release every note the chain is currently holding.
|
|
216
|
+
*
|
|
217
|
+
* Call this when your device stops. Notes are held by [makenote] on the Max
|
|
218
|
+
* side, so a UI that simply stops sending leaves them sounding forever.
|
|
219
|
+
*/
|
|
220
|
+
export function flushNotes(): void {
|
|
221
|
+
outlet(CHAIN_OUT.flush);
|
|
70
222
|
}
|
|
71
223
|
|
|
72
224
|
/**
|
|
@@ -76,16 +228,16 @@ export function uiReady(): void {
|
|
|
76
228
|
* These are UTF-8 safe: btoa alone throws on anything outside latin1.
|
|
77
229
|
*/
|
|
78
230
|
export function encodeBase64(s: string): string {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
231
|
+
const bytes = new TextEncoder().encode(s);
|
|
232
|
+
let binary = "";
|
|
233
|
+
for (const b of bytes) binary += String.fromCharCode(b);
|
|
234
|
+
return btoa(binary);
|
|
83
235
|
}
|
|
84
236
|
|
|
85
237
|
export function decodeBase64(s: string): string {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
238
|
+
const binary = atob(s);
|
|
239
|
+
const bytes = Uint8Array.from(binary, (c) => c.charCodeAt(0));
|
|
240
|
+
return new TextDecoder().decode(bytes);
|
|
89
241
|
}
|
|
90
242
|
|
|
91
243
|
/**
|
|
@@ -96,10 +248,6 @@ export function decodeBase64(s: string): string {
|
|
|
96
248
|
* maxSimulate("tick", 1, 4.25)
|
|
97
249
|
*/
|
|
98
250
|
if (typeof window !== "undefined" && !window.max) {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
if (fn) fn(...args);
|
|
102
|
-
else console.warn(`[m4l-jweb] no handler bound for "${name}"`);
|
|
103
|
-
};
|
|
104
|
-
console.info("[m4l-jweb] running outside Max. Drive the device with: maxSimulate('tempo', 128)");
|
|
251
|
+
window.maxSimulate = simulate;
|
|
252
|
+
console.info("[m4l-jweb] running outside Max. Drive the device with: maxSimulate('tempo', 128)");
|
|
105
253
|
}
|