Simple example of how to get audio data from live microphone input via getUserMedia.
This file contains 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
window.onload = function() { | |
var context = new webkitAudioContext(); | |
var analyser = context.createAnalyser(); | |
var buffer = new Float32Array(analyser.frequencyBinCount); | |
function analyze() { | |
requestAnimationFrame(analyze); | |
analyser.getFloatFrequencyData(buffer); | |
// buffer is now filled with data | |
// if there are -100 values only set your systems microphone & speakers sample rates to the same values | |
console.log(buffer[0]); | |
} | |
function connectStream(stream) { | |
// uncomment if you want to playback the recorded audio too | |
//var audioElement = document.getElementById('audio'); | |
//audioElement.autoplay = true; | |
//audioElement.src = window.webkitURL.createObjectURL(stream); | |
var source = context.createMediaStreamSource(stream); | |
source.connect(analyser); | |
analyze(); | |
} | |
function microphoneError(e) { | |
alert('MicrophoneError error!', e); | |
}; | |
if (navigator.webkitGetUserMedia) { | |
navigator.webkitGetUserMedia({audio: true, video: false}, connectStream, microphoneError); | |
} else { | |
alert('Your browser does not support webkitGetUserMedia.'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment