Files
JIANT/web/emu/wavetable-test.mjs
T
Claude 484643ecc2 Emulator: ADD found by the firmware's own report; WAVE's user tables work in the browser
- browser-additive-test.py: V10.8.87's engine order ends STRING ADD WAVE INPUT, so two
  steps back from DRUM landed on WAVE (9), not ADD (13), and run 47552111627 failed. KNOB 2
  now steps one detent at a time, asking the device after each, until it reports 13 (the
  engines passed are printed; same checks after: ADD, keyboard audio, edition, storage).
- wt-api.h (appended to a pin with editor_wt.c, as pm-api.h): the emulator never ran wt_boot
  (main.c does, FELUCCA_OTA only), so every WT_LIST / WT_BEGIN answered rc 4 and Studio
  could not upload a table; and WAVE reads the tables through hostsim.c's host_wt[], not
  the emulator's NOR, so a table stored there played silent. web_wt_boot after web_boot,
  web_wt_sync (host_wt = NOR 0xD0000..0xD7FFF when the flash was written) before each render.
- wavetable-test.mjs: a .jwt uploaded (102..104), listed, read back (106), played by WAVE
  from U1 (not as SINE), kept and played after a reboot; run by emulator.yml / emulator-beta.yml.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-12-05 18:20:58 +00:00

103 lines
6.1 KiB
JavaScript

// 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`);