Skip to content

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

Rust

Sing: a demo song gives your own lyrics back when it ends (it replaced them for good)

Jameson Hodged32c2adcf37e
diff --git a/src/ui/singing.ts b/src/ui/singing.ts
index 9a6fbaf..77e2c1b 100644
--- a/src/ui/singing.ts
+++ b/src/ui/singing.ts
@@ -231,6 +231,20 @@ interface DemoRun {
let demoRun: DemoRun | null = null;
/** bumped by every stop: a demo still waiting for audio or the dictionary then gives up */
let demoToken = 0;
+/**
+ * The player's own lyrics and cursor while a demo shows the song's words: given back when the
+ * demo ends or stops, so a demo never costs anyone the words they typed.
+ */
+let ownWords: { lyrics: string; cursor: number } | null = null;
+
+function endDemo(): void {
+ demoRun = null;
+ const w = ownWords;
+ ownWords = null;
+ if (!w) return void useSing.setState({ demo: null, demoAt: -1 });
+ const syllables = lyricsToSyllables(w.lyrics);
+ useSing.setState({ demo: null, demoAt: -1, lyrics: w.lyrics, syllables, cursor: Math.min(w.cursor, Math.max(0, syllables.length - 1)) });
+}
engine.onPlanned((m) => {
const d = demoRun;
@@ -239,7 +253,10 @@ engine.onPlanned((m) => {
d.times.forEach(([a, b], k) => addNote(d.midi[k], m.t0 + a, d.text[k], true, m.t0 + b - 0.02));
});
-/** Sing a demo song (after `ready`, e.g. audio startup, resolves; unless stopped meanwhile). */
+/**
+ * Sing a demo song (after `ready`, e.g. audio startup, resolves; unless stopped meanwhile). The
+ * lyrics show the song's words while it plays; the player's own come back when it ends or stops.
+ */
export async function playDemo(name: string, body: BodyMix, ready?: () => Promise<void>): Promise<void> {
const song = DEMO_SONGS.find((s) => s.name === name);
if (!song) return;
@@ -270,6 +287,8 @@ export async function playDemo(name: string, body: BodyMix, ready?: () => Promis
notes.forEach((n, i) => parent.push(n.syl.melisma && i > 0 ? parent[i - 1] : i));
const shown = parent.map((p) => written(notes[p].syl));
demoRun = { id, times, midi: notes.map((n) => n.midi), text: notes.map((n) => n.syl.text), shown, parent, end: plan.end, offset: null };
+ const s = useSing.getState();
+ ownWords ??= { lyrics: s.lyrics, cursor: s.cursor };
useSing.setState({ lyrics: song.lyrics, syllables: syl, cursor: 0, demo: song.name, demoAt: -1 });
}
@@ -277,13 +296,12 @@ function stopDemo(): void {
demoToken++;
const d = demoRun;
if (!d) return;
- demoRun = null;
engine.stopSpeech();
// notes not yet sung leave the roll; the one sounding ends now
const t = now();
roll.notes = roll.notes.filter((n) => !(n.demo && n.t0 > t));
for (const n of roll.notes) if (n.demo && n.t1 !== null && n.t1 > t) n.t1 = t;
- useSing.setState({ demo: null, demoAt: -1 });
+ endDemo();
}
/** Called every frame while Sing mode is shown: follow the demo's progress. */
@@ -291,11 +309,7 @@ export function tickSinging(): void {
const d = demoRun;
if (!d || d.offset === null) return;
const t = now() - d.offset;
- if (t > d.end + 0.4) {
- demoRun = null;
- useSing.setState({ demo: null, demoAt: -1 });
- return;
- }
+ if (t > d.end + 0.4) return endDemo();
let k = -1;
while (k + 1 < d.times.length && d.times[k + 1][0] <= t) k++;
if (k !== useSing.getState().demoAt) useSing.setState({ demoAt: k });
diff --git a/tests/singing-live.test.ts b/tests/singing-live.test.ts
index f6ed440..9ada67d 100644
--- a/tests/singing-live.test.ts
+++ b/tests/singing-live.test.ts
@@ -15,10 +15,12 @@ const midiToHz = (m: number) => 440 * Math.pow(2, (m - 69) / 12);
const FS = 48000;
let voice = new WasmVoice(FS);
+/** what the worklet answers when a scheduled plan starts (singing.ts registers one at import) */
+let planned: (m: { type: "planned"; id: number; t0: number; end: number }) => void = () => {};
const fakeEngine = {
latest: null,
voiceClock: () => voice.clock,
- onPlanned: () => () => {},
+ onPlanned: (f: typeof planned) => ((planned = f), () => {}),
scheduleGestures: (gs: Gesture[], lead = 0.006) => {
voice.schedule(gs, lead);
},
@@ -39,7 +41,7 @@ const engineStub = new Proxy(
{ get: (t, k) => (k in t ? t[k as string] : () => {}), set: (t, k, v) => ((t[k as string] = v), true) },
);
mock.module("../src/audio/AudioEngine.ts", () => ({ engine: engineStub }));
-const { useSing, singNoteOn, singNoteOff, playDemo, stopSinging } = await import("../src/ui/singing.ts");
+const { useSing, singNoteOn, singNoteOff, playDemo, stopSinging, tickSinging } = await import("../src/ui/singing.ts");
const gesturesOf = () => voice.score();
/** breath the score is giving now (strength-weighted activation of breath gestures) */
const breathNow = () => {
@@ -157,6 +159,38 @@ describe("Sing mode, live keys", () => {
engineStub.schedule = schedule;
});
+ test("a demo shows the song's words while it plays, then gives the player's own back", async () => {
+ voice = new WasmVoice(FS);
+ const sing = useSing.getState;
+ sing().setLyrics("my own words to sing");
+ sing().setCursor(2);
+ // stopped (a song chip pressed again, the mode or body changed)
+ await playDemo("Made of air", MALE, () => Promise.resolve());
+ expect(sing().demo).toBe("Made of air");
+ expect(sing().lyrics).not.toBe("my own words to sing");
+ stopSinging();
+ expect(sing().lyrics).toBe("my own words to sing");
+ expect(sing().syllables.map((s) => s.text)).toEqual(["my", "own", "words", "to", "sing"]);
+ expect(sing().cursor).toBe(2);
+ // played to the end
+ await playDemo("Made of air", MALE, () => Promise.resolve());
+ planned({ type: "planned", id: 1, t0: voice.clock - 1000, end: 0 });
+ tickSinging();
+ expect(sing().demo).toBeNull();
+ expect(sing().lyrics).toBe("my own words to sing");
+ expect(sing().cursor).toBe(2);
+ // a second song started while the first plays gives back the player's words, not the first song's
+ await playDemo("Made of air", MALE, () => Promise.resolve());
+ await playDemo("Made of air", MALE, () => Promise.resolve());
+ stopSinging();
+ expect(sing().lyrics).toBe("my own words to sing");
+ // words typed during a demo are the player's from then on
+ await playDemo("Made of air", MALE, () => Promise.resolve());
+ sing().setLyrics("typed during the song");
+ expect(sing().demo).toBeNull();
+ expect(sing().lyrics).toBe("typed during the song");
+ });
+
test("a quick next note drops the last one's breath-out; leaving Sing cancels a pending ending", () => {
voice = new WasmVoice(FS);
useSing.getState().setVibrato(0);