Skip to content

Instantly share code, notes, and snippets.

@torgeir
Created September 19, 2013 17:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save torgeir/6627296 to your computer and use it in GitHub Desktop.
Save torgeir/6627296 to your computer and use it in GitHub Desktop.
Microphone input as audio source js
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
window.addEventListener('load', init, false);
function init () {
try {
record(new AudioContext());
}
catch (e) {
console.error(e);
alert('Web Audio API is not supported in this browser');
}
}
function record (context) {
navigator.getUserMedia({ audio: true }, sound, error);
function sound (stream) {
var analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0.2;
analyser.fftSize = 1024;
var node = context.createJavaScriptNode(2048, 1, 1);
var values = 0;
var average;
node.onaudioprocess = function () {
// bitcount is fftsize / 2
var array = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(array);
var length = array.length;
for (var i = 0; i < length; i++) {
values += array[i];
}
average = values / length;
oppdaterGui(average);
average = values = 0;
};
var input = context.createMediaStreamSource(stream);
input.connect(analyser);
analyser.connect(node);
node.connect(context.destination);
//input.connect(context.destination); // hello feedback
}
function error () {
console.log(arguments);
}
}
@Kishan033
Copy link

What is oppdaterGui?

@muhamedsahin
Copy link

wow

@torgeir
Copy link
Author

torgeir commented Jun 19, 2021

Ehm. Kinda late to the game here @Kishan033; oppdaterGui is "update gui" in Norwegian - probably some rendering I did of the average value..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment