Skip to content

Commit acf0caf

Browse files
malmen237Linda Malm
andauthored
Fix/ifb issues (#390)
Co-authored-by: Linda Malm <lindamalm@Lindas-MacBook-Pro.local>
1 parent 16cfe77 commit acf0caf

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/components/calls-page/use-speaker-detection.tsx

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

src/components/production-line/use-volume-reducer.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const useVolumeReducer = ({
1212
shouldReduceVolume: boolean;
1313
value: number;
1414
}) => {
15-
const increaseVolumeTimeoutRef = useRef<number | null>(null);
1615
const hasReducedRef = useRef(false);
1716

1817
useEffect(() => {
@@ -30,20 +29,12 @@ export const useVolumeReducer = ({
3029
}
3130

3231
if (!shouldReduceVolume && hasReducedRef.current) {
33-
increaseVolumeTimeoutRef.current = window.setTimeout(() => {
34-
audioElements?.forEach((audioElement) => {
35-
// eslint-disable-next-line no-param-reassign
36-
audioElement.volume = value;
37-
});
38-
hasReducedRef.current = false;
39-
}, 2000);
32+
audioElements?.forEach((audioElement) => {
33+
// eslint-disable-next-line no-param-reassign
34+
audioElement.volume = value;
35+
});
36+
hasReducedRef.current = false;
4037
}
4138
}
42-
43-
return () => {
44-
if (increaseVolumeTimeoutRef.current) {
45-
window.clearTimeout(increaseVolumeTimeoutRef.current);
46-
}
47-
};
4839
}, [audioElements, line?.programOutputLine, shouldReduceVolume, value]);
4940
};

0 commit comments

Comments
 (0)