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

squiggl

v1.0.3

Published

A javascript library to create a squiggly SVG waveform from audio data.

Downloads

6

Readme

squiggl

A javascript library to create a squiggly SVG waveform from audio data. Inspired by the podcast player by Megaphone.

Example waveform

Responsive Demo

I decided to split the squiggl functionality into 3 parts (one async, two sync) so that the audio data can be read once, then re-formatted and re-rendered multiple times without too much I/O. This way, the waveform can be re-rendered in response to changing screen widths.

Check it out.

Installation

Via <script> tag (example):

<script src="https://unpkg.com/squiggl/dist/index.js"></script>

Via npm i squiggl:

const squiggl = require('squiggl');
// or
import squiggl from 'squiggl';

Usage

The squiggl object consists of 3 functions:

  • extractVolumeData
  • formatVolumeData
  • createElement

These functions are used in tandem to:

  1. Extract the useful bits of data we need from an audio file.
  2. Format the data into a shape that we can use.
  3. Create an SVG element that is the visualization of the data.

Quick Example

// Step 0.1: Define squiggly SVG element's parent container.
var container = document.querySelector( '#waveform-container' );
// Step 0.2: Define options.
const options = {
  svg: {
    id: 'squigglSvg',
    width: 810,
    height: 60
  },
  path: {
    arcRadius: 2.25, // recommended
    stroke: '#eaeaef',
    strokeWidth: 1.2 // recommended
  }
};

fetch( '/radiolab.mp3' )
  // Step 0.3: Retrieve an audio file.
  .then( res => res.arrayBuffer() )
  // Step 1: Extract the useful bits of data we need.
  .then( audioData => squiggl.extractVolumeData( audioData ) )
  .then( volumeData => {
    // Step 2: Format the data into a shape that we can use.
    const formattedVolumeData = squiggl.formatVolumeData( volumeData, options.svg.width, options.path.arcRadius );
    // Step 3: Create an SVG element that is the visualization of the data.
    const svgElement = squiggl.createElement( formatVolumeData, options );

    container.appendChild( svgElement );
  });

Detailed Explanation

options

squiggl requires you to define certain attributes of its internal <svg> and <path> elements through the options parameter. This allows you to dynamically define certain visual properties of the waveform.

// Example
const options = {
  svg: {
    id: 'squigglSvg',
    width: 810,
    height: 60
  },
  path: {
    arcRadius: 2.25, // recommended
    stroke: '#eaeaef',
    strokeWidth: 1.2 // recommended
  }
};

extractVolumeData( audioData )

| Parameter | Type | Description | | --- | --- | --- | | audioData | ArrayBuffer | This is decoded using the decodeAudioData function from either window.AudioContext or window.webkitAudioContext (whichever is available). We can then call getChannelData(0) from the resulting buffer to pull out the volume data. |

| Returns | | --- | | Array of Numbers between -1.0 and 1.0 |

This function doesn't care where you get your audio data from (HTTP request, file input, etc...) as long as it's compatible with the AudioContext API.

formatVolumeData( volumeData, width, arcRadius )

| Parameter | Type | Description | | --- | --- | --- | | volumeData | Array of Numbers between -1.0 and 1.0 | Ideally generated from the extractVolumeData function. | | width | Number | The desired width of your SVG. Used to determine how many squiggles to draw. | | arcRadius | Number | The radius of each turn in the waveform. Used to determine how many squiggles to draw. |

| Returns | | --- | | Array of Numbers between 0.0 and 1.0 |

This function formats the audio data into a smaller set of numbers that models the number of squiggles in the waveform, and their magnitudes.

createElement( volumeData, options )

| Parameter | Type | Description | | --- | --- | --- | | volumeData | Array of Numbers between 0.0 and 1.0 | Ideally generated from the formatVolumeData function. | | options | Object | See above. |

| Returns | | --- | | An <svg> Element that is ready to be added to the DOM. |