2022-04-05 05:09:35 +08:00
|
|
|
import React, { useState, useEffect, useRef } from 'react';
|
2017-06-04 10:40:14 +08:00
|
|
|
import PropTypes from 'prop-types';
|
2022-04-05 05:09:35 +08:00
|
|
|
import hark from 'hark';
|
|
|
|
import Styled from './styles';
|
|
|
|
|
|
|
|
const VOL_POLLING_INTERVAL_MS = 100;
|
|
|
|
const VOL_FLOOR = 0;
|
|
|
|
const VOL_CEIL = 50;
|
|
|
|
const DB_AMPL = 65;
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
2022-04-19 04:05:26 +08:00
|
|
|
stream: PropTypes.shape({
|
|
|
|
active: PropTypes.bool,
|
|
|
|
id: PropTypes.string,
|
|
|
|
}),
|
2022-04-05 05:09:35 +08:00
|
|
|
volumeFloor: PropTypes.number,
|
|
|
|
volumeRange: PropTypes.number,
|
2016-12-09 02:17:17 +08:00
|
|
|
optimum: PropTypes.number,
|
|
|
|
high: PropTypes.number,
|
2022-04-19 04:05:26 +08:00
|
|
|
low: PropTypes.number,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
2022-04-05 05:09:35 +08:00
|
|
|
const AudioStreamVolume = ({
|
2024-06-11 21:05:08 +08:00
|
|
|
volumeFloor = VOL_FLOOR,
|
|
|
|
volumeRange = VOL_CEIL,
|
|
|
|
low = VOL_FLOOR,
|
|
|
|
optimum = Math.round(0.3 * VOL_CEIL),
|
|
|
|
high = Math.round(0.4 * VOL_CEIL),
|
|
|
|
stream = null,
|
2022-04-05 05:09:35 +08:00
|
|
|
}) => {
|
|
|
|
const harkObserver = useRef(null);
|
|
|
|
const volumeRef = useRef(0);
|
|
|
|
const [volume, setVolume] = useState(0);
|
|
|
|
|
|
|
|
const handleVolumeChange = (dbVolume) => {
|
|
|
|
const previousVolume = volumeRef.current;
|
|
|
|
// Normalize it into 0 - range . DB_AMPL is the "relevance factor" -
|
|
|
|
// original formula is / 20
|
2022-04-19 04:05:26 +08:00
|
|
|
const linearVolume = (10 ** (dbVolume / DB_AMPL)) * (volumeRange);
|
2022-04-05 05:09:35 +08:00
|
|
|
// If the current linear volume is lower than 1/10 of the total volume range,
|
|
|
|
// ignore to minimize re-renders. Otherwise: generate the next volume val
|
|
|
|
// by smoothing the transition with the previous value and rounding it up
|
|
|
|
const nextVolume = (linearVolume <= (volumeRange / 10))
|
|
|
|
? volumeFloor
|
|
|
|
: Math.round((0.65 * previousVolume) + (0.35 * linearVolume));
|
|
|
|
|
|
|
|
if (previousVolume !== nextVolume) {
|
|
|
|
volumeRef.current = nextVolume;
|
|
|
|
setVolume(nextVolume);
|
2016-12-20 01:15:48 +08:00
|
|
|
}
|
2022-04-19 04:05:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const observeVolumeChanges = (_stream) => {
|
|
|
|
if (_stream) {
|
|
|
|
harkObserver.current = hark(_stream, { interval: VOL_POLLING_INTERVAL_MS });
|
|
|
|
harkObserver.current.on('volume_change', handleVolumeChange);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const stopObservingVolumeChanges = () => {
|
|
|
|
harkObserver.current?.stop();
|
|
|
|
harkObserver.current = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
observeVolumeChanges();
|
|
|
|
return stopObservingVolumeChanges;
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
stopObservingVolumeChanges();
|
|
|
|
observeVolumeChanges(stream);
|
|
|
|
}, [stream]);
|
2016-12-09 02:17:17 +08:00
|
|
|
|
2022-04-05 05:09:35 +08:00
|
|
|
return (
|
|
|
|
<Styled.VolumeMeter
|
2022-07-16 04:54:16 +08:00
|
|
|
data-test={volume > 0 ? 'hasVolume' : 'hasNoVolume'}
|
2022-04-05 05:09:35 +08:00
|
|
|
min={volumeFloor}
|
|
|
|
low={low}
|
|
|
|
max={high * 1.25}
|
|
|
|
optimum={optimum}
|
|
|
|
high={high}
|
|
|
|
value={volume}
|
|
|
|
/>
|
|
|
|
);
|
2022-04-19 04:05:26 +08:00
|
|
|
};
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
AudioStreamVolume.propTypes = propTypes;
|
|
|
|
|
2022-04-05 05:09:35 +08:00
|
|
|
export default React.memo(AudioStreamVolume);
|