From caba0284edd854c4cae9667bb2cdae20b2ca30bd Mon Sep 17 00:00:00 2001 From: Christopher Rudmin Date: Tue, 21 Jul 2020 12:16:17 -0400 Subject: [PATCH] Fix error handling --- dist-unminified/recorder.js | 2 +- dist/recorder.min.js | 2 +- src/recorder.js | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/dist-unminified/recorder.js b/dist-unminified/recorder.js index 470a4602..1f10ab52 100644 --- a/dist-unminified/recorder.js +++ b/dist-unminified/recorder.js @@ -115,7 +115,7 @@ eval("var g;\n\n// This works in non-strict mode\ng = (function() {\n\treturn th /***/ (function(module, exports, __webpack_require__) { "use strict"; -eval("/* WEBPACK VAR INJECTION */(function(global) {\n\nvar AudioContext = global.AudioContext || global.webkitAudioContext;\n\n\n// Constructor\nvar Recorder = function( config = {} ){\n\n if ( !Recorder.isRecordingSupported() ) {\n throw new Error(\"Recording is not supported in this browser\");\n }\n\n this.state = \"inactive\";\n this.config = Object.assign({\n bufferLength: 4096,\n encoderApplication: 2049,\n encoderFrameSize: 20,\n encoderPath: 'encoderWorker.min.js',\n encoderSampleRate: 48000,\n maxFramesPerPage: 40,\n mediaTrackConstraints: true,\n monitorGain: 0,\n numberOfChannels: 1,\n recordingGain: 1,\n resampleQuality: 3,\n streamPages: false,\n wavBitDepth: 16,\n sourceNode: { context: null },\n }, config );\n\n this.encodedSamplePosition = 0;\n this.initAudioContext();\n this.initialize = this.initWorklet().then(() => this.initEncoder());\n};\n\n\n// Static Methods\nRecorder.isRecordingSupported = function(){\n const getUserMediaSupported = global.navigator && global.navigator.mediaDevices && global.navigator.mediaDevices.getUserMedia;\n return AudioContext && getUserMediaSupported && global.WebAssembly;\n};\n\nRecorder.version = '8.0.1';\n\n\n// Instance Methods\nRecorder.prototype.clearStream = function(){\n if ( this.stream ){\n\n if ( this.stream.getTracks ) {\n this.stream.getTracks().forEach(track => track.stop());\n }\n\n else {\n this.stream.stop();\n }\n }\n};\n\nRecorder.prototype.close = function() {\n this.monitorGainNode.disconnect();\n this.recordingGainNode.disconnect();\n\n if (this.sourceNode) {\n this.sourceNode.disconnect();\n }\n\n this.clearStream();\n\n if (this.encoder) {\n this.encoderNode.disconnect();\n this.encoder.postMessage({ command: \"close\" });\n }\n\n if ( !this.config.sourceNode.context ){\n return this.audioContext.close();\n }\n\n return Promise.resolve();\n}\n\nRecorder.prototype.encodeBuffers = function( inputBuffer ){\n if ( this.state === \"recording\" ) {\n var buffers = [];\n for ( var i = 0; i < inputBuffer.numberOfChannels; i++ ) {\n buffers[i] = inputBuffer.getChannelData(i);\n }\n\n this.encoder.postMessage({\n command: \"encode\",\n buffers: buffers\n });\n }\n};\n\nRecorder.prototype.initAudioContext = function(){\n this.audioContext = this.config.sourceNode.context ? this.config.sourceNode.context : new AudioContext();\n\n this.monitorGainNode = this.audioContext.createGain();\n this.setMonitorGain( this.config.monitorGain );\n\n this.recordingGainNode = this.audioContext.createGain();\n this.setRecordingGain( this.config.recordingGain );\n};\n\nRecorder.prototype.initEncoder = function() {\n\n if (this.audioContext.audioWorklet) {\n this.encoderNode = new AudioWorkletNode(this.audioContext, 'encoder-worklet', { numberOfOutputs: 0 });\n this.encoder = this.encoderNode.port;\n }\n\n else {\n console.log('audioWorklet support not detected. Falling back to scriptProcessor');\n\n // Skip the first buffer\n this.encodeBuffers = () => delete this.encodeBuffers;\n\n this.encoderNode = this.audioContext.createScriptProcessor( this.config.bufferLength, this.config.numberOfChannels, this.config.numberOfChannels );\n this.encoderNode.onaudioprocess = ({ inputBuffer }) => this.encodeBuffers( inputBuffer );\n this.encoderNode.connect( this.audioContext.destination ); // Requires connection to destination to process audio\n this.encoder = new global.Worker(this.config.encoderPath);\n }\n};\n\nRecorder.prototype.initSourceNode = function(){\n if ( this.config.sourceNode.context ) {\n this.sourceNode = this.config.sourceNode;\n return Promise.resolve();\n }\n\n return global.navigator.mediaDevices.getUserMedia({ audio : this.config.mediaTrackConstraints }).then( stream => {\n this.stream = stream;\n this.sourceNode = this.audioContext.createMediaStreamSource( stream );\n });\n};\n\nRecorder.prototype.initWorker = function(){\n var onPage = (this.config.streamPages ? this.streamPage : this.storePage).bind(this);\n\n this.recordedPages = [];\n this.totalLength = 0;\n\n return new Promise(resolve => {\n var callback = ({ data }) => {\n switch( data['message'] ){\n case 'ready':\n resolve();\n break;\n case 'page':\n this.encodedSamplePosition = data['samplePosition'];\n onPage(data['page']);\n break;\n case 'done':\n this.encoder.removeEventListener( \"message\", callback );\n this.finish();\n break;\n }\n };\n\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if( this.encoder.start ) {\n this.encoder.start()\n }\n\n // exclude sourceNode\n const {sourceNode, ...config} = this.config;\n\n this.encoder.postMessage( Object.assign({\n command: 'init',\n originalSampleRate: this.audioContext.sampleRate,\n wavSampleRate: this.audioContext.sampleRate\n }, config));\n });\n};\n\nRecorder.prototype.initWorklet = function() {\n if (this.audioContext.audioWorklet) {\n return this.audioContext.audioWorklet.addModule(this.config.encoderPath);\n }\n\n return Promise.resolve();\n}\n\nRecorder.prototype.pause = function( flush ) {\n if ( this.state === \"recording\" ) {\n\n this.state = \"paused\";\n this.recordingGainNode.disconnect();\n\n if ( flush && this.config.streamPages ) {\n return new Promise(resolve => {\n\n var callback = ({ data }) => {\n if ( data[\"message\"] === 'flushed' ) {\n this.encoder.removeEventListener( \"message\", callback );\n this.onpause();\n resolve();\n }\n };\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if ( this.encoder.start ) {\n this.encoder.start()\n }\n\n this.encoder.postMessage( { command: \"flush\" } );\n });\n }\n this.onpause();\n return Promise.resolve();\n }\n};\n\nRecorder.prototype.resume = function() {\n if ( this.state === \"paused\" ) {\n this.state = \"recording\";\n this.recordingGainNode.connect(this.encoderNode);\n this.onresume();\n }\n};\n\nRecorder.prototype.setRecordingGain = function( gain ){\n this.config.recordingGain = gain;\n\n if ( this.recordingGainNode && this.audioContext ) {\n this.recordingGainNode.gain.setTargetAtTime(gain, this.audioContext.currentTime, 0.01);\n }\n};\n\nRecorder.prototype.setMonitorGain = function( gain ){\n this.config.monitorGain = gain;\n\n if ( this.monitorGainNode && this.audioContext ) {\n this.monitorGainNode.gain.setTargetAtTime(gain, this.audioContext.currentTime, 0.01);\n }\n};\n\nRecorder.prototype.start = function(){\n if ( this.state === \"inactive\" ) {\n this.encodedSamplePosition = 0;\n\n return this.audioContext.resume()\n .then(() => this.initialize)\n .then(() => Promise.all([this.initSourceNode(), this.initWorker()]))\n .then(() => {\n this.state = \"recording\";\n this.encoder.postMessage({ command: 'getHeaderPages' });\n this.sourceNode.connect( this.monitorGainNode );\n this.sourceNode.connect( this.recordingGainNode );\n this.monitorGainNode.connect( this.audioContext.destination );\n this.recordingGainNode.connect( this.encoderNode );\n this.onstart();\n });\n }\n return Promise.resolve();\n};\n\nRecorder.prototype.stop = function(){\n if ( this.state !== \"inactive\" ) {\n this.state = \"inactive\";\n\n // macOS and iOS requires the source to remain connected (in case stopped while paused)\n this.recordingGainNode.connect( this.encoderNode ); \n\n this.monitorGainNode.disconnect();\n this.clearStream();\n\n return new Promise(resolve => {\n var callback = ({ data }) => {\n if ( data[\"message\"] === 'done' ) {\n this.encoder.removeEventListener( \"message\", callback );\n resolve();\n }\n };\n\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if( this.encoder.start ) {\n this.encoder.start()\n }\n\n this.encoder.postMessage({ command: \"done\" });\n });\n }\n return Promise.resolve();\n};\n\nRecorder.prototype.storePage = function( page ) {\n this.recordedPages.push( page );\n this.totalLength += page.length;\n};\n\nRecorder.prototype.streamPage = function( page ) {\n this.ondataavailable( page );\n};\n\nRecorder.prototype.finish = function() {\n if( !this.config.streamPages ) {\n var outputData = new Uint8Array( this.totalLength );\n this.recordedPages.reduce( function( offset, page ){\n outputData.set( page, offset );\n return offset + page.length;\n }, 0);\n\n this.ondataavailable( outputData );\n }\n this.onstop();\n};\n\n\n// Callback Handlers\nRecorder.prototype.ondataavailable = function(){};\nRecorder.prototype.onpause = function(){};\nRecorder.prototype.onresume = function(){};\nRecorder.prototype.onstart = function(){};\nRecorder.prototype.onstop = function(){};\n\n\nmodule.exports = Recorder;\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../node_modules/webpack/buildin/global.js */ \"./node_modules/webpack/buildin/global.js\")))//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvcmVjb3JkZXIuanMuanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9SZWNvcmRlci8uL3NyYy9yZWNvcmRlci5qcz8wNTZmIl0sInNvdXJjZXNDb250ZW50IjpbIlwidXNlIHN0cmljdFwiO1xuXG52YXIgQXVkaW9Db250ZXh0ID0gZ2xvYmFsLkF1ZGlvQ29udGV4dCB8fCBnbG9iYWwud2Via2l0QXVkaW9Db250ZXh0O1xuXG5cbi8vIENvbnN0cnVjdG9yXG52YXIgUmVjb3JkZXIgPSBmdW5jdGlvbiggY29uZmlnID0ge30gKXtcblxuICBpZiAoICFSZWNvcmRlci5pc1JlY29yZGluZ1N1cHBvcnRlZCgpICkge1xuICAgIHRocm93IG5ldyBFcnJvcihcIlJlY29yZGluZyBpcyBub3Qgc3VwcG9ydGVkIGluIHRoaXMgYnJvd3NlclwiKTtcbiAgfVxuXG4gIHRoaXMuc3RhdGUgPSBcImluYWN0aXZlXCI7XG4gIHRoaXMuY29uZmlnID0gT2JqZWN0LmFzc2lnbih7XG4gICAgYnVmZmVyTGVuZ3RoOiA0MDk2LFxuICAgIGVuY29kZXJBcHBsaWNhdGlvbjogMjA0OSxcbiAgICBlbmNvZGVyRnJhbWVTaXplOiAyMCxcbiAgICBlbmNvZGVyUGF0aDogJ2VuY29kZXJXb3JrZXIubWluLmpzJyxcbiAgICBlbmNvZGVyU2FtcGxlUmF0ZTogNDgwMDAsXG4gICAgbWF4RnJhbWVzUGVyUGFnZTogNDAsXG4gICAgbWVkaWFUcmF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Q==\n//# sourceURL=webpack-internal:///./src/recorder.js\n"); +eval("/* WEBPACK VAR INJECTION */(function(global) {\n\nvar AudioContext = global.AudioContext || global.webkitAudioContext;\n\n\n// Constructor\nvar Recorder = function( config = {} ){\n\n if ( !Recorder.isRecordingSupported() ) {\n throw new Error(\"Recording is not supported in this browser\");\n }\n\n this.state = \"inactive\";\n this.config = Object.assign({\n bufferLength: 4096,\n encoderApplication: 2049,\n encoderFrameSize: 20,\n encoderPath: 'encoderWorker.min.js',\n encoderSampleRate: 48000,\n maxFramesPerPage: 40,\n mediaTrackConstraints: true,\n monitorGain: 0,\n numberOfChannels: 1,\n recordingGain: 1,\n resampleQuality: 3,\n streamPages: false,\n wavBitDepth: 16,\n sourceNode: { context: null },\n }, config );\n\n this.encodedSamplePosition = 0;\n this.initAudioContext();\n this.initialize = this.initWorklet().then(() => this.initEncoder());\n};\n\n\n// Static Methods\nRecorder.isRecordingSupported = function(){\n const getUserMediaSupported = global.navigator && global.navigator.mediaDevices && global.navigator.mediaDevices.getUserMedia;\n return AudioContext && getUserMediaSupported && global.WebAssembly;\n};\n\nRecorder.version = '8.0.2';\n\n\n// Instance Methods\nRecorder.prototype.clearStream = function(){\n if ( this.stream ){\n\n if ( this.stream.getTracks ) {\n this.stream.getTracks().forEach(track => track.stop());\n }\n\n else {\n this.stream.stop();\n }\n }\n};\n\nRecorder.prototype.close = function() {\n this.monitorGainNode.disconnect();\n this.recordingGainNode.disconnect();\n\n if (this.sourceNode) {\n this.sourceNode.disconnect();\n }\n\n this.clearStream();\n\n if (this.encoder) {\n this.encoderNode.disconnect();\n this.encoder.postMessage({ command: \"close\" });\n }\n\n if ( !this.config.sourceNode.context ){\n return this.audioContext.close();\n }\n\n return Promise.resolve();\n}\n\nRecorder.prototype.encodeBuffers = function( inputBuffer ){\n if ( this.state === \"recording\" ) {\n var buffers = [];\n for ( var i = 0; i < inputBuffer.numberOfChannels; i++ ) {\n buffers[i] = inputBuffer.getChannelData(i);\n }\n\n this.encoder.postMessage({\n command: \"encode\",\n buffers: buffers\n });\n }\n};\n\nRecorder.prototype.initAudioContext = function(){\n this.audioContext = this.config.sourceNode.context ? this.config.sourceNode.context : new AudioContext();\n\n this.monitorGainNode = this.audioContext.createGain();\n this.setMonitorGain( this.config.monitorGain );\n\n this.recordingGainNode = this.audioContext.createGain();\n this.setRecordingGain( this.config.recordingGain );\n};\n\nRecorder.prototype.initEncoder = function() {\n\n if (this.audioContext.audioWorklet) {\n this.encoderNode = new AudioWorkletNode(this.audioContext, 'encoder-worklet', { numberOfOutputs: 0 });\n this.encoder = this.encoderNode.port;\n }\n\n else {\n console.log('audioWorklet support not detected. Falling back to scriptProcessor');\n\n // Skip the first buffer\n this.encodeBuffers = () => delete this.encodeBuffers;\n\n this.encoderNode = this.audioContext.createScriptProcessor( this.config.bufferLength, this.config.numberOfChannels, this.config.numberOfChannels );\n this.encoderNode.onaudioprocess = ({ inputBuffer }) => this.encodeBuffers( inputBuffer );\n this.encoderNode.connect( this.audioContext.destination ); // Requires connection to destination to process audio\n this.encoder = new global.Worker(this.config.encoderPath);\n }\n};\n\nRecorder.prototype.initSourceNode = function(){\n if ( this.config.sourceNode.context ) {\n this.sourceNode = this.config.sourceNode;\n return Promise.resolve();\n }\n\n return global.navigator.mediaDevices.getUserMedia({ audio : this.config.mediaTrackConstraints }).then( stream => {\n this.stream = stream;\n this.sourceNode = this.audioContext.createMediaStreamSource( stream );\n });\n};\n\nRecorder.prototype.initWorker = function(){\n var onPage = (this.config.streamPages ? this.streamPage : this.storePage).bind(this);\n\n this.recordedPages = [];\n this.totalLength = 0;\n\n return new Promise(resolve => {\n var callback = ({ data }) => {\n switch( data['message'] ){\n case 'ready':\n resolve();\n break;\n case 'page':\n this.encodedSamplePosition = data['samplePosition'];\n onPage(data['page']);\n break;\n case 'done':\n this.encoder.removeEventListener( \"message\", callback );\n this.finish();\n break;\n }\n };\n\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if( this.encoder.start ) {\n this.encoder.start()\n }\n\n // exclude sourceNode\n const {sourceNode, ...config} = this.config;\n\n this.encoder.postMessage( Object.assign({\n command: 'init',\n originalSampleRate: this.audioContext.sampleRate,\n wavSampleRate: this.audioContext.sampleRate\n }, config));\n });\n};\n\nRecorder.prototype.initWorklet = function() {\n if (this.audioContext.audioWorklet) {\n return this.audioContext.audioWorklet.addModule(this.config.encoderPath);\n }\n\n return Promise.resolve();\n}\n\nRecorder.prototype.pause = function( flush ) {\n if ( this.state === \"recording\" ) {\n\n this.state = \"paused\";\n this.recordingGainNode.disconnect();\n\n if ( flush && this.config.streamPages ) {\n return new Promise(resolve => {\n\n var callback = ({ data }) => {\n if ( data[\"message\"] === 'flushed' ) {\n this.encoder.removeEventListener( \"message\", callback );\n this.onpause();\n resolve();\n }\n };\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if ( this.encoder.start ) {\n this.encoder.start()\n }\n\n this.encoder.postMessage( { command: \"flush\" } );\n });\n }\n this.onpause();\n return Promise.resolve();\n }\n};\n\nRecorder.prototype.resume = function() {\n if ( this.state === \"paused\" ) {\n this.state = \"recording\";\n this.recordingGainNode.connect(this.encoderNode);\n this.onresume();\n }\n};\n\nRecorder.prototype.setRecordingGain = function( gain ){\n this.config.recordingGain = gain;\n\n if ( this.recordingGainNode && this.audioContext ) {\n this.recordingGainNode.gain.setTargetAtTime(gain, this.audioContext.currentTime, 0.01);\n }\n};\n\nRecorder.prototype.setMonitorGain = function( gain ){\n this.config.monitorGain = gain;\n\n if ( this.monitorGainNode && this.audioContext ) {\n this.monitorGainNode.gain.setTargetAtTime(gain, this.audioContext.currentTime, 0.01);\n }\n};\n\nRecorder.prototype.start = function(){\n if ( this.state === \"inactive\" ) {\n this.state = 'loading';\n this.encodedSamplePosition = 0;\n\n return this.audioContext.resume()\n .then(() => this.initialize)\n .then(() => Promise.all([this.initSourceNode(), this.initWorker()]))\n .then(() => {\n this.state = \"recording\";\n this.encoder.postMessage({ command: 'getHeaderPages' });\n this.sourceNode.connect( this.monitorGainNode );\n this.sourceNode.connect( this.recordingGainNode );\n this.monitorGainNode.connect( this.audioContext.destination );\n this.recordingGainNode.connect( this.encoderNode );\n this.onstart();\n })\n .catch(error => {\n this.state = 'inactive';\n throw error;\n });\n }\n return Promise.resolve();\n};\n\nRecorder.prototype.stop = function(){\n if ( this.state === \"paused\" || this.state === \"recording\" ) {\n this.state = \"inactive\";\n\n // macOS and iOS requires the source to remain connected (in case stopped while paused)\n this.recordingGainNode.connect( this.encoderNode ); \n\n this.monitorGainNode.disconnect();\n this.clearStream();\n\n return new Promise(resolve => {\n var callback = ({ data }) => {\n if ( data[\"message\"] === 'done' ) {\n this.encoder.removeEventListener( \"message\", callback );\n resolve();\n }\n };\n\n this.encoder.addEventListener( \"message\", callback );\n\n // must call start for messagePort messages\n if( this.encoder.start ) {\n this.encoder.start()\n }\n\n this.encoder.postMessage({ command: \"done\" });\n });\n }\n return Promise.resolve();\n};\n\nRecorder.prototype.storePage = function( page ) {\n this.recordedPages.push( page );\n this.totalLength += page.length;\n};\n\nRecorder.prototype.streamPage = function( page ) {\n this.ondataavailable( page );\n};\n\nRecorder.prototype.finish = function() {\n if( !this.config.streamPages ) {\n var outputData = new Uint8Array( this.totalLength );\n this.recordedPages.reduce( function( offset, page ){\n outputData.set( page, offset );\n return offset + page.length;\n }, 0);\n\n this.ondataavailable( outputData );\n }\n this.onstop();\n};\n\n\n// Callback Handlers\nRecorder.prototype.ondataavailable = function(){};\nRecorder.prototype.onpause = function(){};\nRecorder.prototype.onresume = function(){};\nRecorder.prototype.onstart = function(){};\nRecorder.prototype.onstop = function(){};\n\n\nmodule.exports = Recorder;\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! ./../node_modules/webpack/buildin/global.js */ \"./node_modules/webpack/buildin/global.js\")))//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiLi9zcmMvcmVjb3JkZ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/ }) diff --git a/dist/recorder.min.js b/dist/recorder.min.js index c9277621..8e7194c7 100644 --- a/dist/recorder.min.js +++ b/dist/recorder.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Recorder=t():e.Recorder=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function o(i){if(t[i])return t[i].exports;var n=t[i]={i:i,l:!1,exports:{}};return e[i].call(n.exports,n,n.exports,o),n.l=!0,n.exports}return o.m=e,o.c=t,o.d=function(e,t,i){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(o.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)o.d(i,n,function(t){return e[t]}.bind(null,n));return i},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=0)}([function(e,t,o){"use strict";(function(t){var o=t.AudioContext||t.webkitAudioContext,i=function(e={}){if(!i.isRecordingSupported())throw new Error("Recording is not supported in this browser");this.state="inactive",this.config=Object.assign({bufferLength:4096,encoderApplication:2049,encoderFrameSize:20,encoderPath:"encoderWorker.min.js",encoderSampleRate:48e3,maxFramesPerPage:40,mediaTrackConstraints:!0,monitorGain:0,numberOfChannels:1,recordingGain:1,resampleQuality:3,streamPages:!1,wavBitDepth:16,sourceNode:{context:null}},e),this.encodedSamplePosition=0,this.initAudioContext(),this.initialize=this.initWorklet().then(()=>this.initEncoder())};i.isRecordingSupported=function(){const e=t.navigator&&t.navigator.mediaDevices&&t.navigator.mediaDevices.getUserMedia;return o&&e&&t.WebAssembly},i.version="8.0.1",i.prototype.clearStream=function(){this.stream&&(this.stream.getTracks?this.stream.getTracks().forEach(e=>e.stop()):this.stream.stop())},i.prototype.close=function(){return this.monitorGainNode.disconnect(),this.recordingGainNode.disconnect(),this.sourceNode&&this.sourceNode.disconnect(),this.clearStream(),this.encoder&&(this.encoderNode.disconnect(),this.encoder.postMessage({command:"close"})),this.config.sourceNode.context?Promise.resolve():this.audioContext.close()},i.prototype.encodeBuffers=function(e){if("recording"===this.state){for(var t=[],o=0;odelete this.encodeBuffers,this.encoderNode=this.audioContext.createScriptProcessor(this.config.bufferLength,this.config.numberOfChannels,this.config.numberOfChannels),this.encoderNode.onaudioprocess=({inputBuffer:e})=>this.encodeBuffers(e),this.encoderNode.connect(this.audioContext.destination),this.encoder=new t.Worker(this.config.encoderPath))},i.prototype.initSourceNode=function(){return this.config.sourceNode.context?(this.sourceNode=this.config.sourceNode,Promise.resolve()):t.navigator.mediaDevices.getUserMedia({audio:this.config.mediaTrackConstraints}).then(e=>{this.stream=e,this.sourceNode=this.audioContext.createMediaStreamSource(e)})},i.prototype.initWorker=function(){var e=(this.config.streamPages?this.streamPage:this.storePage).bind(this);return this.recordedPages=[],this.totalLength=0,new Promise(t=>{var o=({data:i})=>{switch(i.message){case"ready":t();break;case"page":this.encodedSamplePosition=i.samplePosition,e(i.page);break;case"done":this.encoder.removeEventListener("message",o),this.finish()}};this.encoder.addEventListener("message",o),this.encoder.start&&this.encoder.start();const{sourceNode:i,...n}=this.config;this.encoder.postMessage(Object.assign({command:"init",originalSampleRate:this.audioContext.sampleRate,wavSampleRate:this.audioContext.sampleRate},n))})},i.prototype.initWorklet=function(){return this.audioContext.audioWorklet?this.audioContext.audioWorklet.addModule(this.config.encoderPath):Promise.resolve()},i.prototype.pause=function(e){if("recording"===this.state)return this.state="paused",this.recordingGainNode.disconnect(),e&&this.config.streamPages?new Promise(e=>{var t=({data:o})=>{"flushed"===o.message&&(this.encoder.removeEventListener("message",t),this.onpause(),e())};this.encoder.addEventListener("message",t),this.encoder.start&&this.encoder.start(),this.encoder.postMessage({command:"flush"})}):(this.onpause(),Promise.resolve())},i.prototype.resume=function(){"paused"===this.state&&(this.state="recording",this.recordingGainNode.connect(this.encoderNode),this.onresume())},i.prototype.setRecordingGain=function(e){this.config.recordingGain=e,this.recordingGainNode&&this.audioContext&&this.recordingGainNode.gain.setTargetAtTime(e,this.audioContext.currentTime,.01)},i.prototype.setMonitorGain=function(e){this.config.monitorGain=e,this.monitorGainNode&&this.audioContext&&this.monitorGainNode.gain.setTargetAtTime(e,this.audioContext.currentTime,.01)},i.prototype.start=function(){return"inactive"===this.state?(this.encodedSamplePosition=0,this.audioContext.resume().then(()=>this.initialize).then(()=>Promise.all([this.initSourceNode(),this.initWorker()])).then(()=>{this.state="recording",this.encoder.postMessage({command:"getHeaderPages"}),this.sourceNode.connect(this.monitorGainNode),this.sourceNode.connect(this.recordingGainNode),this.monitorGainNode.connect(this.audioContext.destination),this.recordingGainNode.connect(this.encoderNode),this.onstart()})):Promise.resolve()},i.prototype.stop=function(){return"inactive"!==this.state?(this.state="inactive",this.recordingGainNode.connect(this.encoderNode),this.monitorGainNode.disconnect(),this.clearStream(),new Promise(e=>{var t=({data:o})=>{"done"===o.message&&(this.encoder.removeEventListener("message",t),e())};this.encoder.addEventListener("message",t),this.encoder.start&&this.encoder.start(),this.encoder.postMessage({command:"done"})})):Promise.resolve()},i.prototype.storePage=function(e){this.recordedPages.push(e),this.totalLength+=e.length},i.prototype.streamPage=function(e){this.ondataavailable(e)},i.prototype.finish=function(){if(!this.config.streamPages){var e=new Uint8Array(this.totalLength);this.recordedPages.reduce((function(t,o){return e.set(o,t),t+o.length}),0),this.ondataavailable(e)}this.onstop()},i.prototype.ondataavailable=function(){},i.prototype.onpause=function(){},i.prototype.onresume=function(){},i.prototype.onstart=function(){},i.prototype.onstop=function(){},e.exports=i}).call(this,o(1))},function(e,t){var o;o=function(){return this}();try{o=o||new Function("return this")()}catch(e){"object"==typeof window&&(o=window)}e.exports=o}])})); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Recorder=t():e.Recorder=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function o(i){if(t[i])return t[i].exports;var n=t[i]={i:i,l:!1,exports:{}};return e[i].call(n.exports,n,n.exports,o),n.l=!0,n.exports}return o.m=e,o.c=t,o.d=function(e,t,i){o.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},o.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},o.t=function(e,t){if(1&t&&(e=o(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(o.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)o.d(i,n,function(t){return e[t]}.bind(null,n));return i},o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,"a",t),t},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},o.p="",o(o.s=0)}([function(e,t,o){"use strict";(function(t){var o=t.AudioContext||t.webkitAudioContext,i=function(e={}){if(!i.isRecordingSupported())throw new Error("Recording is not supported in this browser");this.state="inactive",this.config=Object.assign({bufferLength:4096,encoderApplication:2049,encoderFrameSize:20,encoderPath:"encoderWorker.min.js",encoderSampleRate:48e3,maxFramesPerPage:40,mediaTrackConstraints:!0,monitorGain:0,numberOfChannels:1,recordingGain:1,resampleQuality:3,streamPages:!1,wavBitDepth:16,sourceNode:{context:null}},e),this.encodedSamplePosition=0,this.initAudioContext(),this.initialize=this.initWorklet().then(()=>this.initEncoder())};i.isRecordingSupported=function(){const e=t.navigator&&t.navigator.mediaDevices&&t.navigator.mediaDevices.getUserMedia;return o&&e&&t.WebAssembly},i.version="8.0.2",i.prototype.clearStream=function(){this.stream&&(this.stream.getTracks?this.stream.getTracks().forEach(e=>e.stop()):this.stream.stop())},i.prototype.close=function(){return this.monitorGainNode.disconnect(),this.recordingGainNode.disconnect(),this.sourceNode&&this.sourceNode.disconnect(),this.clearStream(),this.encoder&&(this.encoderNode.disconnect(),this.encoder.postMessage({command:"close"})),this.config.sourceNode.context?Promise.resolve():this.audioContext.close()},i.prototype.encodeBuffers=function(e){if("recording"===this.state){for(var t=[],o=0;odelete this.encodeBuffers,this.encoderNode=this.audioContext.createScriptProcessor(this.config.bufferLength,this.config.numberOfChannels,this.config.numberOfChannels),this.encoderNode.onaudioprocess=({inputBuffer:e})=>this.encodeBuffers(e),this.encoderNode.connect(this.audioContext.destination),this.encoder=new t.Worker(this.config.encoderPath))},i.prototype.initSourceNode=function(){return this.config.sourceNode.context?(this.sourceNode=this.config.sourceNode,Promise.resolve()):t.navigator.mediaDevices.getUserMedia({audio:this.config.mediaTrackConstraints}).then(e=>{this.stream=e,this.sourceNode=this.audioContext.createMediaStreamSource(e)})},i.prototype.initWorker=function(){var e=(this.config.streamPages?this.streamPage:this.storePage).bind(this);return this.recordedPages=[],this.totalLength=0,new Promise(t=>{var o=({data:i})=>{switch(i.message){case"ready":t();break;case"page":this.encodedSamplePosition=i.samplePosition,e(i.page);break;case"done":this.encoder.removeEventListener("message",o),this.finish()}};this.encoder.addEventListener("message",o),this.encoder.start&&this.encoder.start();const{sourceNode:i,...n}=this.config;this.encoder.postMessage(Object.assign({command:"init",originalSampleRate:this.audioContext.sampleRate,wavSampleRate:this.audioContext.sampleRate},n))})},i.prototype.initWorklet=function(){return this.audioContext.audioWorklet?this.audioContext.audioWorklet.addModule(this.config.encoderPath):Promise.resolve()},i.prototype.pause=function(e){if("recording"===this.state)return this.state="paused",this.recordingGainNode.disconnect(),e&&this.config.streamPages?new Promise(e=>{var t=({data:o})=>{"flushed"===o.message&&(this.encoder.removeEventListener("message",t),this.onpause(),e())};this.encoder.addEventListener("message",t),this.encoder.start&&this.encoder.start(),this.encoder.postMessage({command:"flush"})}):(this.onpause(),Promise.resolve())},i.prototype.resume=function(){"paused"===this.state&&(this.state="recording",this.recordingGainNode.connect(this.encoderNode),this.onresume())},i.prototype.setRecordingGain=function(e){this.config.recordingGain=e,this.recordingGainNode&&this.audioContext&&this.recordingGainNode.gain.setTargetAtTime(e,this.audioContext.currentTime,.01)},i.prototype.setMonitorGain=function(e){this.config.monitorGain=e,this.monitorGainNode&&this.audioContext&&this.monitorGainNode.gain.setTargetAtTime(e,this.audioContext.currentTime,.01)},i.prototype.start=function(){return"inactive"===this.state?(this.state="loading",this.encodedSamplePosition=0,this.audioContext.resume().then(()=>this.initialize).then(()=>Promise.all([this.initSourceNode(),this.initWorker()])).then(()=>{this.state="recording",this.encoder.postMessage({command:"getHeaderPages"}),this.sourceNode.connect(this.monitorGainNode),this.sourceNode.connect(this.recordingGainNode),this.monitorGainNode.connect(this.audioContext.destination),this.recordingGainNode.connect(this.encoderNode),this.onstart()}).catch(e=>{throw this.state="inactive",e})):Promise.resolve()},i.prototype.stop=function(){return"paused"===this.state||"recording"===this.state?(this.state="inactive",this.recordingGainNode.connect(this.encoderNode),this.monitorGainNode.disconnect(),this.clearStream(),new Promise(e=>{var t=({data:o})=>{"done"===o.message&&(this.encoder.removeEventListener("message",t),e())};this.encoder.addEventListener("message",t),this.encoder.start&&this.encoder.start(),this.encoder.postMessage({command:"done"})})):Promise.resolve()},i.prototype.storePage=function(e){this.recordedPages.push(e),this.totalLength+=e.length},i.prototype.streamPage=function(e){this.ondataavailable(e)},i.prototype.finish=function(){if(!this.config.streamPages){var e=new Uint8Array(this.totalLength);this.recordedPages.reduce((function(t,o){return e.set(o,t),t+o.length}),0),this.ondataavailable(e)}this.onstop()},i.prototype.ondataavailable=function(){},i.prototype.onpause=function(){},i.prototype.onresume=function(){},i.prototype.onstart=function(){},i.prototype.onstop=function(){},e.exports=i}).call(this,o(1))},function(e,t){var o;o=function(){return this}();try{o=o||new Function("return this")()}catch(e){"object"==typeof window&&(o=window)}e.exports=o}])})); \ No newline at end of file diff --git a/src/recorder.js b/src/recorder.js index 4c0e740e..8bdf05d9 100755 --- a/src/recorder.js +++ b/src/recorder.js @@ -241,6 +241,7 @@ Recorder.prototype.setMonitorGain = function( gain ){ Recorder.prototype.start = function(){ if ( this.state === "inactive" ) { + this.state = 'loading'; this.encodedSamplePosition = 0; return this.audioContext.resume() @@ -254,13 +255,17 @@ Recorder.prototype.start = function(){ this.monitorGainNode.connect( this.audioContext.destination ); this.recordingGainNode.connect( this.encoderNode ); this.onstart(); + }) + .catch(error => { + this.state = 'inactive'; + throw error; }); } return Promise.resolve(); }; Recorder.prototype.stop = function(){ - if ( this.state !== "inactive" ) { + if ( this.state === "paused" || this.state === "recording" ) { this.state = "inactive"; // macOS and iOS requires the source to remain connected (in case stopped while paused)