diff --git a/.github/workflows/emulator-beta.yml b/.github/workflows/emulator-beta.yml index 1c7164e3..8effda49 100644 --- a/.github/workflows/emulator-beta.yml +++ b/.github/workflows/emulator-beta.yml @@ -75,8 +75,14 @@ jobs: cat web/emu/additive-api.h >> build/emusrc/web/emu/felucca_web.c cp web/emu/transfer-api.h build/emusrc/web/emu/transfer-api.h cat web/emu/transfer-api.h >> build/emusrc/web/emu/felucca_web.c + if grep -q "void wt_boot(void)" build/emusrc/firmware/src/editor_wt.c 2>/dev/null; then # (WAVE) wt-api.h + cp web/emu/wt-api.h build/emusrc/web/emu/wt-api.h + cat web/emu/wt-api.h >> build/emusrc/web/emu/felucca_web.c + WT=1 + fi (cd build/emusrc && sh web/emu/build.sh) node web/emu/additive-boot-test.mjs build/emusrc/build/emu/felucca.wasm + if [ "${WT:-0}" = 1 ]; then node web/emu/wavetable-test.mjs build/emusrc/build/emu/felucca.wasm; fi - name: Replace only emu/ run: | mkdir -p build/pages diff --git a/.github/workflows/emulator.yml b/.github/workflows/emulator.yml index 7ed05ced..95000352 100644 --- a/.github/workflows/emulator.yml +++ b/.github/workflows/emulator.yml @@ -45,6 +45,7 @@ jobs: run: | E=$(tr -d ' \n' < web/EMULATOR_EDITION) P=$(tr -d ' \n' < web/EMULATOR 2>/dev/null || tr -d ' \n' < web/PREVIEW) + WT=0 if [ -n "$P" ] && git cat-file -e "$P^{commit}" 2>/dev/null; then rm -rf /tmp/emusrc; git worktree add -f --detach /tmp/emusrc "$P" if [ "$E" = ADDITIVE ]; then @@ -59,6 +60,12 @@ jobs: cp web/emu/pm-api.h /tmp/emusrc/web/emu/pm-api.h cat web/emu/pm-api.h >> /tmp/emusrc/web/emu/felucca_web.c fi + # (WAVE) the user wavetables checked at power-on as the FM-1 does (wt-api.h); a pin before WAVE has no editor_wt.c + if grep -q "void wt_boot(void)" /tmp/emusrc/firmware/src/editor_wt.c 2>/dev/null; then + cp web/emu/wt-api.h /tmp/emusrc/web/emu/wt-api.h + cat web/emu/wt-api.h >> /tmp/emusrc/web/emu/felucca_web.c + WT=1 + fi # (V10.8.72) the pin is the public release (web/STABLE): no "(preview)" mark, and its SOURCE_COMMIT PV=1; [ "$P" = "$(tr -d ' \n' < web/STABLE)" ] && PV=0 if (cd /tmp/emusrc && python3 -c "import sys; sys.path.insert(0, 'tools'); import build; build.generate()" \ @@ -90,6 +97,9 @@ jobs: if [ "$E" = ADDITIVE ]; then node web/emu/additive-boot-test.mjs build/emu/felucca.wasm fi + if [ "$WT" = 1 ]; then # (WAVE) Studio's user wavetables: uploaded, listed, played, kept (wt-api.h) + node web/emu/wavetable-test.mjs build/emu/felucca.wasm + fi # (ports) devices.html is this tree's (above), so is its MPC panel's firmware: felucca_hp.wasm (the big CPUs' # build: SAMPLE, GRAIN, the slots), built here whatever web/EMULATOR pins; felucca.wasm stays the pinned one EMU_ONLY=hp sh web/emu/build.sh diff --git a/web/emu/browser-additive-test.py b/web/emu/browser-additive-test.py index 9840e66d..22bd367d 100644 --- a/web/emu/browser-additive-test.py +++ b/web/emu/browser-additive-test.py @@ -30,12 +30,24 @@ try: page.locator('[role="slider"][aria-label="SELECT"]').focus() for _ in range(12): # (to the chain's last page whatever its length: SELECT stops there; V10.7.23 needs 7, later 6) page.keyboard.press('ArrowRight');page.wait_for_timeout(150) - page.locator('[role="slider"][aria-label="KNOB 2"]').focus() - page.keyboard.press('ArrowLeft');page.wait_for_timeout(150) # (V10.54: INPUT is the last engine, ADD before it) - page.keyboard.press('ArrowLeft');page.wait_for_timeout(150) - page.locator('[role="slider"][aria-label="KNOB 2"]').evaluate('(el)=>el.blur()') - page.evaluate("__node.port.postMessage({type:'diagnostics'})") + # KNOB 2 steps back through the engines one detent at a time until the firmware itself reports ADD (13): + # the display order's tail moves between releases (V10.54: .. ADD INPUT; V10.8.87: .. STRING ADD WAVE INPUT), + # so the test asks the firmware after each step instead of counting detents. 20 steps cover the whole list + # (it wraps); every engine the knob passes is recorded so a failure says where it went. + knob=page.locator('[role="slider"][aria-label="KNOB 2"]');seen=[] + def diag(): + page.evaluate("window.__edition=null;__node.port.postMessage({type:'diagnostics'})") + page.wait_for_function('window.__edition',timeout=5000) + return page.evaluate('__edition') + for _ in range(20): + knob.focus();page.keyboard.press('ArrowLeft');page.wait_for_timeout(150) + d=diag();seen.append(d['engine']) + if d['engine']==13: break + knob.evaluate('(el)=>el.blur()') + assert seen[-1]==13,f'KNOB 2 on PRESETS never reached ADD (13); engines seen: {seen}' + assert len(set(seen))>1,f'KNOB 2 did not change the engine (not on PRESETS?): {seen}' page.wait_for_function('window.__edition && __edition.additive===true && __edition.engine===13',timeout=5000) + print('engines stepped through on PRESETS:',seen,flush=True) page.evaluate("""() => {const a=__node.context.createAnalyser();a.fftSize=512;__node.connect(a);window.__analyser=a;}""") page.keyboard.down('b');peak=0 for _ in range(12): diff --git a/web/emu/wavetable-test.mjs b/web/emu/wavetable-test.mjs new file mode 100644 index 00000000..958f3a6e --- /dev/null +++ b/web/emu/wavetable-test.mjs @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) +// (WAVE, editor protocol 1.7) the user wavetables in the emulator's wasm, as JIANT Studio uses them: a .jwt uploaded +// through WT_BEGIN / WT_DATA / WT_END (102..104, web_ed), listed (WT_LIST 101), read back (WT_GET 106), played by +// WAVE from U1 (it must sound, and not as SINE), and still there after a reboot with the saved flash (the page's +// storage). Run as worklet.js runs the device: web_wt_boot after web_boot, web_wt_sync before each render (wt-api.h). +// node web/emu/wavetable-test.mjs build/emu/felucca.wasm +import fs from 'node:fs'; +import assert from 'node:assert/strict'; +const bytes = fs.readFileSync(process.argv[2]); +async function boot(saved) { + const { instance } = await WebAssembly.instantiate(bytes, {}), e = instance.exports; + e._initialize(); e.web_nor_erase(); + if (saved) new Uint8Array(e.memory.buffer, e.web_nor(), e.web_nor_size()).set(saved); + e.web_seed(12345); e.web_boot(); + assert.ok(e.web_wt_boot && e.web_wt_sync, 'no wt-api.h in this build'); + e.web_wt_boot(); return e; +} +function render(e, ms) { + const out = []; + for (let n = Math.round(ms * 44.1); n > 0; n -= 128) { + const f = Math.min(128, n); e.web_wt_sync(); e.web_render(f); + for (const x of new Float32Array(e.memory.buffer, e.web_out_l(), f)) { assert.ok(Number.isFinite(x)); out.push(x); } + } + return out; +} +function ed(e, args) { + new Uint8Array(e.memory.buffer).set(args, e.web_ed_buf()); + const n = e.web_ed(args.length); + return [...new Uint8Array(e.memory.buffer, e.web_ed_reply(), n)]; +} +const u32 = (v) => [0, 7, 14, 21, 28].map((s) => (v >>> s) & 127); +const r32 = (a, i) => (a[i] | a[i + 1] << 7 | a[i + 2] << 14 | a[i + 3] << 21 | a[i + 4] << 28) >>> 0; +const v14 = (v) => [(v + 8192) & 127, (v + 8192) >> 7]; +const rv14 = (a, i) => (a[i] | a[i + 1] << 7) - 8192; +function pack7(b) { + const o = []; + for (let i = 0; i < b.length; i += 7) { + const g = b.slice(i, i + 7); let m = 0; + g.forEach((x, j) => { m |= (x >> 7) << j; }); o.push(m, ...g.map((x) => x & 127)); + } + return o; +} +const T = Array.from({ length: 256 }, (_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; }); +const crc = (b) => { let c = 0xFFFFFFFF; for (const x of b) c = T[(c ^ x) & 255] ^ (c >>> 8); return (c ^ 0xFFFFFFFF) >>> 0; }; +const le32 = (h, at, v) => { for (let k = 0; k < 4; k++) h[at + k] = (v >>> (8 * k)) & 255; }; + +const e = await boot(); render(e, 3000); +// INFO: the version, NENGINES, P_COUNT, G_COUNT, NSTEP, P_E0, the engines' names +const info = ed(e, [1]); let i = info.indexOf(0); const ver = String.fromCharCode(...info.slice(0, i)); i++; +const NENG = info[i], PE0 = info[i + 4]; i += 5; const names = []; +for (let k = 0; k < NENG; k++) { const j = info.indexOf(0, i); names.push(String.fromCharCode(...info.slice(i, j))); i = j + 1; } +const WAVE = names.indexOf('WAVE'); assert.ok(WAVE >= 0, 'no WAVE engine: ' + names.join(' ')); + +// a .jwt v1: 8 frames of a square, 8-bit LINEAR, named SQTEST +const F = 8, codes = []; +for (let f = 0; f < F; f++) for (let p = 0; p < 128; p++) codes.push(p < 64 ? 110 : 256 - 110); +const h = new Array(256).fill(0); +[0x4A, 0x57, 0x54, 0x31].forEach((x, k) => { h[k] = x; }); +h[4] = 1; h[5] = F; h[6] = 7; h[7] = 8; +[...'SQTEST'].forEach((c, k) => { h[12 + k] = c.charCodeAt(0); }); +for (let k = 0; k < 64; k++) h[164 + k] = 255; +le32(h, 28, codes.length); const id = crc([...h.slice(32), ...codes]); le32(h, 24, id); +const rec = [...h, ...codes]; + +let L = ed(e, [101]); +assert.deepEqual(L.slice(0, 5), [1, 0, 6, 6, 6], 'WT_LIST on a blank flash: ' + L.slice(0, 5)); +let r = ed(e, [102, 0, ...u32(rec.length), ...u32(crc(rec))]); assert.deepEqual(r.slice(0, 2), [0, 0], 'WT_BEGIN ' + r); +for (let o = 0; o < rec.length; o += 128) { + r = ed(e, [103, 0, ...u32(o), ...pack7(rec.slice(o, o + 128))]); + assert.equal(r[1], 0, `WT_DATA at ${o}: rc ${r[1]}`); assert.equal(r32(r, 2), o + 128); +} +r = ed(e, [104, 0]); assert.equal(r[1], 0, 'WT_END rc ' + r[1]); assert.equal(r32(r, 2), id, 'WT_END id'); +const listed = (L) => ({ state: L[5], frames: L[6], bits: L[7], used: L[10], id: r32(L, 13), name: String.fromCharCode(...L.slice(18, 30)).replace(/\0+$/, '') }); +let u1 = listed(ed(e, [101])); +assert.deepEqual([u1.state, u1.frames, u1.bits, u1.id, u1.name], [1, F, 8, id, 'SQTEST'], 'WT_LIST U1 ' + JSON.stringify(u1)); +r = ed(e, [106, 0, ...u32(256), 64, 0]); // WT_GET: the first 64 codes back +assert.equal(r[1], 0, 'WT_GET rc ' + r[1]); + +// WAVE on the selected track: the same note with A = SINE (0), then U1 (16) +function note(e, a) { + const s = ed(e, [3, 0, PE0, ...v14(a)]); assert.equal(rv14(s, 2), a, `SET A ${a}: ${rv14(s, 2)}`); + render(e, 300); e.web_midi(0x90, 57, 110); const x = render(e, 400).slice(4410); e.web_midi(0x80, 57, 0); render(e, 1500); + return x; +} +const peak = (x) => Math.max(...x.map(Math.abs)); +const apart = (a, b) => a.reduce((s, v, k) => s + Math.abs(v - b[k]), 0) / a.length; +r = ed(e, [8, WAVE, 0]); assert.equal(r[0], WAVE, 'PRESET WAVE'); +const sine = note(e, 0), user = note(e, 16); +u1 = listed(ed(e, [101])); assert.equal(u1.used, 1, 'U1 not named by the music'); +assert.ok(peak(sine) > 1e-3, 'WAVE SINE silent'); assert.ok(peak(user) > 1e-3, 'WAVE U1 silent: ' + peak(user)); +assert.ok(apart(sine, user) > 1e-3, 'WAVE U1 sounds as SINE'); + +// a reboot with the flash as saved: the table listed and played again +const saved = new Uint8Array(e.memory.buffer, e.web_nor(), e.web_nor_size()).slice(); +const e2 = await boot(saved); render(e2, 3000); +u1 = listed(ed(e2, [101])); assert.deepEqual([u1.state, u1.id, u1.name], [1, id, 'SQTEST'], 'U1 after the reboot ' + JSON.stringify(u1)); +ed(e2, [8, WAVE, 0]); const again = note(e2, 16); +assert.ok(peak(again) > 1e-3, 'WAVE U1 silent after the reboot'); +assert.ok(apart(sine, again) > 1e-3, 'WAVE U1 sounds as SINE after the reboot'); +console.log(`PASS ${ver} WAVE user tables: U1 uploaded (101..104), listed, read back (106), played (peak ${peak(user).toFixed(3)}, ` + + `SINE ${peak(sine).toFixed(3)}, apart ${apart(sine, user).toFixed(3)}), kept and played after a reboot`); diff --git a/web/emu/worklet.js b/web/emu/worklet.js index b32de121..a3a91adf 100644 --- a/web/emu/worklet.js +++ b/web/emu/worklet.js @@ -143,6 +143,7 @@ if (typeof registerProcessor === "function") { ex.web_master(m.master ?? 700); if (ex.web_seed) ex.web_seed((Math.random() * 4294967296) >>> 0); // (JIANT 0.4) this session's macros ex.web_boot(); + if (ex.web_wt_boot) ex.web_wt_boot(); // (WAVE) the user wavetables U1..U6 checked, as the FM-1 at power-on (wt-api.h) if (ex.web_service) ex.web_service(); // (ports) the recorder's first object const samples = ex.web_sample_count ? ex.web_sample_count() : 0; const kitReady = !!(samples && m.sampleKit && ex.web_sample_kit && !ex.web_sample_kit()); @@ -338,6 +339,7 @@ if (typeof registerProcessor === "function") { ex.web_input(n); } const t0 = clock(); + if (ex.web_wt_sync) ex.web_wt_sync(); // (WAVE) the user tables WAVE reads = the flash as written (wt-api.h) ex.web_render(n); if (this.tap) this.tapBlock(ex, n); this.pos = (this.pos || 0) + n; diff --git a/web/emu/wt-api.h b/web/emu/wt-api.h new file mode 100644 index 00000000..ccbc8fc7 --- /dev/null +++ b/web/emu/wt-api.h @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */ +/* (WAVE, editor protocol 1.7) the user wavetables U1..U6 in the emulator. Two things the FM-1 has and a pinned + * felucca_web.c does not: + * - the power-on check: main.c fm1_main runs editor_wt.c wt_boot (the directory's current copy, each table's CRC, which + * slots play); web_power_on does not (FELUCCA_OTA is 0 here), so eng_wave.c's wvs.sec[] stays all 0 ("a table in + * sector 0" for every slot): wt_dir then finds slots stored but no directory, and every WT_LIST / WT_BEGIN / WT_OP + * answers rc 4 (no flash): Studio cannot upload a table, and one restored from the page's saved flash never plays. + * - what WAVE reads: eng_wave.c reads the tables through XIP, WV_FLASH(off); the web build takes tests/hostsim.c's + * WV_FLASH, a host_wt[] of its own, not this file's NOR (nor[], which editor_wt.c writes through st_erase / st_prog): + * a table uploaded was stored and listed but played as zeros (silence). The FM-1 reads the flash itself, so here + * host_wt[] is kept the NOR's 0xD0000..0xD7FFF: copied whole whenever the flash was written since the last copy. + * The worklet calls web_wt_sync() before each render; every write to that area comes from a request (web_ed, + * web_sysex) handled between renders, so the audio always reads the NOR as it is, as on the device. + * web_wt_boot() the copy, then wt_boot, once the device is up and the page's saved flash restored (worklet.js + * calls it right after web_boot); again is harmless (it only reads the flash) + * web_wt_sync() the copy, only when the flash was written since the last one (a counter compare otherwise) + * Appended to a pinned felucca_web.c by the site's build (.github/workflows/emulator.yml, emulator-beta.yml) when the + * pin has editor_wt.c, as additive-api.h and pm-api.h are; once (the guard). Nothing of the firmware changes for this + * file. */ +#ifndef JIANT_WT_API +#define JIANT_WT_API 1 +_Static_assert(sizeof host_wt == 0x8000u && sizeof nor >= 0xD8000u, "the user wavetables' flash, 0xD0000..0xD7FFF"); +static uint32_t web_wt_seen, web_wt_ever; +static void web_wt_copy(void) +{ + memcpy(host_wt, nor + 0xD0000u, sizeof host_wt); + web_wt_seen = web_flash_writes; + web_wt_ever = 1; +} +EXPORT void web_wt_sync(void) +{ + if (!web_wt_ever || web_wt_seen != web_flash_writes) + web_wt_copy(); +} +EXPORT void web_wt_boot(void) +{ + web_wt_copy(); + if (web_booted && flash_ok) + wt_boot(); +} +#endif