-
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
7 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
type TUseControlVolumeOptions = { | ||
stream: MediaStream | null; | ||
}; | ||
|
||
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]); | ||
|
||
const setVolume = (value: number) => { | ||
if (gainNodeRef.current) { | ||
const clampedValue = Math.max(0, Math.min(1, value)); | ||
gainNodeRef.current.gain.value = clampedValue; | ||
|
||
console.log("Setting Gain Node Volume:", clampedValue); | ||
} | ||
}; | ||
|
||
return { setVolume, audioContext }; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import styled from "@emotion/styled"; | ||
import { FC, useState } from "react"; | ||
import { NoSoundIcon, FullSoundIcon } from "../../assets/icons/icon"; | ||
|
||
const SliderWrapper = styled.div` | ||
width: 100%; | ||
margin: 2rem 0; | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
position: relative; | ||
`; | ||
|
||
const IconWrapper = styled.div` | ||
width: 5rem; | ||
height: 5rem; | ||
padding: 0.5rem; | ||
`; | ||
|
||
const SliderTrack = styled.div` | ||
width: 80%; | ||
height: 0.4rem; | ||
background-color: #e0e0e0; | ||
border-radius: 0.2rem; | ||
position: relative; | ||
`; | ||
|
||
const SliderThumb = styled.div<{ position: number }>` | ||
width: 1.5rem; | ||
height: 1.5rem; | ||
background-color: #59cbe8; | ||
border-radius: 50%; | ||
position: absolute; | ||
top: -0.6rem; | ||
left: ${({ position }) => `${position}%`}; | ||
transform: translateX(-50%); | ||
cursor: pointer; | ||
`; | ||
|
||
type SliderProps = { | ||
min: number; | ||
max: number; | ||
step?: number; | ||
initialValue?: number; | ||
setVolume: (value: number) => void; | ||
}; | ||
|
||
export const Slider: FC<SliderProps> = ({ | ||
min, | ||
max, | ||
step = 1, | ||
initialValue = min, | ||
setVolume, | ||
}) => { | ||
const [value, setValue] = useState(initialValue); | ||
|
||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const newValue = Number(e.target.value); | ||
setValue(newValue); | ||
setVolume(newValue / max); | ||
|
||
console.log("Slider value: ", newValue); | ||
console.log("Normalized value: ", newValue / max); | ||
}; | ||
|
||
const thumbPosition = ((value - min) / (max - min)) * 100; | ||
|
||
return ( | ||
<SliderWrapper> | ||
<IconWrapper> | ||
<NoSoundIcon /> | ||
</IconWrapper> | ||
<SliderTrack> | ||
<SliderThumb position={thumbPosition} /> | ||
<input | ||
type="range" | ||
min={min} | ||
max={max} | ||
step={step} | ||
value={value} | ||
onChange={handleInputChange} | ||
style={{ | ||
width: "100%", | ||
position: "absolute", | ||
top: 0, | ||
height: "0.4rem", | ||
opacity: 0, | ||
pointerEvents: "all", | ||
}} | ||
/> | ||
</SliderTrack> | ||
<IconWrapper> | ||
<FullSoundIcon /> | ||
</IconWrapper> | ||
</SliderWrapper> | ||
); | ||
}; |