npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-audio-waveform

v0.0.5

Published

Generate stunning audio waveforms Svelte and Canvas. Transform an array of peak data into beautifully rendered, customizable waveforms for music players, podcasts, audio editing tools, and more.

Downloads

316

Readme

Svelte Audio Waveform

The fastest way to add stunning audio visualizations to your Svelte apps. Create beautiful, responsive waveform visualizations from audio files with progress indication and customizable styling. Perfect when you need professional-looking audio visualization without the complexity of heavyweight solutions like Wavesurfer.

Perfect for:

  • 🎵 Music players
  • 🎙️ Podcast interfaces
  • 📞 Voice message previews
  • ✂️ Audio editing tools
  • 🌟 Any project where audio visualization matters

Installation

Install the package using npm:

npm install svelte-audio-waveform

Features

  • 🎨 Customizable colors and styling
  • 🌈 Gradients support
  • 📊 Bar and wave display modes
  • 🎯 Progress indication
  • 📱 Responsive design
  • 🔍 High-resolution support (600/1200 peaks)

Svelte Audio Waveform Demo


Basic Usage

<script>
  import AudioWaveform from 'svelte-audio-waveform';

  let peaks = []; // Array of peak data
  let position = 0.5; // Current playback position as a percentage (0 to 1)
</script>

<AudioWaveform 
  peaks={peaks} 
  position={position}
/>

Examples

1. Standard Waveform

A basic waveform with default settings.

<AudioWaveform 
  peaks={peaks} 
  position={0.5} 
/>

2. Bar Mode

Display the waveform as bars instead of a continuous wave.

<AudioWaveform 
  peaks={peaks} 
  position={0.5}
  barWidth={2}
/>

3. Custom Colors

Customize the waveform and progress colors.

<AudioWaveform 
  peaks={peaks} 
  position={0.5}
  color="#444444"
  progressColor="#ff0000"
/>

4. Gradient Colors

Apply beautiful gradients to your waveform.

<AudioWaveform 
  peaks={peaks} 
  position={0.5}
  gradientColors={["red", "blue", "green"]}
  progressGradientColors={["yellow", "orange", "purple"]}
/>

5. Full Example with Audio File Upload

This example shows how to load an audio file, generate peaks, calculate playback progress, and display a waveform with playback controls.

<script lang="ts">
  import AudioWaveform from 'svelte-audio-waveform';

  let peaks = [];
  let progress = 0; // Current playback progress as a percentage (0 to 1)
  let audio: HTMLAudioElement;
  let isPlaying = false;

  async function handleAudioFile(event: Event) {
    const file = (event.target as HTMLInputElement).files?.[0];
    if (!file) return;

    // Generate peaks (use your own utility or copy from repo)
    const buffer = await createAudioBuffer(file);
    peaks = getPeaks(buffer, { numberOfBuckets: 1200 });

    // Create audio element for playback
    const url = URL.createObjectURL(file);
    audio = new Audio(url);

    // Set up audio events to calculate progress
    audio.addEventListener('timeupdate', () => {
      progress = audio.currentTime / audio.duration; // Calculate progress as a percentage (0 to 1)
    });

    audio.addEventListener('play', () => {
      isPlaying = true;
    });

    audio.addEventListener('pause', () => {
      isPlaying = false;
    });

    audio.addEventListener('ended', () => {
      isPlaying = false;
      progress = 0;
    });
  }

  function handleSeek(event: CustomEvent<number>) {
    if (!audio) return;
    const newTime = event.detail * audio.duration; // Convert percentage to time in seconds
    audio.currentTime = newTime;
    progress = event.detail; // Update progress directly as a percentage (0 to 1)
  }

  function togglePlayPause() {
    if (!audio) return;
    if (isPlaying) {
      audio.pause();
    } else {
      audio.play();
    }
  }
</script>

<div class="audio-player">
<input type="file" accept="audio/*" on:change={handleAudioFile} />

{#if peaks.length > 0}
<div class="waveform-container">
  
<AudioWaveform 
    peaks={peaks} 
    position={progress}
    color="#444444"
    progressColor="#ff0000"
    onSeek={handleSeek}
/>

<div class="controls">
<button on:click={togglePlayPause}>
{isPlaying ? '⏸️ Pause' : '▶️ Play'}
</button>

<div class="time-display">
{formatTime(progress * audio.duration)} / {formatTime(audio.duration)}
</div>
</div>
</div>
{/if}
</div>

API Reference

AudioWaveform Props

| Prop | Type | Default | Description | |-----------------------|---------------------|-------------------|-----------------------------------------------------| | peaks | number[] | required | Array of audio peak values | | position | number | required | Current playback position as a percentage (0 to 1) | | color | string | 'grey' | Waveform base color | | progressColor | string | 'purple' | Progress color | | gradientColors | string[] | undefined | Colors for gradient styling | | progressGradientColors | string[] | undefined | Gradient colors for the progress bar | | height | number | required | Height of the waveform | | width | number | required | Width of the waveform | | barWidth | number | undefined | Width of bars (if using bar mode) |


Utilities (Optional)

This package does not include utility functions like getPeaks, createAudioBuffer, or formatTime. However, you can copy these utilities from the source repository if needed.


Acknowledgments