musquito v3 - Sounds Made Easy!

Introduction

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 latest version v3 creating and playing sounds is much more simple 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

In the previous two versions (v1 and v2) you need to always create an audio group to play the sound. From version 3 you can play a sound by a single line of code.

Installation

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

npm install musquito --save  

"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 more information and the API docs please checkout the official website. Please give a star to the repo.

blog comments powered by Disqus