Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

Review fixes: the face keeps real time at a low frame rate; a skipped beat step still nods

Jameson Hodgef40c8cb44f6d
diff --git a/src/ui/components/AnatomyView.tsx b/src/ui/components/AnatomyView.tsx
index 5ad503c..7dfc54f 100644
--- a/src/ui/components/AnatomyView.tsx
+++ b/src/ui/components/AnatomyView.tsx
@@ -89,11 +89,14 @@ export function AnatomyView(props: { slots?: SlotsFn; info?: InfoFn; showFormant
let formants: number[] = [];
const loop = (now: number) => {
const dt = Math.min(0.05, (now - last) / 1000);
+ // the face's expression keeps real time (a held note lasts as long at a low frame rate)
+ const elapsed = Math.min(0.5, (now - last) / 1000);
last = now;
if (Math.min(2, window.devicePixelRatio || 1) !== dpr) fit();
const st = useStore.getState();
const tele = engine.latest && sameBody(engine.latest.body, st.body) ? engine.latest : null;
const a = tele ? VoiceModel.articFromArray(tele.t.artic) : st.artic;
+ r.face.update(tele, elapsed);
const m = model.current;
if (m && w > 0 && h > 0) {
const f = m.frame(a);
diff --git a/src/ui/components/AnatomyView3D.tsx b/src/ui/components/AnatomyView3D.tsx
index 0fbd2dd..ecd501e 100644
--- a/src/ui/components/AnatomyView3D.tsx
+++ b/src/ui/components/AnatomyView3D.tsx
@@ -120,11 +120,14 @@ export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showForma
let formants: number[] = [];
const loop = (now: number) => {
const dt = Math.min(0.05, (now - last) / 1000);
+ // the face's expression keeps real time (a held note lasts as long at a low frame rate)
+ const elapsed = Math.min(0.5, (now - last) / 1000);
last = now;
if (Math.min(2, window.devicePixelRatio || 1) !== dpr) fit();
const st = useStore.getState();
const tele = engine.latest && sameBody(engine.latest.body, st.body) ? engine.latest : null;
const a = tele ? VoiceModel.articFromArray(tele.t.artic) : st.artic;
+ r.face.update(tele, elapsed);
const m = model.current;
if (m && w > 0 && h > 0) {
if (posed !== m) {
diff --git a/src/ui/render/anatomy.ts b/src/ui/render/anatomy.ts
index 5199b79..74ed186 100644
--- a/src/ui/render/anatomy.ts
+++ b/src/ui/render/anatomy.ts
@@ -153,7 +153,7 @@ export class AnatomyRenderer {
private lastEcho = -1;
/** where the eye looks */
private gaze = new Gaze();
- /** brows, lids and cheeks, from the voice */
+ /** brows, lids and cheeks, from the voice (the view updates it) */
readonly face = new Expression();
constructor() {
@@ -242,7 +242,6 @@ export class AnatomyRenderer {
opts: { hover: HandleId | null; drag: HandleId | null; dt: number; formants: number[]; slots: Slot[]; info: string[] },
): void {
this.clock += opts.dt;
- this.face.update(tele, opts.dt);
const G = this.geometry(model, f, a);
const pts = (v: Vec[]) => v.map((p) => this.P(p));
const line = (v: Vec[], color: string, w: number, o: { alpha?: number; hot?: boolean; close?: boolean } = {}) => beamPts(ctx, pts(v), color, w, o);
diff --git a/src/ui/render/anatomy3d.ts b/src/ui/render/anatomy3d.ts
index 109636e..bae6e7c 100644
--- a/src/ui/render/anatomy3d.ts
+++ b/src/ui/render/anatomy3d.ts
@@ -641,7 +641,7 @@ export class Anatomy3D {
compact = false;
private clock = 0;
private readonly gaze = new Gaze();
- /** brows, lids, cheeks and the head's nods, from the voice */
+ /** brows, lids, cheeks and the head's nods, from the voice (the view updates it) */
readonly face = new Expression();
/** the camera in the head's own frame (the head nods and sways under it) */
private readonly eye = new THREE.Vector3();
@@ -735,7 +735,6 @@ export class Anatomy3D {
const rest = this.restFor(model);
const k = model.tract.scale;
const face = this.face;
- face.update(tele, dt);
this.headPose(rest, k);
const phi = jawAngle(model, a.jaw);
const C = rest.condyle;
diff --git a/src/ui/render/expression.ts b/src/ui/render/expression.ts
index 3541fac..7a2d964 100644
--- a/src/ui/render/expression.ts
+++ b/src/ui/render/expression.ts
@@ -76,6 +76,8 @@ export class Expression {
private secondBlink = false;
private lastStep = -1;
+ /** dt: the real time since the last update (s), not a frame-capped one, so a held note or a blink
+ * lasts as long at a low frame rate */
update(tele: TelemetryMessage | null, dt: number): void {
this.clock += dt;
const t = tele?.t;
@@ -120,8 +122,13 @@ export class Expression {
this.lid = Math.max(blink, this.hold);
// ---- the 3D head: chin up for high notes, a nod on the beat, a sway on a long note, the vibrato's wobble
+ // a nod for each beat (four steps) the sequencer passed since the last frame, the bar's first the
+ // hardest; a slow frame that skips the beat's own step still nods, once
const step = tele?.beatStep ?? -1;
- if (step !== this.lastStep && step >= 0 && step % 4 === 0) this.nodSpring.v -= step % 16 === 0 ? 1.3 : 0.8;
+ if (step >= 0 && this.lastStep >= 0 && step !== this.lastStep) {
+ const wrapped = step < this.lastStep;
+ if (wrapped || Math.floor(step / 4) !== Math.floor(this.lastStep / 4)) this.nodSpring.v -= wrapped || Math.floor(step / 16) !== Math.floor(this.lastStep / 16) ? 1.3 : 0.8;
+ }
this.lastStep = step;
this.nod = this.nodSpring.step(0.13 * this.brow, dt);
const sway = smooth(0.6, 2, this.held) * 0.07 * Math.sin(this.clock * 1.7);
diff --git a/tests/expression.test.ts b/tests/expression.test.ts
index 1aa04b1..c592253 100644
--- a/tests/expression.test.ts
+++ b/tests/expression.test.ts
@@ -72,4 +72,26 @@ describe("expression", () => {
// the octave note held for two seconds: eyes nearly shut by its end
expect(lids[399]).toBeGreaterThan(0.6);
});
+
+ test("a slow frame rate keeps time, and a skipped beat step still nods", () => {
+ const v = new WasmVoice(FS);
+ v.render(480);
+ const base = telemetry(v);
+ const at = (f0: number, beatStep = -1): TelemetryMessage => ({ ...base, beatStep, t: { ...base.t, f0, voicing: 1, gate: 1, outRms: 0.1 } });
+ // three frames a second: two seconds low, then two an octave up
+ const e = new Expression();
+ for (let i = 0; i < 6; i++) e.update(at(110), 1 / 3);
+ for (let i = 0; i < 6; i++) e.update(at(220), 1 / 3);
+ expect(e.lid).toBeGreaterThan(0.6);
+ // the sequencer moves from step 3 to 5 between frames: the beat at 4 nods the head
+ const b = new Expression();
+ b.update(at(110, 3), 0.01);
+ b.update(at(110, 5), 0.01);
+ let nod = 0;
+ for (let i = 0; i < 30; i++) {
+ b.update(at(110, 5), 0.01);
+ nod = Math.min(nod, b.nod);
+ }
+ expect(nod).toBeLessThan(-0.02);
+ });
});