You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioInputPreview.web.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React, { useEffect, useState } from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. // @ts-ignore
  4. import JitsiMeetJS from '../../base/lib-jitsi-meet/_.web';
  5. const JitsiTrackEvents = JitsiMeetJS.events.track;
  6. /**
  7. * The type of the React {@code Component} props of {@link AudioInputPreview}.
  8. */
  9. interface IProps {
  10. /**
  11. * The JitsiLocalTrack to show an audio level meter for.
  12. */
  13. track: any;
  14. }
  15. const useStyles = makeStyles()(theme => {
  16. return {
  17. container: {
  18. display: 'flex'
  19. },
  20. section: {
  21. flex: 1,
  22. height: '4px',
  23. borderRadius: '1px',
  24. backgroundColor: theme.palette.ui04,
  25. marginRight: theme.spacing(1),
  26. '&:last-of-type': {
  27. marginRight: 0
  28. }
  29. },
  30. activeSection: {
  31. backgroundColor: theme.palette.success01
  32. }
  33. };
  34. });
  35. const NO_OF_PREVIEW_SECTIONS = 11;
  36. const AudioInputPreview = (props: IProps) => {
  37. const [ audioLevel, setAudioLevel ] = useState(0);
  38. const { classes, cx } = useStyles();
  39. /**
  40. * Starts listening for audio level updates from the library.
  41. *
  42. * @param {JitsiLocalTrack} track - The track to listen to for audio level
  43. * updates.
  44. * @private
  45. * @returns {void}
  46. */
  47. function _listenForAudioUpdates(track: any) {
  48. _stopListeningForAudioUpdates();
  49. track?.on(
  50. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  51. setAudioLevel);
  52. }
  53. /**
  54. * Stops listening to further updates from the current track.
  55. *
  56. * @private
  57. * @returns {void}
  58. */
  59. function _stopListeningForAudioUpdates() {
  60. props.track?.off(
  61. JitsiTrackEvents.TRACK_AUDIO_LEVEL_CHANGED,
  62. setAudioLevel);
  63. }
  64. useEffect(() => {
  65. _listenForAudioUpdates(props.track);
  66. return _stopListeningForAudioUpdates;
  67. }, []);
  68. useEffect(() => {
  69. _listenForAudioUpdates(props.track);
  70. setAudioLevel(0);
  71. }, [ props.track ]);
  72. const audioMeterFill = Math.ceil(Math.floor(audioLevel * 100) / (100 / NO_OF_PREVIEW_SECTIONS));
  73. return (
  74. <div className = { classes.container } >
  75. {new Array(NO_OF_PREVIEW_SECTIONS).fill(0)
  76. .map((_, idx) =>
  77. (<div
  78. className = { cx(classes.section, idx < audioMeterFill && classes.activeSection) }
  79. key = { idx } />)
  80. )}
  81. </div>
  82. );
  83. };
  84. export default AudioInputPreview;