Media

Audio visualizer

Draws sound as it plays: bars for loudness from low notes to high, or the wave itself. Point it at an <audio> or <video> element, or a microphone.

Installation

npx sinaui add visualizer

For a file on another site, give the element crossorigin="anonymous", and the host must allow cross-origin requests. Otherwise the visualizer only hears silence.

Examples

Bars

Press play.

<script lang="ts">
	import Visualizer from '#lib/ui/Visualizer.svelte';

	let audio = $state<HTMLAudioElement>();
</script>

<!-- crossorigin="anonymous": the file's host must allow it, or the visualizer hears silence. -->
<div class="demo">
	<Visualizer source={audio} label="Sound levels of Taxim Hicaz" />
	<audio
		bind:this={audio}
		controls
		crossorigin="anonymous"
		preload="metadata"
		src="https://upload.wikimedia.org/wikipedia/commons/transcoded/0/0c/Taxim_Hicaz_%28c._1928%29.ogg/Taxim_Hicaz_%28c._1928%29.ogg.mp3"
	></audio>
</div>

<style>
	.demo {
		display: grid;
		gap: 0.75rem;
		inline-size: 100%;
		max-inline-size: 28rem;
	}
	audio {
		inline-size: 100%;
	}
</style>

Wave

<script lang="ts">
	import Visualizer from '#lib/ui/Visualizer.svelte';

	let audio = $state<HTMLAudioElement>();
</script>

<div class="demo">
	<Visualizer source={audio} variant="wave" />
	<audio
		bind:this={audio}
		controls
		crossorigin="anonymous"
		preload="metadata"
		src="https://upload.wikimedia.org/wikipedia/commons/transcoded/0/0c/Taxim_Hicaz_%28c._1928%29.ogg/Taxim_Hicaz_%28c._1928%29.ogg.mp3"
	></audio>
</div>

<style>
	.demo {
		display: grid;
		gap: 0.75rem;
		inline-size: 100%;
		max-inline-size: 28rem;
	}
	audio {
		inline-size: 100%;
	}
</style>

Microphone

Your browser asks first. Nothing is recorded or sent anywhere.

<script lang="ts">
	import { Mic01Icon, MicOff01Icon } from '@hugeicons/core-free-icons';
	import Button from '#lib/ui/Button.svelte';
	import Icon from '#lib/ui/Icon.svelte';
	import Visualizer from '#lib/ui/Visualizer.svelte';

	let stream = $state<MediaStream | null>(null);
	let problem = $state('');
	let asking = $state(false);

	async function start() {
		problem = '';
		asking = true;
		try {
			stream = await navigator.mediaDevices.getUserMedia({ audio: true });
		} catch (e) {
			problem =
				(e as DOMException).name === 'NotAllowedError'
					? 'Microphone access was turned down. Allow it in your browser’s site settings, then try again.'
					: 'No microphone could be found. Plug one in, then try again.';
		} finally {
			asking = false;
		}
	}
	function stop() {
		for (const t of stream?.getTracks() ?? []) t.stop();
		stream = null;
	}
	// Leaving the page turns the microphone off.
	$effect(() => stop);
</script>

<div class="demo">
	<Visualizer
		source={stream}
		mirror
		label={stream ? 'Your microphone’s sound levels' : undefined}
	/>
	{#if stream}
		<Button variant="secondary" onclick={stop}
			><Icon icon={MicOff01Icon} size={18} />Stop listening</Button
		>
	{:else}
		<Button loading={asking} onclick={start}
			><Icon icon={Mic01Icon} size={18} />Listen to the microphone</Button
		>
	{/if}
	{#if problem}<p role="alert">{problem}</p>{/if}
</div>

<style>
	.demo {
		display: grid;
		justify-items: center;
		gap: 0.75rem;
		inline-size: 100%;
		max-inline-size: 28rem;
	}
	p {
		margin: 0;
		color: var(--ui-danger);
		font-size: 0.875rem;
		text-align: center;
	}
</style>

Props

PropTypeDefault
sourceHTMLMediaElement | MediaStream | null
variant'bars' | 'wave''bars'
barsnumber32
mirrorboolean: grow from the middlefalse
labelstring

It takes the accent color, and its height from CSS (4rem by default): pass a class to change either.

Accessibility

  • Decorative by default and hidden from screen readers; give it a label to name it.
  • It only draws while sound is playing and it’s on screen.
  • With reduced motion, it updates a few times a second instead of every frame.
Introduction
Theming
Changelog
Button
Action button
Confirm button
Copy button
Hold to confirm
Split button
Calendar
Checkbox
Color picker
Combobox
Date picker
Date range picker
File drop
Form
Input
Mention input
Multi select
Number field
OTP input
Password input
Phone input
Radio cards
Radio group
Rich text editor
Search field
Segmented control
Select
Signature pad
Slider
Switch
Tag input
Upload
Wheel picker
Textarea
Time field
Toggle group
Toolbar
Alert dialog
Command palette
Context menu
Dialog
Drawer
Dropdown
Hover card
Popover
Tooltip
Announcement bar
Alert
Empty state
Meter
Progress
Rolling number
Skeleton
Spinner
Toast
Avatar
Avatar group
Badge
Card
Carousel
Chart
Chat
Code block
Data table
Folder
Kbd
Mind map
Resizable panels
Scroll area
Separator
Sortable
Table
Tag group
Virtual list
Audio player
Image
Image compare
Video
Visualizer
Breadcrumb
File tree
Menubar
Navigation menu
Pagination
Sidebar layout
Stepper
Tabs
Alarm clock
Clock
Dynamic island
Fluid orb
Hourglass
Stopwatch
Timer
AI chat
AI media
Auth
Board
Booking
Dashboard
File explorer
Messaging
Notifications
Settings
Team
Overview
Pricing
Agent chat
Calendar
Voice agent
Mail inbox
CMS
Blog
Marketing site
E-commerce
AI agents
SaaS landing page
SaaS app
Accordion
Dark theme
Light theme