Skip to content

Instantly share code, notes, and snippets.

@buddyeorl
Last active November 1, 2020 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buddyeorl/5ffde5249888b3bc4fc6e5d05a60e75a to your computer and use it in GitHub Desktop.
Save buddyeorl/5ffde5249888b3bc4fc6e5d05a60e75a to your computer and use it in GitHub Desktop.
How to implement Voice Activity Detection using javascript
//new audiocontext
let audioCtx = new (window.AudioContext || window.webkitAudioContext)();
//analyzer for the audio context
let analyser =audioCtx.createAnalyser();
//request microphone access
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(async (stream) => {
//analize sound waves
//stream source
let source = audioCtx.createMediaStreamSource(stream);
//analyser now can read data from the source
source.connect(analyser);
//set the fftSize
analyser.fftSize = 2048;
//get the buffer length from the analyser
let bufferLength = analyser.frequencyBinCount;
//create a uint8 array
let dataArray = new Uint8Array(bufferLength)
//call this to get the current frequency and put it into dataArray
analyser.getByteFrequencyData(dataArray)
//now handle the the dataArray, which has frequencies from 0-255 (0 ===total silence)
//if all elements === 0 then no voice
// call analyser.getByteFrequencyData(dataArray) as often as you want to analyze voice frequency
// I used 5ms in my app
handleMicSilence(dataArray);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment