use-audio-recorder
Version:
react hooks use audio recorder: PCM
3 lines (2 loc) • 11.5 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("react");function e(t,e,r,n,o,a,i){try{var c=t[a](i),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,o)}function r(t){return function(){var r=this,n=arguments;return new Promise((function(o,a){var i=t.apply(r,n);function c(t){e(i,o,a,c,u,"next",t)}function u(t){e(i,o,a,c,u,"throw",t)}c(void 0)}))}}var n,o,a=(function(t){var e=function(t){var e=Object.prototype,r=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",a=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag";function c(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{c({},"")}catch(t){c=function(t,e,r){return t[e]=r}}function u(t,e,r,n){var o=Object.create((e&&e.prototype instanceof f?e:f).prototype),a=new S(n||[]);return o._invoke=function(t,e,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return{value:void 0,done:!0}}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var c=x(i,r);if(c){if(c===l)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var u=s(t,e,r);if("normal"===u.type){if(n=r.done?"completed":"suspendedYield",u.arg===l)continue;return{value:u.arg,done:r.done}}"throw"===u.type&&(n="completed",r.method="throw",r.arg=u.arg)}}}(t,r,a),o}function s(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}t.wrap=u;var l={};function f(){}function h(){}function d(){}var p={};c(p,o,(function(){return this}));var v=Object.getPrototypeOf,g=v&&v(v(E([])));g&&g!==e&&r.call(g,o)&&(p=g);var y=d.prototype=f.prototype=Object.create(p);function m(t){["next","throw","return"].forEach((function(e){c(t,e,(function(t){return this._invoke(e,t)}))}))}function w(t,e){var n;this._invoke=function(o,a){function i(){return new e((function(n,i){!function n(o,a,i,c){var u=s(t[o],t,a);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,i,c)}),(function(t){n("throw",t,i,c)})):e.resolve(f).then((function(t){l.value=t,i(l)}),(function(t){return n("throw",t,i,c)}))}c(u.arg)}(o,a,n,i)}))}return n=n?n.then(i,i):i()}}function x(t,e){var r=t.iterator[e.method];if(void 0===r){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,x(t,e),"throw"===e.method))return l;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return l}var n=s(r,t.iterator,e.arg);if("throw"===n.type)return e.method="throw",e.arg=n.arg,e.delegate=null,l;var o=n.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,l):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,l)}function R(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function b(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function S(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(R,this),this.reset(!0)}function E(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var n=-1,a=function e(){for(;++n<t.length;)if(r.call(t,n))return e.value=t[n],e.done=!1,e;return e.value=void 0,e.done=!0,e};return a.next=a}}return{next:L}}function L(){return{value:void 0,done:!0}}return h.prototype=d,c(y,"constructor",d),c(d,"constructor",h),h.displayName=c(d,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===h||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,d):(t.__proto__=d,c(t,i,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},m(w.prototype),c(w.prototype,a,(function(){return this})),t.AsyncIterator=w,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new w(u(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},m(y),c(y,i,"Generator"),c(y,o,(function(){return this})),c(y,"toString",(function(){return"[object Generator]"})),t.keys=function(t){var e=[];for(var r in t)e.push(r);return e.reverse(),function r(){for(;e.length;){var n=e.pop();if(n in t)return r.value=n,r.done=!1,r}return r.done=!0,r}},t.values=E,S.prototype={constructor:S,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(b),!t)for(var e in this)"t"===e.charAt(0)&&r.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function n(r,n){return i.type="throw",i.arg=t,e.next=r,n&&(e.method="next",e.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var c=r.call(a,"catchLoc"),u=r.call(a,"finallyLoc");if(c&&u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,l):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),l},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),b(r),l}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;b(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,r){return this.delegate={iterator:E(t),resultName:e,nextLoc:r},"next"===this.method&&(this.arg=void 0),l}},t}(t.exports);try{regeneratorRuntime=e}catch(t){"object"==typeof globalThis?globalThis.regeneratorRuntime=e:Function("r","regeneratorRuntime = r")(e)}}(o={exports:{}}),o.exports);function i(t,e,r){for(var n=0;n<r.length;n++)t.setUint8(e+n,r.charCodeAt(n))}(n=exports.RecordStatus||(exports.RecordStatus={}))[n.Idle=0]="Idle",n[n.Recording=1]="Recording",n[n.Stopped=2]="Stopped",n[n.Paused=3]="Paused",n[n.Canceled=4]="Canceled",n[n.PermissionDenied=5]="PermissionDenied",n[n.RecorderError=6]="RecorderError",exports.useAudioRecorder=function(e){var n=t.useRef(null),o=t.useRef(null),c=t.useRef(null),u=t.useRef([]),s=t.useRef(0),l=t.useRef(0),f=t.useState(exports.RecordStatus.Idle),h=f[0],d=f[1],p=t.useState(null),v=p[0],g=p[1];return t.useEffect((function(){window.MediaRecorder||d(exports.RecordStatus.RecorderError)}),[]),{startRecording:function(){var t=r(a.mark((function t(){return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(g(null),n.current||(n.current=new AudioContext({sampleRate:(null==e?void 0:e.sampleRate)||16e3})),c.current){t.next=12;break}return t.prev=3,t.next=6,navigator.mediaDevices.getUserMedia({audio:{channelCount:(null==e?void 0:e.channelsCount)||2,sampleRate:(null==e?void 0:e.sampleRate)||16e3}});case 6:c.current=t.sent,t.next=12;break;case 9:t.prev=9,t.t0=t.catch(3),d(exports.RecordStatus.PermissionDenied);case 12:if(c.current){t.next=15;break}return d(exports.RecordStatus.PermissionDenied),t.abrupt("return");case 15:if(!c.current.getTracks().some((function(t){return"ended"===t.readyState}))){t.next=20;break}return t.next=19,navigator.mediaDevices.getUserMedia({audio:{channelCount:(null==e?void 0:e.channelsCount)||2,sampleRate:(null==e?void 0:e.sampleRate)||16e3}});case 19:c.current=t.sent;case 20:o.current=new MediaRecorder(c.current),o.current.ondataavailable=function(){var t=r(a.mark((function t(r){var o;return a.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(o=r.data,exports.RecordStatus.Canceled!==h){t.next=3;break}return t.abrupt("return");case 3:if(t.t0=n.current,!t.t0){t.next=12;break}return t.t1=n.current,t.next=8,o.arrayBuffer();case 8:t.t2=t.sent,t.t3=function(t){var r,n,o,a,c,u,s=(a=(r=t).sampleRate,c=32===(null==(n={bitDepth:(null==e?void 0:e.sampleBits)||16})?void 0:n.bitDepth)?3:1,u=(null==n?void 0:n.bitDepth)||16,function(t,e,r,n,o){var a=o/8,c=n*a,u=new ArrayBuffer(44+t.length*a),s=new DataView(u);switch(i(s,0,"RIFF"),s.setUint32(4,36+t.length*a,!0),i(s,8,"WAVE"),i(s,12,"fmt "),s.setUint32(16,16,!0),s.setUint16(20,e,!0),s.setUint16(22,n,!0),s.setUint32(24,r,!0),s.setUint32(28,r*c,!0),s.setUint16(32,c,!0),s.setUint16(34,o,!0),i(s,36,"data"),s.setUint32(40,t.length*a,!0),o){case 8:!function(t,e,r){for(var n=0;n<r.length;n++,e++){var o=Math.max(-1,Math.min(1,r[n])),a=o<0?128*o:127*o;t.setInt8(e,a=+a+128)}}(s,44,t);break;case 16:!function(t,e,r){for(var n=0;n<r.length;n++,e+=2){var o=Math.max(-1,Math.min(1,r[n]));t.setInt16(e,o<0?32768*o:32767*o,!0)}}(s,44,t);break;case 32:!function(t,e,r){for(var n=0;n<r.length;n++,e+=4)t.setFloat32(e,r[n],!0)}(s,44,t)}return u}(2===(o=r.numberOfChannels)?function(t,e){for(var r=t.length+e.length,n=new Float32Array(r),o=0,a=0;o<r;)n[o++]=t[a],n[o++]=e[a],a++;return n}(r.getChannelData(0),r.getChannelData(1)):r.getChannelData(0),c,a,o,u)),l=new Blob([s],{type:"audio/wav"});g(l)},t.t4=function(t){console.log(t),d(exports.RecordStatus.RecorderError)},t.t1.decodeAudioData.call(t.t1,t.t2,t.t3,t.t4);case 12:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),o.current.onstop=function(){d(exports.RecordStatus.Stopped)},o.current.start(),d(exports.RecordStatus.Recording);case 25:case"end":return t.stop()}}),t,null,[[3,9]])})));return function(){return t.apply(this,arguments)}}(),stopRecording:function(){o.current&&(c.current&&c.current.getTracks().forEach((function(t){return t.stop()})),"inactive"!==o.current.state&&o.current.stop(),d(exports.RecordStatus.Stopped))},pauseRecording:function(){var t;o.current&&"recording"===(null==(t=o.current)?void 0:t.state)&&(o.current.pause(),d(exports.RecordStatus.Paused))},resumeRecording:function(){var t;o.current&&"paused"===(null==(t=o.current)?void 0:t.state)&&(o.current.pause(),d(exports.RecordStatus.Recording))},cancelRecording:function(){o.current&&(c.current&&c.current.getTracks().forEach((function(t){return t.stop()})),d(exports.RecordStatus.Canceled),"inactive"!==o.current.state&&o.current.stop())},blob:v,mediaChunks:u.current,fileSize:s.current,duration:l.current,status:h,previewAudioStream:c.current?new MediaStream(c.current.getAudioTracks()):null}};
//# sourceMappingURL=use-audio-recorder.cjs.production.min.js.map