opus-recorder
Version:
A library for recording opus encoded audio
1 lines • 5.84 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Recorder=e():t.Recorder=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function o(i){if(e[i])return e[i].exports;var n=e[i]={i:i,l:!1,exports:{}};return t[i].call(n.exports,n,n.exports,o),n.l=!0,n.exports}return o.m=t,o.c=e,o.d=function(t,e,i){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(o.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(i,n,function(e){return t[e]}.bind(null,n));return i},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="",o(o.s=0)}([function(t,e,o){"use strict";(function(e){var o=e.AudioContext||e.webkitAudioContext,i=function(t){if(!i.isRecordingSupported())throw new Error("Recording is not supported in this browser");t||(t={}),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},t)};i.isRecordingSupported=function(){return o&&e.navigator&&e.navigator.mediaDevices&&e.navigator.mediaDevices.getUserMedia&&e.WebAssembly},i.prototype.clearStream=function(){this.stream&&(this.stream.getTracks?this.stream.getTracks().forEach(function(t){t.stop()}):this.stream.stop(),delete this.stream),this.audioContext&&this.closeAudioContext&&(this.audioContext.close(),delete this.audioContext)},i.prototype.encodeBuffers=function(t){if("recording"===this.state){for(var e=[],o=0;o<t.numberOfChannels;o++)e[o]=t.getChannelData(o);this.encoder.postMessage({command:"encode",buffers:e})}},i.prototype.initAudioContext=function(t){return t&&t.context?(this.audioContext=t.context,this.closeAudioContext=!1):(this.audioContext=new o,this.closeAudioContext=!0),this.audioContext},i.prototype.initAudioGraph=function(){this.encodeBuffers=function(){delete this.encodeBuffers},this.scriptProcessorNode=this.audioContext.createScriptProcessor(this.config.bufferLength,this.config.numberOfChannels,this.config.numberOfChannels),this.scriptProcessorNode.connect(this.audioContext.destination),this.scriptProcessorNode.onaudioprocess=(t=>{this.encodeBuffers(t.inputBuffer)}),this.monitorGainNode=this.audioContext.createGain(),this.setMonitorGain(this.config.monitorGain),this.monitorGainNode.connect(this.audioContext.destination),this.recordingGainNode=this.audioContext.createGain(),this.setRecordingGain(this.config.recordingGain),this.recordingGainNode.connect(this.scriptProcessorNode)},i.prototype.initSourceNode=function(t){return t&&t.context?e.Promise.resolve(t):e.navigator.mediaDevices.getUserMedia({audio:this.config.mediaTrackConstraints}).then(t=>(this.stream=t,this.audioContext.createMediaStreamSource(t)))},i.prototype.initWorker=function(){var t=(this.config.streamPages?this.streamPage:this.storePage).bind(this);return this.recordedPages=[],this.totalLength=0,this.encoder=new e.Worker(this.config.encoderPath),new Promise((e,o)=>{this.encoder.addEventListener("message",o=>{switch(o.data.message){case"ready":e();break;case"page":t(o.data.page);break;case"done":this.finish()}}),this.encoder.postMessage(Object.assign({command:"init",originalSampleRate:this.audioContext.sampleRate,wavSampleRate:this.audioContext.sampleRate},this.config))})},i.prototype.pause=function(){"recording"===this.state&&(this.state="paused",this.onpause())},i.prototype.resume=function(){"paused"===this.state&&(this.state="recording",this.onresume())},i.prototype.setRecordingGain=function(t){this.config.recordingGain=t,this.recordingGainNode&&this.audioContext&&this.recordingGainNode.gain.setTargetAtTime(t,this.audioContext.currentTime,.01)},i.prototype.setMonitorGain=function(t){this.config.monitorGain=t,this.monitorGainNode&&this.audioContext&&this.monitorGainNode.gain.setTargetAtTime(t,this.audioContext.currentTime,.01)},i.prototype.start=function(t){if("inactive"===this.state)return this.initAudioContext(t),this.initAudioGraph(),Promise.all([this.initSourceNode(t),this.initWorker()]).then(t=>{this.sourceNode=t[0],this.state="recording",this.sourceNode.connect(this.monitorGainNode),this.sourceNode.connect(this.recordingGainNode),this.onstart()})},i.prototype.stop=function(){"inactive"!==this.state&&(this.state="inactive",this.monitorGainNode.disconnect(),this.scriptProcessorNode.disconnect(),this.recordingGainNode.disconnect(),this.sourceNode.disconnect(),this.clearStream(),this.encoder.postMessage({command:"done"}))},i.prototype.storePage=function(t){this.recordedPages.push(t),this.totalLength+=t.length},i.prototype.streamPage=function(t){this.ondataavailable(t)},i.prototype.finish=function(){if(!this.config.streamPages){var t=new Uint8Array(this.totalLength);this.recordedPages.reduce(function(e,o){return t.set(o,e),e+o.length},0),this.ondataavailable(t)}this.onstop()},i.prototype.ondataavailable=function(){},i.prototype.onpause=function(){},i.prototype.onresume=function(){},i.prototype.onstart=function(){},i.prototype.onstop=function(){},t.exports=i}).call(this,o(1))},function(t,e){var o;o=function(){return this}();try{o=o||Function("return this")()||(0,eval)("this")}catch(t){"object"==typeof window&&(o=window)}t.exports=o}])});