SearchCtrlK

  • ↑↓navigate
  • ↵open
  • escclose

Can’t find it? Contact us

Generated audio and images

Declare music, sounds and pictures the page renders itself as Blobs, such as a song from @sdxc/synth, and play them without a file to host.

After reading this guide, you will know

  • How to declare a Blob as an asset beside URLs
  • How the player plays and draws a Blob, and when a generated asset belongs in a file instead
  • How to give the menus ready-made hover and select sounds from @sdxc/novel/sounds

A jam game has no composer, and a procedural story draws its own skies. Every asset kind takes a Blob wherever it takes a URL: a string is a file to fetch, and a Blob is a file the page already holds, which plays or draws without a request. This guide gives the café a theme rendered by @sdxc/synth and a sky drawn as SVG.

Terminal
npm add @sdxc/novel @sdxc/synth

Render the files

renderSong writes a song from a seed, and encodeWav turns its samples into a WAV file. A picture is any image the browser decodes, such as an SVG written as text:

resources/novel/generated.ts
import { encodeWav, renderSong, seedFrom } from "@sdxc/synth";

const SAMPLE_RATE = 22_050;

export function song(id: string): Blob {
	let samples = renderSong(
		{
			seed: seedFrom(id),
			style: "lofi",
			key: "A2",
			mode: "dorian",
			duration: 45,
		},
		{ sampleRate: SAMPLE_RATE },
	);
	return new Blob([encodeWav(samples, SAMPLE_RATE)], { type: "audio/wav" });
}

export function sky(top: string, bottom: string): Blob {
	let svg = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 9">
		<linearGradient id="g" x2="0" y2="1"><stop stop-color="${top}"/><stop offset="1" stop-color="${bottom}"/></linearGradient>
		<rect width="16" height="9" fill="url(#g)"/>
	</svg>`;
	return new Blob([svg], { type: "image/svg+xml" });
}

The same seed gives the same song on every run, so a reload plays the theme the reader heard before.

Declare them beside URLs

Pass the Blobs to assets() where a URL would go. A group can mix both, and a per-locale map takes either for each locale:

resources/novel/declarations.ts
import { assets } from "@sdxc/novel";

import { sky, song } from "~/resources/novel/generated";

import cafe from "./images/cafe.webp";
import rain from "./audio/rain.ogg";

export const ASSETS = assets({
	backgrounds: { cafe, dusk: sky("#f4a261", "#264653") },
	music: { theme: song("cafe-theme") },
	ambience: { rain },
});

Scenes use them like any other asset: ctx.show(ASSETS.backgrounds.dusk), ctx.play(ASSETS.music.theme, { fade: 1500 }).

How the player handles a Blob

  • Images, music and ambience use one object URL per Blob, made the first time it shows or plays and kept for the life of the page, so every replay reuses it.

  • Sounds and voices decode straight from the Blob's bytes.

  • The server draws a Blob image without src, and the browser sets it as it hydrates.

  • Prefetch, novel.links() and the offline cache list URLs only, since a Blob has nothing to fetch.

  • Saves keep asset ids, so a loaded save draws the Blob the declaration holds now.

Sound the interface

@sdxc/novel/sounds synthesizes hover and select sounds in the browser, so the menus sound without a file to ship. renderInterfaceSounds() renders a preset into the two declarations the player's sounds prop takes. Synthesis costs a few milliseconds, so render once the player is on the page, in a task the component queues:

resources/components/game.tsx
import type { Handle } from "remix/component";

import { renderInterfaceSounds } from "@sdxc/novel/sounds";
import { NovelPlayer } from "@sdxc/novel/ui";

import { novel } from "~/app/initializers/novel";

function Game(handle: Handle) {
	let sounds: ReturnType<typeof renderInterfaceSounds> | undefined;
	handle.queueTask(() => {
		sounds = renderInterfaceSounds("soft");
		void handle.update();
	});
	return () => <NovelPlayer.Default novel={novel} sounds={sounds} />;
}

Three presets ship, each under a quarter of a second and quiet:

PresetHoverSelect
softa rounded sine popa brush of paper
ticka dry switch ticka short click with some body
chimea soft sine notetwo rising notes

To change one sound, spread a preset from INTERFACE_SOUND_PRESETS and pass the result. Each sound is an @sdxc/synth instrument, its notes, a gain and a tail in seconds:

typescript
import { INTERFACE_SOUND_PRESETS, renderInterfaceSounds } from "@sdxc/novel/sounds";

let soft = INTERFACE_SOUND_PRESETS.soft;
let sounds = renderInterfaceSounds({
	...soft,
	hover: { ...soft.hover, notes: [{ at: 0, length: 0.02, pitch: "G5" }] },
});

A sound written from scratch is a short note from renderScore, declared like any other sound and passed as sounds={UI_SOUNDS}:

resources/novel/generated.ts
import { assets } from "@sdxc/novel";
import { PRESETS, encodeWav, renderScore } from "@sdxc/synth";

export function blip(pitch: string, length = 0.125): Blob {
	let samples = renderScore(
		{
			tempo: 120,
			tracks: [
				{ instrument: PRESETS.pluck, notes: [{ at: 0, length, pitch }] },
			],
		},
		{ sampleRate: SAMPLE_RATE },
	);
	return new Blob([encodeWav(samples, SAMPLE_RATE)], { type: "audio/wav" });
}

export const UI_SOUNDS = assets({
	sounds: { hover: blip("E6", 0.0625), select: blip("A5") },
}).sounds;

The hover sound plays as the pointer enters a button, tab or link, or keyboard and gamepad focus moves to one, and the select sound as the reader picks one, on the title screen and in play. Clicking the stage to advance stays silent. Both play on their own channel, which the settings screen lists as Interface, so a reader can turn them down without losing the story's sounds.

Test the declarations

check() reads Blob assets like any other, so the declaration test from Make your first novel covers them. A test can also assert that a rendered file is what the player expects:

resources/novel/generated.test.ts
import { expect, test } from "vitest";

import { song } from "~/resources/novel/generated";

test("the theme is a WAV file", async () => {
	let theme = song("cafe-theme");
	let header = new TextDecoder().decode((await theme.arrayBuffer()).slice(0, 4));

	expect(theme.type).toBe("audio/wav");
	expect(header).toBe("RIFF");
});

Where to go next

Documentation

Can’t find it? Contact us