JSPM

  • Created
  • Published
  • Downloads 9
  • Score
    100M100P100Q45645F
  • License MIT

Web Audio Library

Package Exports

  • waud.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (waud.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

waud logo

Web Audio Library with HTML5 audio fallback.

Build Status npm version

Installation

npm install waud.js

For haxe users:

haxelib install waud

API Documentation

  • Waud.preferredSampleRate - You can set the preferred audio sample rate. Default: 44100
  • Waud.init(?dom) - To initialise the library, make sure you call this first. You can also pass an optional parent DOM element to it where all the HTML5 sounds will be appended and also used for touch events to unlock audio on iOS devices.
  • Waud.enableTouchUnlock(?callback) - Helper function to unlock audio on iOS devices. You can pass an optional callback which will be called on touchend event.
  • Waud.autoMute() - Helper function to automatically mute audio when the browser window is not in focus. Will un-mute when the window gains focus.
  • Waud.isWebAudioSupported - To check web audio support.
  • Waud.isHTML5AudioSupported to check HTML5 audio support.
  • Waud.getFormatSupportString() - Returns a string with all the format support information. (ex: OGG: probably, WAV: probably, MP3: probably, AAC: probably, M4A: maybe)

The following functions can be used to check format support (returns true or false):

  • Waud.isOGGSupported()
  • Waud.isWAVSupported()
  • Waud.isMP3Supported()
  • Waud.isAACSupported()
  • Waud.isM4ASupported()

There are 3 classes available for audio playback.

  • WaudSound (recommended) - to automatically use web audio api with HTML5 audio fallback.
  • WebAudioAPISound - to force web audio api
  • HTML5Sound - to force HTML5 audio

HTML5Sound on iOS devices have some limitations you should be aware of. Setting volume audio is not possible and mute is implemented by pausing and replaying the sound if it's already playing.

The following are the options available when creating sound instabce:

  • autoplay - true or false (default: false)
  • loop - true or false (default: false)
  • volume - between 0 and 1 (default: 1)
  • onload - callback function when the sound is loaded with sound instance as parameter (default: none)
  • onend - callback function when the sound playback ends with sound instance as parameter (default: none)
  • onerror - callback function when there is an error in loading/decoding with sound instance as parameter (default: none)

Example:

var snd = new WaudSound("assets/loop.mp3", { autoplay: false, loop: true, volume: 0.5, onload: _playBgSound });

Available functions on sound instance:

  • play(?sprite) - optional sprite name when using audio sprite, returns sound instance for chaining if needed
  • stop()
  • mute(val) - true or false
  • loop(val) - true or false
  • setVolume(val) - between 0 and 1
  • getVolume() - returns value between 0 and 1
  • isPlaying() - returns true or false
  • onEnd(callback) - callback will get sound instance as parameter

Waud.sounds will hold all the sounds that are loaded. To access any sound use Waud.sounds.get(url) where url is the path used to load the sound.

Audio Sprite

Use waudsprite to generate audio sprite.

var audSprite = new WaudSound("assets/sprite.json");
audSprite.play("glass");

Issues

Found any bug? Please create a new issue.

Demo

Usage

JavaScript
Waud.init();
Waud.enableTouchUnlock(touchUnlock);
Waud.autoMute();

var _bgSnd = new WaudSound("assets/loop.mp3", {
    "autoplay": false, "loop":true, "volume": 0.5, "onload": _playBgSound
});

var snd2 = new WaudSound("assets/sound1.wav", {
    "autoplay": false,
    "loop":true,
    "onload": function (snd) { snd.play(); },
    "onend": function (snd) { console.log("ended"); },
    "onerror": function (snd) { console.log("error"); }
});

//Touch unlock event for iOS devices
function touchUnlock() {
    if (!_bgSnd.isPlaying()) _bgSnd.play();
}

function _playBgSound(snd) {
    if (!snd.isPlaying()) snd.play();
}
Haxe
class Main {

    var _bgSnd:IWaudSound;
    var _snd2:IWaudSound;

    public function new() {
        Waud.init();
        Waud.enableTouchUnlock(touchUnlock);
        Waud.autoMute();
        
        _bgSnd = new WaudSound("assets/loop.mp3", { autoplay: false, loop: true, volume: 0.5, onload: _playBgSound });
        _snd2 = new WaudSound("assets/sound1.wav", {
            autoplay: false,
            loop: false,
            onload: function (snd) { snd.play(); },
            onend: function (snd) { trace("ended"); },
            onerror: function (snd) { trace("error"); }
        });
    }
    
    //Touch unlock event for iOS devices
    function touchUnlock() {
        if (!_bgSnd.isPlaying()) _bgSnd.play();
    }
    
    function _playBgSound(snd:IWaudSound) {
        if (!snd.isPlaying()) snd.play();
    }
    
    static function main() {
        new Main();
    }
}

Licensing Information

MIT license

This content is released under the MIT License.