UNPKG

proctor-ai-sdk

Version:

A powerful SDK for preventing cheating during online exams using face detection, voice detection, and tab monitoring.

28 lines (26 loc) 1 kB
//⬅️ Detect background voice let audioContext, analyser, source, micStream, voiceInterval; export const startVoiceDetection = (threshold = 0.1, interval = 1000, logFn) => { navigator.mediaDevices.getUserMedia({ audio: true }).then((stream) => { audioContext = new AudioContext(); micStream = stream; source = audioContext.createMediaStreamSource(stream); analyser = audioContext.createAnalyser(); source.connect(analyser); const data = new Uint8Array(analyser.fftSize); voiceInterval = setInterval(() => { analyser.getByteFrequencyData(data); const avg = data.reduce((a, b) => a + b, 0) / data.length / 255; if (avg > threshold) { if (logFn) logFn("background-voice-detected"); } }, interval); }); }; export const stopVoiceDetection = () => { clearInterval(voiceInterval); if (micStream) { micStream.getTracks().forEach((track) => track.stop()); } if (audioContext) audioContext.close(); };