diff --git a/vite/src/comps/timer.jsx b/vite/src/comps/timer.jsx index 6306057..64a4970 100644 --- a/vite/src/comps/timer.jsx +++ b/vite/src/comps/timer.jsx @@ -40,6 +40,14 @@ export const Countdown=forwardRef(({time, callback, auto, ...props}, ref)=>{ } + function stop(){ + console.log('stop countdown'); + if(refInterval.current) { + clearInterval(refInterval.current); + refInterval.current = null; + } + // refDisplay.current.innerText = ''; + } useEffect(()=>{ @@ -53,7 +61,8 @@ export const Countdown=forwardRef(({time, callback, auto, ...props}, ref)=>{ }, [time, callback]); useImperativeHandle(ref, () => ({ - restart, + restart, + stop, })); return ( diff --git a/vite/src/comps/voiceanalysis.jsx b/vite/src/comps/voiceanalysis.jsx new file mode 100644 index 0000000..c3a7ebc --- /dev/null +++ b/vite/src/comps/voiceanalysis.jsx @@ -0,0 +1,79 @@ +import { useEffect, useRef, useState } from "react"; + +export default function VoiceAnalysis(){ + + const [volume, setVolume] = useState(0); + const refVolumeInterval=useRef(); + const refAnalyser=useRef(); + const refVolumes=useRef(); + + + async function setup(){ + try{ + + + + const audioStream = await navigator.mediaDevices.getUserMedia({ + audio: { + echoCancellation: true + }, + video: false + }); + + audioStream.getAudioTracks().forEach(track => { + console.log(`Track ID: ${track.id}, Label: ${track.label}`); + }); + + const audioContext = new AudioContext(); + const source = audioContext.createMediaStreamSource(audioStream); + const analyser = audioContext.createAnalyser(); + analyser.fftSize = 512; + analyser.minDecibels = -127; + analyser.maxDecibels = 0; + analyser.smoothingTimeConstant = 0.4; + + source.connect(analyser); + + const volumes = new Uint8Array(analyser.frequencyBinCount); + + refAnalyser.current = analyser; + refVolumes.current = volumes; + }catch(e){ + console.error('error start audio stream',e); + } + + } + const volumeCallback = () => { + + const volumes = refVolumes.current; + + refAnalyser.current.getByteFrequencyData(volumes); + + let volumeSum = 0; + for(const volume of volumes) + volumeSum += volume; + const averageVolume = volumeSum / volumes.length; + // console.log(`Average Volume: ${averageVolume}`); + setVolume(averageVolume); + + }; + + useEffect(()=>{ + setup().then(() => { + refVolumeInterval.current = setInterval(() => { + volumeCallback(); + }, 100); // Adjust the interval as needed + }); + + return () => { + clearInterval(refVolumeInterval.current); + }; + },[]); + + return ( +
Volume: {volume}
*/} + +