don't request mic access until threshold initially changed

practiced-songs
Matthew Huntington 2 years ago
parent 986058ecb1
commit 3bff27a77d

@ -77,16 +77,20 @@
this.running = true; this.running = true;
this.intervalID = setInterval(()=>{ this.intervalID = setInterval(()=>{
const array = new Uint8Array(this.analyser.frequencyBinCount); if(this.micLevel > -1){
this.analyser.getByteFrequencyData(array);
const arraySum = array.reduce((a, value) => a + value, 0); const array = new Uint8Array(this.analyser.frequencyBinCount);
this.micLevel = arraySum / array.length; this.analyser.getByteFrequencyData(array);
const arraySum = array.reduce((a, value) => a + value, 0);
if(this.micLevel > this.micThreshold){ this.micLevel = arraySum / array.length;
this.micThresholdExceeded = true;
} else { if(this.micLevel > this.micThreshold){
this.secondsToSubtract++; this.micThresholdExceeded = true;
this.micThresholdExceeded = false; } else {
this.secondsToSubtract++;
this.micThresholdExceeded = false;
}
} }
this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds; this.totalSeconds = getAccumulatedSeconds(Date.now(), this.startTime) - this.secondsToSubtract + this.savedPreviousSeconds;
@ -112,33 +116,40 @@
this.savedPreviousSeconds = this.totalSeconds; this.savedPreviousSeconds = this.totalSeconds;
this.secondsToSubtract = 0; this.secondsToSubtract = 0;
window.localStorage.setItem('lastTotalSeconds', this.totalSeconds); window.localStorage.setItem('lastTotalSeconds', this.totalSeconds);
} },
}, activateMic(){
mounted() {
axios.get(import.meta.env.VITE_PRACTICE_TRACKER_API_URL+'categories').then((response)=>{
this.categories = response.data
})
navigator.mediaDevices.getUserMedia({ if(this.micThreshold > -1){
audio: true
}).then((stream) => {
const audioContext = new AudioContext(); navigator.mediaDevices.getUserMedia({
this.analyser = audioContext.createAnalyser(); audio: true
const microphone = audioContext.createMediaStreamSource(stream); }).then((stream) => {
const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
this.analyser.smoothingTimeConstant = 0.8; const audioContext = new AudioContext();
this.analyser.fftSize = 1024; this.analyser = audioContext.createAnalyser();
const microphone = audioContext.createMediaStreamSource(stream);
const scriptProcessor = audioContext.createScriptProcessor(2048, 1, 1);
microphone.connect(this.analyser); this.analyser.smoothingTimeConstant = 0.8;
this.analyser.connect(scriptProcessor); this.analyser.fftSize = 1024;
scriptProcessor.connect(audioContext.destination);
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 @@
<em> <em>
{{formatTime(totalSeconds)}} {{formatTime(totalSeconds)}}
</em> </em>
Mic Threshold (current level: {{Math.round(micLevel)}}): <input type="number" v-model="micThreshold"/> Mic Threshold (current level: {{Math.round(micLevel)}}):
<input type="number" @change="activateMic" v-model="micThreshold"/>
<button :disabled="running" @click="start">Start</button> <button :disabled="running" @click="start">Start</button>
<button :disabled="!running" @click="stop">Stop</button> <button :disabled="!running" @click="stop">Stop</button>
<button :disabled="running || totalSeconds === 0" @click="reset">Reset</button> <button :disabled="running || totalSeconds === 0" @click="reset">Reset</button>

Loading…
Cancel
Save