087c169Physically simulated pizzicato double bass and an SF2 built from itJeremy Magland 1// The FE body for a set of parameters, cached in bodies/fe_<hash>.json (computed when missing).
2import fs from "node:fs";
3import path from "node:path";
4import { feInputsKey, type Params } from "../src/core/params.ts";
5import { parametricBody } from "../src/core/body.ts";
6import { computeFEBody } from "../src/fe/index.ts";
7import type { BodyModel } from "../src/core/types.ts";
9const DIR = path.join(import.meta.dirname, "..", "bodies");
11function hash(s: string): string {
12 let h = 0x811c9dc5;
13 for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 0x01000193) >>> 0; }
14 return h.toString(16).padStart(8, "0");
15}
17export function feBodyFor(p: Params, log = console.log): BodyModel {
18 const key = feInputsKey(p);
19 const f = path.join(DIR, `fe_${hash(key)}.json`);
20 if (fs.existsSync(f)) {
21 const b = JSON.parse(fs.readFileSync(f, "utf8")) as BodyModel;
22 if (b.meta?.feKey === key) return b;
23 }
24 log(`computing the FE body (${path.basename(f)})…`);
25 const b = computeFEBody(p);
26 fs.mkdirSync(DIR, { recursive: true });
27 fs.writeFileSync(f, JSON.stringify(b));
28 log(` ${b.modes.length} modes in ${Number((b.meta?.timings as { total: number })?.total ?? 0).toFixed(1)} s`);
29 return b;
30}
32/** The body the parameters ask for. */
33export function bodyFor(p: Params, log = console.log): BodyModel {
34 return p.engine.bodyModel === "fe" ? feBodyFor(p, log) : parametricBody(p);
35}