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

@ninefour/euphony

v1.0.2

Published

Library for playback and visualization of audio via the WebAudio API

Downloads

8

Readme

euphony

euphony is a lightweight audio playback and visualization library for Javscript with zero dependencies and full Typescript support

Despite being designed with audio visulization in mind it can just as well be used for simple audio playback. The library allows for easy loading, playing, pausing, synchronization, visualization and more of audio files

For more thorough documentation see: https://dawsinb.github.io/euphonydocs/

Set Up

To install via npm use

npm install @ninefour/euphony

Then in your node.js file import as needed

import { Playback, Group } from '@ninefour/euphony';

Usage

Note: Almost all browsers require user interaction (usually a click) before allowing audio playback. For clarity and brevity these examples work assuming said user interaction has already happened

Basic Playback

Load a local audio file and play it back

const audio = new Source();
audio.load('/path/to/audio.mp3').then(audio.play());

Or load an external audio file

audio.load('http://yoursite.com/path/to/audio.mp3');

If you have multiple audio sources that need to be downmixed you can make use of euphony's groups

const melody = new Source();
const percussion = new Source();
const song = new Group({ sources: [melody, percussion] });

Promise.all([
	melody.load('/path/to/melody.mp3'),
	percussion.load('/path/to/percussion.mp3')
]).then(song.play());

You can even create sub-groups

// melody
const melody = new Source();
// percussion group
const kick = new Source();
const hat = new Source();
const snare = new Source();
const percussion = new Group({ sources: [kick, hat, snare] })

// put it all together
const song = new Group({ sources: [melody, percussion] });

Options for configuring your Playbacks/Groups can be given in the constructor, or set afterwards

// set in constructor
const melody = new Source({ volume: 0.8, loop: true })
// set individually
const percussion = new Source();
percussion.volume = 0.5;
percussion.loop = true;

Visualization

All Playbacks and Groups come with an attached Analyser that allows for easy computation and retrival of frequency and or waveform data. To save on computational resources analysis data is only computed when specifically called. Further, the analysis is split between computing frequency and waveform data seperately with updateFrequency() and updateWaveform() respectively

For conveniece the frequency or waveform data can be updated for all members of a group by calling updateFrequencyAll() or updateWaveformAll()

The data available on updateFrequency() is as follows:

  • Frequency: Array of normalized frequency data where each member of the array is a number between 0.0 and 1.0 representing the amplitude of the frequency at a given frequency band starting from 0 hz
  • Ampltiude: The mean normalized frequency data representing the overall amplitude of the audio
  • Signal: A boolean signal that is true when the amplitude is over a given threshold

The data available on updateWaveform() is as follows:

  • Waveform: Array of normalized waveform (aka time-domain) data, where each member is a number between -1.0 and 1.0

Example Visualization Set Up

A basic example template of how one might go about setting up euphony to playback audio and retrieve audio analysis data to use in an animation

// initialize audio
const melody = new Playback({ loop: true });
const percussion = new Playback({ loop: true });
const song = new Group({ sources: [melody, percussion] });
Promise.all([
	melody.load('/path/to/melody.mp3'),
	percussion.load('/path/to/percussion.mp3')
]).then(song.play());

// set up render loop
const render = () => {
	// update frequency data
	melody.analyser.updateFrequency();
	percussion.analyser.updateFrequency();
	// or waveform data
	song.analyser.updateWaveform();
	
	// access data to use in animations ...
	
	requestAnimationFrame(render);
}
requestAnimationFrame(render);

Example Projects

For some more real world examples of euphony in action check out these codesandboxes:

  • TODO