Git Product home page Git Product logo

webaudiodemo's Introduction

HTML 5 webAudio api demo

Live Demo

[demo] ( https://mikealbo.github.io/webAudioDemo/)

about

This demo handles the importing and decoding of the audio files passed to it. The demo also includes a playback function. The WebAudio AudioContext is not supported in IE as of yet.

Links

[The W3 standard] (https://www.w3.org/TR/webaudio/#AudioContext-section)

html5 Rocks webAudio tutorial

[html5 rocks version of a buffer loader] (https://www.html5rocks.com/en/tutorials/webaudio/intro/js/buffer-loader.js)

setup

loads the init function when the document is ready

window.addEventListener('load', init, false);

init function sets the window.AudioContext, if not supported alerts the user of error. Note: webKit requires "webkitAudioContext".

function init(){
    try {
        window.AudioContext = window.AudioContext || window.webkitAudioContext;
        context = new AudioContext();
        console.log("AudioContext created!");
    }
    catch(e) {
        alert("Web Audio not supported in your browser, please try a newer release of Safari, Chrome, or Firefox");
        }
}

create a new instance...

creates a new instance of AudioLoadBuffer and calls the loadData function()

var newAudio = new AudioLoadBuffer(audioSources);
newAudio.loadData();

audioSources

audioSources, is a JS object that contains the link to the audio file to be imported.

var audioSources = {
    exSound1: {
        webLink : 'audioFiles/simonSound1.mp3'
    },
    exSound2: {
        webLink : 'audioFiles/simonSound2.mp3'
    }
    }

This is also the var the the buffer outputs to and is used in playback.

var audioSources = {
    exSound1: {
        webLink : 'audioFiles/simonSound1.mp3',
        playback: AudioBuffer
    },
    exSound2: {
        webLink : 'audioFiles/simonSound2.mp3',
        playback: AudioBuffer
    }
    }

AudioLoadBuffer

A constructor that, takes the audioSourc file and calls the XMLHttpRequest() on each object. Upon success, the response is added to the audioSource file.

loadData

this.loadData = function(){
        for(var file in source){
           httpReq(file);
        }

httpReq

  function httpReq(file){
        //console.log(url);
        var request = new XMLHttpRequest();
        request.open('GET', source[file].webLink, true);
        request.responseType = 'arraybuffer'; 
        request.onload = function(){
             context.decodeAudioData(request.response, function(res){
                audioSources[file].playback = res;
                 
            });
            
        }
        request.send();  
    }

Note : You may encounter access issues if the you are not connecting to your server.

playBack

The playback function builds the buffer, conects to the speakers, and starts (plays) the file.

 function playBack(buffer){
    var source = context.createBufferSource();
    source.buffer = buffer;
    source.connect(context.destination);
    source.start(0);
}

demo interface

  1. playSound - switch function, that calls playBack
  2. button event listeners, call playSound and passes argument

things to do...

  1. extract the main functionality into reusable code
  2. imporve error handling

webaudiodemo's People

Watchers

James Cloos avatar Michael Albonetti 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.