!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.OpusMediaRecorder=t():e.OpusMediaRecorder=t()}("undefined"!=typeof self?self:this,(function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){(function(t){function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function o(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var n=[],r=!0,o=!1,i=void 0;try{for(var s,a=e[Symbol.iterator]();!(r=(s=a.next()).done)&&(n.push(s.value),!t||n.length!==t);r=!0);}catch(e){o=!0,i=e}finally{try{r||null==a.return||a.return()}finally{if(o)throw i}}return n}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return i(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);"Object"===n&&e.constructor&&(n=e.constructor.name);if("Map"===n||"Set"===n)return Array.from(e);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return i(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};s(this,f);var o=n.mimeType,i=n.audioBitsPerSecond,a=(n.videoBitsPerSecond,n.bitsPerSecond),c=r.encoderWorkerFactory,u=r.OggOpusEncoderWasmPath,p=r.WebMOpusEncoderWasmPath;if((t=l.call(this))._stream=e,t._state="inactive",t._mimeType=o||"",t._audioBitsPerSecond=i||a,t.workerState="inactive",!f.isTypeSupported(t._mimeType))throw new TypeError("invalid arguments, a MIME Type is not supported");switch(f._parseType(t._mimeType).subtype){case"wave":case"wav":t._mimeType="audio/wave";break;case"webm":t._mimeType="audio/webm";break;case"ogg":t._mimeType="audio/ogg";break;default:switch(m&&m.name){case"chrome":t._mimeType="audio/webm";break;case"firefox":t._mimeType="audio/ogg";break;case"edge":t._mimeType="audio/webm";break;case"ios":case"safari":t._mimeType="audio/wave";break;default:t._mimeType="audio/webm"}}switch(t._mimeType){case"audio/wave":t._wasmPath="";break;case"audio/webm":t._wasmPath=p||"";break;case"audio/ogg":t._wasmPath=u||"";break;default:throw new Error("Internal Error: Unexpected MIME Type: ".concat(t._mimeType))}var d="";return document.currentScript?d=document.currentScript.src:self.location&&(d=self.location.href),d=d.substr(0,d.lastIndexOf("/"))+"/encoderWorker.umd.js","function"==typeof f.encoderWorker&&(d=URL.createObjectURL(new Blob(["(".concat(f.encoderWorker,")()")]))),t._workerFactory="function"==typeof c?c:function(e){return new Worker(d)},t._spawnWorker(),t}return n=f,i=[{key:"isTypeSupported",value:function(e){if("string"==typeof e&&!e)return!0;try{var t=f._parseType(e),n=t.type,r=t.subtype,o=t.codec}catch(e){return!1}if("audio"!==n||"ogg"!==r&&"webm"!==r&&"wave"!==r&&"wav"!==r)return!1;switch(r){case"ogg":case"webm":if("opus"!==o&&o)return!1;break;case"wave":case"wav":if(o)return!1}return!0}},{key:"_parseType",value:function(e){try{var t=o(e.match(/^(\w+)\/(\w+)(;\s*codecs=(\w+))?$/),5),n=t[1],r=t[2],i=t[4]}catch(t){return"string"!=typeof e||e?null:{type:"",subtype:"",codec:""}}return{type:n,subtype:r,codec:i}}}],(r=[{key:"_spawnWorker",value:function(){var e=this;this.worker=this._workerFactory(),this.worker.onmessage=function(t){return e._onmessageFromWorker(t)},this.worker.onerror=function(t){return e._onerrorFromWorker(t)},this._postMessageToWorker("loadEncoder",{mimeType:this._mimeType,wasmPath:this._wasmPath})}},{key:"_postMessageToWorker",value:function(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};switch(e){case"loadEncoder":var r=n.mimeType,o=n.wasmPath;this.worker.postMessage({command:e,mimeType:r,wasmPath:o});break;case"init":var i=n.sampleRate,s=n.channelCount,a=n.bitsPerSecond;this.worker.postMessage({command:e,sampleRate:i,channelCount:s,bitsPerSecond:a}),this.workerState="encoding",this.source.connect(this.processor),this.processor.connect(this.context.destination);var c=new t.Event("start");this.dispatchEvent(c);break;case"pushInputData":var u=n.channelBuffers,l=n.length,f=n.duration;this.worker.postMessage({command:e,channelBuffers:u,length:l,duration:f});break;case"getEncodedData":case"done":this.worker.postMessage({command:e});break;default:throw new Error("Internal Error: Incorrect postMessage requested.")}}},{key:"_onmessageFromWorker",value:function(e){var n,r=e.data,o=r.command,i=r.buffers;switch(o){case"readyToInit":var s=this.sampleRate,a=this.channelCount;this.workerState="readyToInit","recording"===this.state&&this._postMessageToWorker("init",{sampleRate:s,channelCount:a,bitsPerSecond:this.audioBitsPerSecond});break;case"encodedData":case"lastEncodedData":var c=new Blob(i,{type:this._mimeType});(n=new t.Event("dataavailable")).data=c,this.dispatchEvent(n),"lastEncodedData"===o&&(n=new t.Event("stop"),this.dispatchEvent(n),this.workerState="closed")}}},{key:"_onerrorFromWorker",value:function(e){this.source.disconnect(),this.processor.disconnect(),this.worker.terminate(),this.workerState="closed";var n=["FileName: "+e.filename,"LineNumber: "+e.lineno,"Message: "+e.message].join(" - "),r=new t.Event("error");r.name="UnknownError",r.message=n,this.dispatchEvent(r)}},{key:"_enableAudioProcessCallback",value:function(e){var t=this,n=0;this.processor.onaudioprocess=function(r){for(var o=r.inputBuffer,i=(r.playbackTime,o.sampleRate,o.length),s=o.duration,a=o.numberOfChannels,c=new Array(a),u=0;u=e&&(t._postMessageToWorker("getEncodedData"),n=0)}}},{key:"start",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Number.MAX_SAFE_INTEGER;if("inactive"!==this.state)throw new Error("DOMException: INVALID_STATE_ERR, state must be inactive.");if(e<0)throw new TypeError("invalid arguments, timeslice should be 0 or higher.");e/=1e3,"closed"===this.workerState&&this._spawnWorker(),this.context=new v;var t=this.stream.getAudioTracks();if(!t[0])throw new Error("DOMException: UnkownError, media track not found.");if(this.channelCount=t[0].getSettings().channelCount||1,this.sampleRate=this.context.sampleRate,this.source=this.context.createMediaStreamSource(this.stream),this.processor=this.context.createScriptProcessor(4096,this.channelCount,this.channelCount),this._state="recording",this._enableAudioProcessCallback(e),"readyToInit"===this.workerState){var n=this.sampleRate,r=this.channelCount;this._postMessageToWorker("init",{sampleRate:n,channelCount:r,bitsPerSecond:this.audioBitsPerSecond})}}},{key:"stop",value:function(){if("inactive"===this.state)throw new Error("DOMException: INVALID_STATE_ERR, state must NOT be inactive.");this.source.disconnect(),this.processor.disconnect(),this.context.close(),this._postMessageToWorker("done"),this._state="inactive"}},{key:"pause",value:function(){if("inactive"===this.state)throw new Error("DOMException: INVALID_STATE_ERR, state must NOT be inactive.");this.source.disconnect(),this.processor.disconnect();var e=new t.Event("pause");this.dispatchEvent(e),this._state="paused"}},{key:"resume",value:function(){if("inactive"===this.state)throw new Error("DOMException: INVALID_STATE_ERR, state must NOT be inactive.");this.source.connect(this.processor),this.processor.connect(this.context.destination);var e=new t.Event("resume");this.dispatchEvent(e),this._state="recording"}},{key:"requestData",value:function(){if("inactive"===this.state)throw new Error("DOMException: INVALID_STATE_ERR, state must NOT be inactive.");this._postMessageToWorker("getEncodedData")}},{key:"stream",get:function(){return this._stream}},{key:"mimeType",get:function(){return this._mimeType}},{key:"state",get:function(){return this._state}},{key:"videoBitsPerSecond",get:function(){}},{key:"audioBitsPerSecond",get:function(){return this._audioBitsPerSecond}}])&&a(n.prototype,r),i&&a(n,i),f}(h);["start","stop","dataavailable","pause","resume","error"].forEach((function(e){return y(w.prototype,e)})),m&&"edge"===m.name&&(p=Worker.prototype.postMessage,Worker.prototype.postMessage=function(e){p.apply(this,[e])}),e.exports=w}).call(this,n(1))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){"use strict";n.r(t),n.d(t,"defineEventAttribute",(function(){return y})),n.d(t,"EventTarget",(function(){return v}));const r=new WeakMap,o=new WeakMap;function i(e){const t=r.get(e);return console.assert(null!=t,"'this' is expected an Event object, but got",e),t}function s(e,t){r.set(this,{eventTarget:e,event:t,eventPhase:2,currentTarget:e,canceled:!1,stopped:!1,passiveListener:null,timeStamp:t.timeStamp||Date.now()}),Object.defineProperty(this,"isTrusted",{value:!1,enumerable:!0});const n=Object.keys(t);for(let e=0;e0){const e=new Array(arguments.length);for(let t=0;t1)for(var n=1;n