UNPKG

mstf-kit

Version:

一个现代化的 JavaScript/TypeScript 工具库,提供了丰富的常用工具函数

2 lines (1 loc) 8.01 kB
"use strict";function e(){return{audioCtx:new(window.AudioContext||window.webkitAudioContext),currentSource:null,isPlaying:!1,audioQueue:[],wavHeader:null,previousChunk:"",isFirstChunk:!0,audioChunks:[],hasStartedPlaying:!1}}function n(e){try{return function(e){const n=new Uint8Array(e.length);for(let t=0;t<e.length;t++)n[t]=e.charCodeAt(t);return n}(atob(e))}catch(n){return console.error("Base64解码错误:",n),new Uint8Array(0)}}function t(e){if(e.currentSource){try{e.currentSource.stop()}catch(n){}e.currentSource=null}e.isPlaying=!1,e.audioQueue=[],e.wavHeader=null,e.previousChunk="",e.isFirstChunk=!0,e.audioChunks=[]}async function o(e,n,t,o){return new Promise((a=>{if(n.isPlaying)return n.audioQueue.push(e),void a();const u=n.audioCtx.createBufferSource();u.buffer=e,u.connect(n.audioCtx.destination),u.onended=()=>{n.isPlaying=!1,n.currentSource=null,0===n.audioQueue.length&&o&&o(),a(),r(n,t,o)},n.currentSource=u,n.isPlaying=!0,u.start(0),t&&!n.hasStartedPlaying&&(n.hasStartedPlaying=!0,t())}))}async function r(e,n,t){if(e.audioQueue.length>0&&!e.isPlaying){const r=e.audioQueue.shift();await o(r,e,n,t)}}async function a(e,n,t="audio/wav"){const o=new Uint8Array(n.length+e.length);return o.set(n),o.set(e,n.length),new Blob([o],{type:t})}function u(e,n,t="audio/wav"){let o=n.length;e.forEach((e=>{o+=e.length}));const r=new Uint8Array(o);let a=0;return r.set(n),a+=n.length,e.forEach((e=>{r.set(e,a),a+=e.length})),new Blob([r],{type:t})}async function i(e,n,t,r){try{if(!n.wavHeader)return;const a=new Uint8Array(n.wavHeader.length+e.length);a.set(n.wavHeader),a.set(e,n.wavHeader.length);const u=await n.audioCtx.decodeAudioData(a.buffer.slice(0));n.isPlaying?n.audioQueue.push(u):await o(u,n,t,r)}catch(a){}}function l(e,t,o,r,u,l,s){if(e.startsWith("data: "))try{const c=e.substring(6);if(!c.trim())return;const d=function(e,n){if(!e)return null;if(!n)return"string"==typeof e?e:e.audio&&"string"==typeof e.audio?e.audio:e.data&&e.data.audio&&"string"==typeof e.data.audio?e.data.audio:null;const t=n.split(".");let o=e;for(const r of t){if(!o||"object"!=typeof o||!(r in o))return null;o=o[r]}return"string"==typeof o?o:null}(JSON.parse(c),u);d&&async function(e,t,o,r,u,l){try{const s=n(e);if(t.audioChunks.push(s),t.isFirstChunk){if(s.length>=44&&"RIFF"===String.fromCharCode(...s.slice(0,4))){t.wavHeader=s.slice(0,44);const e=s.slice(44);if(t.isFirstChunk=!1,e.length>0){const n=await a(e,t.wavHeader);null==r||r(n),o&&await i(e,t,u,l)}return}return}const c=await a(s,t.wavHeader);null==r||r(c),o&&await i(s,t,u,l)}catch(s){console.error("音频处理错误:",s)}}(d,t,o,r,l,s)}catch(c){}}function s(n={}){const{autoPlay:o=!1,returnBlob:a=!1,mimeType:i="audio/wav",onAudioData:s,onError:c,onComplete:d,onStart:y,onEnded:p,audioField:f}=n,g=e();let h=0,w=!1,b=null;return{onDownloadProgress:e=>{var n,t;if(!w)try{const r=null==(t=null==(n=e.event)?void 0:n.target)?void 0:t.response;if(r&&"string"==typeof r){const e=r.substring(h);if(h=r.length,e){const n=e.split("\n");for(const e of n)e.trim()&&e.startsWith("data: ")&&l(e.trim(),g,o,s,f,y,p)}}if(e.loaded===e.total)if(a&&g.wavHeader&&g.audioChunks.length>0){const e=u(g.audioChunks,g.wavHeader,i);null==b||b(e),null==d||d(e)}else null==d||d()}catch(r){r instanceof Error&&(console.error("Axios进度处理错误:",r),null==c||c(r))}},pause:()=>{g.currentSource&&(g.currentSource.stop(),g.currentSource=null),g.isPlaying=!1},resume:()=>{!g.isPlaying&&g.audioQueue.length>0&&r(g,y,p)},abort:()=>{w=!0,t(g)},isPlaying:()=>g.isPlaying,finalBlob:a?new Promise((e=>{b=e})):void 0}}Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"}),exports.createAxiosAudioOptions=function(e={}){{const{onDownloadProgress:n,pause:t,resume:o,abort:r,isPlaying:a,finalBlob:u}=s(e);return{axiosConfig:{responseType:"text",onDownloadProgress:n},pause:t,resume:o,abort:r,isPlaying:a,finalBlob:u}}},exports.createBase64StreamProcessor=function(o={}){const{autoPlay:l=!1,returnBlob:s=!1,mimeType:c="audio/wav",onAudioData:d,onError:y,onComplete:p}=o,f=e();let g=!1,h=null,w=!1,b=null,P=null,m=null;return{processChunk:async(e,t=!1)=>{if(!w)try{const o=n(e);if(g)if(f.audioChunks.push(o),f.wavHeader){const e=await a(o,f.wavHeader,c);null==d||d(e),l&&i(o,f)}else{const e=new Blob([o],{type:c});if(null==d||d(e),l){const n=new Audio;n.src=URL.createObjectURL(e),n.play()}}else if(o.length>=44&&"RIFF"===String.fromCharCode(...o.slice(0,4))){f.wavHeader=o.slice(0,44);const e=o.slice(44);f.audioChunks.push(e),g=!0;const n=u([e],f.wavHeader,c);null==d||d(n),l&&(m=new Audio,m.src=URL.createObjectURL(n),m.play())}else{f.audioChunks.push(o);const e=new Blob([o],{type:c});null==d||d(e),l&&(m=new Audio,m.src=URL.createObjectURL(e),m.play())}if(t){let e;if(f.wavHeader&&f.audioChunks.length>0)e=u(f.audioChunks,f.wavHeader,c);else{const n=f.audioChunks;let t=0;n.forEach((e=>{t+=e.length}));const o=new Uint8Array(t);let r=0;n.forEach((e=>{o.set(e,r),r+=e.length})),e=new Blob([o],{type:c})}P=e,b&&URL.revokeObjectURL(b),b=URL.createObjectURL(e),h&&h(e),null==p||p(e)}}catch(o){o instanceof Error&&(console.error("处理base64数据块错误:",o),null==y||y(o))}},getBlob:()=>P,getAudioUrl:()=>b,pause:()=>{m&&m.pause(),f.currentSource&&(f.currentSource.stop(),f.currentSource=null),f.isPlaying=!1},resume:()=>{m&&m.play(),!f.isPlaying&&f.audioQueue.length>0&&r(f)},abort:()=>{w=!0,t(f),b&&(URL.revokeObjectURL(b),b=null),m&&(m.pause(),m.src="",m=null),P=null},isPlaying:()=>f.isPlaying||!!m&&!m.paused,finalBlob:s?new Promise((e=>{h=e})):void 0}},exports.processArrayBuffer=function(e,n={}){const{autoPlay:t=!1,mimeType:o="audio/wav",onAudioData:r,onComplete:a}=n,u=new Blob([e],{type:o});if(t){const e=new Audio;e.src=URL.createObjectURL(u),e.play()}return null==r||r(u),null==a||a(u),u},exports.processAxiosProgress=s,exports.processBase64Audio=function(o,a={}){const{autoPlay:u=!1,mimeType:l="audio/wav",onAudioData:s,onComplete:c}=a,d=e();let y=null;try{const e=n(o);if(e.length>=44&&"RIFF"===String.fromCharCode(...e.slice(0,4))){d.wavHeader=e.slice(0,44);const n=e.slice(44),o=new Blob([e],{type:l});return u&&(y=new Audio,y.src=URL.createObjectURL(o),y.play(),i(n,d)),null==s||s(o),null==c||c(o),{blob:o,pause:()=>{y&&y.pause(),d.currentSource&&(d.currentSource.stop(),d.currentSource=null),d.isPlaying=!1},resume:()=>{y&&y.play(),!d.isPlaying&&d.audioQueue.length>0&&r(d)},abort:()=>{y&&(y.pause(),y.src="",y=null),t(d)},isPlaying:()=>d.isPlaying||!!y&&!y.paused}}{const n=new Blob([e],{type:l});return u&&(y=new Audio,y.src=URL.createObjectURL(n),y.play()),null==s||s(n),null==c||c(n),{blob:n,pause:()=>{y&&y.pause()},resume:()=>{y&&y.play()},abort:()=>{y&&(y.pause(),y.src="",y=null),t(d)},isPlaying:()=>!!y&&!y.paused}}}catch(p){return console.error("处理base64音频数据错误:",p),{blob:new Blob([],{type:l}),pause:()=>{},resume:()=>{},abort:()=>{t(d)},isPlaying:()=>!1}}},exports.processBlob=function(e,n={}){const{autoPlay:t=!1,onAudioData:o,onComplete:r}=n;if(t){const n=new Audio;n.src=URL.createObjectURL(e),n.play()}return null==o||o(e),null==r||r(e),e},exports.processStreamResponse=async function(n,o={}){const{autoPlay:a=!1,returnBlob:i=!1,mimeType:s="audio/wav",onAudioData:c,onError:d,onComplete:y,onStart:p,onEnded:f,audioField:g}=o,h=e(),w=new TextDecoder;let b="",P=null;const m=i?new Promise((e=>{P=e})):void 0;let v=!1;return(async()=>{try{for(;!v;){const{value:e,done:t}=await n.read();if(t)break;b+=w.decode(e,{stream:!0});const o=b.split("\n");b=o.pop()||"";for(const n of o)n.trim()&&l(n.trim(),h,a,c,g,p,f)}if(!v&&b.trim()&&l(b.trim(),h,a,c,g,p,f),i&&h.wavHeader&&h.audioChunks.length>0){const e=u(h.audioChunks,h.wavHeader,s);null==P||P(e),null==y||y(e)}else null==y||y()}catch(e){e instanceof Error&&(console.error("流处理错误:",e),null==d||d(e))}})(),{pause:()=>{h.currentSource&&(h.currentSource.stop(),h.currentSource=null),h.isPlaying=!1},resume:()=>{!h.isPlaying&&h.audioQueue.length>0&&r(h,p,f)},abort:()=>{v=!0,n.cancel().catch((()=>{})),t(h)},isPlaying:()=>h.isPlaying,finalBlob:m}};