import{assert as a}from"../misc.js";import{FRAME_HEADER_SIZE as f,readFrameHeader as h}from"../../shared/mp3-misc.js";export class Mp3Reader{constructor(e){this.reader=e,this.pos=0,this.fileSize=null}readBytes(e){const{view:s,offset:t}=this.reader.getViewAndOffset(this.pos,this.pos+e);return this.pos+=e,new Uint8Array(s.buffer,t,e)}readU16(){const{view:e,offset:s}=this.reader.getViewAndOffset(this.pos,this.pos+2);return this.pos+=2,e.getUint16(s,!1)}readU32(){const{view:e,offset:s}=this.reader.getViewAndOffset(this.pos,this.pos+4);return this.pos+=4,e.getUint32(s,!1)}readAscii(e){const{view:s,offset:t}=this.reader.getViewAndOffset(this.pos,this.pos+e);this.pos+=e;let o="";for(let r=0;r<e;r++)o+=String.fromCharCode(s.getUint8(t+r));return o}readId3(){return this.readAscii(3)!=="ID3"?(this.pos-=3,null):(this.pos+=3,{size:decodeSynchsafe(this.readU32())})}readNextFrameHeader(e){for(a(this.fileSize),e??=this.fileSize;this.pos<=e-f;){const s=this.readU32();this.pos-=4;const t=h(s,this);if(t)return t}return null}}export const decodeSynchsafe=i=>{let e=2130706432,s=0;for(;e!==0;)s>>=1,s|=i&e,e>>=8;return s};
