react-audio-recorder-wavdownloader
Version:
1 lines • 7.39 kB
JavaScript
var AudioRecorder=function(t){function e(n){if(o[n])return o[n].exports;var i=o[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,e),i.l=!0,i.exports}var o={};return e.m=t,e.c=o,e.i=function(t){return t},e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=3)}([function(t,e,o){"use strict";function n(t,e){var o=window.URL.createObjectURL(t),n=document.createEvent("Event");n.initEvent("click",!0,!0);var i=document.createElement("A");return i.href=o,i.download=e,i.dispatchEvent(n),i.click(),i}e.a=n},function(t,e,o){"use strict";var n=o(4),i=function(){function t(){this.recordingNodes=[]}return Object.defineProperty(t.prototype,"bufferLength",{get:function(){return this.buffers[0].length*t.bufferSize},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"audioDuration",{get:function(){return this.bufferLength/t.audioContext.sampleRate},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"audioData",{get:function(){return this.encodingCache||o.i(n.a)(this.buffers,this.bufferLength,t.audioContext.sampleRate)},enumerable:!0,configurable:!0}),t.prototype.startRecording=function(){var e=this;return new Promise(function(o,n){navigator.getUserMedia({audio:!0},function(n){var i=t.audioContext,a=i.createGain(),r=i.createMediaStreamSource(n),s=i.createScriptProcessor(t.bufferSize,2,2);e.encodingCache&&(e.encodingCache=null),s.onaudioprocess=function(t){console.log("audio process",e),e.encodingCache&&(e.encodingCache=null);for(var o=0;o<2;o++){var n=t.inputBuffer.getChannelData(o);e.buffers[o].push(new Float32Array(n))}},r.connect(a),a.connect(s),s.connect(i.destination),e.recordingStream=n,e.recordingNodes.push(r,a,s),o(n)},function(t){n(t)})})},t.prototype.stopRecording=function(){this.recordingStream&&(this.recordingStream.getTracks()[0].stop(),delete this.recordingStream);for(var t in this.recordingNodes)this.recordingNodes[t].disconnect(),delete this.recordingNodes[t]},t.prototype.startPlayback=function(e,o){var n=this;return void 0===e&&(e=!1),new Promise(function(i,a){var r=new FileReader;r.readAsArrayBuffer(n.audioData),r.onloadend=function(){t.audioContext.decodeAudioData(r.result,function(a){var r=t.audioContext.createBufferSource();r.buffer=a,r.connect(t.audioContext.destination),r.loop=e,r.start(0),r.onended=o,n.playbackNode=r,i(r)})}})},t.prototype.stopPlayback=function(){this.playbackNode.stop()},t.prototype.reset=function(){this.playbackNode&&(this.playbackNode.stop(),this.playbackNode.disconnect(0),delete this.playbackNode),this.stopRecording(),this.buffers=[[],[]]},t.audioContext=new AudioContext,t.bufferSize=2048,t}();e.a=i},function(t,e){t.exports=React},function(t,e,o){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n=o(2),i=(o.n(n),o(1)),a=o(0),r=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])};return function(e,o){function n(){this.constructor=e}t(e,o),e.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}}(),s=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.waveInterface=new i.a,e.state={isRecording:!1,isPlaying:!1,audioData:e.props.initialAudio},e.onAudioEnded=function(){e.setState({isPlaying:!1}),e.props.onEnded&&e.props.onEnded()},e.onRemoveClick=function(){e.waveInterface.reset(),e.state.audioData&&e.props.onChange&&e.props.onChange({duration:0,audioData:null}),e.setState({isPlaying:!1,isRecording:!1,audioData:null})},e.onDownloadClick=function(){return o.i(a.a)(e.state.audioData,e.props.filename)},e.onButtonClick=function(t){e.state.audioData?e.state.isPlaying?(e.stopPlayback(),t.preventDefault()):e.startPlayback():e.state.isRecording?e.stopRecording():e.startRecording()},e}return r(e,t),e.prototype.componentWillReceiveProps=function(t){t.initialAudio&&t.initialAudio!==this.props.initialAudio&&this.state.audioData&&t.initialAudio!==this.state.audioData&&(this.waveInterface.reset(),this.setState({audioData:t.initialAudio,isPlaying:!1,isRecording:!1}))},e.prototype.componentDidUpdate=function(){this.props.startRecording&&!this.state.isRecording&&this.startRecording(),this.props.reset&&this.onRemoveClick()},e.prototype.componentWillMount=function(){this.waveInterface.reset()},e.prototype.componentWillUnmount=function(){this.waveInterface.reset()},e.prototype.startRecording=function(){var t=this;this.state.isRecording||this.waveInterface.startRecording().then(function(){t.setState({isRecording:!0}),t.props.onRecordStart&&t.props.onRecordStart()}).catch(function(t){throw t})},e.prototype.stopRecording=function(){this.waveInterface.stopRecording(),this.setState({isRecording:!1,audioData:this.waveInterface.audioData}),this.props.onChange&&this.props.onChange({duration:this.waveInterface.audioDuration,audioData:this.waveInterface.audioData})},e.prototype.startPlayback=function(){var t=this;this.state.isPlaying||this.waveInterface.startPlayback(this.props.loop,this.onAudioEnded).then(function(){t.setState({isPlaying:!0}),t.props.onPlay&&t.props.onPlay()})},e.prototype.stopPlayback=function(){this.waveInterface.stopPlayback(),this.setState({isPlaying:!1}),this.props.onAbort&&this.props.onAbort()},e.prototype.render=function(){return n.createElement("div",{className:"AudioRecorder"},n.createElement("button",{className:["AudioRecorder-button",this.state.audioData?"hasAudio":"",this.state.isRecording?"isRecording":""].join(" "),onClick:this.onButtonClick},this.state.audioData&&!this.state.isPlaying&&this.props.playLabel,!this.state.audioData&&!this.state.isRecording&&this.props.recordLabel,!this.state.audioData&&this.state.isRecording&&this.props.recordingLabel),this.state.audioData&&n.createElement("button",{className:"AudioRecorder-remove",onClick:this.onRemoveClick},this.props.removeLabel),this.state.audioData&&this.props.downloadable&&n.createElement("button",{className:"AudioRecorder-download",onClick:this.onDownloadClick},this.props.downloadLabel))},e.defaultProps={reset:!1,startRecording:!1,loop:!1,downloadable:!0,className:"",style:{},filename:"output.wav",playLabel:"🔊 Play",playingLabel:"❚❚ Playing",recordLabel:"● Record",recordingLabel:"● Recording",removeLabel:"✖ Remove",downloadLabel:"💾 Save"},e}(n.Component);e.default=s},function(t,e,o){"use strict";function n(t,e,o){for(var n=0;n<o.length;n++)t.setUint8(e+n,o.charCodeAt(n))}function i(t,e){for(var o=new Float64Array(e),n=0,i=0;i<t.length;i++){var a=t[i];o.set(a,n),n+=a.length}return o}function a(t,e){for(var o=t.length+e.length,n=new Float64Array(o),i=0,a=0;a<o;)n[a++]=t[i],n[a++]=e[i],i++;return n}function r(t,e,o,r){void 0===r&&(r=1);var s=i(t[0],e),c=i(t[1],e),u=a(s,c),d=new ArrayBuffer(44+2*u.length),p=new DataView(d);return n(p,0,"RIFF"),p.setUint32(4,44+2*u.length,!0),n(p,8,"WAVE"),n(p,12,"fmt "),p.setUint32(16,16,!0),p.setUint16(20,1,!0),p.setUint16(22,2,!0),p.setUint32(24,o,!0),p.setUint32(28,4*o,!0),p.setUint16(32,4,!0),p.setUint16(34,16,!0),n(p,36,"data"),p.setUint32(40,2*u.length,!0),u.forEach(function(t,e){p.setInt16(44+2*e,t*(32767*r),!0)}),new Blob([p],{type:"audio/wav"})}e.a=r}]);