mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-26 08:51:20 -08:00
Refactor ASR plugin options and improve audio processing
- Renamed `hideTimeout` to `autoHideTimeout` for clarity. - Updated the default options destructuring for better readability. - Removed unnecessary comments and cleaned up the audio processing logic. - Ensured that the audio buffer is processed correctly before sending to the `onAudioChunk` callback.
This commit is contained in:
1 parent
1ece90c582
commit
3f0a1bd433
12 files changed
+41
-45
No files matched your search
@@ -5,5 +5,5 @@
|
||||
* (c) 2017-2025 Harvey Zack
|
||||
* Released under the MIT License.
|
||||
*/
|
||||
!function(e,t,r,n,o,a,l,i){var u="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},s="function"==typeof u[n]&&u[n],c=s.i||{},d=s.cache||{},p="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function f(t,r){if(!d[t]){if(!e[t]){if(o[t])return o[t];var a="function"==typeof u[n]&&u[n];if(!r&&a)return a(t,!0);if(s)return s(t,!0);if(p&&"string"==typeof t)return p(t);var l=Error("Cannot find module '"+t+"'");throw l.code="MODULE_NOT_FOUND",l}c.resolve=function(r){var n=e[t][1][r];return null!=n?n:r},c.cache={};var i=d[t]=new f.Module(t);e[t][0].call(i.exports,c,i,i.exports,u)}return d[t].exports;function c(e){var t=c.resolve(e);return!1===t?{}:f(t)}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.require=p,this.exports={}},f.modules=e,f.cache=d,f.parent=s,f.distDir=void 0,f.publicUrl=void 0,f.devServer=void 0,f.i=c,f.register=function(t,r){e[t]=[function(e,t){t.exports=r},{}]},Object.defineProperty(f,"root",{get:function(){return u[n]}}),u[n]=f;for(var y=0;y<t.length;y++)f(t[y]);if(r){var m=f(r);"object"==typeof exports&&"undefined"!=typeof module?module.exports=m:"function"==typeof define&&define.amd&&define(function(){return m})}}({"40eeC":[function(e,t,r,n){var o=e("@parcel/transformer-js/src/esmodule-helpers.js");o.defineInteropFlag(r),o.export(r,"default",()=>i);var a=e("bundle-text:./style.less"),l=o.interopDefault(a);function i(e={}){let{hideTimeout:t=1e4,length:r=3,interval:n=100,sampleRate:o=16e3,onAudioChunk:a=()=>null}=e;return e=>{let l=!1,i=null,u=null,s=null,c=null,d=[],p=null,f=!1,y=null,m=null,b=null,g=e.layers.add({name:"asr",html:""});function v(){g.style.display="none"}function x(e){"string"==typeof e&&(clearTimeout(y),y=setTimeout(v,t),g.style.display="",g.innerHTML=e.split(/(?<=[、。!?!?\.])\s*/u).map(e=>e.trim()).filter(Boolean).slice(-r).map(e=>`<div class="art-asr-line">${e}</div>`).join(""))}let h=` class RecorderProcessor extends AudioWorkletProcessor { process(inputs) { const input = inputs[0]; if (input && input[0]) { this.port.postMessage(input[0]); } return true; } } registerProcessor('recorder-processor', RecorderProcessor); `,w=()=>{let e=new Blob([h],{type:"application/javascript"});return URL.createObjectURL(e)};async function P(){return i||"suspended"===(i=new(window.AudioContext||window.webkitAudioContext)({sampleRate:o})).state&&await i.resume(),i}async function j(){if(!u&&e.video)try{return u=i.createMediaElementSource(e.video)}catch(e){console.warn("[artplayerPluginAsr] Direct connection failed, trying fallback...",e)}try{if(m||(m=await A(e.video)),m)return b=i.createMediaStreamSource(m)}catch(e){console.warn("[artplayerPluginAsr] MediaStream fallback failed:",e)}return null}async function A(e){try{if(e.captureStream)return e.captureStream();if(e.mozCaptureStream)return e.mozCaptureStream();return console.warn("[artplayerPluginAsr] captureStream not supported"),null}catch(e){return console.warn("[artplayerPluginAsr] Error capturing stream:",e),null}}async function M(){if(!l)try{await P();let e=await j();if(!e)throw Error("Could not establish audio source");if(!f){let e=w();await i.audioWorklet.addModule(e),URL.revokeObjectURL(e),f=!0}(c=i.createGain()).gain.value=1,(s=new AudioWorkletNode(i,"recorder-processor")).port.onmessage=e=>{d.push(new Float32Array(e.data))},e.connect(s),e.connect(c),c.connect(i.destination);let t=n/1e3*o;p=setInterval(async()=>{if(0===d.length)return;let e=new Float32Array(0);for(;e.length<t&&d.length;){let t=d.shift(),r=new Float32Array(e.length+t.length);r.set(e,0),r.set(t,e.length),e=r}if(e.length<t)return;let r=e.slice(0,t),n=function(e){let t=e.length,r=new ArrayBuffer(2*t),n=new DataView(r);for(let r=0;r<t;r++){let t=Math.max(-1,Math.min(1,e[r]));n.setInt16(2*r,t<0?32768*t:32767*t,!0)}return r}(r),o=await a(n);x(o)},n),l=!0}catch(e){console.error("[artplayerPluginAsr] Initialization failed:",e),await C()}}async function C(){l&&(l=!1,clearInterval(p),p=null,s&&(s.disconnect(),s.port.onmessage=null,s=null),c&&(c.disconnect(),c=null),d=[])}async function O(){await C(),b&&(b.disconnect(),b=null),u&&(u.disconnect(),u=null),m&&(m.getTracks().forEach(e=>e.stop()),m=null),i&&(await i.close(),i=null),f=!1}return e.on("play",M),e.on("pause",C),e.on("destroy",O),{name:"artplayerPluginAsr",stop:O,hide:v,append:x}}}if("undefined"!=typeof document&&!document.getElementById("artplayer-plugin-asr")){let e=document.createElement("style");e.id="artplayer-plugin-asr",e.textContent=l.default,document.head.appendChild(e)}"undefined"!=typeof window&&(window.artplayerPluginAsr=i)},{"bundle-text:./style.less":"dKHY3","@parcel/transformer-js/src/esmodule-helpers.js":"5lQoW"}],dKHY3:[function(e,t,r,n){t.exports=".art-video-player .art-layer-asr{z-index:150;justify-content:end;gap:var(--art-subtitle-gap);padding:0 2%;padding-bottom:var(--art-subtitle-bottom);transition:padding-bottom var(--art-transition-duratLine truncated
|
||||
!function(e,t,r,n,o,a,l,i){var u="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:{},s="function"==typeof u[n]&&u[n],c=s.i||{},d=s.cache||{},p="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);function f(t,r){if(!d[t]){if(!e[t]){if(o[t])return o[t];var a="function"==typeof u[n]&&u[n];if(!r&&a)return a(t,!0);if(s)return s(t,!0);if(p&&"string"==typeof t)return p(t);var l=Error("Cannot find module '"+t+"'");throw l.code="MODULE_NOT_FOUND",l}c.resolve=function(r){var n=e[t][1][r];return null!=n?n:r},c.cache={};var i=d[t]=new f.Module(t);e[t][0].call(i.exports,c,i,i.exports,u)}return d[t].exports;function c(e){var t=c.resolve(e);return!1===t?{}:f(t)}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.require=p,this.exports={}},f.modules=e,f.cache=d,f.parent=s,f.distDir=void 0,f.publicUrl=void 0,f.devServer=void 0,f.i=c,f.register=function(t,r){e[t]=[function(e,t){t.exports=r},{}]},Object.defineProperty(f,"root",{get:function(){return u[n]}}),u[n]=f;for(var y=0;y<t.length;y++)f(t[y]);if(r){var m=f(r);"object"==typeof exports&&"undefined"!=typeof module?module.exports=m:"function"==typeof define&&define.amd&&define(function(){return m})}}({"40eeC":[function(e,t,r,n){var o=e("@parcel/transformer-js/src/esmodule-helpers.js");o.defineInteropFlag(r),o.export(r,"default",()=>i);var a=e("bundle-text:./style.less"),l=o.interopDefault(a);function i(e={}){let{length:t=3,interval:r=100,sampleRate:n=16e3,autoHideTimeout:o=1e4,onAudioChunk:a=()=>null}=e;return e=>{let l=!1,i=null,u=null,s=null,c=null,d=[],p=null,f=!1,y=null,m=null,b=null,g=e.layers.add({name:"asr",html:""});function v(){g.style.display="none"}function x(e){"string"==typeof e&&(clearTimeout(y),y=setTimeout(v,o),g.style.display="",g.innerHTML=e.split(/(?<=[、。!?!?\.])\s*/u).map(e=>e.trim()).filter(Boolean).slice(-t).map(e=>`<div class="art-asr-line">${e}</div>`).join(""))}let h=` class RecorderProcessor extends AudioWorkletProcessor { process(inputs) { const input = inputs[0]; if (input && input[0]) { this.port.postMessage(input[0]); } return true; } } registerProcessor('recorder-processor', RecorderProcessor); `,w=()=>{let e=new Blob([h],{type:"application/javascript"});return URL.createObjectURL(e)};async function P(){return i||"suspended"===(i=new(window.AudioContext||window.webkitAudioContext)({sampleRate:n})).state&&await i.resume(),i}async function j(){if(!u&&e.video)try{return u=i.createMediaElementSource(e.video)}catch(e){console.warn("[artplayerPluginAsr] Direct connection failed, trying fallback...",e)}try{if(m||(m=await A(e.video)),m)return b=i.createMediaStreamSource(m)}catch(e){console.warn("[artplayerPluginAsr] MediaStream fallback failed:",e)}return null}async function A(e){try{if(e.captureStream)return e.captureStream();if(e.mozCaptureStream)return e.mozCaptureStream();return console.warn("[artplayerPluginAsr] captureStream not supported"),null}catch(e){return console.warn("[artplayerPluginAsr] Error capturing stream:",e),null}}async function M(){if(!l)try{await P();let e=await j();if(!e)throw Error("Could not establish audio source");if(!f){let e=w();await i.audioWorklet.addModule(e),URL.revokeObjectURL(e),f=!0}(c=i.createGain()).gain.value=1,(s=new AudioWorkletNode(i,"recorder-processor")).port.onmessage=e=>{d.push(new Float32Array(e.data))},e.connect(s),e.connect(c),c.connect(i.destination);let t=r/1e3*n;p=setInterval(async()=>{if(0===d.length)return;let e=new Float32Array(0);for(;e.length<t&&d.length;){let t=d.shift(),r=new Float32Array(e.length+t.length);r.set(e,0),r.set(t,e.length),e=r}if(e.length<t)return;let r=e.slice(0,t),n=function(e){let t=e.length,r=new ArrayBuffer(2*t),n=new DataView(r);for(let r=0;r<t;r++){let t=Math.max(-1,Math.min(1,e[r]));n.setInt16(2*r,t<0?32768*t:32767*t,!0)}return r}(r),o=await a(n);x(o)},r),l=!0}catch(e){console.error("[artplayerPluginAsr] Initialization failed:",e),await C()}}async function C(){l&&(l=!1,clearInterval(p),p=null,s&&(s.disconnect(),s.port.onmessage=null,s=null),c&&(c.disconnect(),c=null),d=[])}async function O(){await C(),b&&(b.disconnect(),b=null),u&&(u.disconnect(),u=null),m&&(m.getTracks().forEach(e=>e.stop()),m=null),i&&(await i.close(),i=null),f=!1}return e.on("play",M),e.on("pause",C),e.on("destroy",O),{name:"artplayerPluginAsr",stop:O,hide:v,append:x}}}if("undefined"!=typeof document&&!document.getElementById("artplayer-plugin-asr")){let e=document.createElement("style");e.id="artplayer-plugin-asr",e.textContent=l.default,document.head.appendChild(e)}"undefined"!=typeof window&&(window.artplayerPluginAsr=i)},{"bundle-text:./style.less":"dKHY3","@parcel/transformer-js/src/esmodule-helpers.js":"5lQoW"}],dKHY3:[function(e,t,r,n){t.exports=".art-video-player .art-layer-asr{z-index:150;justify-content:end;gap:var(--art-subtitle-gap);padding:0 2%;padding-bottom:var(--art-subtitle-bottom);transition:padding-bottom var(--art-transition-dLine truncated
|
||||
//# sourceMappingURL=artplayer-plugin-asr.esm.js.map
|
||||
Reference in new issue
Block a user