2017-06-04 10:40:14 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-05-04 01:19:21 +08:00
|
|
|
import { logClient } from '/imports/ui/services/api';
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
low: PropTypes.number,
|
|
|
|
optimum: PropTypes.number,
|
|
|
|
high: PropTypes.number,
|
|
|
|
deviceId: PropTypes.string,
|
|
|
|
};
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
low: 0,
|
2017-03-27 23:45:24 +08:00
|
|
|
optimum: 0.05,
|
|
|
|
high: 0.3,
|
2016-12-20 01:15:48 +08:00
|
|
|
deviceId: undefined,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
|
|
|
|
|
|
class AudioStreamVolume extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.createAudioContext = this.createAudioContext.bind(this);
|
|
|
|
this.closeAudioContext = this.closeAudioContext.bind(this);
|
|
|
|
this.handleConnectStreamToProcessor = this.handleConnectStreamToProcessor.bind(this);
|
|
|
|
this.handleAudioProcess = this.handleAudioProcess.bind(this);
|
|
|
|
this.handleError = this.handleError.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
instant: 0,
|
|
|
|
slow: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.createAudioContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.deviceId !== this.props.deviceId) {
|
|
|
|
this.closeAudioContext().then(() => {
|
|
|
|
this.setState({
|
|
|
|
instant: 0,
|
|
|
|
slow: 0,
|
|
|
|
});
|
2016-12-19 19:35:33 +08:00
|
|
|
this.createAudioContext();
|
2016-12-09 02:17:17 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.closeAudioContext();
|
|
|
|
}
|
|
|
|
|
|
|
|
createAudioContext() {
|
|
|
|
this.audioContext = new AudioContext();
|
|
|
|
this.scriptProcessor = this.audioContext.createScriptProcessor(2048, 1, 1);
|
|
|
|
this.scriptProcessor.onaudioprocess = this.handleAudioProcess;
|
|
|
|
this.source = null;
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
const constraints = {
|
2016-12-20 01:15:48 +08:00
|
|
|
audio: true,
|
2016-12-09 02:17:17 +08:00
|
|
|
};
|
|
|
|
|
2016-12-20 01:15:48 +08:00
|
|
|
const { deviceId } = this.props;
|
|
|
|
|
|
|
|
if (deviceId) {
|
|
|
|
constraints.audio = {
|
|
|
|
deviceId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:17 +08:00
|
|
|
return navigator.mediaDevices
|
|
|
|
.getUserMedia(constraints)
|
|
|
|
.then(this.handleConnectStreamToProcessor)
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
|
|
|
|
|
|
|
closeAudioContext() {
|
|
|
|
return this.audioContext.close().then(() => {
|
|
|
|
this.audioContext = null;
|
|
|
|
this.scriptProcessor = null;
|
|
|
|
this.source = null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
handleConnectStreamToProcessor(stream) {
|
|
|
|
this.source = this.audioContext.createMediaStreamSource(stream);
|
|
|
|
this.source.connect(this.scriptProcessor);
|
2017-09-20 01:47:57 +08:00
|
|
|
// this.scriptProcessor.connect(this.audioContext.destination);
|
2016-12-09 02:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleAudioProcess(event) {
|
|
|
|
const input = event.inputBuffer.getChannelData(0);
|
|
|
|
const sum = input.reduce((a, b) => a + (b * b), 0);
|
|
|
|
const instant = Math.sqrt(sum / input.length);
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
this.setState(prevState => ({
|
|
|
|
instant,
|
2016-12-09 02:17:17 +08:00
|
|
|
slow: 0.75 * prevState.slow + 0.25 * instant,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(error) {
|
2017-05-04 01:19:21 +08:00
|
|
|
logClient('error', { error, method: 'handleError' });
|
2016-12-09 02:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { low, optimum, high, deviceId, ...props } = this.props;
|
|
|
|
const { instant, slow } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<meter
|
|
|
|
{...props}
|
|
|
|
min={0}
|
|
|
|
max={high * 1.25}
|
|
|
|
low={low}
|
|
|
|
optimum={optimum}
|
|
|
|
high={high}
|
|
|
|
value={this.state.slow}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
2016-12-09 02:17:17 +08:00
|
|
|
|
|
|
|
AudioStreamVolume.propTypes = propTypes;
|
|
|
|
AudioStreamVolume.defaultProps = defaultProps;
|
|
|
|
|
|
|
|
export default AudioStreamVolume;
|