Files

20 lines
115 KiB
JavaScript
Raw Permalink Normal View History

2026-12-03 16:11:47 -05:00
// @sendspin/sendspin-js 5.0.0 (Apache-2.0), bundled with audited noble MIT dependencies.
// Modification: optional opus-encdec decoder imports disabled; this build negotiates PCM only.
// See licenses/vendor and THIRD-PARTY-NOTICES. No source maps or remote imports.
var lr=Object.defineProperty;var ur=(e,A)=>()=>(e&&(A=e(e=0)),A);var dr=(e,A)=>{for(var t in A)lr(e,t,{get:A[t],enumerable:!0})};var fe={};dr(fe,{default:()=>fr});var fr,pe=ur(()=>{throw new Error("Optional Opus fallback is not distributed; negotiate PCM");fr=void 0});var GA=class{constructor(A,t){this.webCodecsDecoder=null,this.webCodecsDecoderReady=null,this.webCodecsFormat=null,this.useNativeOpus=!0,this.nativeDecoderQueue=[],this.opusDecoder=null,this.opusDecoderModule=null,this.opusDecoderReady=null,this.flacDecodingContext=null,this.flacDecodingContextSampleRate=0,this.flacDecodingContextChannels=0,this.onDecodedChunk=A,this.currentGeneration=t}async handleBinaryMessage(A,t,n){if(new Uint8Array(A)[0]===4){let r=new DataView(A,1,8),i=Number(r.getBigInt64(0,!1)),a=A.slice(9);if(t.codec==="opus"&&this.useNativeOpus&&(await this.initWebCodecsDecoder(t),this.useNativeOpus&&this.webCodecsDecoder&&this.queueToNativeOpusDecoder(a,i,n)))return;try{let o=await this.decode(a,t);o&&n===this.currentGeneration()&&this.onDecodedChunk({samples:o.samples,sampleRate:o.sampleRate,serverTimeUs:i,generation:n})}catch(o){console.error("Sendspin: Failed to decode audio buffer:",o)}}}async decode(A,t){return t.codec==="opus"?this.decodeOpusWithEncdec(A,t):t.codec==="flac"?this.decodeFLAC(A,t):t.codec==="pcm"?this.decodePCM(A,t):null}decodePCM(A,t){let n=t.bit_depth??16;if(n!==16&&n!==24&&n!==32)return console.warn(`Sendspin: unsupported PCM bit_depth ${n}`),null;let s=n/8,r=new DataView(A),i=A.byteLength/(s*t.channels),a=[];for(let o=0;o<t.channels;o++)a.push(new Float32Array(i));for(let o=0;o<t.channels;o++){let h=a[o];for(let l=0;l<i;l++){let u=(l*t.channels+o)*s,c=0;if(n===16)c=r.getInt16(u,!0)/32768;else if(n===24){let f=r.getUint8(u),d=r.getUint8(u+1),m=r.getUint8(u+2)<<16|d<<8|f;m&8388608&&(m|=4278190080),c=m/8388608}else n===32&&(c=r.getInt32(u,!0)/2147483648);h[l]=c}}return{samples:a,sampleRate:t.sample_rate}}getFlacDecodingContext(A,t){return(!this.flacDecodingContext||this.flacDecodingContextSampleRate!==A||this.flacDecodingContextChannels!==t)&&(this.flacDecodingContext=new OfflineAudioContext(t,1,A),this.flacDecodingContextSampleRate=A,this.flacDecodingContextChannels=t),this.flacDecodingContext}async decodeFLAC(A,t){try{let n=A;if(t.codec_header){let a=Uint8Array.from(atob(t.codec_header),h=>h.charCodeAt(0)),o=new Uint8Array(a.length+A.byteLength);o.set(a,0),o.set(new Uint8Array(A),a.length),n=o.buffer}let r=await this.getFlacDecodingContext(t.sample_rate,t.channels).decodeAudioData(n),i=[];for(let a=0;a<r.numberOfChannels;a++)i.push(new Float32Array(r.getChannelData(a)));return{samples:i,sampleRate:r.sampleRate}}catch(n){return console.error("Error decoding FLAC data:",n),null}}async initWebCodecsDecoder(A){let t=()=>{if(!this.webCodecsDecoder)return!1;let n=!!this.webCodecsFormat&&this.webCodecsFormat.sample_rate===A.sample_rate&&this.webCodecsFormat.channels===A.channels;if(this.webCodecsDecoder.state==="configured"&&n)return!0;if(this.webCodecsDecoder.state==="closed")return!1;try{return this.webCodecsDecoder.configure({codec:"opus",sampleRate:A.sample_rate,numberOfChannels:A.channels}),this.webCodecsFormat=A,!0}catch{return!1}};if(!t()){if(this.webCodecsDecoderReady){if(await this.webCodecsDecoderReady,t())return;try{this.webCodecsDecoder?.close()}catch{}this.webCodecsDecoder=null,this.webCodecsDecoderReady=null,this.webCodecsFormat=null}if(this.webCodecsDecoderReady){await this.webCodecsDecoderReady;return}this.webCodecsDecoderReady=this.createWebCodecsDecoder(A),await this.webCodecsDecoderReady}}async createWebCodecsDecoder(A){if(typeof AudioDecoder>"u"){this.useNativeOpus=!1;return}try{if(!(await AudioDecoder.isConfigSupported({codec:"opus",sampleRate:A.sample_rate,numberOfChannels:A.channels})).supported){console.log("[NativeOpus] WebCodecs Opus not supported, will use fallback"),this.useNativeOpus=!1;return}this.webCodecsDecoder=new AudioDecoder({output:n=>this.handleAudioData(n),error:n=>{console.error("[NativeOpus] WebCodecs decoder error:",n)}}),this.webCodecsDecoder.configure({codec:"opus",sampleRate:A.sample_r
/*! Bundled license information:
@noble/hashes/esm/utils.js:
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
@noble/curves/esm/utils.js:
@noble/curves/esm/abstract/modular.js:
@noble/curves/esm/abstract/curve.js:
@noble/curves/esm/abstract/edwards.js:
@noble/curves/esm/abstract/montgomery.js:
@noble/curves/esm/ed25519.js:
(*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
@noble/ciphers/esm/utils.js:
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
*/