Public
Edited
Feb 1
Insert cell
Insert cell
{
const svg = cm.svg;
const url = await FileAttachment("pink.wav").url();
const sound = new Tone.Player(url).toDestination();
const player = usePlayer(sound);

const [vars, dispose] = cm
.flow()
.let("progress", 0)
.on("keydown", () => player.start())
.on("loop", (d) => (d.progress = player.progress()))
.join();

function usePlayer(player) {
let startTime;

const start = (...params) => {
startTime = params[0] ?? Tone.now();
player.start(...params);
};

const progress = () => {
if (player.state !== "started") return 0;
const currentTime = Tone.now() - startTime;
return currentTime / player._buffer.duration;
};

return { start, progress };
}

await Tone.loaded;

invalidation.then(() => dispose());

return svg.svg({ width, height: 100 }, [
svg.rect({ x: 0, y: 0, width, height: 100, fill: "black" }),
svg.circle({
cx: cm.$(() => cm.map(vars.progress, 0, 1, 0, width)),
cy: 50,
r: 40,
fill: "white"
})
]);
}
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more