@@ -10,7 +10,7 @@ export const useSpeakerDetection = ({
1010} ) => {
1111 const [ isSomeoneSpeaking , setIsSomeoneSpeaking ] = useState ( false ) ;
1212 const [ shouldReduceVolume , setShouldReduceVolume ] = useState ( false ) ;
13- const startTimeoutRef = useRef < number | null > ( null ) ;
13+ const shouldReduceVolumeRef = useRef ( false ) ;
1414
1515 useEffect ( ( ) => {
1616 if ( isProgramOutputAdded ) {
@@ -25,24 +25,49 @@ export const useSpeakerDetection = ({
2525 }
2626 } , [ calls , isProgramOutputAdded ] ) ;
2727
28+ const startTimeoutRef = useRef < number | null > ( null ) ;
29+ const stopTimeoutRef = useRef < number | null > ( null ) ;
30+
2831 useEffect ( ( ) => {
2932 if ( isSomeoneSpeaking ) {
30- if ( ! shouldReduceVolume ) {
33+ if ( stopTimeoutRef . current !== null ) {
34+ window . clearTimeout ( stopTimeoutRef . current ) ;
35+ stopTimeoutRef . current = null ;
36+ }
37+
38+ if ( ! shouldReduceVolumeRef . current && startTimeoutRef . current === null ) {
3139 startTimeoutRef . current = window . setTimeout ( ( ) => {
40+ shouldReduceVolumeRef . current = true ;
3241 setShouldReduceVolume ( true ) ;
42+ startTimeoutRef . current = null ;
43+ } , 1000 ) ;
44+ }
45+ } else {
46+ if ( startTimeoutRef . current !== null ) {
47+ window . clearTimeout ( startTimeoutRef . current ) ;
48+ startTimeoutRef . current = null ;
49+ }
50+
51+ if ( shouldReduceVolumeRef . current && stopTimeoutRef . current === null ) {
52+ stopTimeoutRef . current = window . setTimeout ( ( ) => {
53+ shouldReduceVolumeRef . current = false ;
54+ setShouldReduceVolume ( false ) ;
55+ stopTimeoutRef . current = null ;
3356 } , 1000 ) ;
3457 }
35- } else if ( shouldReduceVolume ) {
36- setShouldReduceVolume ( false ) ;
3758 }
3859
3960 return ( ) => {
4061 if ( startTimeoutRef . current !== null ) {
4162 window . clearTimeout ( startTimeoutRef . current ) ;
4263 startTimeoutRef . current = null ;
4364 }
65+ if ( stopTimeoutRef . current !== null ) {
66+ window . clearTimeout ( stopTimeoutRef . current ) ;
67+ stopTimeoutRef . current = null ;
68+ }
4469 } ;
45- } , [ isSomeoneSpeaking , shouldReduceVolume ] ) ;
70+ } , [ isSomeoneSpeaking ] ) ;
4671
4772 return { isSomeoneSpeaking, shouldReduceVolume } ;
4873} ;
0 commit comments