A podcast player
An hour-long episode that shows its whole wave at once, chapters to jump between, speed and skip buttons, zoom to find a moment, and a description for listeners who cannot see it.
1. Prepare the peaks on upload
An hour of audio should never be decoded in the browser. When an episode is uploaded, write its peaks once, at 40 points per second for zooming in:
// On your server (Node), after decoding the upload:
import { writeFile } from 'node:fs/promises';
import { describeSound, outlineSound, writeWaveform } from 'sonicframe/analysis';
const bytes = writeWaveform(channels, {
sampleRate,
samplesPerPeak: Math.round(sampleRate / 40),
bits: 8,
});
await writeFile(`episodes/${id}.dat`, bytes);
2. Build the player
import { markers } from 'sonicframe';
import { createPlayer, loadWaveform } from 'sonicframe/player';
const player = createPlayer({
container: document.querySelector('#episode'),
src: '/episodes/12.mp3',
preload: 'none',
peaks: () => loadWaveform('/episodes/12.dat', { maxPeaks: 200000 }),
theme: 'card',
look: { line: ['play', 'back', 'wave', 'forward', 'time', 'speed'], skip: 15 },
zoom: { wheel: true, keys: true, follow: true, buttons: true },
minimap: { autoHide: true },
a11y: {
label: 'Episode 12: the sound of cities',
description: { keys: true },
zoom: { announce: true },
},
plugins: [
markers({
items: [
{ time: 0, label: 'Intro' },
{ time: 312, label: 'Interview' },
{ time: 2410, label: 'Questions' },
],
}),
],
});
-
The length shows at once, from the
.datfile, and no audio loads until play. - Chapters are markers: a click or M jumps to the next.
- Back and forward skip 15 seconds; speed steps through 1×, 1.5×, 2×.
- Zoom with Ctrl + wheel or +, and the view follows the sound while it plays.
- The wave in words tells a listener where the sound and the pauses are.
3. Remember where they stopped
remember brings the listener back where the
episode was left, at the volume and speed they chose. An
episode left near its end starts over.
const player = createPlayer({
// …the options above, and:
remember: { key: 'podcast', sound: 'episode-12' },
keys: true, // Space, M, the arrows, > and <
a11y: { remember: { announce: true } }, // "Resuming at 12:30"
});
The position, the volume and the speed stay in the
page's localStorage (see
Remembering); player.forget() clears them.