diff --git a/docs/webapp/editor/index.html b/docs/webapp/editor/index.html index 967098e..9d36ccc 100644 --- a/docs/webapp/editor/index.html +++ b/docs/webapp/editor/index.html @@ -3713,6 +3713,22 @@ class SetGate { } clear() { this.m.clear(); } } +/* the FM-1's MIDI ports. Every FM-1 firmware has the same USB IDs, so Windows and macOS often keep the port name of the + * first firmware they saw ("X0X FM-1", "USB-MIDI"…): the names the installer accepts (fm1ota.js FM1_PORT, the same + * list), as in/out pairs of one name, "Felucca" / "SLOOP" first, then FM-1, then the generic names. connect() keeps the + * first pair that answers INFO, so a generic name of other gear is tried and left */ +const FM1_PORT = /fm-1|felucca|sloop|clipper|ota|composite|sinco|usb-midi/i; +function fm1Ports(access) { + const live = (p) => p.state !== "disconnected" && FM1_PORT.test(p.name || ""); + const ins = [...access.inputs.values()].filter(live), outs = [...access.outputs.values()].filter(live); + const rank = (n) => (/felucca|sloop/i.test(n) ? 0 : /fm-1/i.test(n) ? 1 : 2); + const pairs = []; + for (const input of ins) { + const output = outs.find((o) => o.name === input.name) || (outs.length === 1 ? outs[0] : null); + if (output) pairs.push({ input, output }); + } + return pairs.sort((a, b) => rank(a.input.name || "") - rank(b.input.name || "")); +} /*PROTO-END*/ /* ===================================================================== UI === */ @@ -4888,19 +4904,29 @@ async function connect() { } access.onstatechange = onState; } - const isF = (p) => /felucca/i.test(p.name || "") && p.state !== "disconnected"; - const input = [...access.inputs.values()].find(isF); - const output = [...access.outputs.values()].find(isF); - if (!input || !output) { sayK("nodevice"); return; } - const link = new Link((d) => output.send(d), { - timeout: 300, - onUnknown: (f) => console.debug("ignored reply", f), - onTimeout: () => sayK("noreply"), - onPush: (f) => onPush(f), - onGap: () => { rewatch = true; }, - }); - input.onmidimessage = (e) => link.receive(e.data); - if (input.open) await input.open().catch(() => {}); + const pairs = fm1Ports(access); + if (!pairs.length) { sayK("nodevice"); return; } + let link = null, input = null, output = null; + for (const pr of pairs) { /* the first pair that answers (a generic name may be other gear) */ + const l = new Link((d) => pr.output.send(d), { + timeout: 300, + onUnknown: (f) => console.debug("ignored reply", f), + onTimeout: () => sayK("noreply"), + onPush: (f) => onPush(f), + onGap: () => { rewatch = true; }, + }); + pr.input.onmidimessage = (e) => l.receive(e.data); + if (pr.input.open) await pr.input.open().catch(() => {}); + try { + await l.request(req.info(), { timeout: 500, retries: 3, quiet: true }); /* (the first frame can be lost) */ + link = l, input = pr.input, output = pr.output; + break; + } catch (e) { + l.close(); + pr.input.onmidimessage = null; + } + } + if (!link) { sayK(pairs.length ? "noreply" : "nodevice"); return; } dev = { link, input, output, info: null, pdesc: [], gdesc: [], names: [], dump: null, steps: [], slotUsed: [null, null, null, null], keys: null, watch: false, bank: null }; sets.clear(); /* (another FM-1, maybe) */ @@ -4941,7 +4967,7 @@ function disconnect(reasonKey = "disconnected") { function onState(e) { const p = e.port; - if (!p || !/felucca/i.test(p.name || "")) return; + if (!p || !FM1_PORT.test(p.name || "")) return; if (dev && p.state === "disconnected" && (p.id === dev.input.id || p.id === dev.output.id)) { disconnect("lost"); } else if (!dev && wantConnected && p.state === "connected" && p.type === "output") { diff --git a/docs/webapp/editor/sloop-editor.html b/docs/webapp/editor/sloop-editor.html index b5b29af..9336815 100644 --- a/docs/webapp/editor/sloop-editor.html +++ b/docs/webapp/editor/sloop-editor.html @@ -3711,6 +3711,22 @@ class SetGate { } clear() { this.m.clear(); } } +/* the FM-1's MIDI ports. Every FM-1 firmware has the same USB IDs, so Windows and macOS often keep the port name of the + * first firmware they saw ("X0X FM-1", "USB-MIDI"…): the names the installer accepts (fm1ota.js FM1_PORT, the same + * list), as in/out pairs of one name, "Felucca" / "SLOOP" first, then FM-1, then the generic names. connect() keeps the + * first pair that answers INFO, so a generic name of other gear is tried and left */ +const FM1_PORT = /fm-1|felucca|sloop|clipper|ota|composite|sinco|usb-midi/i; +function fm1Ports(access) { + const live = (p) => p.state !== "disconnected" && FM1_PORT.test(p.name || ""); + const ins = [...access.inputs.values()].filter(live), outs = [...access.outputs.values()].filter(live); + const rank = (n) => (/felucca|sloop/i.test(n) ? 0 : /fm-1/i.test(n) ? 1 : 2); + const pairs = []; + for (const input of ins) { + const output = outs.find((o) => o.name === input.name) || (outs.length === 1 ? outs[0] : null); + if (output) pairs.push({ input, output }); + } + return pairs.sort((a, b) => rank(a.input.name || "") - rank(b.input.name || "")); +} /*PROTO-END*/ /* ===================================================================== UI === */ @@ -4886,19 +4902,29 @@ async function connect() { } access.onstatechange = onState; } - const isF = (p) => /felucca/i.test(p.name || "") && p.state !== "disconnected"; - const input = [...access.inputs.values()].find(isF); - const output = [...access.outputs.values()].find(isF); - if (!input || !output) { sayK("nodevice"); return; } - const link = new Link((d) => output.send(d), { - timeout: 300, - onUnknown: (f) => console.debug("ignored reply", f), - onTimeout: () => sayK("noreply"), - onPush: (f) => onPush(f), - onGap: () => { rewatch = true; }, - }); - input.onmidimessage = (e) => link.receive(e.data); - if (input.open) await input.open().catch(() => {}); + const pairs = fm1Ports(access); + if (!pairs.length) { sayK("nodevice"); return; } + let link = null, input = null, output = null; + for (const pr of pairs) { /* the first pair that answers (a generic name may be other gear) */ + const l = new Link((d) => pr.output.send(d), { + timeout: 300, + onUnknown: (f) => console.debug("ignored reply", f), + onTimeout: () => sayK("noreply"), + onPush: (f) => onPush(f), + onGap: () => { rewatch = true; }, + }); + pr.input.onmidimessage = (e) => l.receive(e.data); + if (pr.input.open) await pr.input.open().catch(() => {}); + try { + await l.request(req.info(), { timeout: 500, retries: 3, quiet: true }); /* (the first frame can be lost) */ + link = l, input = pr.input, output = pr.output; + break; + } catch (e) { + l.close(); + pr.input.onmidimessage = null; + } + } + if (!link) { sayK(pairs.length ? "noreply" : "nodevice"); return; } dev = { link, input, output, info: null, pdesc: [], gdesc: [], names: [], dump: null, steps: [], slotUsed: [null, null, null, null], keys: null, watch: false, bank: null }; sets.clear(); /* (another FM-1, maybe) */ @@ -4939,7 +4965,7 @@ function disconnect(reasonKey = "disconnected") { function onState(e) { const p = e.port; - if (!p || !/felucca/i.test(p.name || "")) return; + if (!p || !FM1_PORT.test(p.name || "")) return; if (dev && p.state === "disconnected" && (p.id === dev.input.id || p.id === dev.output.id)) { disconnect("lost"); } else if (!dev && wantConnected && p.state === "connected" && p.type === "output") { diff --git a/docs/webapp/sw.js b/docs/webapp/sw.js index 50e7ea2..40b6d47 100644 --- a/docs/webapp/sw.js +++ b/docs/webapp/sw.js @@ -4,7 +4,7 @@ // https, so Web MIDI asks as usual (a downloaded file on Android opens as content://, where Chrome refuses MIDI). // Online the network always wins (the page as published, never an old one); the copy is only the fallback. // A new site (any file changed) is a new sw.js: the new copy replaces the old one. -const STAMP = "2.6-6403957414d4"; // version + a hash of the files +const STAMP = "2.6-ad5001bfbfbd"; // version + a hash of the files const CACHE = "sloop-site-" + STAMP; const FILES = ["../firmware/sloop-2.6.fwsc", "editor/", "editor/FUKIAI-LICENSE.txt", "editor/fukiai.ttf", "editor/preview-worklet.js", "editor/preview.js", "editor/sloop-preview.wasm", "icon-192.png", "icon-512.png", "icon-maskable-512.png", "installer/", "sloop.webmanifest"]; // relative to this file const KEEP = new Set(FILES.map((f) => new URL(f, self.location).href)); diff --git a/web/editor.html b/web/editor.html index 1404c11..681f7a3 100644 --- a/web/editor.html +++ b/web/editor.html @@ -3713,6 +3713,22 @@ class SetGate { } clear() { this.m.clear(); } } +/* the FM-1's MIDI ports. Every FM-1 firmware has the same USB IDs, so Windows and macOS often keep the port name of the + * first firmware they saw ("X0X FM-1", "USB-MIDI"…): the names the installer accepts (fm1ota.js FM1_PORT, the same + * list), as in/out pairs of one name, "Felucca" / "SLOOP" first, then FM-1, then the generic names. connect() keeps the + * first pair that answers INFO, so a generic name of other gear is tried and left */ +const FM1_PORT = /fm-1|felucca|sloop|clipper|ota|composite|sinco|usb-midi/i; +function fm1Ports(access) { + const live = (p) => p.state !== "disconnected" && FM1_PORT.test(p.name || ""); + const ins = [...access.inputs.values()].filter(live), outs = [...access.outputs.values()].filter(live); + const rank = (n) => (/felucca|sloop/i.test(n) ? 0 : /fm-1/i.test(n) ? 1 : 2); + const pairs = []; + for (const input of ins) { + const output = outs.find((o) => o.name === input.name) || (outs.length === 1 ? outs[0] : null); + if (output) pairs.push({ input, output }); + } + return pairs.sort((a, b) => rank(a.input.name || "") - rank(b.input.name || "")); +} /*PROTO-END*/ /* ===================================================================== UI === */ @@ -4888,19 +4904,29 @@ async function connect() { } access.onstatechange = onState; } - const isF = (p) => /felucca/i.test(p.name || "") && p.state !== "disconnected"; - const input = [...access.inputs.values()].find(isF); - const output = [...access.outputs.values()].find(isF); - if (!input || !output) { sayK("nodevice"); return; } - const link = new Link((d) => output.send(d), { - timeout: 300, - onUnknown: (f) => console.debug("ignored reply", f), - onTimeout: () => sayK("noreply"), - onPush: (f) => onPush(f), - onGap: () => { rewatch = true; }, - }); - input.onmidimessage = (e) => link.receive(e.data); - if (input.open) await input.open().catch(() => {}); + const pairs = fm1Ports(access); + if (!pairs.length) { sayK("nodevice"); return; } + let link = null, input = null, output = null; + for (const pr of pairs) { /* the first pair that answers (a generic name may be other gear) */ + const l = new Link((d) => pr.output.send(d), { + timeout: 300, + onUnknown: (f) => console.debug("ignored reply", f), + onTimeout: () => sayK("noreply"), + onPush: (f) => onPush(f), + onGap: () => { rewatch = true; }, + }); + pr.input.onmidimessage = (e) => l.receive(e.data); + if (pr.input.open) await pr.input.open().catch(() => {}); + try { + await l.request(req.info(), { timeout: 500, retries: 3, quiet: true }); /* (the first frame can be lost) */ + link = l, input = pr.input, output = pr.output; + break; + } catch (e) { + l.close(); + pr.input.onmidimessage = null; + } + } + if (!link) { sayK(pairs.length ? "noreply" : "nodevice"); return; } dev = { link, input, output, info: null, pdesc: [], gdesc: [], names: [], dump: null, steps: [], slotUsed: [null, null, null, null], keys: null, watch: false, bank: null }; sets.clear(); /* (another FM-1, maybe) */ @@ -4941,7 +4967,7 @@ function disconnect(reasonKey = "disconnected") { function onState(e) { const p = e.port; - if (!p || !/felucca/i.test(p.name || "")) return; + if (!p || !FM1_PORT.test(p.name || "")) return; if (dev && p.state === "disconnected" && (p.id === dev.input.id || p.id === dev.output.id)) { disconnect("lost"); } else if (!dev && wantConnected && p.state === "connected" && p.type === "output") { diff --git a/web/test_web.mjs b/web/test_web.mjs index e3291e3..ff41d34 100644 --- a/web/test_web.mjs +++ b/web/test_web.mjs @@ -34,7 +34,7 @@ const E = vm.runInNewContext(proto + ` CHOP, chopNovelty, chopHits, chopSnap, chopGrid, chopEqual, chopList, chopPick, chopFit, chopZones, wavFile, zipStore, crc32, UP, bank, capturePatch, auditionPatch, startWatch, libraryFile, readLibraryFile, paramKeys, patternFromSteps, stepsFromPattern, upName, mixer, GM_DRUM, drumName, parseNotes, fmtValue, F, DRUM_LANES, LV_NAMES, emptyDrum, lockable, MICRO, FC, fillGet, fillSet, pack7, unpack7, - loudDb, matchGains, withGain, LEVEL_MIN, backupCapture, backupRestore, backupObjects, b64enc, b64dec, FM6, KIT, kitPad, kitZones, kitLen, kitBytes, kitFit, kitSplit, kitLaneOf, kitPlace, zipRead, ROLL, SMF, ARR, mockSettings, fm6CartWrite, fm6CartPreset, fm6CartSlots, fm6CartPresets, DSYN, DSYN_MOCK_NAMES, DSYN_MOCK_KITS, SetGate, bkProjFormat, bkDevFormat, bkFwAtLeast })`, + loudDb, matchGains, withGain, LEVEL_MIN, backupCapture, backupRestore, backupObjects, b64enc, b64dec, FM6, KIT, kitPad, kitZones, kitLen, kitBytes, kitFit, kitSplit, kitLaneOf, kitPlace, zipRead, ROLL, SMF, ARR, mockSettings, fm6CartWrite, fm6CartPreset, fm6CartSlots, fm6CartPresets, DSYN, DSYN_MOCK_NAMES, DSYN_MOCK_KITS, SetGate, bkProjFormat, bkDevFormat, bkFwAtLeast, FM1_PORT_ED: FM1_PORT, fm1Ports })`, { setTimeout, clearTimeout, setInterval, clearInterval, console, TextEncoder, TextDecoder, Blob, Response, DecompressionStream }); async function editorMock() { @@ -1474,6 +1474,20 @@ async function updater() { ok(["FM-1", "Felucca", "Felucca Update", "Clipper", "SLOOP", "MIDIIN2 (Clipper)", "USB-MIDI"].every((n) => FM1_PORT.test(n)) && !["Arturia KeyStep Pro", "loopMIDI Port", "Microsoft GS Wavetable Synth"].some((n) => FM1_PORT.test(n)), "fm1ota.js: FM1_PORT takes the FM-1's names (SLOOP, CLIPPER, the loader, the stock firmware), not other gear"); + /* the editor finds the FM-1 by the installer's names (a port name kept from another firmware: "X0X FM-1") */ + ok(E.FM1_PORT_ED.source === FM1_PORT.source && E.FM1_PORT_ED.flags === FM1_PORT.flags, + "editor: its FM1_PORT is the installer's"); + const port = (name, type, state = "connected") => ({ id: `${type}-${name}`, name, type, state }); + const acc = (names, gone = []) => ({ + inputs: new Map([...names, ...gone].map((n) => [n, port(n, "input", gone.includes(n) ? "disconnected" : "connected")])), + outputs: new Map([...names, ...gone].map((n) => [n, port(n, "output", gone.includes(n) ? "disconnected" : "connected")])), + }); + const pn = (a) => E.fm1Ports(a).map((x) => x.input.name + "/" + x.output.name).join(","); + ok(pn(acc(["X0X FM-1"])) === "X0X FM-1/X0X FM-1", "editor: a port named after another firmware (X0X FM-1) is found"); + ok(pn(acc(["Arturia KeyStep Pro", "USB-MIDI", "X0X FM-1", "Felucca"], ["SLOOP"])) === + "Felucca/Felucca,X0X FM-1/X0X FM-1,USB-MIDI/USB-MIDI", + "editor: Felucca first, then FM-1, then generic names; other gear and unplugged ports left out"); + ok(pn(acc(["Arturia KeyStep Pro", "loopMIDI Port"])) === "", "editor: no FM-1 name: nothing to try"); } const stock = new FakeFM1(image); stock.boot("ota-FM-1_015", "FM-1 Update");