the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Review fixes: body switch keeps the performer's breath and the queued beat; playhead from scheduled hits
Jameson Hodgeadd47996df65
diff --git a/src/engine/control/beatPlayer.ts b/src/engine/control/beatPlayer.tsindex d26257f..0954cad 100644--- a/src/engine/control/beatPlayer.ts+++ b/src/engine/control/beatPlayer.ts@@ -9,6 +9,9 @@ import type { Gesture } from "./score.ts";const midiToHz = (m: number) => 440 * Math.pow(2, (m - 69) / 12);+/** scheduled steps remembered for the playhead (well over a horizon's worth) */+const MARKS = 32;+export class BeatPlayer {steps: (BeatStep | null)[] = [];bpm = 96;@@ -22,6 +25,10 @@ export class BeatPlayer {private anchorTime = 0;private anchorStep = 0;private next = 0;+ /** step number and hit time of recently scheduled steps (−1 = empty slot) */+ private readonly markN = new Int32Array(MARKS).fill(-1);+ private readonly markT = new Float64Array(MARKS);+ private markPos = 0;get stepDur(): number {return 60 / this.bpm / this.stepsPerBeat;@@ -37,6 +44,7 @@ export class BeatPlayer {this.anchorTime = clock + this.horizon;this.anchorStep = 0;this.next = 0;+ this.markN.fill(-1);}stop(): void {@@ -57,13 +65,41 @@ export class BeatPlayer {/** The voice clock restarted (new voice after a body change): keep the beat's phase. */rebase(delta: number): void {this.anchorTime += delta;+ for (let i = 0; i < MARKS; i++) this.markT[i] += delta;}- /** Step under the playhead at `clock` (−1 when stopped). */+ /**+ * The score holding the scheduled hits was replaced (new voice): schedule again every step+ * not yet heard at `clock`, at the times they were given.+ */+ rewind(clock: number): void {+ let first = -1, firstT = 0;+ for (let i = 0; i < MARKS; i++) {+ const n = this.markN[i];+ if (n >= 0 && this.markT[i] >= clock && (first < 0 || n < first)) {+ first = n;+ firstT = this.markT[i];+ }+ }+ if (first < 0) return;+ this.anchorStep = first;+ this.anchorTime = firstT - (first % 2 === 1 ? this.swing * 0.5 * this.stepDur : 0);+ this.next = first;+ for (let i = 0; i < MARKS; i++) if (this.markN[i] >= first) this.markN[i] = -1;+ }++ /** Step under the playhead at `clock`: the last scheduled step whose hit has come (−1: none). */stepAt(clock: number): number {if (!this.playing || this.steps.length === 0) return -1;- const n = Math.floor((clock - this.anchorTime) / this.stepDur) + this.anchorStep;- return n < 0 ? -1 : n % this.steps.length;+ let best = -1, bestT = -Infinity;+ for (let i = 0; i < MARKS; i++) {+ const t = this.markT[i];+ if (this.markN[i] >= 0 && t <= clock && t > bestT) {+ best = this.markN[i];+ bestT = t;+ }+ }+ return best < 0 ? -1 : best % this.steps.length;}/** Schedule every step whose hit falls within the horizon. */@@ -73,6 +109,9 @@ export class BeatPlayer {const hit = this.stepTime(this.next);if (hit > clock + this.horizon) break;const L = this.steps.length;+ this.markN[this.markPos] = this.next;+ this.markT[this.markPos] = hit;+ this.markPos = (this.markPos + 1) % MARKS;const cell = this.steps[this.next % L];if (cell && hit >= clock) {// sustained sounds (hum, open hat) last until the next sounddiff --git a/src/engine/creatures/voice.ts b/src/engine/creatures/voice.tsindex 98af225..4b762fe 100644--- a/src/engine/creatures/voice.ts+++ b/src/engine/creatures/voice.ts@@ -27,6 +27,8 @@ interface ActiveCall {fired: Set<number>;/** params the call automates (restored to the performer's values when it ends) */owns: Set<string>;+ /** every key the call mentions, including "gate" and "!events" */+ names: string[];}export class CreatureVoice {@@ -101,8 +103,13 @@ export class CreatureVoice {const ref = def.pitchRef ?? 0;this.endCall();const owns = new Set<string>();- for (const k of def.keys) for (const n of Object.keys(k.v)) if (n !== "gate" && !n.startsWith("!")) owns.add(n);- this.call = { def, t0: this.clock, pitch: pitch && ref ? pitch / ref : 1, amp, fired: new Set(), owns };+ const names = new Set<string>();+ for (const k of def.keys)+ for (const n of Object.keys(k.v)) {+ names.add(n);+ if (n !== "gate" && !n.startsWith("!")) owns.add(n);+ }+ this.call = { def, t0: this.clock, pitch: pitch && ref ? pitch / ref : 1, amp, fired: new Set(), owns, names: [...names] };}stopCall(): void {@@ -131,10 +138,7 @@ export class CreatureVoice {this.endCall();return;}- // collect all params mentioned- const names = new Set<string>();- for (const k of keys) for (const n of Object.keys(k.v)) names.add(n);- for (const n of names) {+ for (const n of c.names) {// find surrounding keys that define nlet prev: { t: number; x: number } | null = null, next: { t: number; x: number; curve?: string } | null = null;for (const k of keys) {diff --git a/src/ui/store.ts b/src/ui/store.tsindex cc82659..208ffa3 100644--- a/src/ui/store.ts+++ b/src/ui/store.ts@@ -120,6 +120,8 @@ export const useStore = create<State>((set, get) => ({if (prev === m) return;// notes held across the switch would keep breathing into the other voiceset({ mode: m, heldNotes: [] });+ // speech belongs to Speak: under a creature the human clock stops and it would resume later+ if (prev === "speak" || m === "bestiary") engine.stopSpeech();if ((prev === "bestiary") !== (m === "bestiary")) {// a sustain belongs to the voice it was started onset({ hold: false });diff --git a/src/worklet/processor.ts b/src/worklet/processor.tsindex 026d441..93d298b 100644--- a/src/worklet/processor.ts+++ b/src/worklet/processor.ts@@ -133,12 +133,15 @@ class ChimeraProcessor extends AudioWorkletProcessor {this.body = this.pendingBody;this.pendingBody = null;this.voice = this.makeVoice(this.body);- // the new voice's clock starts at zero: keep the beat's phase across the switch+ // the new voice's clock starts at zero: keep the beat's phase across the switch, and+ // queue again the hits the old voice's score was holding (built for the new body)this.beat.body = this.body;this.beat.rebase(this.voice.clock - prev.clock);+ this.beat.rewind(this.voice.clock);this.voice.setSource({ ...prev.source, f0: DEFAULT_F0[this.body] });this.voice.setArticulation({ ...prev.artic.target }, prev.artic.tipFree);- this.voice.setGate(prev.gate > 0.5 ? 1 : 0);+ // the performer's breath, not the voice's (which includes breath from the score)+ this.voice.setGate(this.gateLevel);this.post({ type: "ready", sampleRate, body: this.body, nPh: this.voice.airway.nPh, nOr: this.voice.airway.nOr, creature: this.creature ? this.creature.def.id : "human" });}if (this.pendingCreature !== null) {diff --git a/tests/beatbox.test.ts b/tests/beatbox.test.tsindex 1df196b..819f492 100644--- a/tests/beatbox.test.ts+++ b/tests/beatbox.test.ts@@ -71,16 +71,35 @@ describe("beat player", () => {expect(gap).toBeLessThanOrEqual(60 / 100 / 4 + 1e-9);expect(after[2].t - after[1].t).toBeCloseTo(60 / 160 / 4, 9);});- test("reports the step under the playhead and nothing when stopped", () => {+ test("the playhead follows the scheduled hits, swing included, and nothing when stopped", () => {const p = new BeatPlayer();p.steps = parseBeat("B . t .");- p.setTempo(120, 0);+ p.setTempo(120, 0.6);expect(p.stepAt(1)).toBe(-1);p.start(0);+ run(p, 0, 1.2);+ const step = 0.125, late = 0.3 * step;+ expect(p.stepAt(p.horizon - 0.01)).toBe(-1);expect(p.stepAt(p.horizon + 0.01)).toBe(0);- expect(p.stepAt(p.horizon + 3 * 0.125 + 0.01)).toBe(3);- expect(p.stepAt(p.horizon + 4 * 0.125 + 0.01)).toBe(0);+ // step 1 is swung: until its hit the playhead stays on step 0+ expect(p.stepAt(p.horizon + step + late - 0.005)).toBe(0);+ expect(p.stepAt(p.horizon + step + late + 0.005)).toBe(1);+ expect(p.stepAt(p.horizon + 4 * step + 0.01)).toBe(0);p.stop();expect(p.stepAt(1)).toBe(-1);});+ test("after the voice is replaced, steps not yet heard are scheduled again at their times", () => {+ const p = new BeatPlayer();+ p.steps = parseBeat("B t Pf t");+ p.setTempo(120, 0.3);+ p.start(0);+ const first = run(p, 0, 0.5);+ // a new voice whose clock restarts at 0 while the old one was at 0.5+ p.rebase(-0.5);+ p.rewind(0);+ const again = run(p, 0, 0.3);+ const pending = first.filter((h) => h.t > 0.5).map((h) => h.t - 0.5);+ expect(again.length).toBeGreaterThanOrEqual(pending.length);+ pending.forEach((t, i) => expect(again[i].t).toBeCloseTo(t, 9));+ });});