3.0.1 • Published 4 years ago

musquito v3.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
4 years ago

musquito

musquito is an audio engine created using Web Audio API for HTML5 games and interactive websites. It provides a simple abstraction to create and play sounds easier.

Below are some of the core features supported by the library.

  • Built on the powerful Web Audio API
  • Simple API to create and play sounds
  • Supports sound groups
  • Supports variety of codecs
  • Supports audio sprites
  • Supports streaming using HTML5 audio nodes
  • Fading
  • Caching
  • Auto Garbage Management

Browser Support

  • Google Chrome
  • Firefox
  • Safari
  • Opera
  • Microsoft Edge

Installation

At this time musquito is available only in npm and you can install it using the below command.

npm install musquito --save

You can also directly reference files available from the distribution folder.

<script src="musquito/dist/musquito.min.js"></script>

"Hello World" example

A simple example of how to create and play a gun fire sound.

import $buzz from 'musquito';

$buzz.play('gun.mp3');

Passing Additional Parameters

The below example shows how you can pass additional parameters like volume, rate and callback.

$buzz.play({
  src: ['greenade.mp3', 'greenade.wav'],
  volume: 0.5,
  rate: 2,
  playEndCallback: () => alert('Playback started')
});

Using Sprites

Audio Sprites are like image sprites concatenates multiple small sounds in a single file. You can create audio sprite using this tool.

Below is an example of how to use sprites.

$buzz.play({
  src: 'sprite.mp3',
  sprite: {
    "beep": [
      0,
      0.48108843537414964
    ],
    "button": [
      2,
      2.4290249433106577
    ],
    "click": [
      4,
      4.672018140589569
    ]
  },
  sound: 'beep'
});

Pausing and Stopping Sounds

Calling the play method returns the sound id and you can use it to call other methods like pause, stop, change the volume and more properties of the sound.

const soundid = $buzz.play('bg.mp3');

// Pausing sound
$buzz.pause(soundid);

// Stopping sound
$buzz.stop(soundid);

Fading Sounds

You can fade the volume of a playing sound linearly or exponentially as shown below.

const soundid = $buzz.play('bg.mp3');

setTimeout(() => {
  $buzz.fade(soundid, 0, 3);
}, 2000);

Playing Long Audio Files

To stream long audio files and play using HTML5 audio node you can pass the stream parameter as true.

$buzz.play({
  src: 'bg.mp3',
  stream: true
});

Advanced Example

The below example shows how we can setup audio engine by passing audio resources with shorthand identifiers initially before playing sounds. The setup method also takes lot of other arguments to configure the engine, please refer the API docs.

$buzz.setup({
  src: {
    bg: 'bg.mp3',
    sprite: {
      url: 'sprite.mp3',
      sprite: {
        "beep": [
          0,
          0.48108843537414964
        ],
        "button": [
          2,
          2.4290249433106577
        ],
        "click": [
          4,
          4.672018140589569
        ]
      }
    }
  },
  oninit: () => {
    // Playing sounds with identifiers
    $buzz.play('#bg');
    $buzz.play('#sprite.button');
  }
});

Creating Audio Groups

Sometimes it's convenient to create a sound group which is called as "Buzz" that helps to create and manage multiple sounds for a single audio resource. Buzzes also supports events. The below example shows how we can create a sound group for a sprite and play multiple sounds easier.

const buzz = $buzz({
  src: 'sprite.mp3',
  sprite:{
    "beep": [
      0,
      0.48108843537414964
    ],
    "button": [
      2,
      2.4290249433106577
    ],
    "click": [
      4,
      4.672018140589569
    ]
  }
});

buzz.play('beep');
buzz.play('button');
buzz.play('click');

For demos and detailed documentation please visit here.

API

$buzz static methods

Sets-up the audio engine. When you call the $buzz function the setup method will be automatically called. Often you call this method manually during the application startup to pass the parameters to the audio engine and also when you like to pre-load all the audio resources upfront. Before calling any method in engine you should call this method first.

The parameters you can pass to the setup method are below.

MethodReturnsDescription
setup(args?: object)$buzzSets-up the audio engine. The different parameters you can pass in arguments object are volume, muted, maxNodesPerSource, cleanUpInterval, autoEnable, src, preload, progress and event handler functions like oninit, onstop, onmute, onvolume, onsuspend, onresume, onerror and ondone.
play(idOrSoundArgs: number,string,Array,object)$buzz,numberCreates and plays a new sound or the existing sound for the passed id. Returns sound id if new one is created.
pause(id: number)$buzzPauses the sound for the passed id.
stop(id?: number)$buzzStops the sound for the passed id or all the playing sounds. Stopping engine fires the "stop" event.
mute(id?: number)$buzzMutes the sound if id is passed or the engine. Fires the "mute" event if engine is muted.
unmute(id?: number)$buzzUn-mutes the sound if id is passed or the engine. Fires the "mute" event if engine is un-muted.
volume(vol?: number, id?: number)$buzz,numberGets/sets the volume for the audio engine that controls global volume for all sounds or the sound of the passed id. Fires the "volume" event in case of engine. The value of the passed volume should be from 0 to 1.
fade(id: number, to: number, duration: number, type = 'linear','exponential')$buzzFades the sound belongs to the passed id.
fadeStop(id: number)$buzzStops the running fade.
rate(id: number, rate?: number)$buzz,numberGets/sets the rate of the passed sound. The value of the passed rate should be from 1 to 5.
seek(id: number, seek?: number)$buzz,numberGets/sets the current position of the passed sound.
loop(id: number, loop?: boolean)$buzz,booleanGets/sets the loop parameter of the sound.
destroy(id: number)$buzzDestroys the passed sound.
load(urls: string, Array, progressCallback: function)PromiseLoads single or multiple audio resources into audio buffers and returns them.
loadMedia(urls: string, Array)PromisePre-loads single or multiple HTML5 audio nodes with the passed resources and returns them.
unload(urls: string, Array)$buzzUnloads single or multiple loaded audio buffers from cache.
unloadMedia(urls: string, Array)$buzzReleases audio nodes allocated for the passed urls.
register(src: stringArrayobject, key: string)$buzzAssigns a short-hand key for the audio source. In case of object the properties are url, format and sprite.
unregister(src: stringArrayobject, key: string)$buzzRemoves the assigned key for the audio source.
getSource(key: string)string,Array,objectReturns the assigned audio source for the passed key.
suspend()$buzzStops all the playing sounds and suspends the engine immediately.
resume()$buzzResumes the engine from the suspended mode.
terminate()$buzzShuts down the engine.
muted()booleanReturns whether the engine is currently muted or not.
state()EngineStateReturns the state of the engine. The different values are "notready", "ready", "suspending", "suspended", "resuming", "destroying", "done" and "no-audio".
buzz(id: number)BuzzReturns the buzz for the passed id.
buzzes()ArrayReturns all the buzzes.
sound(id: number)SoundReturns the sound for the passed id. Sound is an internal object and you don't have to deal with it usually.
sounds()ArrayReturns all the sounds created directly by engine. Sounds created by sound groups are not included.
context()AudioContextReturns the created audio context.
isAudioAvailable()booleanReturns true if Web Audio API is available.
on(eventName: string, handler: function, once = false)$buzzSubscribes to an event.
off(eventName: string, handler: function)$buzzUn-subscribes from an event.
masterGain()GainNodeReturns the master gain node.
bufferLoader()BufferLoaderReturns buffer loader.
mediaLoader()MediaLoaderReturns media loader.

$buzz function

$buzz(args: string|Array<string>|object)

$buzz is the single API that helps you to create and manage sounds. It's a function that returns a Buzz object. The Buzz object helps to control group of sounds created for a single audio source.

You can pass a single audio source, array of audio sources or an object. If an array of audio sources is passed then the first compatible one is picked for playing. If you need to pass additional information like initial volume, playback speed then you need to pass an object.

The different options you can pass in arguments object for the $buzz function.

NameTypeRequiredDefaultDescription
srcstring, ArrayyesSingle or array of audio sources. If an array of audio sources is passed then the first compatible one is picked for playing.
idnumbernoAuto-generatedThe unique identifier for the Buzz object.
volumenumberno1.0The initial volume of the sound. The value should be from 0.0 to 1.0.
ratenumberno1.0The initial playback speed. The value should be from 0.5 to 5.0.
loopbooleannofalsePass true to play the sound repeatedly.
mutedbooleannofalsePass true to keep the sound muted initially.
preloadbooleannofalsePass true to pre-load the sound.
autoplaybooleannofalsePass true to play the sound at-once created.
streambooleannofalsePassing true will use HTML5 audio node for playing the sound. This option you can use to play long audio files like background music in a game. This feature is available only in version v2.
formatstring, ArraynofalseSingle or array of audio formats for the passed audio sources.
spriteobjectnoThe sprite definition object that contains the starting and ending positions of each sound embedded in the sprite.
onloadfunctionnoThe event handler for "load" event.
onloadprogressfunctionnoThe event handler for "loadprogress" event.
onunloadfunctionnoThe event handler for "unload" event.
onplaystartfunctionnoThe event handler for "playstart" event.
onplayendfunctionnoThe event handler for "playend" event.
onstopfunctionnoThe event handler for "stop" event.
onpausefunctionnoThe event handler for "pause" event.
onmutefunctionnoThe event handler for "mute" event.
onvolumefunctionnoThe event handler for "volume" event.
onratefunctionnoThe event handler for "rate" event.
onseekfunctionnoThe event handler for "seek" event.
onerrorfunctionnoThe event handler for "error" event.
ondestroyfunctionnoThe event handler for "destroy" event.

Buzz object methods

MethodReturnsDescription
load(soundId?: string)BuzzLoads the audio buffer or preloads a HTML5 audio node. The soundId can be passed only in the case stream buzz type. When you pass it that particular sound's audio node will be pre-loaded.
play(soundOrId?: string, number)BuzzPlays a new sound or the passed sound defined in the sprite or the sound that belongs to the passed id.
pause(id?: number)BuzzPauses the sound belongs to the passed id or all the sounds belongs to this group.
stop(id?: number)BuzzStops the sound belongs to the passed id or all the sounds belongs to this group.
mute(id?: number)BuzzMutes the sound belongs to the passed id or all the sounds belongs to this group.
unmute(id?: number)BuzzUn-mutes the sound belongs to the passed id or all the sounds belongs to this group.
volume(volume?: number, id?: number)Buzz, numberGets/sets the volume of the passed sound or the group. The passed value should be from 0.0 to 1.0.
rate(rate?: number, id?: number)Buzz, numberGets/sets the rate of the passed sound or the group. The passed value should be from 0.5 to 5.0.
seek(id: number, seek?: number)Buzz, numberGets/sets the current playback position of the sound.
loop(loop?: boolean, id?: number)Buzz, booleanGets/sets the looping behavior of a sound or the group.
fade(to: number, duration: number, type = 'linear', id?: number)BuzzFades the volume of a playing sound or all sounds belongs to the group.
fadeStop(id?: number)BuzzStops the current running fade of the passed sound or all sounds belongs to the group.
playing(id: number)booleanReturns true if the passed sound is playing.
muted(id?: number)booleanReturns true if the passed sound is muted or the group is muted.
state(id?: number)BuzzState, SoundStateReturns the state of the passed sound or the group.
duration(id?: number)numberReturns the duration of the passed sound or the total duration of the sound.
unload()BuzzUnloads the loaded audio buffer.
destroy()BuzzStops and destroys all the sounds belong to this group and release other dependencies.
on(eventName: string, handler: function, once = false, id?: number)BuzzSubscribes to an event for the sound or the group.
off(eventName: string, handler: function, id?: number)BuzzUn-subscribes from an event for the sound or the group.
id()numberReturns the unique id of the sound.
loadState()LoadStateReturns the audio resource loading status. The different values are "notloaded", "loading" and "loaded".
isLoaded()booleanReturns true if the audio source is loaded.
sound(id: number)SoundReturns the sound for the passed id.
sounds()ArrayReturns all the sounds belongs to this buzz group.
alive(id: number)booleanReturns true if the passed sound exists.
gain()GainNodeReturns the gain node.

License

MIT

Copyright © 2020 · Vijaya Anand · All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3.0.1

4 years ago

3.0.0

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-beta

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago