Last active
November 1, 2020 03:36
-
-
Save buddyeorl/5ffde5249888b3bc4fc6e5d05a60e75a to your computer and use it in GitHub Desktop.
How to implement Voice Activity Detection using javascript
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
//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