mic level basic integration

mic_level
Matthew Huntington 2 years ago
parent 1120077a59
commit 5bcb462215

@ -36,7 +36,9 @@
comments:'', comments:'',
manualHours:0, manualHours:0,
manualMinutes:0, manualMinutes:0,
manualSeconds:0 manualSeconds:0,
analyser:null,
secondsToSubtract:0
} }
}, },
methods: { methods: {
@ -70,9 +72,20 @@
this.startTime = Date.now(); this.startTime = Date.now();
this.running = true; this.running = true;
this.intervalID = setInterval(()=>{ 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); window.localStorage.setItem('lastTotalSeconds', this.totalSeconds);
}, 100);
}, 1000);
}, },
updateSavedPreviousSeconds(event){ updateSavedPreviousSeconds(event){
this.savedPreviousSeconds = parseInt(event.target.value); this.savedPreviousSeconds = parseInt(event.target.value);
@ -96,6 +109,28 @@
axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{ axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{
this.categories = response.data 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);
});
} }
} }
</script> </script>

Loading…
Cancel
Save