/ proof-of-concept / cymsimweb
Sign in
proof-of-concept / cymsimweb
cymsimweb / src / model / simulator.ts
56 lines · 2.6 KBCodeBlameHistory
1e29264cymsimweb: WebGPU companion of cymsim with YAML/form editor, live spectrogram and playbackJeremy Magland 1// The interface shared by the CPU and the GPU time loops.
2export interface ChunkResult {
3 firstSample: number; // audio index of pressureFront[0]
4 pressureFront: Float32Array; // baffled Rayleigh pressure [Pa]
5 pressureBack: Float32Array; // diffracted back term [Pa]; unbaffled = front - back
3534482Linear spectrogram axis by default; capture and replay the displacement movie at a chosen slowdownJeremy Magland 6 frames: Float32Array[]; // displacement w at every node, captured every `movieEvery` audio samples
7 frameSamples: number[]; // audio sample index of each frame
13f4dbdexport .cymsim fileJeremy Magland 8 /** Normal acceleration of every node [m/s^2] for the samples [firstSample, firstSample + count), row-major (count, N); present when recording. */
9 acceleration: Float32Array | null;
10 dispFrames: Float32Array[]; // displacement w for the .cymsim record, every `dispEvery` audio samples
11 dispSamples: number[];
14export interface HitState { peakForce: number; contactTime: number; velocity: number; height: number }
16export interface Diagnostics {
17 time: number; kinetic: number; membrane: number; bending: number; exciters: number; maxWoverH: number; hits: HitState[];
13f4dbdexport .cymsim fileJeremy Magland 20/** What a backend needs to continue a run: its own arrays, opaque to everything but the same backend kind. */
21export interface BackendState {
22 kind: "cpu" | "gpu";
23 step: number; // sub-steps done
24 samplesDone: number;
25 arrays: Record<string, Float32Array | Float64Array | Uint32Array>;
28export interface BackendOptions {
29 /** > 0 captures w for the replay movie every that many audio samples. */
30 movieEvery?: number;
31 /** > 0 captures w for the .cymsim displacement record every that many audio samples. */
32 dispEvery?: number;
33 /** Keep the audio-rate normal acceleration of every node (returned per chunk). */
34 recordAcceleration?: boolean;
38 readonly kind: "cpu" | "gpu";
39 readonly samplesDone: number;
3534482Linear spectrogram axis by default; capture and replay the displacement movie at a chosen slowdownJeremy Magland 40 /** Largest chunk (audio samples) runChunk accepts. */
41 readonly maxChunk: number;
1e29264cymsimweb: WebGPU companion of cymsim with YAML/form editor, live spectrogram and playbackJeremy Magland 42 /** Advance by nSamples audio samples and return the pressure samples completed meanwhile. */
43 runChunk(nSamples: number): Promise<ChunkResult>;
44 diagnostics(): Promise<Diagnostics>;
45 /** Transverse displacement w at every node, for the plate view. */
46 displacement(): Promise<Float32Array>;
13f4dbdexport .cymsim fileJeremy Magland 47 /** The integrator state, for a .cymsim file; ``importState`` continues from one (same backend kind and set-up). */
48 exportState(): Promise<BackendState>;
49 importState(state: BackendState): Promise<void>;
13f4dbdexport .cymsim fileJeremy Magland 52
53/** Sub-steps per audio sample times audio samples between file displacement frames, for a frame rate [fps]. */
54export function dispEveryFor(fs: number, rate: number): number {
55 return rate > 0 ? Math.max(1, Math.round(fs / rate)) : 0;
↑↓ move↵openescclose