diff --git a/src/components/timer.vue b/src/components/timer.vue index 3c51897..6e23154 100644 --- a/src/components/timer.vue +++ b/src/components/timer.vue @@ -36,7 +36,9 @@ comments:'', manualHours:0, manualMinutes:0, - manualSeconds:0 + manualSeconds:0, + analyser:null, + secondsToSubtract:0 } }, methods: { @@ -70,9 +72,20 @@ this.startTime = Date.now(); this.running = true; this.intervalID = setInterval(()=>{ - this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) + this.savedPreviousSeconds; + + const array = new Uint8Array(this.analyser.frequencyBinCount); + this.analyser.getByteFrequencyData(array); + const arraySum = array.reduce((a, value) => a + value, 0); + const micLevel = arraySum / array.length; + + if(micLevel < 50){ + this.secondsToSubtract++; + } + + this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); - }, 100); + + }, 1000); }, updateSavedPreviousSeconds(event){ this.savedPreviousSeconds = parseInt(event.target.value); @@ -96,6 +109,28 @@ axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{ this.categories = response.data }) + + navigator.mediaDevices.getUserMedia({ + audio: true + }).then((stream) => { + + const audioContext = new AudioContext(); + this.analyser = audioContext.createAnalyser(); + const microphone = audioContext.createMediaStreamSource(stream); + const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1); + + 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); + }); + } }