From 3bff27a77d1f8b2660d22e599d659768bb095e7d Mon Sep 17 00:00:00 2001 From: Matthew Huntington Date: Sun, 31 Mar 2024 11:05:59 -0400 Subject: [PATCH] don't request mic access until threshold initially changed --- src/components/timer.vue | 76 +++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/components/timer.vue b/src/components/timer.vue index 2326c4c..6ba8cfd 100644 --- a/src/components/timer.vue +++ b/src/components/timer.vue @@ -77,16 +77,20 @@ this.running = true; this.intervalID = setInterval(()=>{ - const array = new Uint8Array(this.analyser.frequencyBinCount); - this.analyser.getByteFrequencyData(array); - const arraySum = array.reduce((a, value) => a + value, 0); - this.micLevel = arraySum / array.length; - - if(this.micLevel > this.micThreshold){ - this.micThresholdExceeded = true; - } else { - this.secondsToSubtract++; - this.micThresholdExceeded = false; + if(this.micLevel > -1){ + + const array = new Uint8Array(this.analyser.frequencyBinCount); + this.analyser.getByteFrequencyData(array); + const arraySum = array.reduce((a, value) => a + value, 0); + this.micLevel = arraySum / array.length; + + if(this.micLevel > this.micThreshold){ + this.micThresholdExceeded = true; + } else { + this.secondsToSubtract++; + this.micThresholdExceeded = false; + } + } this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; @@ -112,33 +116,40 @@ this.savedPreviousSeconds = this.totalSeconds; this.secondsToSubtract = 0; window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); - } - }, - mounted() { - axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{ - this.categories = response.data - }) + }, + activateMic(){ - navigator.mediaDevices.getUserMedia({ - audio: true - }).then((stream) => { + if(this.micThreshold > -1){ - const audioContext = new AudioContext(); - this.analyser = audioContext.createAnalyser(); - const microphone = audioContext.createMediaStreamSource(stream); - const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1); + navigator.mediaDevices.getUserMedia({ + audio: true + }).then((stream) => { - this.analyser.smoothingTimeConstant = 0.8; - this.analyser.fftSize = 1024; + const audioContext = new AudioContext(); + this.analyser = audioContext.createAnalyser(); + const microphone = audioContext.createMediaStreamSource(stream); + const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1); - microphone.connect(this.analyser); - this.analyser.connect(scriptProcessor); - scriptProcessor.connect(audioContext.destination); + this.analyser.smoothingTimeConstant = 0.8; + this.analyser.fftSize = 1024; + microphone.connect(this.analyser); + this.analyser.connect(scriptProcessor); + scriptProcessor.connect(audioContext.destination); + + }) + .catch((err) => { + console.error(err); + }); + + } + + } + }, + mounted() { + axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{ + this.categories = response.data }) - .catch((err) => { - console.error(err); - }); } } @@ -150,7 +161,8 @@ {{formatTime(totalSeconds)}} - Mic Threshold (current level: {{Math.round(micLevel)}}): + Mic Threshold (current level: {{Math.round(micLevel)}}): +