simple jscript volume meter code example
Example: js get mic level
var audioContext = null;
var meter = null;
var rafID = null;
var mediaStreamSource = null;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
audioContext = new AudioContext();
function onMicrophoneDenied() {
alert('Stream generation failed.');
}
function onMicrophoneGranted(stream) {
mediaStreamSource = audioContext.createMediaStreamSource(stream);
meter = createAudioMeter(audioContext);
mediaStreamSource.connect(meter);
onLevelChange();
}
function onLevelChange(time) {
if (meter.checkClipping()) {
console.warn(meter.volume);
} else {
console.log(meter.volume);
}
rafID = window.requestAnimationFrame(onLevelChange);
}
try {
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
navigator.getUserMedia(
{
"audio": {
"mandatory": {
"googEchoCancellation": "false",
"googAutoGainControl": "false",
"googNoiseSuppression": "false",
"googHighpassFilter": "false"
},
"optional": []
},
},
onMicrophoneGranted,
onMicrophoneDenied
);
} catch (e) {
alert('getUserMedia threw exception :' + e);
}