-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
89 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,26 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
type TAudioElement = { | ||
audioCtx: AudioContext; | ||
gainNode: GainNode; | ||
source: MediaStreamAudioSourceNode; | ||
}; | ||
|
||
type TUseControlVolumeOptions = { | ||
stream: MediaStream | null; | ||
audioElements: TAudioElement[]; | ||
}; | ||
|
||
type TUseControlVolume = (options: TUseControlVolumeOptions) => { | ||
setVolume: (value: number) => void; | ||
audioContext: AudioContext | null; | ||
}; | ||
|
||
export const useControlVolume: TUseControlVolume = ({ stream }) => { | ||
const [audioContext, setAudioContext] = useState<AudioContext | null>(null); | ||
const gainNodeRef = useRef<GainNode | null>(null); | ||
|
||
useEffect(() => { | ||
if (!stream) return; | ||
|
||
console.log("Initializing Audio Context"); | ||
|
||
const audioCtx = new AudioContext(); | ||
setAudioContext(audioCtx); | ||
|
||
const source = audioCtx.createMediaStreamSource(stream); | ||
|
||
const gainNode = audioCtx.createGain(); | ||
gainNode.gain.value = 0.5; | ||
|
||
source.connect(gainNode); | ||
// Outputs the audio to the speakers | ||
gainNode.connect(audioCtx.destination); | ||
|
||
gainNodeRef.current = gainNode; | ||
|
||
// eslint-disable-next-line consistent-return | ||
return () => { | ||
source.disconnect(); | ||
gainNode.disconnect(); | ||
audioCtx.close(); | ||
gainNodeRef.current = null; | ||
}; | ||
}, [stream]); | ||
|
||
export const useControlVolume: TUseControlVolume = ({ audioElements }) => { | ||
const setVolume = (value: number) => { | ||
if (gainNodeRef.current) { | ||
audioElements.forEach(({ gainNode }) => { | ||
const clampedValue = Math.max(0, Math.min(1, value)); | ||
gainNodeRef.current.gain.value = clampedValue; | ||
gainNode.gain.setValueAtTime(clampedValue, gainNode.context.currentTime); | ||
|
||
console.log("Setting Gain Node Volume:", clampedValue); | ||
} | ||
}); | ||
}; | ||
|
||
return { setVolume, audioContext }; | ||
return { setVolume }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters