UNPKG

react-mic-record

Version:
158 lines (119 loc) 5.67 kB
'use strict'; exports.__esModule = true; exports.default = undefined; var _class, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _MicrophoneRecorder = require('../libs/MicrophoneRecorder'); var _MicrophoneRecorder2 = _interopRequireDefault(_MicrophoneRecorder); var _AudioContext = require('../libs/AudioContext'); var _AudioContext2 = _interopRequireDefault(_AudioContext); var _AudioPlayer = require('../libs/AudioPlayer'); var _AudioPlayer2 = _interopRequireDefault(_AudioPlayer); var _Visualizer = require('../libs/Visualizer'); var _Visualizer2 = _interopRequireDefault(_Visualizer); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } 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 _AudioContext2.default(); if (audioElem) { this.audioPlayer = new _AudioPlayer2.default(audioElem, this.audioContext); } else { this.microphoneRecorder = new _MicrophoneRecorder2.default(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 _Visualizer2.default(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 _react2.default.createElement('canvas', { ref: function ref(c) { return _this3.visualizerRef = c; }, height: height, width: width, className: className }); }; return ReactMicRecord; }(_react2.default.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); exports.default = ReactMicRecord; module.exports = exports['default'];