mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-06 21:04:32 +01:00
Claude-Session: https://claude.ai/code/session_01UVqsRnhKjiABTzc6Ety3dd Co-authored-by: Claude <noreply@anthropic.com>
39 lines
2.7 KiB
JavaScript
39 lines
2.7 KiB
JavaScript
/* JIANT FM's MATH USER formula (V10.8.75, editor protocol 1.5) in Studio: the editor's own MATH code (between its
|
|
* MATH-BEGIN / MATH-END markers: MathVM, FVM = JIANT BYTE's compiler ported, mathGenerate), taken from the published
|
|
* editor page so there is one copy of it; a formula compiled to the device's bytecode, heard here, sent with FORMULA (100)
|
|
* and read back. Its text is kept in this browser by its bytecode (the editor's own "jiantMathUser" store, same site)
|
|
* const M = await mathLib(url) { FVM, MathVM, mathGenerate } or null
|
|
* compile(M, text) { ok, code, fix, size, fn(t, a) -> 0..255 } or { ok: false, error, pos }
|
|
* preview(M, text, secs) plays it (8 kHz, as a bytebeat is heard), returns a stop function
|
|
*/
|
|
let lib = null;
|
|
export async function mathLib(url) {
|
|
if (lib) return lib;
|
|
const r = await fetch(url, { cache: "no-cache" }); if (!r.ok) return null;
|
|
const h = await r.text(), a = h.indexOf("/*MATH-BEGIN*/"), b = h.indexOf("/*MATH-END*/");
|
|
if (a < 0 || b < 0) return null;
|
|
try { lib = new Function(h.slice(a, b) + "; return { FVM, MathVM, mathGenerate };")(); } catch (_) { return null; }
|
|
return lib.FVM ? lib : null;
|
|
}
|
|
export function compile(M, src) {
|
|
const f = M.FVM.compile(src);
|
|
if (!f.ok) return { ok: false, error: f.error, pos: f.pos };
|
|
const code = f.code;
|
|
const fn = f.fix ? (t, a) => ((M.FVM.sample(code, M.FVM.run(code, t, (t << 11) >>> 0, a)) >> 8) + 128) & 255 : (t, a) => M.FVM.run(code, t, 0, a) & 255;
|
|
return { ok: true, code, fix: !!f.fix, size: code.length, max: M.FVM.MAX, fn };
|
|
}
|
|
let ac = null;
|
|
export function preview(c, secs = 4, a = 64) {
|
|
ac = ac || new AudioContext();
|
|
const sr = 8000, n = sr * secs, buf = ac.createBuffer(1, n, sr), d = buf.getChannelData(0);
|
|
for (let t = 0; t < n; t++) { let v = 128; try { v = c.fn(t, a); } catch (_) {} d[t] = (v - 128) / 128 * 0.35; }
|
|
const s = ac.createBufferSource(); s.buffer = buf; s.connect(ac.destination); ac.resume(); s.start();
|
|
return () => { try { s.stop(); } catch (_) {} };
|
|
}
|
|
export function wave(c, w, a = 64) { // the first w samples, for a picture
|
|
const out = []; for (let t = 0; t < w; t++) { try { out.push(c.fn(t * 8, a)); } catch (_) { out.push(128); } } return out;
|
|
}
|
|
const KEY = "jiantMathUser";
|
|
export function keepText(code, src) { try { const m = JSON.parse(localStorage.getItem(KEY) || "{}"); m[code.join(",")] = src; const k = Object.keys(m); if (k.length > 200) delete m[k[0]]; localStorage.setItem(KEY, JSON.stringify(m)); } catch (_) {} }
|
|
export function textOf(code) { try { return JSON.parse(localStorage.getItem(KEY) || "{}")[code.join(",")] ?? null; } catch (_) { return null; } }
|