Coder Social home page Coder Social logo

hadryan / audiosampleloader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scottmichaud/audiosampleloader

0.0 2.0 0.0 113 KB

A utility class, similar in usage to XMLHttpRequest Level 2, which accepts URL(s) to sound file(s) and writes the resulting decoded AudioBuffer(s) to its "response" attribute (or a list/array of AudioBuffers if the urls are a list/array of strings).

License: Other

JavaScript 100.00%

audiosampleloader's Introduction

#AudioSampleLoader

AudioSampleLoader() is a utility class to help load, and decode, one or more audio samples using WebAudio API. It is designed to closely resemble XMLHttpRequest Level 2. It is asynchronous.

Sample Usage

Creating an Instance

var loader = new AudioSampleLoader();

This document will assume that your instance is a variable called loader. Like always, you are free to name it whatever you want (or even have multiple instances active, each with its own variable name).

Setting Attributes

src (required) is the URL to your audio files.

loader.src = 'audio/file.mp3';

or

loader.src = ['audio/file1.mp3', 'audio/file2.mp3', 'sounds/filec.mp3'];

ctx (optional) is your WebAudio context that will perform the actual decode. If it is not provided, one will be created on send(). If it exists but is not an AudioContext, an error will be thrown and the function will terminate.

loader.ctx = yourAudioContext;

onload (required) is your callback function which is executed after the successful decode. When this function is called, your data will be available at loader.response as an AudioBuffer (or an array of AudioBuffers, depending on src).

loader.onload = function () { window.mySample = loader.response; };

onerror (optional) is your callback function which is executed on failure. Currently, this is when: your URL is not a string or pure string array, XHR calls its onerror callback, or the WebAudio context's decodeAudioData() method calls its onerror callback. If not provided, it will do nothing.

loader.onerror = function () { alert('Awwwwww snap!'); };

Sending the Request

When the above attributes are properly set, you can then put AudioSampleLoader to work by calling its send() method.

loader.send();

Reading Your Results

If everything goes well, your AudioSampleLoader will call onload. At this time, your decoded audio samples will be available at loader.response. If src was a string, response will be the AudioBuffer decoded from the file at that URL. If src was a list of strings, response will be a list of AudioBuffer objects, where src[0] will correspond to response[0], src[1] will correspond to response[1], and so forth.

Acknowledgements

Special thanks to HTML5Rocks for being ahead of the curve with WebAudio API documentation. Also, they made a similar class as a part of their tutorial.

audiosampleloader's People

Contributors

scottmichaud avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.