Emulator: Studio's stems and MIDI recorder (jiantEmu.tap) and RADIO (jiantEmu.gen) on the published page

The page and worklet built with the firmware (the public /emu/ and /preview/emu/) lacked the tap main's page has, so
JIANT Studio's RECORD input "JIANT emulator · stems (8 channels)" recorded nothing. The wasm already exports
web_stems, web_stem_out, web_midi_tap and web_midi_take: main's worklet.js taken whole (each call checked), the page's
jiantEmu.tap / jiantEmu.gen and their messages added. Checked headless: 8 stems with sound while a GEN piece plays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
This commit is contained in:
Claude committed 2026-12-04 16:50:42 +00:00
1 parent 8b042c54e7
commit 99f8d28907
2 files changed
+82 -2

No files matched your search

+20 -1
View File
@@ -493,7 +493,8 @@ async function midiOn() {
/* (studio) JIANT Studio's editor talks to this emulator as to an FM-1 (web/emu/editor-api.h): a frame in (sysex), the
* replies and the live pushes out to each listener; on() says whether the emulator runs */
const sysexListeners = new Set();
const sysexListeners = new Set(), tapListeners = new Set();
const genWait = new Map(); let genSeq = 0;
window.jiantEmu = {
on: () => !!node,
midiInputs: () => midiAccess ? [...midiAccess.inputs.values()].filter((p) => p.state !== "disconnected").map((p) => ({ id: p.id, name: p.name || p.id })) : [],
@@ -503,6 +504,22 @@ window.jiantEmu = {
},
sysex: (bytes) => { if (node) node.port.postMessage({ type: "sysex", data: Array.from(bytes) }); },
listen: (f) => { sysexListeners.add(f); return () => sysexListeners.delete(f); },
/* (studio) the recorder: f gets { at, planes: [T1..T6, FX L, FX R] (44.1 kHz), midi: [{ at, data }] } about every
93 ms while tapped; returns stop. The stems as MENU > USB STEMS sends them, MIDI as MIDI OUT does */
/* (studio RADIO) the generator: gen(cmd, arg, genres) -> { ok, info: [radio, genre, seed, mood, energy, form, piece,
playing], genres }; cmd null only reads. ok false: this firmware has no web_gen_info */
gen: (cmd = null, arg = 0, genres = false) => new Promise((res) => {
if (!node) { res({ ok: false }); return; }
const id = ++genSeq; genWait.set(id, res);
node.port.postMessage({ type: "gen", id, cmd, arg, genres });
setTimeout(() => { if (genWait.delete(id)) res({ ok: false }); }, 1500);
}),
tap: (f) => {
if (!node) return null;
tapListeners.add(f);
if (tapListeners.size === 1) node.port.postMessage({ type: "tap", on: true });
return () => { tapListeners.delete(f); if (!tapListeners.size && node) node.port.postMessage({ type: "tap", on: false }); };
},
};
// ------------------------------------------------------------------ switching on
@@ -535,6 +552,8 @@ async function switchOn() {
rej(new Error(m.message));
}
else if (m.type === "sysex") sysexListeners.forEach((f) => m.msgs.forEach(f));
else if (m.type === "tap-chunk") tapListeners.forEach((f) => f(m));
else if (m.type === "gen") { const r = genWait.get(m.id); if (r) { genWait.delete(m.id); r(m); } }
else if (m.type === "frame") {
if (m.sysex) sysexListeners.forEach((f) => m.sysex.forEach(f));
if (m.fb) drawScreen(m.fb);
+62 -1
View File
@@ -79,6 +79,21 @@ export function edServe(ex, req) {
const n = ex.web_ed(req.length);
return n ? Array.from(new Uint8Array(ex.memory.buffer, ex.web_ed_reply(), n)) : null;
}
/* (pm) the Project Manager's two device actions the editor protocol has no command for (pm-api.h): { pm: "save", n, name }
* (SAVE > PROJECT n of the music playing), { pm: "load", n } (PROJECT n opened), { pm: "cur" }: the reply [rc] (pm-api.h's
* codes; "cur": [the open project | 128 for a generator draft]), [255] when this build has neither */
export function pmServe(ex, q) {
if (q.pm === "cur") return ex.web_pm_cur ? [ex.web_pm_cur() & 255] : ex.web_song_cur ? [ex.web_song_cur() & 127] : [255];
if (q.pm === "load") return ex.web_pm_load ? [ex.web_pm_load(q.n >>> 0)] : [255];
if (q.pm === "save") {
if (!ex.web_pm_save) return [255];
const nm = new Uint8Array(ex.memory.buffer, ex.web_pm_name(), 13), s = String(q.name || "").slice(0, 12);
nm.fill(0);
for (let i = 0; i < s.length; i++) nm[i] = s.charCodeAt(i) & 127;
return [ex.web_pm_save(q.n >>> 0)];
}
return [255];
}
/* (studio) the editor's replies and pushes waiting in the device (editor-api.h), each a whole F0..F7 message */
export function sysexOut(ex) {
const out = [];
@@ -151,6 +166,13 @@ if (typeof registerProcessor === "function") {
else if (m.type === "enc") ex.web_enc(m.role, m.n | 0);
else if (m.type === "master") ex.web_master(m.value | 0);
else if (m.type === "midi") ex.web_midi(m.data[0] | 0, m.data[1] | 0, m.data[2] | 0);
else if (m.type === "tap") { // (studio) the stems and MIDI OUT to JIANT Studio's recorder
const on = !!m.on && !!ex.web_stems;
if (ex.web_stems) ex.web_stems(on ? 1 : 0);
if (ex.web_midi_tap) ex.web_midi_tap(on ? 1 : 0);
this.tap = on ? { planes: Array.from({ length: 8 }, () => new Float32Array(4096)), n: 0, at: this.pos, midi: [] } : null;
this.port.postMessage({ type: "tap", on, stems: !!ex.web_stems });
}
else if (m.type === "sysex") { // (studio) the web editor's frame (editor-api.h): its replies at once
if (!ex.web_sysex || !Array.isArray(m.data) || m.data.length > 640) return;
new Uint8Array(ex.memory.buffer, ex.web_sysex_buf(), m.data.length).set(m.data.map((v) => v & 255));
@@ -158,6 +180,26 @@ if (typeof registerProcessor === "function") {
const out = sysexOut(ex);
if (out.length) this.port.postMessage({ type: "sysex", msgs: out });
}
else if (m.type === "gen") { // (studio RADIO) the generator: a command (web_gen_cmd), then its state
let rc = null; // 10 RESOUND (arg: a seed 0..9999), 11 its undo; else web_gen_cmd
if (m.cmd === 10) rc = ex.web_gen_resound ? ex.web_gen_resound(m.arg >>> 0) : null;
else if (m.cmd === 11) rc = ex.web_gen_resound_undo ? ex.web_gen_resound_undo() : null;
else if (m.cmd != null && ex.web_gen_cmd) ex.web_gen_cmd(m.cmd >>> 0, m.arg | 0);
let info = null, genres = null;
if (ex.web_gen_info) {
const p = ex.web_gen_info(); // a static int32[8] of the firmware's
if (p) info = Array.from(new Int32Array(ex.memory.buffer, p, 8));
}
if (m.genres && ex.web_gen_genre) {
genres = [];
for (let i = 0, a; i < 64 && (a = ex.web_gen_genre(i)); i++) {
const b = new Uint8Array(ex.memory.buffer, a, 32), z = b.indexOf(0);
genres.push(String.fromCharCode(...b.subarray(0, z < 0 ? 32 : z)));
}
}
const rs = ex.web_gen_resound_seed ? ex.web_gen_resound_seed() >>> 0 : 0xFFFFFFFF;
this.port.postMessage({ type: "gen", id: m.id, ok: !!ex.web_gen_info, info, genres, rc, resound: !!ex.web_gen_resound, rs: rs === 0xFFFFFFFF ? null : rs });
}
else if (m.type === "visible") this.period = m.on ? 1 / 30 : 1 / 4;
else if (m.type === "meter") { // the output's level since the last ask (tests, the page)
this.metering = true;
@@ -170,7 +212,7 @@ if (typeof registerProcessor === "function") {
port.onmessage = (e) => {
const q = e.data || {}, x = this.ex;
let reply = null, error = "";
try { reply = x ? edServe(x, q.req) : null; } catch (err) { error = String(err && err.message || err); }
try { reply = !x ? null : q.pm ? pmServe(x, q) : edServe(x, q.req); } catch (err) { error = String(err && err.message || err); }
port.postMessage({ seq: q.seq, reply, error, version: x ? edVersion(x) : "" });
if (x) this.sentDraws = -1; // (the screen after a restore, at once)
};
@@ -257,6 +299,23 @@ if (typeof registerProcessor === "function") {
}
}
/* (studio) a block's stems (web_stem_out: T1..T6, FX L, FX R) and the MIDI OUT packets it sent (each at its sample),
posted about every 93 ms: { at: the first sample's number, planes, midi: [{ at, data }] } */
tapBlock(ex, n) {
const T = this.tap;
for (let c = 0; c < 8; c++) T.planes[c].set(new Float32Array(ex.memory.buffer, ex.web_stem_out(c), n), T.n);
for (let p; ex.web_midi_take && (p = ex.web_midi_take()) !== 0;) {
const cin = p & 15, len = cin === 0xC || cin === 0xD ? 2 : cin >= 0x8 && cin <= 0xE ? 3 : cin === 0xF ? 1 : 0;
if (len) T.midi.push({ at: this.pos, data: [(p >>> 8) & 255, (p >>> 16) & 255, (p >>> 24) & 255].slice(0, len) });
}
T.n += n;
if (T.n + 128 > 4096) {
const planes = T.planes.map((pl) => pl.slice(0, T.n));
this.port.postMessage({ type: "tap-chunk", at: T.at, planes, midi: T.midi }, planes.map((pl) => pl.buffer));
T.n = 0; T.at = this.pos + n; T.midi = [];
}
}
process(inputs, outputs) {
try { return this.renderBlock(inputs, outputs); }
catch (error) {
@@ -280,6 +339,8 @@ if (typeof registerProcessor === "function") {
}
const t0 = clock();
ex.web_render(n);
if (this.tap) this.tapBlock(ex, n);
this.pos = (this.pos || 0) + n;
this.busy += clock() - t0;
this.frames += n;
out[0].set(new Float32Array(ex.memory.buffer, ex.web_out_l(), n));