musquito, A Web Audio API Library

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.

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

  • Simple API to create and play sounds
  • Supports variety of codecs
  • Supports audio sprites
  • Fading
  • Caching

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-1.0.0.min.js"></script>

"Hello World" example

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

ES6 and above

import $buzz from 'musquito';

const buzz = $buzz('gunfire.mp3');

buzz.play();

Classic JavaScript

var buzz = $buzz('gunfire.mp3');

buzz.play();

Advanced example

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

const buzz = $buzz({
  src: ['gunfire.mp3', 'gunfire.wav'],
  volume: 0.5,
  rate: 2,
  onplaystart: () => console.log('Playback started'),
  onplayend: () => console.log('Playback ended')
});

buzz.play();

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.

const buzz = $buzz({
  src: 'gamesprite.mp3',
  sprite:{
    'gun': [0, 0.48],
    'bomb': [2, 2.42],
    'greenade': [4, 4.67]
  }
});

buzz.play('gun');
buzz.play('bomb');

Fading Sounds

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

const buzz = $buzz({
  src: 'bg.mp3'
});

buzz.play();
...

buzz.fade(0, 3);

API

To know all the API methods please visit here.

Source Code

https://github.com/VJAI/musquito

License

MIT

blog comments powered by Disqus