react-mic-record
Version:
Record audio from your microphone
136 lines (109 loc) • 5.09 kB
JavaScript
var _class, _temp;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import React from 'react';
import MicrophoneRecorder from '../libs/MicrophoneRecorder';
import AudioContext from '../libs/AudioContext';
import AudioPlayer from '../libs/AudioPlayer';
import Visualizer from '../libs/Visualizer';
var ReactMicRecord = (_temp = _class = function (_React$Component) {
_inherits(ReactMicRecord, _React$Component);
function ReactMicRecord(props) {
_classCallCheck(this, ReactMicRecord);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props));
_this.audioContext = null;
_this.microphoneRecorder = null;
_this.state = {
canvas: null,
canvasCtx: null
};
return _this;
}
ReactMicRecord.prototype.componentDidMount = function componentDidMount() {
var _this2 = this;
var _props = this.props,
onStop = _props.onStop,
onStart = _props.onStart,
onData = _props.onData,
audioElem = _props.audioElem,
audioBitsPerSecond = _props.audioBitsPerSecond,
mimeType = _props.mimeType;
var canvas = this.visualizerRef;
var canvasCtx = canvas.getContext("2d");
var options = { audioBitsPerSecond: audioBitsPerSecond, mimeType: mimeType };
this.audioContext = new AudioContext();
if (audioElem) {
this.audioPlayer = new AudioPlayer(audioElem, this.audioContext);
} else {
this.microphoneRecorder = new MicrophoneRecorder(onStart, onStop, onData, options, this.audioContext);
}
this.setState({ canvas: canvas, canvasCtx: canvasCtx }, function () {
return _this2.visualize();
});
};
ReactMicRecord.prototype.componentWillUnmount = function componentWillUnmount() {
if (this.microphoneRecorder) {
this.microphoneRecorder.unMount();
this.clear();
}
};
ReactMicRecord.prototype.visualize = function visualize() {
var _props2 = this.props,
backgroundColor = _props2.backgroundColor,
strokeColor = _props2.strokeColor,
width = _props2.width,
height = _props2.height,
visualSetting = _props2.visualSetting;
var _state = this.state,
canvas = _state.canvas,
canvasCtx = _state.canvasCtx;
this.visualizer = new Visualizer(this.audioContext, canvasCtx, canvas, width, height, backgroundColor, strokeColor);
if (visualSetting === 'sinewave') {
this.visualizer.visualizeSineWave();
} else if (visualSetting === 'frequencyBars') {
this.visualizer.visualizeFrequencyBars();
}
};
ReactMicRecord.prototype.clear = function clear() {
var _state2 = this.state,
canvasCtx = _state2.canvasCtx,
width = _state2.width,
height = _state2.height;
canvasCtx.clearRect(0, 0, width, height);
};
ReactMicRecord.prototype.render = function render() {
var _this3 = this;
var _props3 = this.props,
record = _props3.record,
width = _props3.width,
height = _props3.height,
className = _props3.className;
if (record) {
if (this.microphoneRecorder) {
this.microphoneRecorder.startRecording();
this.visualize();
}
} else {
if (this.microphoneRecorder) {
this.microphoneRecorder.stopRecording();
this.clear();
}
}
return React.createElement('canvas', { ref: function ref(c) {
return _this3.visualizerRef = c;
}, height: height, width: width, className: className });
};
return ReactMicRecord;
}(React.Component), _class.defaultProps = {
backgroundColor: 'rgba(255, 255, 255, 0.5)',
strokeColor: '#000000',
className: 'visualizer',
audioBitsPerSecond: 128000,
mimeType: 'audio/webm;codecs=opus',
record: false,
width: 640,
height: 100,
visualSetting: 'sinewave'
}, _temp);
export { ReactMicRecord as default };