Skip to content

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

Rust

Sing: the engine's other demo songs are in the Tune menu again, with their own words (#87's tunes left out "Ночной ветер" and contributed songs) (#98)

jamesondh-agents[bot]188fbac120b4
diff --git a/src/song/tunes.ts b/src/song/tunes.ts
index 1d84f94..cdd43be 100644
--- a/src/song/tunes.ts
+++ b/src/song/tunes.ts
@@ -3,6 +3,8 @@
* written for this instrument except Twinkle (traditional). Each phrase is one line of lyrics; a song longer than the tune goes round again.
* Notes are [midi, beats], at the female voice's pitch; `octave` shifts the other bodies, as the
* demo songs do. Phrases end on a long note: the breath before the next line comes out of it.
+ * After the moods come the engine's other demo songs (contributed ones among them), by name and
+ * with their own words.
*/
import { DEMO_SONGS } from "../wasm/engine.ts";
@@ -12,17 +14,23 @@ export interface Tune {
bpm: number;
octave: { male: number; female: number; child: number };
phrases: [number, number][][];
+ /** a demo song's own words: choosing it puts them in the lyrics, and Sing sings them a syllable to a note */
+ lyrics?: string;
}
+/** the demo songs the moods take their melodies from */
+const borrowed = new Set<string>();
+
/** a demo song's melody (the engine's catalog, shared with `chimera render song`) cut into phrases of `sizes` notes */
function fromDemo(id: string, name: string, demo: string, sizes: number[]): Tune {
const d = DEMO_SONGS.find((s) => s.name === demo)!;
+ borrowed.add(demo);
let at = 0;
const phrases = sizes.map((n) => d.notes.slice(at, (at += n)).map(([m, b]): [number, number] => [m, b]));
return { id, name, bpm: d.bpm, octave: d.octave, phrases };
}
-export const TUNES: readonly Tune[] = [
+const MOODS: Tune[] = [
// "Made of air", the instrument's own: minor, unhurried
fromDemo("dreamy", "Dreamy", "Made of air", [9, 10]),
{
@@ -79,3 +87,15 @@ export const TUNES: readonly Tune[] = [
},
fromDemo("twinkle", "Twinkle, twinkle", "Twinkle, twinkle", [7, 7]),
];
+
+/** every other demo song as written: one phrase, its own words (a song added to the engine's catalog shows up here) */
+const SONGS: Tune[] = DEMO_SONGS.filter((d) => !borrowed.has(d.name)).map((d) => ({
+ id: `song:${d.name}`,
+ name: d.name,
+ bpm: d.bpm,
+ octave: d.octave,
+ phrases: [d.notes.map(([m, b]): [number, number] => [m, b])],
+ lyrics: d.lyrics,
+}));
+
+export const TUNES: readonly Tune[] = [...MOODS, ...SONGS];
diff --git a/src/ui/singing.ts b/src/ui/singing.ts
index 472aa3a..7d4e54d 100644
--- a/src/ui/singing.ts
+++ b/src/ui/singing.ts
@@ -80,6 +80,8 @@ function kept(): { lyrics: string; tune: string; tempo: number; key: number } {
}
const atLoad = kept();
+/** the player's words while a demo song's are in the lyrics */
+let ownWords: string | null = null;
export const useSing = create<SingState>((set, get) => ({
lyrics: atLoad.lyrics,
@@ -97,7 +99,20 @@ export const useSing = create<SingState>((set, get) => ({
},
setCursor: (i) => set({ cursor: Math.max(0, Math.min(i, Math.max(0, get().syllables.length - 1))) }),
setVibrato: (cents) => set({ vibrato: cents }),
- setTune: (id) => set({ tune: id }),
+ setTune: (id) => {
+ const words = TUNES.find((t) => t.id === id)?.lyrics;
+ const lyrics = get().lyrics;
+ const asWritten = TUNES.some((t) => t.lyrics === lyrics);
+ if (words !== undefined) {
+ // a demo song brings its words; the player's own come back with a mood
+ if (!asWritten) ownWords = lyrics;
+ if (lyrics !== words) get().setLyrics(words);
+ } else if (asWritten && ownWords !== null) {
+ get().setLyrics(ownWords);
+ ownWords = null;
+ }
+ set({ tune: id });
+ },
setTempo: (x) => set({ tempo: x }),
setKey: (semitones) => set({ key: semitones }),
}));
@@ -325,7 +340,15 @@ export async function playTune(id: string, body: BodyMix, ready?: () => Promise<
// read again: the dictionary may have arrived since the words were typed
const own = lyricsToSyllables(st.lyrics);
const words = own.length ? own : lyricsToSyllables(la(tune));
- const { notes, from } = fitSong(tune, words, tune.octave[nearestBody(body)] * 12 + st.key);
+ const shift = tune.octave[nearestBody(body)] * 12 + st.key;
+ // a demo song's own words, unedited: a syllable to each note as written (as `chimera render song`)
+ const { notes, from } =
+ tune.lyrics !== undefined && st.lyrics === tune.lyrics && own.length
+ ? {
+ notes: tune.phrases[0].map(([midi, beats], i) => ({ midi: midi + shift, beats, syl: own[i % own.length] })),
+ from: tune.phrases[0].map((_, i) => i % own.length),
+ }
+ : fitSong(tune, words, shift);
if (!notes.length) return false;
const bpm = tune.bpm * st.tempo;
const t0 = 0.35;
diff --git a/tests/singing-live.test.ts b/tests/singing-live.test.ts
index 1198b0f..baf419e 100644
--- a/tests/singing-live.test.ts
+++ b/tests/singing-live.test.ts
@@ -218,6 +218,46 @@ describe("Sing mode, live keys", () => {
engineStub.schedule = schedule;
});
+ test("every demo song can be chosen and sings its own words a syllable to a note; the player's words come back with a mood", async () => {
+ // #87's tunes menu left out the contributed demo songs ("Ночной ветер", Daisy Bell)
+ const { DEMO_SONGS } = await import("../src/wasm/engine.ts");
+ const { TUNES } = await import("../src/song/tunes.ts");
+ const melodies = TUNES.map((t) => JSON.stringify(t.phrases.flat()));
+ for (const d of DEMO_SONGS) expect(melodies).toContain(JSON.stringify(d.notes.map(([m, b]) => [m, b])));
+ const songs = TUNES.filter((t) => t.lyrics !== undefined);
+ expect(songs.map((t) => t.name)).toContain("Ночной ветер");
+ voice = new WasmVoice(FS);
+ const sing = useSing.getState;
+ let sent: { gestures: Gesture[] } | null = null;
+ const schedule = engineStub.schedule as typeof fakeEngine.schedule;
+ engineStub.schedule = (plan: { gestures: Gesture[] }) => ((sent = plan), schedule(plan));
+ sing().setLyrics("my own words");
+ for (const song of songs) {
+ sing().setTune(song.id);
+ expect(sing().lyrics).toBe(song.lyrics!);
+ expect(await playTune(song.id, MALE, () => Promise.resolve())).toBe(true);
+ expect(sent).not.toBeNull();
+ // a syllable to each note: the n-th note is sung while the n-th syllable is lit
+ const beat = 60 / song.bpm;
+ const n = Math.min(5, song.phrases[0].length - 1);
+ const start = song.phrases[0].slice(0, n).reduce((t, [, b]) => t + b * beat, 0.35);
+ planned({ type: "planned", id: 1, t0: voice.clock - start - 0.01, end: 0 });
+ tickSinging();
+ expect(sing().at).toBe(n % sing().syllables.length);
+ stopSinging();
+ }
+ sing().setTune("joyful");
+ expect(sing().lyrics).toBe("my own words");
+ // edited words are fitted to the song like any tune's
+ sing().setTune(songs[0].id);
+ sing().setLyrics("new words");
+ expect(await playTune(songs[0].id, MALE, () => Promise.resolve())).toBe(true);
+ stopSinging();
+ engineStub.schedule = schedule;
+ sing().setTune("dreamy");
+ sing().setLyrics("Hello there");
+ });
+
test("empty lyrics sing the tune on la, highlighting nothing", async () => {
voice = new WasmVoice(FS);
const sing = useSing.getState;