UNPKG

keolis-services

Version:

Keolis services includes Microphone, Location, Voice Activity Detector, Heart rate through web bluetooth API, text to speech, stop watch with our own cloud.

58 lines (51 loc) 2.1 kB
const { startRecording, stopRecording } = require('../services/record'); const vad = require('@mjyc/voice-activity-detection'); navigator.mediaDevices.getUserMedia({ audio: true }); var audioContext; export const startVoiceDetect = () => { try { window.AudioContext = window.AudioContext || window.webkitAudioContext; audioContext = new AudioContext(); navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; navigator.getUserMedia({ audio: true }, startUserMedia, handleMicConnectError); } catch (e) { handleUserMediaError(); } } const handleUserMediaError = () => { console.warn('Mic input is not supported by the browser.'); } const handleMicConnectError = () => { console.warn('Could not connect microphone. Possible rejected by the user or is blocked by the browser.'); } let isStartRecording = false; const startUserMedia = (stream) => { var options = { minNoiseLevel: 0.92, maxNoiseLevel: 1, avgNoiseMultiplier: 1, onVoiceStart: function() { console.info('voice Detected!!!!!!'); if (document.getElementById('voice-detector-status')) document.getElementById('voice-detector-status').innerHTML = 'Voice detected'; isStartRecording = true; startRecording(); setTimeout(function() { console.log('Recording stopped') stopRecording(); isStartRecording = false; }, 5000); return true; }, onVoiceStop: function() { if (!isStartRecording) audioContext.resume(); }, onUpdate: function(val) {} }; vad(audioContext, stream, options); } setInterval(function() { if (!isStartRecording && audioContext) { audioContext.resume(); if (document.getElementById('voice-detector-status')) document.getElementById('voice-detector-status').innerHTML = ''; } }, 2000);