mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-06 12:54:29 +01:00
Firmware image byte-identical to bcb1d20f (felucca.bin, ota.bin, felucca.fwsc, bt.bin: same SHA-256).
- drumx_voice.c: dv_exp2 / dv_kb written anew as dx_pow2 / dx_fall (same integer results for every input,
same machine code); fx.c and tools/audio_layout.txt follow the names. Header: Juan Jiant.
- tools/gen_lyrics.py: the Huffman code lengths by a two-queue construction and canonical codes from the
per-length counts (felucca_lyrics.h identical). Header: Juan Jiant.
- tests/jiant_test_util.h (new): the verdict line, RMS / peak, FFT, Hann power spectrum, peak frequency,
xorshift noise. animal, string_add, voice, delay, drumx, drumx_oct, filter, stems_usb, swarm, uac_sched
and fm6bank tests use it instead of helpers copied from Felucca's tests; their output and demo files are
the same byte for byte. Headers: Juan Jiant.
- song_main.c: the quick chain's bars (quick_bars) and the song's row check (song_unsaved) rewritten; header
Juan Jiant, SLOOP's design credited. song_play.c keeps Felucca's notice and gains Andre Rezemini's (the
SONG REC take follows SLOOP's seq.c; ISR code pinned in flash, left as it is).
- Charles Vestal's notice where his code remains: web/emu/felucca_web.c, worklet.js, index.html (X0X) and
hal/fm1_adc.h (Velo's row drive). LICENSING.md says which files carry which notice.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
144 lines
6.3 KiB
JavaScript
144 lines
6.3 KiB
JavaScript
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
|
|
// Copyright (C) 2026 Charles Vestal (X0X)
|
|
// Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant)
|
|
// Felucca in an AudioWorklet: felucca.wasm is the whole FM-1 firmware against a simulated FM-1
|
|
// (web/emu/felucca_web.c). Each render quantum asks it for 128 frames, which runs the device's clock forward;
|
|
// between quanta the page's input goes in and, about 30 times a second (4 when the page is hidden), the screen,
|
|
// the lights and any changed flash go out. After X0X's web/emu/worklet.js (charlesvestal/fm1-x0x, GPL-3.0).
|
|
|
|
// The flash the page keeps: storage.c's areas (projects, user presets and their FM6 patches, the songs
|
|
// 0x97000..0xDFFFF; the autosave 0xE5000..0xE6FFF (1.2); the settings and the second FM6 copy
|
|
// 0xFC000..0xFFFFF), only the sectors that hold something (an erased sector is all 0xFF). {offset: bytes}
|
|
export const STORE_AREAS = [[0x97000, 0xE0000], [0xE5000, 0xE7000], [0xFC000, 0x100000]];
|
|
export function storeSectors(nor) {
|
|
const out = {};
|
|
for (const [a, b] of STORE_AREAS)
|
|
for (let off = a; off < b; off += 4096) {
|
|
const s = nor.subarray(off, off + 4096);
|
|
if (s.some((v) => v !== 0xFF)) out[off] = s.slice();
|
|
}
|
|
return out;
|
|
}
|
|
export function restoreSectors(nor, sectors) {
|
|
for (const [k, v] of Object.entries(sectors || {})) {
|
|
const off = +k, b = v instanceof Uint8Array ? v : new Uint8Array(v);
|
|
if (STORE_AREAS.some(([a, e]) => off >= a && off + 4096 <= e && off % 4096 === 0) && b.length === 4096)
|
|
nor.set(b, off);
|
|
}
|
|
}
|
|
|
|
export const HOLD_MS = 24;
|
|
|
|
if (typeof registerProcessor === "function") {
|
|
const clock = globalThis.performance ? () => globalThis.performance.now() : () => Date.now();
|
|
class Felucca extends AudioWorkletProcessor {
|
|
constructor() {
|
|
super();
|
|
this.ex = null;
|
|
this.sentDraws = -1;
|
|
this.sentWrites = -1;
|
|
this.sentLeds = "";
|
|
this.lastFrame = 0;
|
|
this.period = 1 / 30;
|
|
this.busy = 0;
|
|
this.frames = 0;
|
|
this.queue = []; // button / key changes, each held >= HOLD_MS of device time
|
|
this.nextOk = 0;
|
|
this.port.onmessage = (e) => this.onMessage(e.data);
|
|
}
|
|
|
|
async onMessage(m) {
|
|
if (m.type === "load") {
|
|
try {
|
|
const { instance } = await WebAssembly.instantiate(m.wasm, {});
|
|
const ex = instance.exports;
|
|
ex._initialize();
|
|
ex.web_nor_erase();
|
|
restoreSectors(new Uint8Array(ex.memory.buffer, ex.web_nor(), ex.web_nor_size()), m.sectors);
|
|
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();
|
|
this.ex = ex;
|
|
this.sentWrites = ex.web_flash_writes_count();
|
|
this.port.postMessage({ type: "ready", rate: ex.web_sample_rate() });
|
|
} catch (err) {
|
|
this.port.postMessage({ type: "error", message: String(err && err.message || err) });
|
|
}
|
|
return;
|
|
}
|
|
const ex = this.ex;
|
|
if (!ex) return;
|
|
if (m.type === "buttons" || m.type === "keys") this.queue.push(m);
|
|
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 === "visible") this.period = m.on ? 1 / 30 : 1 / 4;
|
|
}
|
|
|
|
publish() {
|
|
const ex = this.ex, mem = ex.memory.buffer;
|
|
const msg = { type: "frame" };
|
|
const leds = [ex.web_lit_buttons(), ex.web_lit_keys(), ex.web_dim_buttons(), ex.web_dim_keys(), ex.web_dim_level(),
|
|
ex.web_breath_buttons(), ex.web_breath_keys(), ex.web_mid_keys ? ex.web_mid_keys() : 0];
|
|
const an = ex.web_anim_levels ? ex.web_anim_levels() : 0; // the power-on sweep: each LED's level
|
|
const anim = an ? Array.from(new Uint8Array(mem, an, 41)) : null;
|
|
const key = leds.join(",") + (anim ? ";" + anim.join(",") : "");
|
|
if (key !== this.sentLeds) {
|
|
this.sentLeds = key;
|
|
msg.leds = leds;
|
|
msg.anim = anim;
|
|
}
|
|
if (this.frames >= 44100) { // the share of real time spent running the device
|
|
msg.load = this.busy / (this.frames / 44.1);
|
|
this.busy = this.frames = 0;
|
|
}
|
|
const transfer = [];
|
|
const draws = ex.web_screen_draws();
|
|
if (draws !== this.sentDraws) {
|
|
this.sentDraws = draws;
|
|
msg.fb = new Uint16Array(mem, ex.web_screen(), 240 * 240).slice();
|
|
transfer.push(msg.fb.buffer);
|
|
}
|
|
const writes = ex.web_flash_writes_count();
|
|
if (writes !== this.sentWrites) { // a save is done within one main-loop pass: whole here
|
|
this.sentWrites = writes;
|
|
msg.sectors = storeSectors(new Uint8Array(mem, ex.web_nor(), ex.web_nor_size()));
|
|
}
|
|
if (msg.leds || msg.fb || msg.sectors || msg.load !== undefined) this.port.postMessage(msg, transfer);
|
|
}
|
|
|
|
// A click's down and up can arrive within one render quantum, but the FM-1's contacts never close for less than
|
|
// its debounce, and the UI reads them once a frame: each change of the buttons or the keys stays HOLD_MS of the
|
|
// device's time before the next one goes in (a tap: down for a frame and more, then up).
|
|
input() {
|
|
const ex = this.ex, now = ex.web_now_ms();
|
|
while (this.queue.length && now - this.nextOk >= 0) {
|
|
const m = this.queue.shift();
|
|
if (m.type === "buttons") ex.web_buttons(m.mask >>> 0);
|
|
else ex.web_keys(m.mask >>> 0);
|
|
this.nextOk = now + HOLD_MS;
|
|
}
|
|
}
|
|
|
|
process(inputs, outputs) {
|
|
const ex = this.ex;
|
|
if (!ex) return true;
|
|
if (this.queue.length) this.input();
|
|
const out = outputs[0], n = out[0].length;
|
|
const t0 = clock();
|
|
ex.web_render(n);
|
|
this.busy += clock() - t0;
|
|
this.frames += n;
|
|
out[0].set(new Float32Array(ex.memory.buffer, ex.web_out_l(), n));
|
|
if (out[1]) out[1].set(new Float32Array(ex.memory.buffer, ex.web_out_r(), n));
|
|
if (currentTime - this.lastFrame >= this.period) {
|
|
this.lastFrame = currentTime;
|
|
this.publish();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
registerProcessor("felucca", Felucca);
|
|
}
|