Skip to content

Commit

Permalink
add mic volume indication (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
bianxg authored Nov 18, 2024
1 parent a0d3164 commit 9b136ce
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/components/volume/MicVolume.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import LinearProgress from '@mui/material/LinearProgress';
import { useContext, useEffect, useState } from 'react';
import { ServiceContext } from '../../store/store';
import { VolumeWatcher } from '../../utils/volumeWatcher';
import hark from 'hark';

const MicVolume = (): JSX.Element => {
const { mediaService } = useContext(ServiceContext);
const [ volumeLevel, setVolume ] = useState<number>(0);

useEffect(() => {
let volumeWatcher: VolumeWatcher | undefined;

// Add hark for mic
if (mediaService.previewMicTrack) {
const harkStream = new MediaStream();

harkStream.addTrack(mediaService.previewMicTrack.clone());

const micHark = hark(harkStream, {
play: false,
interval: 100,
threshold: -60,
history: 100
});

volumeWatcher = new VolumeWatcher({ hark: micHark });
}

const onVolumeChange = ({ scaledVolume }: { scaledVolume: number }): void => {
setVolume(scaledVolume*10); // Range: 0-100
};

volumeWatcher?.on('volumeChange', onVolumeChange);

return () => {
volumeWatcher?.off('volumeChange', onVolumeChange);
};
}, [ mediaService.previewMicTrack ]);

return (
<LinearProgress color='success' variant="determinate" value={volumeLevel} />
);
};

export default MicVolume;
2 changes: 2 additions & 0 deletions src/views/join/Join.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import AudioOutputChooser from '../../components/devicechooser/AudioOutputChoose
import { canSelectAudioOutput } from '../../store/selectors';
import TestAudioOutputButton from '../../components/audiooutputtest/AudioOutputTest';
import ImpressumButton from '../../components/controlbuttons/ImpressumButton';
import MicVolume from '../../components/volume/MicVolume';

interface JoinProps {
roomId: string;
Expand Down Expand Up @@ -70,6 +71,7 @@ const Join = ({ roomId }: JoinProps): React.JSX.Element => {
<AudioInputChooser />
{ showAudioOutputChooser && <AudioOutputChooser /> }
<VideoInputChooser />
<MicVolume />
<TestAudioOutputButton />
<BlurSwitch />
<ChooserDiv>
Expand Down

0 comments on commit 9b136ce

Please sign in to comment.