import{computeMp3FrameSize as h,getXingOffset as I,MPEG_V1_BITRATES as g,MPEG_V2_BITRATES as b,SAMPLING_RATES as E,XING as x}from"../../shared/mp3-misc.js";export class Mp3Writer{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.helperView=new DataView(this.helper.buffer)}writeU32(e){this.helperView.setUint32(0,e,!1),this.writer.write(this.helper.subarray(0,4))}writeXingFrame(e){const o=this.writer.getPos(),l=255,f=224|e.mpegVersionId<<3|e.layer<<1,t=(e.mpegVersionId===3?g:b)?.[e.layer];if(!t)throw new Error("Invalid MPEG version and layer combination.");const r=E[e.mpegVersionId]?.[e.frequencyIndex];if(!r||r===-1)throw new Error("Invalid MPEG version and frequency index combination.");const n=0,c=155,s=t.findIndex(m=>h(e.layer,1e3*m,r,n)>=c);if(s===-1)throw new Error("No suitable bitrate found.");const w=s<<4|e.frequencyIndex<<2|n<<1,p=e.channel<<6|e.modeExtension<<4|e.copyright<<3|e.original<<2|e.emphasis;this.helper[0]=l,this.helper[1]=f,this.helper[2]=w,this.helper[3]=p,this.writer.write(this.helper.subarray(0,4));const u=I(e.mpegVersionId,e.channel);this.writer.seek(o+u),this.writeU32(x);let i=0;e.frameCount!==null&&(i|=1),e.fileSize!==null&&(i|=2),e.toc!==null&&(i|=4),this.writeU32(i),this.writeU32(e.frameCount??0),this.writeU32(e.fileSize??0),this.writer.write(e.toc??new Uint8Array(100));const y=h(e.layer,1e3*t[s],r,n);this.writer.seek(o+y)}}
