Skip to content

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

Rust

Pet the head: wiggle the mouse over its crown and it closes its eyes, hearts float up

Jameson Hodgeb604a7ac8ced
diff --git a/src/ui/components/AnatomyView3D.tsx b/src/ui/components/AnatomyView3D.tsx
index 31c0e13..2f3469c 100644
--- a/src/ui/components/AnatomyView3D.tsx
+++ b/src/ui/components/AnatomyView3D.tsx
@@ -9,6 +9,7 @@ import { useEffect, useRef, useState } from "react";
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { Anatomy3D } from "../render/anatomy3d.ts";
+import { Petting } from "../render/petting.ts";
import { drawReadouts } from "../render/readouts.ts";
import { Glow, label } from "../render/vector.ts";
import { engine } from "../../audio/AudioEngine.ts";
@@ -124,6 +125,25 @@ export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showForma
};
canvas.addEventListener("dblclick", home);
+ // petting: a mouse wiggled over the top of the head (no button down: that turns the head)
+ const pet = new Petting();
+ let crown: { x: number; y: number; cmPx: number } | null = null;
+ let lastPt: [number, number] | null = null;
+ const onHover = (e: PointerEvent) => {
+ if (e.buttons !== 0 || e.pointerType === "touch" || !crown) {
+ lastPt = null;
+ return;
+ }
+ const p: [number, number] = [e.offsetX, e.offsetY];
+ // over the crown: within 6 cm of its top, and not below the brow
+ const near = Math.hypot(p[0] - crown.x, p[1] - crown.y) < 6 * crown.cmPx && p[1] < crown.y + 4.5 * crown.cmPx;
+ if (near && lastPt) pet.stroke(Math.hypot(p[0] - lastPt[0], p[1] - lastPt[1]) / crown.cmPx);
+ lastPt = near ? p : null;
+ };
+ const offHead = () => (lastPt = null);
+ canvas.addEventListener("pointermove", onHover);
+ canvas.addEventListener("pointerleave", offHead);
+
let w = 0, h = 0, dpr = 1;
const maxDpr = () => Math.min(feedRef.current?.maxDpr ?? 2, window.devicePixelRatio || 1);
/** a fed head (no settings of its own to keep) goes back to its framing when its pane changes shape */
@@ -169,6 +189,7 @@ export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showForma
a = tele ? VoiceModel.articFromArray(tele.t.artic) : st.artic;
}
const view = feed ? feed.view : st.view;
+ r.face.content = pet.content;
r.face.update(tele, elapsed);
const m = feed ? feed.model : model.current;
if (m && w > 0 && h > 0) {
@@ -215,6 +236,10 @@ export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showForma
drawReadouts(ctx, { w, h, compact: r.compact, slotX: w - Math.min(340, w * 0.42), slotY: h - Math.min(236, h * 0.3) }, formantsRef.current ? formants : [], info, slots);
}
overlayRef.current?.(ctx, r.faceView(m, f, a), m, f, a, { w, h, dt });
+ // the hearts start beside the crown on the side the face looks to, and drift that way
+ crown = r.crown(m);
+ pet.update(elapsed, [crown.x + r.facing * 7 * crown.cmPx, crown.y + 0.5 * crown.cmPx], crown.cmPx);
+ pet.draw(ctx, r.facing, crown.cmPx);
glow.apply(canvas, ctx, dpr);
}
raf = requestAnimationFrame(loop);
@@ -224,6 +249,8 @@ export function AnatomyView3D(props: { slots?: SlotsFn; info?: InfoFn; showForma
cancelAnimationFrame(raf);
ro.disconnect();
canvas.removeEventListener("dblclick", home);
+ canvas.removeEventListener("pointermove", onHover);
+ canvas.removeEventListener("pointerleave", offHead);
controls.dispose();
r.dispose();
gl.dispose();
diff --git a/src/ui/render/anatomy3d.ts b/src/ui/render/anatomy3d.ts
index 81f71b4..5013f5a 100644
--- a/src/ui/render/anatomy3d.ts
+++ b/src/ui/render/anatomy3d.ts
@@ -734,6 +734,16 @@ export class Anatomy3D {
return Math.hypot(x1 - x0, y1 - y0);
}
+ /** the top of the head on screen (CSS px) and the head's scale there (px per body cm) */
+ crown(model: VoiceModel): { x: number; y: number; cmPx: number } {
+ const rest = this.restFor(model);
+ const k = model.tract.scale;
+ const s = rest.head.at(rest.head.y1 - 1.2 * k);
+ const cx = s ? (s.F + s.Bk) / 2 : rest.center.x;
+ const [x, y] = this.project(cx, rest.head.y1, 0);
+ return { x, y, cmPx: this.pxPerCmAt(cx, rest.head.y1, 0) };
+ }
+
/** what the words at the mouth (and other overlays) need of this view */
faceView(model: VoiceModel, f: HumanFrame, a: HumanArtic): FaceView {
const lips = faceHandles(model, f, a).find((h) => h.id === "lips")!.pos;
diff --git a/src/ui/render/expression.ts b/src/ui/render/expression.ts
index 7a2d964..45c5c21 100644
--- a/src/ui/render/expression.ts
+++ b/src/ui/render/expression.ts
@@ -56,6 +56,8 @@ export class Expression {
lid = 0;
/** extra gaze pitch (radians, up): the eyes roll up on high notes */
lookUp = 0;
+ /** being petted: 0..1, set by the view; the eyes close and the brows lift with pleasure */
+ content = 0;
/** 3D head: chin up (+) or down, radians */
nod = 0;
/** 3D head: tilted towards its right shoulder (+), radians */
@@ -104,7 +106,7 @@ export class Expression {
dev = 12 * (l - this.ref);
wobble = 12 * (l - this.fast);
} else this.held = 0;
- const browTo = voiced ? clamp(dev / 6, -0.7, 1) : 0;
+ const browTo = (voiced ? clamp(dev / 6, -0.7, 1) : 0) + 0.3 * this.content;
this.brow = follow(this.brow, browTo, voiced ? 0.12 : 0.45, dt);
this.lookUp = 0.32 * Math.max(0, this.brow);
@@ -119,7 +121,7 @@ export class Expression {
}
const b = (this.clock - this.blinkAt) / 0.17;
const blink = this.blinkAt >= 0 && b < 1 ? Math.sin(Math.PI * b) ** 0.7 : 0;
- this.lid = Math.max(blink, this.hold);
+ this.lid = Math.max(blink, this.hold, 0.95 * this.content);
// ---- 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
diff --git a/src/ui/render/petting.ts b/src/ui/render/petting.ts
new file mode 100644
index 0000000..3eca70a
--- /dev/null
+++ b/src/ui/render/petting.ts
@@ -0,0 +1,100 @@
+/**
+ * Petting a head: wiggle the pointer over the top of it for a moment and it closes its eyes, and
+ * heart outlines float up beside it, as if it likes it. The view feeds the pointer's path over the
+ * crown (in cm on the head, so it doesn't matter how big the head is drawn) and draws the hearts.
+ */
+import { V } from "../colors.ts";
+
+/** path over the crown (cm) that makes a head happy from rest, less what fades meanwhile: an
+ * ordinary wiggle (30 cm/s on the head: a mouse at ~700 px/s over a desktop head) gets there in
+ * about a second and a half */
+const HAPPY_CM = 30;
+/** how fast the feeling fades once the petting stops (level per second) */
+const FADE = 0.25;
+/** happy from this level up; content again (eyes open) below RELAX: a few seconds after it stops */
+const HAPPY = 1, RELAX = 0.35, MAX = 1.3;
+/** a heart every this many seconds while happy, each living LIFE seconds */
+const EVERY = 0.55, LIFE = 1.9;
+
+interface Heart {
+ born: number;
+ /** spawn point, CSS px, and the size the heart grows to */
+ x: number;
+ y: number;
+ size: number;
+ sway: number;
+}
+
+export class Petting {
+ /** 0 at rest; 1 and up is happy */
+ level = 0;
+ happy = false;
+ /** eyes closing with pleasure, 0..1 (eased) */
+ content = 0;
+ readonly hearts: Heart[] = [];
+ private clock = 0;
+ private nextHeart = 0;
+ private seed = 1;
+
+ /** the pointer moved `cm` (head cm) over the crown */
+ stroke(cm: number): void {
+ if (cm > 0) this.level = Math.min(MAX, this.level + cm / HAPPY_CM);
+ }
+
+ /** one frame; `anchor` is where the hearts start (CSS px, beside the crown), `cmPx` the head's scale */
+ update(dt: number, anchor: [number, number] | null, cmPx: number): void {
+ this.clock += dt;
+ this.level = Math.max(0, this.level - FADE * dt);
+ if (this.level >= HAPPY) this.happy = true;
+ else if (this.level < RELAX) this.happy = false;
+ this.content += ((this.happy ? 1 : 0) - this.content) * Math.min(1, dt * (this.happy ? 5 : 3));
+ if (this.happy && anchor && this.clock >= this.nextHeart) {
+ this.nextHeart = this.clock + EVERY;
+ const r = this.rand();
+ this.hearts.push({ born: this.clock, x: anchor[0] + (r - 0.5) * 1.6 * cmPx, y: anchor[1], size: Math.min(44, Math.max(14, (1.9 + 0.6 * this.rand()) * cmPx)), sway: this.rand() * Math.PI * 2 });
+ }
+ while (this.hearts.length && this.clock - this.hearts[0].born > LIFE) this.hearts.shift();
+ }
+
+ /** the hearts, rising and swaying and fading; `side` is the way they drift (towards the face) */
+ draw(ctx: CanvasRenderingContext2D, side: 1 | -1, cmPx: number): void {
+ for (const h of this.hearts) {
+ const u = (this.clock - h.born) / LIFE;
+ const pop = Math.min(1, u / 0.12);
+ const s = h.size * (0.35 + 0.65 * (1 - (1 - pop) ** 3)) * (1 + 0.15 * u);
+ const x = h.x + side * u * 1.5 * cmPx + Math.sin(h.sway + u * 7) * 0.35 * cmPx;
+ const y = h.y - u * 4 * cmPx;
+ heart(ctx, x, y, s, V.pink, Math.min(1, (1 - u) / 0.4));
+ }
+ }
+
+ private rand(): number {
+ // small LCG: the hearts' scatter needs no more
+ this.seed = (this.seed * 16807) % 2147483647;
+ return this.seed / 2147483647;
+ }
+}
+
+/** a heart outline `size` px tall centred on (x, y) */
+export function heart(ctx: CanvasRenderingContext2D, x: number, y: number, size: number, color: string, alpha: number): void {
+ if (alpha <= 0 || size <= 0) return;
+ ctx.save();
+ ctx.globalCompositeOperation = "lighter";
+ ctx.globalAlpha = alpha;
+ ctx.strokeStyle = color;
+ ctx.lineWidth = Math.max(1.2, size / 16);
+ ctx.lineJoin = "round";
+ ctx.beginPath();
+ const k = size / 32;
+ for (let i = 0; i <= 48; i++) {
+ const t = (i / 48) * Math.PI * 2;
+ const hx = 16 * Math.sin(t) ** 3;
+ const hy = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);
+ const px = x + hx * k, py = y - (hy + 2.5) * k;
+ if (i) ctx.lineTo(px, py);
+ else ctx.moveTo(px, py);
+ }
+ ctx.closePath();
+ ctx.stroke();
+ ctx.restore();
+}
diff --git a/tests/petting.test.ts b/tests/petting.test.ts
new file mode 100644
index 0000000..ef47208
--- /dev/null
+++ b/tests/petting.test.ts
@@ -0,0 +1,50 @@
+/**
+ * Petting a head: a short wiggle over the crown makes it happy (eyes closing, hearts), a single
+ * pass doesn't, and it settles a few seconds after the petting stops.
+ */
+import { describe, expect, test } from "bun:test";
+import { Petting } from "../src/ui/render/petting.ts";
+
+/** wiggle at `cmPerS` (head cm per second) for `s` seconds, at 60 frames a second */
+function wiggle(p: Petting, cmPerS: number, s: number) {
+ for (let t = 0; t < s; t += 1 / 60) {
+ p.stroke(cmPerS / 60);
+ p.update(1 / 60, [100, 100], 20);
+ }
+}
+const rest = (p: Petting, s: number) => {
+ for (let t = 0; t < s; t += 1 / 60) p.update(1 / 60, [100, 100], 20);
+};
+
+describe("Petting", () => {
+ test("a second and a half of ordinary wiggling makes it happy: eyes close, hearts float up", () => {
+ const p = new Petting();
+ wiggle(p, 30, 1.6);
+ expect(p.happy).toBe(true);
+ rest(p, 0.6);
+ expect(p.content).toBeGreaterThan(0.9);
+ expect(p.hearts.length).toBeGreaterThan(0);
+ });
+
+ test("passing over the head once does nothing", () => {
+ const p = new Petting();
+ wiggle(p, 30, 0.4); // 12 cm: across the crown and off
+ rest(p, 1);
+ expect(p.happy).toBe(false);
+ expect(p.content).toBeLessThan(0.01);
+ expect(p.hearts.length).toBe(0);
+ });
+
+ test("it settles a few seconds after the petting stops, and the hearts finish", () => {
+ const p = new Petting();
+ wiggle(p, 40, 4);
+ expect(p.happy).toBe(true);
+ rest(p, 2);
+ expect(p.happy).toBe(true); // it lingers
+ rest(p, 4);
+ expect(p.happy).toBe(false);
+ rest(p, 2);
+ expect(p.content).toBeLessThan(0.01);
+ expect(p.hearts.length).toBe(0);
+ });
+});