multiple-police-situations/public/cw/player/recorder-core.js

6 lines
16 KiB
JavaScript
Raw Normal View History

2024-08-05 16:43:08 +08:00
/*
录音
https://github.com/xiangyuecn/Recorder
src: recorder-core.js
*/
!function(x){"use strict";var d=function(){},U=function(e){return new t(e)};U.LM="2022-08-06 20:51";var A="Recorder",S="getUserMedia",P="srcSampleRate",E="sampleRate";U.IsOpen=function(){var e=U.Stream;if(e){var t=e.getTracks&&e.getTracks()||e.audioTracks||[],r=t[0];if(r){var n=r.readyState;return"live"==n||n==r.LIVE}}return!1},U.BufferSize=4096,U.Destroy=function(){for(var e in z(A+" Destroy"),y(),r)r[e]()};var r={};U.BindDestroy=function(e,t){r[e]=t},U.Support=function(){var e=x.AudioContext;if(e||(e=x.webkitAudioContext),!e)return!1;var t=navigator.mediaDevices||{};return t[S]||(t=navigator)[S]||(t[S]=t.webkitGetUserMedia||t.mozGetUserMedia||t.msGetUserMedia),!!t[S]&&(U.Scope=t,U.Ctx&&"closed"!=U.Ctx.state||(U.Ctx=new e,U.BindDestroy("Ctx",function(){var e=U.Ctx;e&&e.close&&(e.close(),U.Ctx=0)})),!0)};var L="ConnectEnableWebM";U[L]=!0;var T="ConnectEnableWorklet";U[T]=!1;var _=function(e,c){var f,i,u,l=e.BufferSize||U.BufferSize,v=U.Ctx,p=e.Stream,s=function(e){var t=p._m=v.createMediaStreamSource(p),r=v.destination,n="createMediaStreamDestination";v[n]&&(r=v[n]()),t.connect(e),e.connect(r)},h="",g=p._call,m=function(e){for(var t in g){for(var r=e.length,n=new Int16Array(r),a=0,o=0;o<r;o++){var i=Math.max(-1,Math.min(1,e[o]));i=i<0?32768*i:32767*i,n[o]=i,a+=Math.abs(i)}for(var s in g)g[s](n,a);return}},d="ScriptProcessor",S="audioWorklet",_=A+" "+S,y="RecProc",C="MediaRecorder",b=C+".WebM.PCM",k=v.createScriptProcessor||v.createJavaScriptNode,M="。由于"+S+"内部1秒375次回调在移动端可能会有性能问题导致回调丢失录音变短PC端无影响暂不建议开启"+S+"。",I=function(){i=p.isWorklet=!1,D(p),z("Connect采用老的"+d+""+(U[T]?"但已":"可")+"设置"+A+"."+T+"=true尝试启用"+S+h+M,3);var e=p._p=k.call(v,l,1,1);s(e);var t="_D220626",r=U[t];r&&z("Use "+A+"."+t,3),e.onaudioprocess=function(e){var t=e.inputBuffer.getChannelData(0);r?(t=new Float32Array(t),setTimeout(function(){m(t)})):m(t)}},w=function(){f=p.isWebM=!1,O(p),i=p.isWorklet=!k||U[T];var t=x.AudioWorkletNode;if(i&&v[S]&&t){var n=function(){return i&&p._na},a=p._na=function(){""!==u&&(clearTimeout(u),u=setTimeout(function(){u=0,n()&&(z(S+"未返回任何音频,恢复使用"+d,3),k&&I())},500))},o=function(){if(n()){var e=p._n=new t(v,y,{processorOptions:{bufferSize:l}});s(e),e.port.onmessage=function(e){u&&(clearTimeout(u),u=""),n()?m(e.data.val):i||z(S+"多余回调",3)},z("Connect采用"+S+",设置"+A+"."+T+"=false可恢复老式"+d+h+M,3)}};v.resume()[g&&"finally"](function(){if(n())if(v[y])o();else{var e,t,r=(t="class "+y+" extends AudioWorkletProcessor{",t+="constructor "+(e=function(e){return e.toString().replace(/^function|DEL_/g,"").replace(/\$RA/g,_)})(function(e){DEL_super(e);var t=this,r=e.processorOptions.bufferSize;t.bufferSize=r,t.buffer=new Float32Array(2*r),t.pos=0,t.port.onmessage=function(e){e.data.kill&&(t.kill=!0,console.log("$RA kill call"))},console.log("$RA .ctor call",e)}),t+="process "+e(function(e,t,r){var n=this,a=n.bufferSize,o=n.buffer,i=n.pos;if((e=(e[0]||[])[0]||[]).length){o.set(e,i);var s=~~((i+=e.length)/a)*a;if(s){this.port.postMessage({val:o.slice(0,s)});var c=o.subarray(s,i);(o=new Float32Array(2*a)).set(c),i=c.length,n.buffer=o}n.pos=i}return!n.kill}),t+='}try{registerProcessor("'+y+'", '+y+')}catch(e){console.error("'+_+'注册失败",e)}',"data:text/javascript;base64,"+btoa(unescape(encodeURIComponent(t))));v[S].addModule(r).then(function(e){n()&&(v[y]=1,o(),u&&a())})[g&&"catch"](function(e){z(S+".addModule失败",1,e),n()&&I()})}})}else I()};!function(){var e=x[C],t="ondataavailable",r="audio/webm; codecs=pcm";f=p.isWebM=U[L];var n=e&&t in e.prototype&&e.isTypeSupported(r);if(h=n?"":"(此浏览器不支持"+b+"",!c||!f||!n)return w();var a=function(){return f&&p._ra},o=(p._ra=function(){""!==u&&(clearTimeout(u),u=setTimeout(function(){a()&&(z(C+"未返回任何音频,降级使用"+S,3),w())},500))},Object.assign({mimeType:r},U.ConnectWebMOptions)),i=p._r=new e(p,o),s=p._rd={sampleRate:v[E]};i[t]=function(e){var t=new FileReader;t.onloadend=function(){if(a()){var e=