Waveform Timeline
An interactive audio waveform for the browser: play, zoom, loop, and mark segments and points on a tempo grid. Press play, zoom in, drag a segment, or add one at the playhead.
Full page version here: /waveform-timeline/app/
What it is
Section titled “What it is”A fork of the BBC's peaks.js, with the API close to upstream and these differences:
- Strict TypeScript, with no
anyin the public API. - Pluggable drivers. The canvas driver draws the views, and the audio driver plays the sound: an
<audio>element, or Web Audio through ClipNode, which owns itsAudioBuffer. - Promises and results.
WaveformTimeline.from()returns a promise,tryFrom()aResult, and the events go through a typed event bus. There are no callback APIs. - No npm dependency. Own code replaces three: a 2D canvas driver in place of Konva, the waveform data in place of waveform-data, and a generator of
.datfiles from WAV files in place of BBC audiowaveform. The demo draws the.datfile that the generator made.
import { ClipNodeAudioDriver, createCanvas2DDriver, WaveformTimeline } from "@joellof/waveform-timeline";
const audioContext = new AudioContext();
const buffer = await fetch("/sample.mp3")
.then((response) => response.arrayBuffer())
.then((bytes) => audioContext.decodeAudioData(bytes));
const timeline = await WaveformTimeline.from({
audio: ClipNodeAudioDriver.from({ buffer, context: audioContext }),
driver: createCanvas2DDriver(),
zoomview: { container: document.getElementById("zoomview-container")! },
overview: { container: document.getElementById("overview-container")! },
zoomLevels: [128, 256, 512, 1024, 2048, 4096],
keyboard: true,
});
timeline.events.addEventListener("player.timeupdate", (event) => console.log(event.time));
timeline.segments.add({ startTime: 4, endTime: 8, labelText: "Verse", editable: true });
timeline.zoom.zoomIn();
timeline.player.play();With an error value in place of a rejection:
const result = await WaveformTimeline.tryFrom(config);
if (!result.ok) {
console.error(result.error);
return;
}
const timeline = result.value;WaveformTimeline.fromUrl({ url, audioContext }) fetches and decodes the audio itself, as the demo does.
Related Projects
Section titled “Related Projects”- ClipNode — AudioWorklet node for buffer playback with loop crossfade
- Tempo Detector (BPM) — the tempo of a song or a loop, in your browser