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

audio-manager

v0.1.18

Published

Play sounds using Web Audio, fallback to HTML5 Audio

Downloads

101

Readme

AudioManager

Play sounds using Web Audio, fallback on HTML5 Audio. AudioManager is specifically designed to works for games that have a big quantity of audio assets. Loading and unloading is made easy and transparent.

Install with NPM

API

// initialisation
var audioManager = new AudioManager(channelIds);
audioManager.init();
audioManager.setVolume(channelId, volume);

// play simple sound
audioManager.playSound(channelId, soundId, volume, panoramic, pitch);

// sound group
audioManager.createSoundGroups(soundGroupDefs, channelId);
audioManager.createSoundGroup(soundGroupId, soundGroupDef, muted);
audioManager.playSoundGroup(channelId, groupId, volume, panoramic, pitch);

// loop
audioManager.playLoopSound(channelId, soundId, volume);
audioManager.stopLoopSound(channelId);
audioManager.stopAllLoopSounds();

// release memory
audioManager.release();

Documentation

Create audioManager object and channels

Pass the list of channels to the constructor as an array of strings.

var channels = ['music', 'sfx', 'ui'];
var audioManager = new AudioManager(channels);

Setup audioManager path to sound assets folder

audioManager.settings.audioPath = 'assets/audio/';

Start audio engine.

To work correctly on iOS, this must be called on an user interaction e.g. user pressing a button. (C.f. this page to understand how this work.)

gameStartButton.on('tap', function () {
	audioManager.init();
});

Set channel volume

By default, channel volume is set to 0 and channel is muted. No sounds will play until channel volume is set.

var volume = 1.0; // volume is a float in range ]0..1]
audioManager.setVolume('ui', volume);

Create and play a simple sound.

Create a sound and playing it in a channel. Sound is created and loaded automatically.

var fileName = 'laser1';
audioManager.playSound('sfx', fileName);

// volume and panoramic can be set optionally
var volume    = 0.7; // volume is a float in range ]0..1]
var panoramic = 0.9; // panoramic is a float in range [-1..1], 0 is the center
audioManager.playSound('sfx', fileName, volume, panoramic);

Sounds creation and preloading can be forced.

audioManager.createSound(fileName).load();

Alternatively, sounds can be played outside channels.

var sound = audioManager.createSound(fileName);
sound.play(volume, panoramic, pitch); // all parameters are optional.

Detect playback end

An onEnd callback function can be set to be triggered everytime the playback ends. Set it back to null to remove it.

sound.onEnd = function () {
	console.log('sound playback ended');
	sound.onEnd = null;
};

Change pitch

This feature is only available with WebAudio.

var pitch = -7.0; // in semi-tone
sound.setPitch(pitch);

The pitch can be set at play.

audioManager.playSound('ui', fileName, volume, panoramic, pitch);

While a sound is playing, the pitch can be changed dynamically

var portamento = 3.0;
sound.setPitch(pitch, portamento);

Create and play sound groups.

A sound group is a collection of sounds that will play alternatively in a round-robin pattern on each play call.

var soundGroupDef = {
	id:  ['punch1', 'punch2'],
	vol: [1.0, 0.8],
	pitch: [0.0]
};
audioManager.createSoundGroups('punch', soundGroupDef);

var volume    = 0.8; // volume is a float in range ]0..1]
var panoramic = 0.3; // panoramic is a float in range [-1..1], 0 is the center
var pitch     = 3.0; // in semi-tone
audioManager.playSoundGroup('sfx', 'punch', volume, panoramic, pitch);

You can create several sound groups in one function call

var soundGroupDefs = {
	groupId1: { id: ['sound1', 'sound2'], vol: [1.0, 0.8], pitch: [0.0] },
	groupId2: { ... },
	...
};
audioManager.createSoundGroups(soundGroupDefs, 'sfx');

Play and stop looped sounds

Only one loop can play per channel. Playing a new looped sound in the same channel will stop current playing sound before starting new one.

var volume   = 1.0; // volume is a float in range ]0..1]
var fileName = 'bgm1';
audioManager.playLoopSound('music', fileName, volume);
audioManager.stopLoopSound('music'); // stop looped sound in channel 'music'
audioManager.stopAllLoopSounds();    // stop all looped sounds in all channel

How looped sound fades and mixes can be set by modifying audioManager settings:

audioManager.settings.defaultFade = 3;    // value in second
audioManager.settings.crossFade   = true; // default is false

Release memory

audioManager.release();

Custom loader function

audioManager.settings.getFileUri can be overriden if you want more control on how to resolve files uri from sound id. The function have one on the following synchronous or asynchronous profile:

function getFileUriSync(audioPath, id) {
	var uri;
	// ...
	return uri;
}
function getFileUriAsync(audioPath, id, callback) {
	var uri;
	var error = null;
	// ...
	return callback(error, uri);
}

For instance, here's how you can add wizAssets to cache files on disc in a PhoneGap application:

if (window.wizAssets) {
    audioManager.settings.getFileUri = function wizAssetsGetUri(audioPath, id, cb) {
        window.wizAssets.downloadFile(audioPath + id + '.mp3', 'audio/' + id + '.mp3', 
            function onSuccess(uri) {
                cb(null, uri)
            }, 
            cb // onFail callback
        );
    }
}