musquito v3 — sounds made easy!

Vijaya Anand
2 min readMay 16, 2020

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. With the release of latest version playing sounds is much more easy now!

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

“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);

Streaming

Long audio files can be played using HTML5 audio nodes by passing stream option 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 the complete documentation please check the website.

PLEASE GIVE A STAR TO THE REPO.

--

--