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

audio2d

v1.0.1

Published

Easy to use API to add the power of web audio to your game.

Downloads

25

Readme

NPM version Known Vulnerabilities npm NPM downloads Gitter

Table of Contents

Installation

To install audio2d, you can use:

$ npm install audio2d

and then initialize it like so:

Current Version

import { Audio2D } from 'audio2d';

Pre 1.0.0

import Audio2D from 'audio2d';

or you can use it as a script from unpkg like so:

<script type="module" src="https://unpkg.com/audio2d@latest/audio2d.js"></script>

A Note About Preloading

After some thought while creating Audio2D, I decided that it should not offer loading of audio assets.

I was going to initally offer it as a feature but after trying to figure out signal timing and letting the user know when the audio was loaded and ready to go, it turned into a signal based monstrosity. The great thing is that it doesn't need to be a monstrosity, there are preloaders out there, like musk-ox for example, that offer to load any of your assets for you, including audio buffers which is what Audio2D works with.

For an example on basic usage with musk-ox, check out the following example:

const a2d = new Audio2D();
const muskox = new MuskOx();

muskox.onComplete.add(loaded);

const loaded = () => {
  const levelUpBuffer = muskox.fetch.audioBuffer('level-up');

  const levelUp = a2d.addAudio('level-up', levelUp);

  levelUp.play();
}

muskox.load.audioBuffer('level-up', './assets/audio/level-up.m4a');

muskox.start();

API

All examples are assumed to be running using existing audio buffers.

addAudio

Creates a web audio clip from an AudioBuffer and returns it so you can play it or perform other actions.

| param | type | description | default | |-----------------|---------------|----------------------------------------------------------------------------------------------------------------|---------| | name | string | The name of the audio clip used to reference it. | | | buffer | AudioBuffer | The buffer of the audio clip. | | | options | Object | | | | options.markers | Array<Marker> | Markers that can define specific points during the audio track that can be played independently of each other. | | | options.trigger | string | An id or classname of a dom element that when clicked will trigger the clip to play. | |

examples:

Creating a basic audio clip with no markers:

const levelUp = a2d.addAudio('level-up', levelUpBuffer);

Creating an audio clip with specific markers:

const sfxMarkers = [
  { name: 'walk', start: 1500, duration: 1000 },
  { name: 'fall': start: 2500, duration: 1500 },
  { name: 'collect-coin': start: 4000, duration: 750 }
];

const sfx = a2d.addAudio('sfx', sfxBuffer, { markers: sxfMarkers });

In the Audio API you can see how to use the markers and play only parts of a track.

getAudio

Gets an audio clip added to the media library with addAudio.

| param | type | description | default | |-----------------|---------------|----------------------------------------------------------------------------------------------------------------|---------| | name | string | The name of the audio clip to get. | |

example:

a2d.addAudio('track1', buffer);

const clip = a2d.getAudio('track1');

removeAudio

Removes an audio clip from the media library.

| param | type | description | default | |-----------------|---------------|----------------------------------------------------------------------------------------------------------------|---------| | name | string | The name of the audio clip to remove. | |

example:

a2d.addAudio('track1', buffer);

a2d.removeAudio('track1');

removeAllAudio

Removes all audio clips from the media library.

example:

a2d.addAudio('track1', buffer1);
a2d.addAudio('track2', buffer2);

a2d.removeAllAudio();

Audio Clip

The following properties and methods are available to use on audio clips after they have been created with addAudio.

Audio Clip Properties

name

Returns the name of the clip.

state

Returns the current state of the clip. These states are either 'STOPPED', 'PLAYING', or 'PAUSED'.

timesPlayed

Returns the number of times that this clip has been played.

currentTime

Returns the current time of the clip.

duration

Returns the duration of the audio clip.

volume

Returns the current volume of this clip. This overrides the global volume if one was set.

volume

Sets the volume for the clip to use from here on out. The volume provided can be any number from 0-100. This overrides any global volume set.

| param | type | description | default | |--------|--------|-------------------------------------------------------------------|---------| | vol | number | The new volume for this clip | |

Audio Clip Methods

addNode

You can choose to add a web audio node to the clip. Nodes will be connected to each other in the order added with the source always at the start and the gain always at the end of the chain.

The available nodes are:

  • biquadFilter

and they can be created through the nodes property on the audio2d instance like so:

example:

const track = a2d.addAudio('track-1', track1Buffer);

const bf = a2d.nodes.biquadFilter();

track.addNode(bf);

After adding nodes, you can then modify the properties of the nodes by the nodes property of the audio clip and the name of the node like so:

const track = a2d.addAudio('track-1', track1Buffer);

a2d.nodes.biquadFilter();

track.addNode(bf);

// Modifying the biquad filter.
track.nodes.biquadFilter.Q.value = 100;
track.nodes.biquadFilter.frequency.value = 1;

play

Plays an audio clip fully or at one of the markers.

| param | type | description | default | |--------|--------|-------------------------------------------------------------------|---------| | marker | string | The name of the marker to play instead of playing the whole clip. | '' |

examples:

Creating an audio clip and playing it in its entirety:

const levelUp = a2d.addAudio('level-up', levelUpBuffer);

levelUp.play();

Creating an audio clip and defining markers to play:

const sfxMarkers = [
  { name: 'walk', start: 1500, duration: 1000 },
  { name: 'fall': start: 2500, duration: 1500 },
  { name: 'collect-coin': start: 4000, duration: 750 }
];

const sfx = a2d.addAudio('sfx', sfxBuffer, { markers: sxfMarkers });

// play just the falling sound.
sfx.play('fall');

pause

Pauses an audio clip.

example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();

setTimeout(() => {
  sfx.pause();
}, 1000);

resume

Resumes playing an audio clip from a paused state.

*example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();

setTimeout(() => {
  sfx.pause();

  setTimeout(() => {
    sfx.resume();
  }, 1000);
}, 1000);

stop

Completely stops the playback of an audio clip and resets it so next time it plays it will play from the beginning.

example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();

setTimeout(() => {
  sfx.stop();
}, 1000);

seek

Seeks to a specific time in the clip.

| param | type | description | default | |--------|--------|----------------------------------------|---------| | time | number | The time, in milliseconds, to seek to. | |

example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();

setTimeout(() => {
  // After a second, we skip to the 12 second mark.
  sfx.seek(12000);
}, 1000);

mute

Mutes an audio clip but saves the previous volume so when its unmuted it will go back to the volume it was before.

example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();
sfx.mute();

unmute

Unmutes an audio clip and sets the volume to what it was before it was muted.

example:

const sfx = a2d.addAudio('sfx', sfxBuffer);

sfx.play();

sfx.mute();
sfx.unmute();

Tests

The tests for Audio2D are browser based so to run them you will need to use:

$ npm run test

and then you'll have to navigate to localhost:8888/test/index.html to see all of the tests run.

License

MIT