Last active
December 27, 2015 02:39
-
-
Save jcready/7253920 to your computer and use it in GitHub Desktop.
Processing Web Audio ChannelData from MediaElementSource.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var audioContext, audioProcess, audioSource, | |
result = document.createElement('h3'), | |
output = document.createElement('span'), | |
mp3 = 'http://songs.jonathancoulton.com/mp3/First%20of%20May.mp3', | |
ogg = 'http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg', | |
gotData = false, data, audio = new Audio(); | |
function connect() { | |
audioContext = window.AudioContext ? new AudioContext() : new webkitAudioContext(), | |
audioSource = audioContext.createMediaElementSource( audio ), | |
audioScript = audioContext.createScriptProcessor( 2048 ); | |
audioSource.connect( audioScript ); | |
audioSource.connect( audioContext.destination ); | |
audioScript.connect( audioContext.destination ); | |
audioScript.addEventListener('audioprocess', function(e){ | |
if ((data = e.inputBuffer.getChannelData(0)[0]*3)) { | |
output.innerHTML = Math.abs(data).toFixed(3); | |
if (!gotData) gotData = true; | |
} | |
}, false); | |
} | |
(function setup(){ | |
audio.volume = 1/3; | |
audio.controls = true; | |
audio.autoplay = true; | |
audio.src = audio.canPlayType('audio/mpeg') ? mp3 : ogg; | |
audio.addEventListener('canplay', connect); | |
result.innerHTML = 'Channel Data: '; | |
output.innerHTML = '0.000'; | |
document.body.appendChild(result).appendChild(output); | |
document.body.appendChild(audio); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsbin.com/OPATogA/edit?js,output