UNPKG

uploadable-react-audio-recorder

Version:
137 lines (136 loc) 5.55 kB
import encodeWAV from './waveEncoder'; /* interface Navigator { webkitGetUserMedia?: typeof navigator.getUserMedia, mozGetUserMedia?: typeof navigator.getUserMedia, msGetUserMedia?: typeof navigator.getUserMedia, }; navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; */ function getAudioContext() { var ctxt = { sampleRate: null, createGain: null, createMediaStreamSource: null, createScriptProcessor: null, destination: null, decodeAudioData: null, createBufferSource: null }; try { ctxt = new AudioContext(); } catch (e) { console.log(e); } return ctxt; } var WAVEInterface = /** @class */ (function () { function WAVEInterface() { this.recordingNodes = []; } Object.defineProperty(WAVEInterface.prototype, "bufferLength", { get: function () { return this.buffers[0].length * WAVEInterface.bufferSize; }, enumerable: true, configurable: true }); Object.defineProperty(WAVEInterface.prototype, "audioDuration", { get: function () { return this.bufferLength / WAVEInterface.audioContext.sampleRate; }, enumerable: true, configurable: true }); Object.defineProperty(WAVEInterface.prototype, "audioData", { get: function () { return this.encodingCache || encodeWAV(this.buffers, this.bufferLength, WAVEInterface.audioContext.sampleRate); }, enumerable: true, configurable: true }); WAVEInterface.prototype.startRecording = function () { var _this = this; return new Promise(function (resolve, reject) { try { navigator.getUserMedia({ audio: true }, function (stream) { var audioContext = WAVEInterface.audioContext; var recGainNode = audioContext.createGain(); var recSourceNode = audioContext.createMediaStreamSource(stream); var recProcessingNode = audioContext.createScriptProcessor(WAVEInterface.bufferSize, 2, 2); if (recGainNode && recSourceNode && recProcessingNode) { if (_this.encodingCache) _this.encodingCache = null; recProcessingNode.onaudioprocess = function (event) { // console.log('audio process', this); if (_this.encodingCache) _this.encodingCache = null; // save left and right buffers for (var i = 0; i < 2; i++) { var channel = event.inputBuffer.getChannelData(i); _this.buffers[i].push(new Float32Array(channel)); } }; recSourceNode.connect(recGainNode); recGainNode.connect(recProcessingNode); recProcessingNode.connect(audioContext.destination); _this.recordingStream = stream; _this.recordingNodes.push(recSourceNode, recGainNode, recProcessingNode); resolve(stream); } }, function (err) { reject(err); }); } catch (e) { reject(e); } }); }; WAVEInterface.prototype.stopRecording = function () { if (this.recordingStream) { this.recordingStream.getTracks()[0].stop(); delete this.recordingStream; } for (var i in this.recordingNodes) { this.recordingNodes[i].disconnect(); delete this.recordingNodes[i]; } }; WAVEInterface.prototype.startPlayback = function (loop, onended) { var _this = this; if (loop === void 0) { loop = false; } return new Promise(function (resolve, reject) { var reader = new FileReader(); reader.readAsArrayBuffer(_this.audioData); reader.onloadend = function () { WAVEInterface.audioContext.decodeAudioData(reader.result, function (buffer) { var source = WAVEInterface.audioContext.createBufferSource(); source.buffer = buffer; source.connect(WAVEInterface.audioContext.destination); source.loop = loop; source.start(0); source.onended = onended; _this.playbackNode = source; resolve(source); }); }; }); }; WAVEInterface.prototype.stopPlayback = function () { this.playbackNode.stop(); }; WAVEInterface.prototype.reset = function () { if (this.playbackNode) { this.playbackNode.stop(); this.playbackNode.disconnect(0); delete this.playbackNode; } this.stopRecording(); this.buffers = [[], []]; }; WAVEInterface.audioContext = getAudioContext(); WAVEInterface.bufferSize = 2048; return WAVEInterface; }()); export default WAVEInterface;