You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

11 lines
12 KiB
JavaScript

import{ar as M,as as R,d as b,q as A,a1 as P,B,ai as x,aa as S,o as C,c as D,_ as U}from "./index-f5e1fd05.js";var _={exports:{}};/*!
*
* js-audio-recorder - js audio recorder plugin
*
* @version v1.0.7
* @homepage https://github.com/2fps/recorder
* @author 2fps <echoweb@126.com> (https://www.zhuyuntao.cn)
* @license MIT
*
*/(function(v,w){(function(m,c){v.exports=c()})(M,function(){return function(m){var c={};function d(o){if(c[o])return c[o].exports;var i=c[o]={i:o,l:!1,exports:{}};return m[o].call(i.exports,i,i.exports,d),i.l=!0,i.exports}return d.m=m,d.c=c,d.d=function(o,i,r){d.o(o,i)||Object.defineProperty(o,i,{enumerable:!0,get:r})},d.r=function(o){typeof Symbol<"u"&&Symbol.toStringTag&&Object.defineProperty(o,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(o,"__esModule",{value:!0})},d.t=function(o,i){if(1&i&&(o=d(o)),8&i||4&i&&typeof o=="object"&&o&&o.__esModule)return o;var r=Object.create(null);if(d.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:o}),2&i&&typeof o!="string")for(var t in o)d.d(r,t,(function(e){return o[e]}).bind(null,t));return r},d.n=function(o){var i=o&&o.__esModule?function(){return o.default}:function(){return o};return d.d(i,"a",i),i},d.o=function(o,i){return Object.prototype.hasOwnProperty.call(o,i)},d.p="",d(d.s=1)}([function(m,c,d){function o(i,r,t){for(var e=0;e<t.length;e++)i.setUint8(r+e,t.charCodeAt(e))}Object.defineProperty(c,"__esModule",{value:!0}),c.compress=function(i,r,t){for(var e=r/t,f=Math.max(e,1),s=i.left,a=i.right,n=Math.floor((s.length+a.length)/e),l=new Float32Array(n),u=0,g=0;u<n;){var p=Math.floor(g);l[u]=s[p],u++,a.length&&(l[u]=a[p],u++),g+=f}return l},c.encodePCM=function(i,r,t){t===void 0&&(t=!0);var e=0,f=i.length*(r/8),s=new ArrayBuffer(f),a=new DataView(s);if(r===8)for(var n=0;n<i.length;n++,e++){var l=(u=Math.max(-1,Math.min(1,i[n])))<0?128*u:127*u;l=+l+128,a.setInt8(e,l)}else for(n=0;n<i.length;n++,e+=2){var u=Math.max(-1,Math.min(1,i[n]));a.setInt16(e,u<0?32768*u:32767*u,t)}return a},c.encodeWAV=function(i,r,t,e,f,s){s===void 0&&(s=!0);var a=t>r?r:t,n=f,l=new ArrayBuffer(44+i.byteLength),u=new DataView(l),g=e,p=0;o(u,p,"RIFF"),p+=4,u.setUint32(p,36+i.byteLength,s),o(u,p+=4,"WAVE"),o(u,p+=4,"fmt "),p+=4,u.setUint32(p,16,s),p+=4,u.setUint16(p,1,s),p+=2,u.setUint16(p,g,s),p+=2,u.setUint32(p,a,s),p+=4,u.setUint32(p,g*a*(n/8),s),p+=4,u.setUint16(p,g*(n/8),s),p+=2,u.setUint16(p,n,s),o(u,p+=2,"data"),p+=4,u.setUint32(p,i.byteLength,s),p+=4;for(var y=0;y<i.byteLength;)u.setUint8(p,i.getUint8(y)),p++,y++;return u}},function(m,c,d){var o,i=this&&this.__extends||(o=function(s,a){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(n,l){n.__proto__=l}||function(n,l){for(var u in l)l.hasOwnProperty(u)&&(n[u]=l[u])})(s,a)},function(s,a){function n(){this.constructor=s}o(s,a),s.prototype=a===null?Object.create(a):(n.prototype=a.prototype,new n)});Object.defineProperty(c,"__esModule",{value:!0});var r=d(2),t=d(0),e=d(3),f=function(s){function a(n){n===void 0&&(n={});var l=s.call(this,n)||this;return l.isrecording=!1,l.ispause=!1,l.isplaying=!1,l}return i(a,s),a.prototype.setOption=function(n){n===void 0&&(n={}),this.setNewOption(n)},a.prototype.start=function(){return this.isrecording?Promise.reject():(this.isrecording=!0,this.startRecord())},a.prototype.pause=function(){this.isrecording&&!this.ispause&&(this.ispause=!0,this.pauseRecord())},a.prototype.resume=function(){this.isrecording&&this.ispause&&(this.ispause=!1,this.resumeRecord())},a.prototype.stop=function(){this.isrecording&&(this.isrecording=!1,this.ispause=!1,this.stopRecord())},a.prototype.play=function(){this.stop(),this.isplaying=!0,this.onplay&&this.onplay(),e.default.addPlayEnd(this.onplayend);var n=this.getWAV();n.byteLength>44&&e.default.play(n.buffer)},a.prototype.getPlayTime=function(){return e.default.getPlayTime()},a.prototype.pausePlay=function(){!this.isrecording&&this.isplaying&&(this.isplaying=!1,this.onpauseplay&&this.onpauseplay(),e.default.pausePlay())},a.prototype.resumePlay=function(){this.isrecording||this.isplaying||(this.isplaying=!0,this.onresumeplay&&this.onresumeplay(),e.default.resumePlay())},a.prototype.stopPlay=function(){this.isrecording||(this.isplaying=!1,this.onstopplay&&this.onstopplay(),e.default.stopPlay())},a.prototype.destroy=function(){return e.default.destroyPlay(),this.destroyRecord()},a.prototype.getRecordAnalyseData=function(){return this.getAnalyseData()},a.prototype.getPlayAnalyseData=function(){return e.default.getAnalyseData()},a.prototype.getPCM=function(){this.stop();var n=this.getData();return n=t.compress(n,this.inputSampleRate,this.outputSampleRate),t.encodePCM(n,this.oututSampleBits,this.littleEdian)},a.prototype.getPCMBlob=function(){return new Blob([this.getPCM()])},a.prototype.downloadPCM=function(n){n===void 0&&(n="recorder");var l=this.getPCMBlob();r.downloadPCM(l,n)},a.prototype.getWAV=function(){var n=this.getPCM();return t.encodeWAV(n,this.inputSampleRate,this.outputSampleRate,this.config.numChannels,this.oututSampleBits,this.littleEdian)},a.prototype.getWAVBlob=function(){return new Blob([this.getWAV()],{type:"audio/wav"})},a.prototype.downloadWAV=function(n){n===void 0&&(n="recorder");var l=this.getWAVBlob();r.downloadWAV(l,n)},a.prototype.download=function(n,l,u){r.download(n,l,u)},a.prototype.getChannelData=function(){var n=this.getPCM(),l=n.byteLength,u=this.littleEdian,g={left:null,right:null};if(this.config.numChannels===2){var p=new DataView(new ArrayBuffer(l/2)),y=new DataView(new ArrayBuffer(l/2));if(this.config.sampleBits===16)for(var h=0;h<l/2;h+=2)p.setInt16(h,n.getInt16(2*h,u),u),y.setInt16(h,n.getInt16(2*h+2,u),u);else for(h=0;h<l/2;h+=2)p.setInt8(h,n.getInt8(2*h)),y.setInt8(h,n.getInt8(2*h+1));g.left=p,g.right=y}else g.left=n;return g},a}(d(5).default);c.default=f},function(m,c,d){function o(i,r,t){var e=document.createElement("a");e.href=window.URL.createObjectURL(i),e.download=r+"."+t,e.click()}Object.defineProperty(c,"__esModule",{value:!0}),c.downloadWAV=function(i,r){r===void 0&&(r="recorder"),o(i,r,"wav")},c.downloadPCM=function(i,r){r===void 0&&(r="recorder"),o(i,r,"pcm")},c.download=function(i,r,t){return o(i,r,t)}},function(m,c,d){Object.defineProperty(c,"__esModule",{value:!0});var o=d(4),i=null,r=0,t=0,e=null,f=null,s=null,a=!1,n=0,l=function(){};function u(){return a=!1,e.decodeAudioData(s.slice(0),function(y){(i=e.createBufferSource()).onended=function(){a||(n=e.currentTime-t+r,l())},i.buffer=y,i.connect(f),f.connect(e.destination),i.start(0,r),t=e.currentTime},function(y){o.throwError(y)})}function g(){i&&(i.stop(),i=null)}var p=function(){function y(){}return y.play=function(h){return e||(e=new(window.AudioContext||window.webkitAudioContext),(f=e.createAnalyser()).fftSize=2048),this.stopPlay(),s=h,n=0,u()},y.pausePlay=function(){g(),r+=e.currentTime-t,a=!0},y.resumePlay=function(){return u()},y.stopPlay=function(){r=0,s=null,g()},y.destroyPlay=function(){this.stopPlay()},y.getAnalyseData=function(){var h=new Uint8Array(f.frequencyBinCount);return f.getByteTimeDomainData(h),h},y.addPlayEnd=function(h){h===void 0&&(h=function(){}),l=h},y.getPlayTime=function(){var h=a?r:e.currentTime-t+r;return n||h},y}();c.default=p},function(m,c,d){Object.defineProperty(c,"__esModule",{value:!0}),c.throwError=function(o){throw new Error(o)}},function(m,c,d){Object.defineProperty(c,"__esModule",{value:!0});var o=d(0),i=function(){function r(t){t===void 0&&(t={}),this.size=0,this.lBuffer=[],this.rBuffer=[],this.tempPCM=[],this.inputSampleBits=16,this.fileSize=0,this.duration=0,this.needRecord=!0;var e,f=new(window.AudioContext||window.webkitAudioContext);this.inputSampleRate=f.sampleRate,this.setNewOption(t),this.littleEdian=(e=new ArrayBuffer(2),new DataView(e).setInt16(0,256,!0),new Int16Array(e)[0]===256),r.initUserMedia()}return r.prototype.setNewOption=function(t){t===void 0&&(t={}),this.config={sampleBits:~[8,16].indexOf(t.sampleBits)?t.sampleBits:16,sampleRate:~[8e3,11025,16e3,22050,24e3,44100,48e3].indexOf(t.sampleRate)?t.sampleRate:this.inputSampleRate,numChannels:~[1,2].indexOf(t.numChannels)?t.numChannels:1},this.outputSampleRate=this.config.sampleRate,this.oututSampleBits=this.config.sampleBits},r.prototype.startRecord=function(){var t=this;return this.context&&this.destroyRecord(),this.initRecorder(),navigator.mediaDevices.getUserMedia({audio:!0}).then(function(e){t.audioInput=t.context.createMediaStreamSource(e),t.stream=e}).then(function(){t.audioInput.connect(t.analyser),t.analyser.connect(t.recorder),t.recorder.connect(t.context.destination)})},r.prototype.pauseRecord=function(){this.needRecord=!1},r.prototype.resumeRecord=function(){this.needRecord=!0},r.prototype.stopRecord=function(){this.audioInput&&this.audioInput.disconnect(),this.source&&this.source.stop(),this.recorder.disconnect(),this.analyser.disconnect(),this.needRecord=!0},r.prototype.destroyRecord=function(){return this.clearRecordStatus(),this.stopStream(),this.closeAudioContext()},r.prototype.getAnalyseData=function(){var t=new Uint8Array(this.analyser.frequencyBinCount);return this.analyser.getByteTimeDomainData(t),t},r.prototype.getData=function(){return this.flat()},r.prototype.clearRecordStatus=function(){this.lBuffer.length=0,this.rBuffer.length=0,this.size=0,this.fileSize=0,this.PCM=null,this.audioInput=null,this.duration=0},r.prototype.flat=function(){var t=null,e=new Float32Array(0);this.config.numChannels===1?t=new Float32Array(this.size):(t=new Float32Array(this.size/2),e=new Float32Array(this.size/2));for(var f=0,s=0;s<this.lBuffer.length;s++)t.set(this.lBuffer[s],f),f+=this.lBuffer[s].length;for(f=0,s=0;s<this.rBuffer.length;s++)e.set(this.rBuffer[s],f),f+=this.rBuffer[s].length;return{left:t,right:e}},r.prototype.initRecorder=function(){var t=this;this.clearRecordStatus(),this.context=new(window.AudioContext||window.webkitAudioContext),this.analyser=this.context.createAnalyser(),this.analyser.fftSize=2048;var e=this.context.createScriptProcessor||this.context.createJavaScriptNode;this.recorder=e.apply(this.context,[4096,this.config.numChannels,this.config.numChannels]),this.recorder.onaudioprocess=function(f){if(t.needRecord){var s,a=f.inputBuffer.getChannelData(0),n=null;t.lBuffer.push(new Float32Array(a)),t.size+=a.length,t.config.numChannels===2&&(n=f.inputBuffer.getChannelData(1),t.rBuffer.push(new Float32Array(n)),t.size+=n.length),t.fileSize=Math.floor(t.size/Math.max(t.inputSampleRate/t.outputSampleRate,1))*(t.oututSampleBits/8),s=100*Math.max.apply(Math,a),t.duration+=4096/t.inputSampleRate,t.onprocess&&t.onprocess(t.duration),t.onprogress&&t.onprogress({duration:t.duration,fileSize:t.fileSize,vol:s})}}},r.prototype.stopStream=function(){this.stream&&this.stream.getTracks&&(this.stream.getTracks().forEach(function(t){return t.stop()}),this.stream=null)},r.prototype.closeAudioContext=function(){return this.context&&this.context.close&&this.context.state!=="closed"?this.context.close():new Promise(function(t){t()})},r.initUserMedia=function(){navigator.mediaDevices===void 0&&(navigator.mediaDevices={}),navigator.mediaDevices.getUserMedia===void 0&&(navigator.mediaDevices.getUserMedia=function(t){var e=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia;return e?new Promise(function(f,s){e.call(navigator,t,f,s)}):Promise.reject(new Error("浏览器不支持 getUserMedia !"))})},r.prototype.transformIntoPCM=function(t,e){var f=new Float32Array(t),s=new Float32Array(e),a=o.compress({left:f,right:s},this.inputSampleRate,this.outputSampleRate);return o.encodePCM(a,this.oututSampleBits,this.littleEdian)},r.getPermission=function(){return this.initUserMedia(),navigator.mediaDevices.getUserMedia({audio:!0}).then(function(t){t&&t.getTracks().forEach(function(e){return e.stop()})})},r}();c.default=i}]).default})})(_);var O=_.exports,k=O;const I=R(k),V=b({name:"VoiceAsk",__name:"VoiceAsk",emits:["changeType","speakEnd"],setup(v,{emit:w}){const m=A(null);let c=P({undefined:void 0}),d=P({undefined:void 0});const o=A("0");B(()=>{o.value="0",c=new I({sampleBits:16,sampleRate:16e3,numChannels:1}),r(),i()});function i(){d=setInterval(()=>{const f=c.getRecordAnalyseData();Array.from(f).some(a=>a>128)?o.value==="0"?o.value="1":o.value==="2"&&c.resume():o.value==="1"?(c.pause(),o.value="2"):o.value==="2"&&t()},500)}const r=()=>{c.start()};async function t(){o.value="0",clearInterval(d),c.stop();const f=c.getWAVBlob(),s=new FormData;s.append("file",f),o.value="3",e("speakEnd",s)}const e=w;return x(m,()=>e("changeType")),S(()=>{c.destroy()}),(f,s)=>(C(),D("div",{ref_key:"container",ref:m,class:"VoiceAsk"},null,512))}});const E=U(V,[["__scopeId","data-v-50b14db9"]]);export{E as default};