local a=require"cc.expect"local b=require"cc.audio.dfpwm"local c,d,e,f,g,h=bit32.band,bit32.bxor,bit32.lshift,bit32.rshift,bit32.btest,bit32.extract;local i,j,k,l,m,n,o,p=math.floor,math.ceil,math.sin,math.abs,math.fmod,math.min,math.max,math.pi;local q,r,s=os.epoch,os.queueEvent,os.pullEvent;local t,u,v,w,x=string.pack,string.unpack,string.sub,string.byte,string.rep;local y,z,A,B=table.pack,table.unpack,table.insert,table.remove;local C=setmetatable({_VERSION="1.9.1",defaultInterpolation="linear"},{__call=function(C,D)a(1,D,"string")local E=assert(fs.open(D,"rb"))local type=C.detect(E.read(64))or"dfpwm"E.seek("set",0)C.play(C.stream[type](function()return E.read(48000)end),peripheral.find("speaker"))E.close()end})C.effects={}C.stream={}local F={}local G;local H="3ac1fa38-811d-4361-a40d-ce53ca607cd1"local function I(J)return J:gsub("-",""):gsub("%x%x",function(K)return string.char(tonumber(K,16))end)end;local L=jit and 30 or 10;local M={dfpwm=I(H),pcm=I"01000000-0000-1000-8000-00aa00389b71",msadpcm=I"02000000-0000-1000-8000-00aa00389b71",alaw=I"06000000-0000-1000-8000-00aa00389b71",ulaw=I"07000000-0000-1000-8000-00aa00389b71",adpcm=I"11000000-0000-1000-8000-00aa00389b71",pcm_float=I"03000000-0000-1000-8000-00aa00389b71"}local N={0x04,0x03,0x07,0x33,0x37,0x3F,0x637,0x63F,0x50F7,0x50FF,0x56F7,0x56FF}local O={[0]=-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8}local P={[0]=7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767}local Q={[0]=230,230,230,230,307,409,512,614,[-8]=768,[-7]=614,[-6]=512,[-5]=409,[-4]=307,[-3]=230,[-2]=230,[-1]=230}local R={tracknumber="trackNumber",["encoded-by"]="encodedBy",sourcemedia="sourceMedia",labelno="labelNumber",discnumber="discNumber",partnumber="partNumber",productnumber="productNumber",catalognumber="catalogNumber",["release date"]="releaseDate",["source medium"]="sourceMedium",["source artist"]="sourceArtist",["guest artist"]="guestArtist",["source work"]="sourceWork",disctotal="discCount",tracktotal="trackCount",parttotal="partCount",tcm="composer"}local S={IPRD="album",INAM="title",IART="artist",IWRI="author",IMUS="composer",IPRO="producer",IPRT="trackNumber",ITRK="trackNumber",IFRM="trackCount",PRT1="partNumber",PRT2="partCount",TLEN="length",IRTD="rating",ICRD="date",ITCH="encodedBy",ISFT="encoder",ISRF="media",IGNR="genre",ICMT="comment",ICOP="copyright",ILNG="language"}local function T(U,V)local W={utf8.codepoint(U,1,-1)}for X,Y in ipairs(W)do if Y>0xFF then W[X]=0x3F end end;return string.char(z(W)),V end;local function Z(_,a0,a1)if _a1 then return a1 else return _ end end;local function a2(_,a3)if type(a3)=="table"and getmetatable(a3)==G then return a3 end;a(_,a3,"Audio")end;local function a4(a5)local a6={}for a7,Y in pairs(a5)do a6[a7]=Y end;return a6 end;local function a8(U,V,a9,aa,ab)local _=0;if ab then for X=0,a9-1 do _=_*256+w(U,V+X)end else for X=0,a9-1 do _=_+w(U,V+X)*2^(8*X)end end;if aa and _>=2^(a9*8-1)then _=_-2^(a9*8)end;return _,V+a9 end;local ac={none=function(ad,ae)return ad[i(ae)]end,linear=function(ad,ae)local af=i(ae)return ad[af]+((ad[af+1]or ad[af])-ad[af])*(ae-af)end,cubic=function(ad,ae)local af=i(ae)local ag,ah,ai,aj,ak=ad[af-1],ad[af],ad[af+1],ad[af+2],ae-af;ag,ai,aj=ag or ah,ai or ah,aj or ai or ah;return(-0.5*ag+1.5*ah-1.5*ai+0.5*aj)*ak^3+(ag-2.5*ah+2*ai-0.5*aj)*ak^2+(-0.5*ag+0.5*ai)*ak+ah end,sinc=function(ad,ae)local af=i(ae)local ak=ae-af;local al=0;for _=-L,L do local am=af+_;local an=ad[am]if an then local ao=p*(ak-_)if ao==0 then al=al+an else al=al+an*k(ao)/ao end end end;return al end}local ap={none=1,linear=1,cubic=0,sinc=0}local aq={none=1,linear=2,cubic=3,sinc=0}local ar={sine=function(ae,as,at)return k(2*ae*p*as)*at end,triangle=function(ae,as,at)return 2.0*l(at*m(2.0*ae*as+1.5,2.0)-at)-at end,square=function(ae,as,at,au)if ae*as%1>=au then return-at else return at end end,sawtooth=function(ae,as,at)return at*m(2.0*ae*as+1.0,2.0)-at end}local av;do local aw={{},{1},{2,-1},{3,-3,1},{4,-6,4,-1}}local function ax(ad,V)local ay={}local az,aA=0,0;function ay.alignToByte()aA=aA-aA%8 end;function ay.readByte()return ay.readUint(8)end;function ay.readUint(_)if _==0 then return 0 end;while aA<_ do local aB=w(ad,V)V=V+1;if aB==nil then return nil end;az=(az*256+aB)%0x100000000000;aA=aA+8 end;aA=aA-_;local aC=i(az/2^aA)if _<32 then aC=aC%2^_ end;return aC end;function ay.readSignedInt(_)local Y=ay.readUint(_)if Y>=2^(_-1)then Y=Y-2^_ end;return Y end;function ay.readRiceSignedInt(aD)local aE=0;while ay.readUint(1)==0 do aE=aE+1 end;aE=aE*2^aD+ay.readUint(aD)if g(aE,1)then return-i(aE/2)-1 else return i(aE/2)end end;return ay end;local function aF(aG,aH,aI,aC)local aJ=aG.readUint(2)if aJ>=2 then error("Reserved residual coding method "..aJ)end;local aK=aJ==0 and 4 or 5;local aL=aJ==0 and 0xF or 0x1F;local aM=aG.readUint(4)local aN=2^aM;if aI%aN~=0 then error("Block size not divisible by number of Rice partitions")end;local aO=i(aI/aN)for X=0,aN-1 do local aP=X*aO+(X==0 and aH or 0)local aQ=(X+1)*aO;local aD=aG.readUint(aK)if aD=2^(aY-1)then b8=b8-2^aY end;aC[b5][X]=b8/2^aY end end end;local function b9(aG,ba,aY,bb,bc)local bd={}for X=1,ba do bd[X]={}end;local aB=aG.readByte()if aB==nil then return false end;local be=aB*64+aG.readUint(6)if be~=0x3FFE then error("Sync code expected")end;aG.readUint(2)local bf=aG.readUint(4)local bg=aG.readUint(4)local b3=aG.readUint(4)aG.readUint(4)aB=aG.readUint(8)local bh=-1;for X=7,0,-1 do if not g(aB,2^X)then break end;bh=bh+1 end;for X=1,bh do aG.readUint(8)end;local aI;if bf==1 then aI=192 elseif 2<=bf and bf<=5 then aI=576*2^(bf-2)elseif bf==6 then aI=aG.readUint(8)+1 elseif bf==7 then aI=aG.readUint(16)+1 elseif 8<=bf and bf<=15 then aI=256*2^(bf-8)else error("Reserved block size")end;if bg==12 then aG.readUint(8)elseif bg==13 or bg==14 then aG.readUint(16)end;aG.readUint(8)b2(aG,aY,b3,aI,bd)aG.alignToByte()aG.readUint(16)if bc then bc(bd)else for K=1,ba do local _=#bb[K]for X=1,aI do bb[K][_+X]=bd[K][X]end end end;return true end;function av(aG,bc,bi)local bd={}local V=1;local aB;aB,V=a8(aG,V,4,false,true)if aB~=0x664C6143 then error("Invalid magic string")end;local bj,ba,aY,bk;local bl=false;local bm={}while not bl do aB,V=w(aG,V),V+1;bl=g(aB,0x80)local type=c(aB,0x7F)local bn;bn,V=a8(aG,V,3,false,true)if type==0 then V=V+10;bj,V=a8(aG,V,2,false,true)bj=bj*16+f(w(aG,V),4)ba=c(f(w(aG,V),1),7)+1;aY=c(w(aG,V),1)*16+f(w(aG,V+1),4)+1;bk,V=a8(aG,V+2,4,false,true)bk=bk+c(w(aG,V-5),15)*2^32;V=V+16 elseif type==4 then local bo;bm.vendor,bo,V=u("3000 then aP=q"utc"sleep(0)end;local ae=(X-1)/br+1;if ae%1==0 then bv[X]=K[ae]else bv[X]=Z(bt(K,ae),-1,1)end end;bq.data[bu]=bv end;return bq end;function F:mono()local bq=setmetatable({sampleRate=self.sampleRate,data={{}},metadata=a4(self.metadata),info=a4(self.info)},G)local bw=bq.data[1]local bx=#self.data;local aP=q"utc"for X=1,#self.data[1]do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;local b8=0;for K=1,bx do b8=b8+self.data[K][X]end;bw[X]=b8/bx end;return bq end;function F:concat(...)local by={self,...}local bz={#self.data[1]}local bx=#self.data;for X=2,#by do a2(X-1,by[X])if by[X].sampleRate~=self.sampleRate then by[X]=by[X]:resample(self.sampleRate)end;bz[X]=#by[X].data[1]bx=o(bx,#by[X].data)end;local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for K=1,bx do local b5={}local V=0;for bA=1,#by do local bB=by[bA].data[K]if bB then for X=1,bz[bA]do b5[V+X]=bB[X]end else for X=1,bz[bA]do b5[V+X]=0 end end;V=V+bz[bA]end;ay.data[K]=b5 end;return ay end;function F:sub(aP,bl)aP=i(a(1,aP,"number","nil")or 0)bl=i(a(2,bl,"number","nil")or 0)local bC=#self.data[1]/self.sampleRate;if aP<0 then aP=bC+aP end;if bl<=0 then bl=bC+bl end;a.range(aP,0,bC)a.range(bl,0,bC)aP,bl=aP*self.sampleRate+1,bl*self.sampleRate+1;local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for K=1,#self.data do local b5={}local bB=self.data[K]for X=aP,bl do b5[X-aP+1]=bB[X]end;ay.data[K]=b5 end;return ay end;function F:combine(...)local by={self,...}local bC=#self.data[1]for X=2,#by do a2(X-1,by[X])if by[X].sampleRate~=self.sampleRate then by[X]=by[X]:resample(self.sampleRate)end;bC=o(bC,#by[X].data[1])end;local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)local V=0;for bA=1,#by do for K=1,#by[bA].data do local bB,b5=by[bA].data[K],{}for X=1,bC do b5[X]=bB[X]or 0 end;ay.data[V+K]=b5 end;V=V+#by[bA].data end;return ay end;function F:split(...)local bD={}for _,bE in ipairs{...}do a(_,bE,"table")if#bE==0 then error("bad argument #".._.." (cannot use empty table)")end;local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for bF,bG in ipairs(bE)do local bB,b5=self.data[a(bF,bG,"number")],{}if not bB then error("channel "..bG.." (in argument ".._..") out of range",2)end;for X=1,#bB do b5[X]=bB[X]end;ay.data[bF]=b5 end;bD[#bD+1]=ay end;return z(bD)end;function F:mix(bH,...)local by={self,...}local bC=#self.data[1]local bx=#self.data;for X=2,#by do a2(X,by[X])if by[X].sampleRate~=self.sampleRate then by[X]=by[X]:resample(self.sampleRate)end;bC=o(bC,#by[X].data[1])bx=o(bx,#by[X].data)end;if type(bH)~="number"then a2(1,bH)if bH.sampleRate~=self.sampleRate then bH=bH:resample(self.sampleRate)end;bC=o(bC,#bH.data[1])bx=o(bx,#bH.data)A(by,2,bH)bH=1 end;local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for K=1,bx do local b5={}local bB={}for bA=1,#by do bB[bA]=by[bA].data[K]end;for X=1,bC do local b8=0;for bA=1,#by do if bB[bA]then b8=b8+(bB[bA][X]or 0)end end;b5[X]=Z(b8*bH,-1,1)end;ay.data[K]=b5 end;return ay end;function F:rep(bI)if type(self)~="table"and type(bI)=="table"then self,bI=bI,self end;a(1,bI,"number")local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for K=1,#self.data do local bB,b5=self.data[K],{}for _=0,bI-1 do local V=_*#bB;for X=1,#bB do b5[V+X]=bB[X]end end;ay.data[K]=b5 end;return ay end;function F:reverse()local ay=setmetatable({sampleRate=self.sampleRate,data={},metadata=a4(self.metadata),info=a4(self.info)},G)for K=1,#self.data do local bB,b5=self.data[K],{}local bC=#bB;for X=1,bC do b5[bC-X+1]=bB[X]end;ay.data[K]=b5 end;return ay end;local function bJ(bK,V)local bL=2^(bK.bitDepth-1)local bM=bK.dataType=="unsigned"and bL or 0;local bN=bK.audio.data;local bO;if bK.dataType=="float"then bO=function(an)return an end else bO=function(an)return an*(an<0 and bL or bL-1)+bM end end;local ad={}local bP=#bN;local bC=#bN[1]if V>bC then return nil end;local aP=q"utc"if bK.interleaved then for _=V,V+bK.len-1 do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;for K=1,bP do ad[(_-1)*bP+K]=bO(bN[K][_])end end elseif bK.multiple then for K=1,bP do ad[K]={}for _=V,V+bK.len-1 do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;local b8=bN[K][_]if not b8 then break end;ad[K][_-V+1]=bO(b8)end end;return V+bK.len,z(ad)else for K=1,bP do for _=V,V+bK.len-1 do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;ad[(K-1)*bC+_]=bO(bN[K][_])end end end;return ad end;function F:pcm(bQ,bR,bS)bQ=a(1,bQ,"number","nil")or 8;bR=a(2,bR,"string","nil")or"signed"a(3,bS,"boolean","nil")if bS==nil then bS=true end;if bQ~=8 and bQ~=16 and bQ~=24 and bQ~=32 then error("bad argument #2 (invalid bit depth)",2)end;if bR~="signed"and bR~="unsigned"and bR~="float"then error("bad argument #3 (invalid data type)",2)end;if bR=="float"and bQ~=32 then error("bad argument #2 (float audio must have 32-bit depth)",2)end;return bJ({audio=self,bitDepth=bQ,dataType=bR,interleaved=bS,len=#self.data[1]},1)end;function F:stream(bT,bQ,bR)bT=a(1,bT,"number","nil")or 131072;bQ=a(2,bQ,"number","nil")or 8;bR=a(3,bR,"string","nil")or"signed"if bQ~=8 and bQ~=16 and bQ~=24 and bQ~=32 then error("bad argument #2 (invalid bit depth)",2)end;if bR~="signed"and bR~="unsigned"and bR~="float"then error("bad argument #3 (invalid data type)",2)end;if bR=="float"and bQ~=32 then error("bad argument #2 (float audio must have 32-bit depth)",2)end;local bK,V={audio=self,bitDepth=bQ,dataType=bR,interleaved=false,multiple=true,len=bT},1;return function()if bK==nil then return nil end;local bU=V/self.sampleRate;local Y={bJ(bK,V)}if Y[1]==nil then bK=nil;return nil end;V=B(Y,1)return Y,bU end,#self.data[1]/self.sampleRate end;function F:wav(bQ)bQ=a(1,bQ,"number","nil")or 16;if bQ==1 then local U=self:dfpwm(true)if self.metadata and next(self.metadata)then local bK={}for a7,Y in pairs(self.metadata)do for bz,bV in pairs(S)do if bV==a7 then bK[#bK+1]=bz;bK[#bK+1]=tostring(Y)break end end end;local bW=t("!2"or"<"local c3=bR=="float"and"f"or(bR=="signed"and"i"or"I")..c0;local bY=c2 ..x(c3,bX)local bL=2^(bQ-1)local ay=setmetatable({sampleRate=bj,data={},metadata={},info={bitDepth=bQ,dataType=bR}},G)for X=1,bZ do ay.data[X]={}end;local V,c4=1,1;local c5={}local c6;if type(ad)=="table"then if bR=="signed"then function c6()local b8=ad[V]V=V+1;return b8/(b8<0 and bL or bL-1)end elseif bR=="unsigned"then function c6()local b8=ad[V]V=V+1;return(b8-128)/(b8<128 and bL or bL-1)end else function c6()local b8=ad[V]V=V+1;return b8 end end elseif bR=="float"then function c6()if V>#c5 then if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return b8 end elseif bR=="signed"then function c6()if V>#c5 then if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return b8/(b8<0 and bL or bL-1)end else function c6()if V>#c5 then if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return(b8-128)/(b8<128 and bL or bL-1)end end;local aP=q"utc"if bS and bZ>1 then local an=ay.data;for X=1,bC do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;for aR=1,bZ do an[aR][X]=c6()end end else for aR=1,bZ do local bv={}ay.data[aR]=bv;for X=1,bC do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;bv[X]=c6()end end end;return ay end;function C.adpcm(ad,bZ,bj,c8,bS,c9,ca)a(1,ad,"string","table")bZ=a(2,bZ,"number","nil")or 1;bj=a(3,bj,"number","nil")or 48000;a(4,c8,"boolean","nil")if c8==nil then c8=true end;a(5,bS,"boolean","nil")if bS==nil then bS=true end;c9=a(6,c9,"number","table","nil")ca=a(7,ca,"number","table","nil")a.range(bZ,1)a.range(bj,1)if c9==nil then c9={}for X=1,bZ do c9[X]=0 end elseif type(c9)=="number"then if bZ~=1 then error("bad argument #6 (table too short)",2)end;c9={a.range(c9,-32768,32767)}else if bZ>#c9 then error("bad argument #6 (table too short)",2)end;for X=1,bZ do a.range(c9[X],-32768,32767)end end;if ca==nil then ca={}for X=1,bZ do ca[X]=0 end elseif type(ca)=="number"then if bZ~=1 then error("bad argument #7 (table too short)",2)end;ca={a.range(ca,0,88)}else if bZ>#ca then error("bad argument #7 (table too short)",2)end;for X=1,bZ do a.range(ca[X],0,88)end end;local V=1;local c6,c5,bC;if type(ad)=="string"then function c6()if c5 then local Y=c5;c5=nil;return Y else local cb=w(ad,V)V=V+1;if c8 then c5,cb=c(cb,0x0F),f(cb,4)else c5,cb=f(cb,4),c(cb,0x0F)end;return cb end end;bC=i(#ad*2/bZ)else function c6()local Y=ad[V]V=V+1;return Y end;bC=#ad/bZ end;local ay=setmetatable({sampleRate=bj,data={},metadata={},info={bitDepth=16,dataType="signed"}},G)local cc={}local aP=q"utc"if bS then local an=ay.data;for aR=1,bZ do an[aR]={}end;for X=1,bC do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;for aR=1,bZ do local cd=c6()cc[aR]=P[ca[aR]]ca[aR]=Z(ca[aR]+O[cd],0,88)local ce=f(cd%8*cc[aR],2)+f(cc[aR],3)if cd>=8 then c9[aR]=Z(c9[aR]-ce,-32768,32767)else c9[aR]=Z(c9[aR]+ce,-32768,32767)end;an[aR][X]=c9[aR]/(c9[aR]<0 and 32768 or 32767)end end else for aR=1,bZ do local bv={}local c9,ca,cc=c9[aR],ca[aR],nil;for X=1,bC do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;local cd=c6()cc=P[ca]ca=Z(ca+O[cd],0,88)local ce=f(cd%8*cc,2)+f(cc,3)if cd>=8 then c9=Z(c9-ce,-32768,32767)else c9=Z(c9+ce,-32768,32767)end;bv[X]=c9/(c9<0 and 32768 or 32767)end;ay.data[aR]=bv end end;return ay end;function C.msadpcm(ad,cf,bZ,bj,cg)a(1,ad,"string")a(2,cf,"number")bZ=a(3,bZ,"number","nil")or 1;bj=a(4,bj,"number","nil")or 48000;a(5,cg,"table","nil")a.range(bj,1)local ch,ci;if cg then if type(cg[1])~="table"then error("bad argument #5 (first entry is not a table)",2)end;if type(cg[2])~="table"then error("bad argument #5 (second entry is not a table)",2)end;if#cg[1]~=#cg[2]then error("bad argument #5 (lists are not the same length)",2)end;ch,ci={},{}for X,Y in ipairs(cg[1])do if type(Y)~="number"then error("bad entry #"..X.." in coefficient list 1 (expected number, got "..type(Y)..")",2)end;ch[X-1]=Y end;for X,Y in ipairs(cg[2])do if type(Y)~="number"then error("bad entry #"..X.." in coefficient list 2 (expected number, got "..type(Y)..")",2)end;ci[X-1]=Y end else ch,ci={[0]=256,512,0,192,240,460,392},{[0]=0,-256,0,64,0,-208,-232}end;local ay=setmetatable({sampleRate=bj,data={{},bZ==2 and{}or nil},metadata={},info={bitDepth=16,dataType="signed"}},G)local cj,b7=ay.data[1],ay.data[2]local aP=q"utc"for _=1,#ad,cf do if bZ==2 then local ck,cl,cm,cn,co,cp,cq,cr=u("=8 then cw=cw-16 end;if cx>=8 then cx=cx-16 end;local c9=Z(i((co*cs+cq*ct)/256)+cw*cm,-32768,32767)cj[#cj+1]=c9/(c9<0 and 32768 or 32767)cq,co=co,c9;cm=o(i(Q[cw]*cm/256),16)c9=Z(i((cp*cu+cr*cv)/256)+cx*cn,-32768,32767)b7[#b7+1]=c9/(c9<0 and 32768 or 32767)cr,cp=cp,c9;cn=o(i(Q[cx]*cn/256),16)end elseif bZ==1 then local cy,cz,cA,cB=u("=8 then cw=cw-16 end;if cx>=8 then cx=cx-16 end;local c9=Z(i((cA*cC+cB*cD)/256)+cw*cz,-32768,32767)cj[#cj+1]=c9/(c9<0 and 32768 or 32767)cB,cA=cA,c9;cz=o(i(Q[cw]*cz/256),16)c9=Z(i((cA*cC+cB*cD)/256)+cx*cz,-32768,32767)cj[#cj+1]=c9/(c9<0 and 32768 or 32767)cB,cA=cA,c9;cz=o(i(Q[cx]*cz/256),16)end else error("Unsupported number of channels: "..bZ)end;if q"utc"-aP>3000 then aP=q"utc"sleep(0)end end;return ay end;function C.g711(ad,cE,bZ,bj)a(1,ad,"string")a(2,cE,"boolean")bZ=a(3,bZ,"number","nil")or 1;bj=a(4,bj,"number","nil")or 8000;local bD={}local bX=jit and 7680 or 32768;local cF=cE and 0xFF or 0x55;for X=1,bZ do bD[X]={}end;local aP=q"utc"for X=1,#ad,bX do local cG={w(ad,X,X+bX-1)}for aR=1,#cG do local cb=d(cG[aR],cF)local cH,cI=c(cb,0x0F),h(cb,4,3)if not cE and cI==0 then cH=cH*4+2 else cH=e(cH*2+33,cI)end;if cE then cH=cH-33 end;bD[(X+aR-2)%bZ+1][i((X+aR-2)/bZ+1)]=cH/(g(cb,0x80)==cE and-0x2000 or 0x2000)end;if q"utc"-aP>3000 then aP=q"utc"sleep(0)end end;return setmetatable({sampleRate=bj,data=bD,metadata={bitDepth=cE and 14 or 13,dataType="signed"},info={}},G)end;function C.dfpwm(ad,bZ,bj)a(1,ad,"string")bZ=a(2,bZ,"number","nil")or 1;bj=a(3,bj,"number","nil")or 48000;a.range(bZ,1)a.range(bj,1)local cJ={}local cK=b.make_decoder()local V=1;local bl=0;local aP=q"utc"while V<=#ad do if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;local aB=cK(v(ad,V,V+6000))if aB==nil or#aB==0 then break end;for X=1,#aB do cJ[bl+X]=aB[X]end;bl=bl+#aB;V=V+6000 end;return C.pcm(cJ,8,"signed",bZ,bj,true,false)end;function C.mdfpwm(ad,bi)a(1,ad,"string")if ad:sub(1,7)~="MDFPWM\3"then error("bad argument #1 (not a MDFPWM file)",2)end;local bn,cL,cM,cN,V=("3000 then aP=q"utc"sleep(0)end;local cQ=cO(v(ad,V,V+5999))if cQ==nil or#cQ==0 then break end;for X=1,#cQ do cJ[bl+X*2-1]=cQ[X]end;local cR=cP(v(ad,V+6000,V+11999))if cR==nil or#cR==0 then break end;for X=1,#cR do cJ[bl+X*2]=cR[X]end;bl=bl+#cQ+#cR;V=V+12000 end;for X=bn*8+1,#cJ do cJ[X]=nil end;local ay=C.pcm(cJ,8,"signed",2,48000,true,false)ay.metadata={artist=cL,title=cM,album=cN}return ay end;function C.wav(ad,bi)a(1,ad,"string")local bZ,bj,bQ,bn,bR,cf,cg;local aB,V=u("c4",ad)if aB~="RIFF"then error("bad argument #1 (not a WAV file)",2)end;V=V+4;aB,V=u("c4",ad,V)if aB~="WAVE"then error("bad argument #1 (not a WAV file)",2)end;local bm={}local ay;while V<=#ad do local cS;aB,cS,V=u("0 then cg={{},{}}for X=1,cU do cg[1][X],cg[2][X]=u("c4I",ad,V)if aB=="COMM"then local cI,cH;bZ,bn,bQ,cI,cH,V=u(">hIhHI7x",ad,V)if d3 then local b8;d2,b8,V=u(">c4s1",ad,V)if#b8%2==0 then V=V+1 end end;bn=bn*bZ*i(bQ/8)local b8=g(cI,0x8000)cI=(c(cI,0x7FFF)-0x3FFE)%0x800;bj=math.ldexp(cH*(b8 and-1 or 1)/0x100000000000000,cI)elseif aB=="SSND"then d1,cf,V=u(">II",ad,V)local ad=v(ad,V+d1,V+d1+bn-1)local ay;if bi then ay=C.new(0,bZ,bj)elseif d2==nil or d2=="NONE"then ay=C.pcm(ad,bQ,"signed",bZ,bj,true,true)elseif d2=="sowt"then ay=C.pcm(ad,bQ,"signed",bZ,bj,true,false)elseif d2=="fl32"or d2=="FL32"then ay=C.pcm(ad,32,"float",bZ,bj,true,true)elseif d2=="alaw"or d2=="ulaw"or d2=="ALAW"or d2=="ULAW"then ay=C.g711(ad,d2=="ulaw"or d2=="ULAW",bZ,bj)else error("Unsupported compression scheme "..d2,2)end;ay.metadata=bm;return ay elseif aB=="NAME"then bm.title=v(ad,V,V+cS-1)V=V+cS elseif aB=="AUTH"then bm.artist=v(ad,V,V+cS-1)V=V+cS elseif aB=="(c) "then bm.copyright=v(ad,V,V+cS-1)V=V+cS elseif aB=="ANNO"then bm.comment=v(ad,V,V+cS-1)V=V+cS else V=V+cS end end;error("invalid AIFF file",2)end;function C.au(ad)a(1,ad,"string")local d4,d1,cS,d5,bj,bZ=u(">c4IIIII",ad)if d4~=".snd"then error("invalid AU file",2)end;if d5==1 then return C.g711(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),true,bZ,bj)elseif d5==2 then return C.pcm(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),8,"signed",bZ,bj,true,true)elseif d5==3 then return C.pcm(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),16,"signed",bZ,bj,true,true)elseif d5==4 then return C.pcm(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),24,"signed",bZ,bj,true,true)elseif d5==5 then return C.pcm(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),32,"signed",bZ,bj,true,true)elseif d5==6 then return C.pcm(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),32,"float",bZ,bj,true,true)elseif d5==27 then return C.g711(v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),false,bZ,bj)else error("unsupported encoding type "..d5,2)end end;function C.flac(ad,bi)a(1,ad,"string")return setmetatable(av(ad,nil,bi),G)end;function C.new(d6,bZ,bj)a(1,d6,"number")bZ=a(2,bZ,"number","nil")or 1;bj=a(3,bj,"number","nil")or 48000;a.range(bZ,1)a.range(bj,1)local ay=setmetatable({sampleRate=bj,data={},metadata={},info={}},G)for K=1,bZ do local bz={}for X=1,d6*bj do bz[X]=0 end;ay.data[K]=bz end;return ay end;function C.tone(d7,d6,at,d8,au,bZ,bj)a(1,d7,"number")a(2,d6,"number")at=a(3,at,"number","nil")or 1;d8=a(4,d8,"string","nil")or"sine"au=a(5,au,"number","nil")or 0.5;bZ=a(6,bZ,"number","nil")or 1;bj=a(7,bj,"number","nil")or 48000;a.range(at,0,1)local c7=ar[d8]if not c7 then error("bad argument #4 (invalid wave type)",2)end;a.range(au,0,1)a.range(bZ,1)a.range(bj,1)local ay=setmetatable({sampleRate=bj,data={},metadata={},info={}},G)for K=1,bZ do local bz={}for X=1,d6*bj do bz[X]=c7(X/bj,d7,at,au)end;ay.data[K]=bz end;return ay end;function C.noise(d6,at,bZ,bj)a(1,d6,"number")at=a(2,at,"number","nil")or 1;bZ=a(3,bZ,"number","nil")or 1;bj=a(4,bj,"number","nil")or 48000;a.range(at,0,1)a.range(bZ,1)a.range(bj,1)local ay=setmetatable({sampleRate=bj,data={},metadata={},info={}},G)local d9=math.random;for K=1,bZ do local bz={}for X=1,d6*bj do bz[X]=(d9()*2-1)*at end;ay.data[K]=bz end;return ay end;function C.pack(ad,bQ,bR,b_)a(1,ad,"string","table")bQ=a(2,bQ,"number","nil")or 8;bR=a(3,bR,"string","nil")or"signed"a(4,b_,"boolean","nil")if bQ~=8 and bQ~=16 and bQ~=24 and bQ~=32 then error("bad argument #2 (invalid bit depth)",2)end;if bR~="signed"and bR~="unsigned"and bR~="float"then error("bad argument #3 (invalid data type)",2)end;if bR=="float"and bQ~=32 then error("bad argument #2 (float audio must have 32-bit depth)",2)end;local c0=bQ/8;local bY=(b_ and">"or"<")..(bR=="float"and"f"or(bR=="signed"and"i"or"I")..c0)local da=v(bY,1,1)..v(bY,2):rep(512)local bD=""for X=1,#ad,512 do if#ad0 do while not de[1]do if df then return end;coroutine.yield(dd)end;local dg=B(de,1)local dh={}if db then db(dg[2])end;dg=dg[1]local di={}if#dg[1]<96000 then di={dg}else for X=0,#dg[1]-1,48000 do local cT={}di[#di+1]=cT;for aR=1,#dg do local b8,K=dg[aR],{}cT[aR]=K;for a7=1,48000 do K[a7]=b8[a7+X]end end end end;for dj,cT in ipairs(di)do for X,Y in ipairs(dd)do dh[X]=function()local dk=peripheral.getName(Y)if _HOST:find("CraftOS-PC v2.6.4")and config and not config.get("standardsMode")then Y.playAudio(cT[X]or cT[1],dc)repeat until select(2,s("speaker_audio_empty"))==dk else while not Y.playAudio(cT[X]or cT[1],dc)do repeat until select(2,s("speaker_audio_empty"))==dk end end end end;parallel.waitForAll(z(dh))end end end)local dl,dm,dn;local dp,dq={{}},{{}}repeat if#dp>0 then local dr=B(dp,1)if dm==dd then dm=nil;A(dp,1,dr)end;if dm==nil or dr[1]==dm then dl,dm=coroutine.resume(bA,z(dr,1,dr.n))if not dl then error(dm,2)end end end;if#dq>0 then local dr=B(dq,1)if dn==dd then dn=nil;A(dq,1,dr)end;if dn==nil or dr[1]==dn then dl,dn=coroutine.resume(cb,z(dr,1,dr.n))if not dl then error(dn,2)end end end;if coroutine.status(cb)=="suspended"and(#dp==0 or#dq==0)then if dm~=nil and dn~=nil then local dr=y(s())dp[#dp+1]=dr;dq[#dq+1]=dr else r("__queue_end")while true do local dr=y(s())if dr[1]=="__queue_end"then break end;dp[#dp+1]=dr;dq[#dq+1]=dr end end end until coroutine.status(cb)=="dead"or df;while coroutine.status(cb)=="suspended"and#dq>0 do local dr=B(dq,1)if dn==nil or dr[1]==dn then dl,dn=coroutine.resume(cb,z(dr,1,dr.n))if not dl then error(dn,2)end end end;while coroutine.status(cb)=="suspended"do dl,dn=coroutine.resume(cb,s())if not dl then error(dn,2)end end end;local ds={}local dt={__index=ds,__name="aukit.player"}function ds:pause()if self.isPaused then return end;if not self.playerTask then error("Player is stopped",2)end;local du=q"utc"self.isPaused=true;self.invalidate=true;for dj,Y in ipairs(self.speakers)do Y.stop()end;self.position=(self.lastPlayback.pos+(du-self.lastPlayback.time)*48-1)/48000 end;function ds:play()if not self.isPaused then return end;if not self.playerTask then error("Player is stopped",2)end;self.isPaused=false end;function ds:livePosition()if not self.playerTask then error("Player is stopped",2)end;if not self.lastPlayback then return 0 end;if self.isPaused then return self.position end;return(self.lastPlayback.pos+(q"utc"-self.lastPlayback.time)*48-1)/48000 end;function ds:seek(V)a.range(V,0)if not self.playerTask then error("Player is stopped",2)end;self.position=V;self.invalidate=true;for dj,Y in ipairs(self.speakers)do Y.stop()end end;function ds:stop()if not self.playerTask then error("Player is stopped",2)end;self.playerTask:remove()if self.loaderTask then self.loaderTask:remove()end;self.playerTask=nil;self.loaderTask=nil end;function C.player(dv,bc,dc,...)a(1,dv,"table")a(2,bc,"function")a(3,dc,"number","table")local dd={...}if type(dc)=="table"then A(dd,1,dc)dc=nil end;if#dd==0 then error("bad argument #3 (expected speakers, got nil)",2)end;local dw=setmetatable({isPaused=false,position=0,volume=dc,speakers=dd},dt)local dx={}local dy=tostring(dw)dw.loaderTask=dv:addTask(function()local _=0;for cT,V in bc do for aR=1,#cT do local K,an=cT[aR],dx[aR]if not an then an={}dx[aR]=an end;for X=1,#K do an[_+X]=K[X]end end;_=_+#cT[1]r("aukit.loader_next",dy)repeat local dj,aD=s("aukit.loader_next")until aD==dy end;dw.loaderTask=nil end)dw.playerTask=dv:addTask(function()while true do local c4=i(dw.position*48000)+1;if not dw.isPaused and dx[1]and dx[1][c4]then local cT={}for aR=1,#dx do cT[aR]={z(dx[aR],c4,n(c4+47999,#dx[aR]))}end;dw.position=dw.position+#cT[1]/48000;local dh={}for X,Y in ipairs(dd)do dh[X]=function()local dk=peripheral.getName(Y)if _HOST:find("CraftOS-PC v2.6.4")and config and not config.get("standardsMode")then Y.playAudio(cT[X]or cT[1],dc)repeat until select(2,s("speaker_audio_empty"))==dk else while not Y.playAudio(cT[X]or cT[1],dc)do repeat until select(2,s("speaker_audio_empty"))==dk;if dw.invalidate then cT=nil;return end end end;dw.lastPlayback={time=os.epoch"utc",pos=c4}end end;parallel.waitForAll(z(dh))dw.invalidate=false elseif not dw.isPaused and dw.loaderTask==nil and c4==#dx[1]+1 then dw.playerTask=nil;return else s()end end end)return dw end;local dz={{"bbbbbbbb",8,"signed"},{"BBBBBBBB",8,"unsigned"},{"hhhhhhhh",16,"signed"},{"iiiiiiii",32,"signed"},{"ffffffff",32,"float"},{"i3i3i3i3i3i3i3i3",24,"signed"},{"IIIIIIII",32,"unsigned"},{"I3I3I3I3I3I3I3I3",24,"unsigned"},{"HHHHHHHH",16,"unsigned"}}function C.detect(ad)a(1,ad,"string")if ad:match"^RIFF....WAVE"then return"wav"elseif ad:match"^FORM....AIF[FC]"then return"aiff"elseif ad:match"^%.snd"then return"au"elseif ad:match"^fLaC"then return"flac"elseif ad:match"^MDFPWM\3"then return"mdfpwm"else for dj,dA in pairs(dz)do local dB,dC=dA[3]=="unsigned"and 2^(dA[2]-1)or 0,dA[3]=="float"and 0.001 or 8*2^(dA[2]-8)local dD={pcall(u,dA[1],ad)}dD[#dD]=nil;if B(dD,1)then local dE,dl=true,true;for dj,Y in ipairs(dD)do if Y~=dB then dE=false end;if YdB+dC then dl=false;break end end;if dl and not dE then return"pcm",z(dA,2)end end;dD={pcall(u,dA[1],ad,#ad-dA[2])}dD[#dD]=nil;if B(dD,1)then local dE,dl=true,true;for dj,Y in ipairs(dD)do if Y~=dB then dE=false end;if YdB+dC then dl=false;break end end;if dl and not dE then return"pcm",z(dA,2)end end end;if ad:match(("\x55"):rep(12))or ad:match(("\xAA"):rep(12))then return"dfpwm"end end;return nil end;function C.stream.pcm(ad,bQ,bR,bZ,bj,b_,dF)local dh,df;if type(ad)=="function"then dh,ad=ad,ad()end;a(1,ad,"string","table")bQ=a(2,bQ,"number","nil")or 8;bR=a(3,bR,"string","nil")or"signed"bZ=a(4,bZ,"number","nil")or 1;bj=a(5,bj,"number","nil")or 48000;a(6,b_,"boolean","nil")a(7,dF,"boolean","nil")if bQ~=8 and bQ~=16 and bQ~=24 and bQ~=32 then error("bad argument #2 (invalid bit depth)",2)end;if bR~="signed"and bR~="unsigned"and bR~="float"then error("bad argument #3 (invalid data type)",2)end;if bR=="float"and bQ~=32 then error("bad argument #2 (float audio must have 32-bit depth)",2)end;a.range(bZ,1)a.range(bj,1)if bZ==1 then dF=false end;local c0=bQ/8;local bC=#ad/(type(ad)=="table"and 1 or c0)/bZ;local bX=jit and 7680 or 32768;local c1=bX*c0;local c2=b_ and">"or"<"local c3=bR=="float"and"f"or(bR=="signed"and"i"or"I")..c0;local bY=c2 ..x(c3,bX)local bL=2^(bQ-1)local V,c4=1,1;local c5={}local c6;if type(ad)=="table"then if bR=="signed"then function c6()if df then return nil end;if dh and V>#ad then ad,V=dh(),1;if not ad then df=true;return nil end end;local b8=ad[V]V=V+1;return b8/(b8<0 and bL or bL-1)end elseif bR=="unsigned"then function c6()if df then return nil end;if dh and V>#ad then ad,V=dh(),1;if not ad then df=true;return nil end end;local b8=ad[V]V=V+1;return(b8-128)/(b8<128 and bL or bL-1)end else function c6()if df then return nil end;if dh and V>#ad then ad,V=dh(),1;if not ad then df=true;return nil end end;local b8=ad[V]V=V+1;return b8 end end elseif bR=="float"then function c6()if df then return nil end;if V>#c5 then if dh and c4>#ad then ad,c4=dh(),1;if not ad then df=true;return nil end end;if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return b8 end elseif bR=="signed"then function c6()if df then return nil end;if V>#c5 then if dh and c4>#ad then ad,c4=dh(),1;if not ad then df=true;return nil end end;if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return b8/(b8<0 and bL or bL-1)end else function c6()if df then return nil end;if V>#c5 then if dh and c4>#ad then ad,c4=dh(),1;if not ad then df=true;return nil end end;if c4+c1>#ad then local c7=c2 ..x(c3,(#ad-c4+1)/c0)c5={u(c7,ad,c4)}c4=c5[#c5]c5[#c5]=nil else c5={u(bY,ad,c4)}c4=c5[#c5]c5[#c5]=nil end;V=1 end;local b8=c5[V]V=V+1;return(b8-128)/(b8<128 and bL or bL-1)end end;local an={}local br=48000/bj;local dG=1-math.exp(-(bj/96000)*2*p)local bt=ac[C.defaultInterpolation]for aR=1,dF and 1 or bZ do an[aR]=setmetatable({},{__index=function(self,X)if dF then for dj=1,bZ do self[X]=(rawget(self,X)or 0)+c6()end;self[X]=self[X]/bZ else self[X]=c6()end;return rawget(self,X)end})end;local _=0;local dl=true;return function()if not dl or df then return nil end;for X=_==0 and ap[C.defaultInterpolation]or 1,aq[C.defaultInterpolation]do if dF then local b8=0;for aR=1,bZ do local K=c6()if not K then return nil end;b8=b8+K end;an[1][X]=b8/bZ else for aR=1,bZ do an[aR][X]=c6()if not an[aR][X]then return nil end end end end;local cT={}for aR=1,#an do cT[aR]={}end;dl=pcall(function()local dH={}for bu=1,#an do local b8=cT[bu][0]or 0;dH[bu]=b8/(b8<0 and 128 or 127)end;for X=1,48000 do for bu=1,#an do local ae=(X-1)/br+1;local b8;if ae%1==0 then b8=an[bu][ae]else b8=bt(an[bu],ae)end;local dI=dH[bu]+dG*(b8-dH[bu])cT[bu][X]=Z(dI*(dI<0 and 128 or 127),-128,127)dH[bu]=b8 end end end)if#cT[1]==0 then return nil end;_=_+#cT[1]for bu=1,#an do if C.defaultInterpolation=="sinc"then local a6,bz={},#an[bu]for X=-L,0 do a6[X]=an[bu][bz+X]end;an[bu]=setmetatable(a6,getmetatable(an[bu]))else local dJ,dK=an[bu][#an[bu]-1],an[bu][#an[bu]]an[bu]=setmetatable({},getmetatable(an[bu]))an[bu][-1],an[bu][0]=dJ,dK end end;return cT,(_-#cT[1])/48000 end,bC/bj end;function C.stream.dfpwm(ad,bj,bZ,dF)a(1,ad,"string","function")bj=a(2,bj,"number","nil")or 48000;bZ=a(3,bZ,"number","nil")or 1;a.range(bj,1)a.range(bZ,1)if bZ==1 then dF=false end;local cK=b.make_decoder()local V=1;local bl=0;local dL=type(ad)=="string"local dM=""return function()local an;if dL then if V>#ad then return nil end;an=v(ad,V,V+6000*bZ)else while#dM#ad then return nil end;dP=v(ad,V,V+5999)dQ=v(ad,V+6000,V+11999)else while#dM<12000 do local cT=ad()if not cT then if#dM==0 then return nil else break end end;dM=dM..cT end;dP=v(dM,1,6000)dQ=v(dM,6001,12000)dM=v(dM,12001)end;local dR=cO(dP)if dR==nil or#dR==0 then return nil end;local dS=cP(dQ)if dS==nil or#dS==0 then return nil end;r("nosleep")repeat until"nosleep"==s()if V-dO+12000>bn then for X=bn/2%6000+1,6000 do dR[X],dS[X]=nil end end;local dN;if dF then local bz={}dN={bz}for X=1,48000 do bz[X]=Z(i(dR[X]+dS[X]/2),-128,127)end else dN={dR,dS}end;r("nosleep")repeat until"nosleep"==s()local bU=V-dO;V=V+#dP+#dQ;return dN,bU/12000 end,bn/12000 end;function C.stream.msadpcm(dT,cf,bZ,bj,dF,cg)a(1,dT,"string","function")a(2,cf,"number")bZ=a(3,bZ,"number","nil")or 1;bj=a(4,bj,"number","nil")or 48000;a(5,dF,"boolean","nil")a(6,cg,"table","nil")a.range(bj,1)local dU=type(dT)=="function"local ch,ci;if cg then if type(cg[1])~="table"then error("bad argument #5 (first entry is not a table)",2)end;if type(cg[2])~="table"then error("bad argument #5 (second entry is not a table)",2)end;if#cg[1]~=#cg[2]then error("bad argument #5 (lists are not the same length)",2)end;ch,ci={},{}for X,Y in ipairs(cg[1])do if type(Y)~="number"then error("bad entry #"..X.." in coefficient list 1 (expected number, got "..type(Y)..")",2)end;ch[X-1]=Y end;for X,Y in ipairs(cg[2])do if type(Y)~="number"then error("bad entry #"..X.." in coefficient list 2 (expected number, got "..type(Y)..")",2)end;ci[X-1]=Y end else ch,ci={[0]=256,512,0,192,240,460,392},{[0]=0,-256,0,64,0,-208,-232}end;local br=48000/bj;local bt=ac[C.defaultInterpolation]local _,V=1,0;local ad=dU and dT()or dT;if bZ==2 then local dV=cf-14;local dW=j(bj/dV)local dX=cf*dW;local bs=i(dV*br)local dY,dZ;return function()if ad==nil then return nil end;local d_=_+dX;local bD={{},not dF and{}or nil}local e0=0;local aP=q"utc"while _3000 then r("nosleep")repeat until"nosleep"==s()aP=q"utc"end;if dU and _>#ad then V=V+#ad;_=_-#ad;ad=dT()if ad==nil then return nil end end;if _>#ad then break end;local cj,b7={},{}if dY then for X=1,#dY do cj[X-#dY-1]=dY[X]b7[X-#dZ-1]=dZ[X]end end;local ck,cl,cm,cn,co,cp,cq,cr=u("=8 then cw=cw-16 end;if cx>=8 then cx=cx-16 end;local c9=Z(i((co*cs+cq*ct)/256)+cw*cm,-32768,32767)cj[#cj+1]=i(c9/(c9<0 and 128 or 127))cq,co=co,c9;cm=o(i(Q[cw]*cm/256),16)c9=Z(i((cp*cu+cr*cv)/256)+cx*cn,-32768,32767)b7[#b7+1]=i(c9/(c9<0 and 128 or 127))cr,cp=cp,c9;cn=o(i(Q[cx]*cn/256),16)end;dY,dZ=cj,b7;for X=1,bs do local ae=(X-1)/br+1;local bz,e1;if ae%1==0 then bz,e1=cj[ae],b7[ae]else bz,e1=bt(cj,ae),bt(b7,ae)end;if dF then bD[1][e0+X]=Z(i(bz+e1/2),-128,127)else bD[1][e0+X],bD[2][e0+X]=Z(i(bz),-128,127),Z(i(e1),-128,127)end end;e0=e0+bs;_=_+cf end;if#bD[1]==0 then return nil end;return bD,(_+V)/dX end,not dU and#ad/cf*dV/bj or nil elseif bZ==1 then local dV=(cf-7)*2;local dW=j(bj/dV)local dX=cf*dW;local bs=i(dV*br)return function()if ad==nil then return nil end;local d_=_+dX;local bD={{}}local e0=0;local aP=q"utc"while _3000 then r("nosleep")repeat until"nosleep"==s()aP=q"utc"end;if dU and _>#ad then V=V+#ad;_=_-#ad;ad=dT()if ad==nil then return nil end end;if _>#ad then break end;local cj={}local cy,cz,cA,cB=u("=8 then cw=cw-16 end;if cx>=8 then cx=cx-16 end;local c9=Z(i((cA*cC+cB*cD)/256)+cw*cz,-32768,32767)cj[#cj+1]=c9/(c9<0 and 128 or 127)cB,cA=cA,c9;cz=o(i(Q[cw]*cz/256),16)c9=Z(i((cA*cC+cB*cD)/256)+cx*cz,-32768,32767)cj[#cj+1]=c9/(c9<0 and 128 or 127)cB,cA=cA,c9;cz=o(i(Q[cx]*cz/256),16)end;for X=1,bs do local ae=(X-1)/br+1;if ae%1==0 then bD[1][e0+X]=Z(i(cj[ae]),-128,127)else bD[1][e0+X]=Z(i(bt(cj,ae)),-128,127)end end;e0=e0+bs;_=_+cf end;if#bD[1]==0 then return nil end;return bD,(_+V)/dX end,not dU and#ad/cf*dV/bj or nil else error("Unsupported number of channels: "..bZ)end end;function C.stream.adpcm(dT,cf,bZ,bj,dF)a(1,dT,"string","function")a(2,cf,"number")bZ=a(3,bZ,"number","nil")or 1;bj=a(4,bj,"number","nil")or 48000;a(5,dF,"boolean","nil")a.range(bj,1)local dU=type(dT)=="function"local br=48000/bj;local bt=ac[C.defaultInterpolation]local _,V=1,0;local ad=dU and dT()or dT;local dV=(cf-4*bZ)*2/bZ;local dW=j(bj/dV)local dX=cf*dW;local bs=i(dV*br)local bl;return function()if ad==nil then return nil end;local d_=_+dX;local bD={{}}if not dF then for X=2,bZ do bD[X]={}end end;local e0=0;if dU and d_>#ad then V=V+_-1;d_=d_-_+1;ad=v(ad,_)_=1;while#ad3000 then r("nosleep")repeat until"nosleep"==s()aP=q"utc"end;if _+bZ*4>#ad then break end;local an={}for X=1,bZ do an[X]={}end;if bl then for X=1,bZ do for aR=1,#bl[X]do an[aR-#bl[X]-1]=bl[X][aR]end end end;local c9,ca,cc={},{},{}for X=1,bZ do c9[X],ca[X]=u("=8 then c9[aR]=Z(c9[aR]-ce,-32768,32767)else c9[aR]=Z(c9[aR]+ce,-32768,32767)end;an[aR][bU+a7]=c9[aR]/(c9[aR]<0 and 128 or 127)end end end;bl=an;if#an[1]3000 then aP=q"utc"sleep(0)end end;bl={}for aR=1,bZ do bl[aR]={}for X=1,L do bl[aR][X]=bD[aR][#bD[aR]-30+X]end end;local bs=i(#bD[1]*br)local e4={}for aR=1,bZ do e4[aR]={}end;for X=1,bs do local ae=(X-1)/br+1;local K={}if ae%1==0 then for aR=1,bZ do K[aR]=bD[aR][ae]end else for aR=1,bZ do K[aR]=bt(bD[aR],ae)end end;if dF then local _=0;for aR=1,bZ do _=_+K[aR]end;e4[1][X]=Z(i(_/bZ),-128,127)else for aR=1,bZ do e4[aR][X]=Z(i(K[aR]),-128,127)end end end;return e4,(e3-1)/bj/bZ end,not dU and#dT/bj/bZ or nil end;function C.stream.wav(ad,dF,e5)local dh;if type(ad)=="function"then dh,ad=ad,ad()end;a(1,ad,"string")local bZ,bj,bQ,bn,bR,cf,cg;local aB,V=u("c4",ad)if aB~="RIFF"then error("bad argument #1 (not a WAV file)",2)end;V=V+4;aB,V=u("c4",ad,V)if aB~="WAVE"then error("bad argument #1 (not a WAV file)",2)end;while V<=#ad do local cS;aB,V=u("c4",ad,V)cS,V=u("0 then cg={{},{}}for X=1,cU do cg[1][X],cg[2][X]=u("I",ad,V)if aB=="COMM"then local cI,cH;bZ,bn,bQ,cI,cH,V=u(">hIhHI7x",ad,V)if d3 then local b8;d2,b8,V=u(">c4s1",ad,V)if#b8%2==0 then V=V+1 end end;bn=bn*bZ*i(bQ/8)local b8=g(cI,0x8000)cI=(c(cI,0x7FFF)-0x3FFE)%0x800;bj=math.ldexp(cH*(b8 and-1 or 1)/0x100000000000000,cI)elseif aB=="SSND"then d1,cf,V=u(">II",ad,V)local ad=v(ad,V+d1,V+d1+bn-1)if not dh and#adI",_)return v(an,bU+d1)else return an end else return dh()end end end;if d2==nil or d2=="NONE"then return C.stream.pcm(ad,bQ,"signed",bZ,bj,true,dF),bn/bZ/(bQ/8)/bj elseif d2=="sowt"then return C.stream.pcm(ad,bQ,"signed",bZ,bj,true,dF),bn/bZ/(bQ/8)/bj elseif d2=="fl32"or d2=="FL32"then return C.stream.pcm(ad,32,"float",bZ,bj,true,dF),bn/bZ/4/bj elseif d2=="alaw"or d2=="ulaw"or d2=="ALAW"or d2=="ULAW"then return C.stream.g711(ad,d2=="ulaw"or d2=="ULAW",bZ,bj,dF),bn/bZ/bj else error("Unsupported compression scheme "..d2,2)end;return C.stream.pcm(ad,bQ,"signed",bZ,bj,true,dF),bn/bZ/(bQ/8)/bj else V=V+cS end end;error("invalid AIFF file",2)end;function C.stream.au(ad,dF,e5)local dh;if type(ad)=="function"then dh,ad=ad,ad()end;a(1,ad,"string")a(2,dF,"boolean","nil")local d4,d1,cS,d5,bj,bZ=u(">c4IIIII",ad)if d4~=".snd"then error("invalid AU file",2)end;if dh then local e6,c7=v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil),nil;ad=function()if e6 then c7,e6=e6;return c7 elseif e5 then local an=dh()if not an then return nil end;if an:match"^.snd"then return v(an,u(">I",v(an,5,8)),nil)else return an end else return dh()end end else ad=v(ad,d1,cS~=0xFFFFFFFF and d1+cS-1 or nil)end;if d5==1 then return C.stream.g711(ad,true,bZ,bj,dF),cS/bZ/bj elseif d5==2 then return C.stream.pcm(ad,8,"signed",bZ,bj,true,dF),cS/bZ/bj elseif d5==3 then return C.stream.pcm(ad,16,"signed",bZ,bj,true,dF),cS/bZ/2/bj elseif d5==4 then return C.stream.pcm(ad,24,"signed",bZ,bj,true,dF),cS/bZ/3/bj elseif d5==5 then return C.stream.pcm(ad,32,"signed",bZ,bj,true,dF),cS/bZ/4/bj elseif d5==6 then return C.stream.pcm(ad,32,"float",bZ,bj,true,dF),cS/bZ/4/bj elseif d5==27 then return C.stream.g711(ad,false,bZ,bj,dF),cS/bZ/bj else error("unsupported encoding type "..d5,2)end end;function C.stream.flac(ad,dF)a(1,ad,"string","function")a(2,dF,"boolean","nil")local e7=false;if type(ad)=="function"then ad=setmetatable({str="",fn=ad,final=false,byte=function(self,aP,cI)while aP>#self.str do e7=true;local an=self.fn()e7=false;if not an then self.final=true;return nil end;self.str=self.str..an end;while cI and cI>#self.str do e7=true;local an=self.fn()e7=false;if not an then self.final=true;return nil end;self.str=self.str..an end;return w(self.str,aP,cI)end},{__len=function(self)return self.final and#self.str or math.huge end})end;local function e8(e9,...)local ea=y(coroutine.resume(e9,...))while ea[1]and e7 do ea=y(coroutine.resume(e9,coroutine.yield(z(ea,2,ea.n))))end;return z(ea,1,ea.n)end;local e9=coroutine.create(av)local dl,bj,bC=e8(e9,ad,coroutine.yield)if not dl then error(bj,2)end;local V=0;local br=48000/bj;local dG=1-math.exp(-(bj/96000)*2*p)local bt=ac[C.defaultInterpolation]local bl={0,0}return function()if coroutine.status(e9)=="dead"then return nil end;local cT={{}}while#cT[1]3000 then aP=q"utc"sleep(0)end;b5[X]=Z(b5[X]*ed,-1,1)end end;return cJ end;function C.effects.speed(cJ,ed)a2(1,cJ)a(2,ed,"number")if ed==1 then return cJ end;local ee=cJ.sampleRate;cJ.sampleRate=cJ.sampleRate*ed;local bq=cJ:resample(ee)cJ.sampleRate,cJ.data=ee,bq.data;return cJ end;function C.effects.fade(cJ,ef,eg,eh,ei)a2(1,cJ)a(2,ef,"number")a(3,eg,"number")a(4,eh,"number")a(5,ei,"number")if eg==1 and ei==1 then return cJ end;local ej=q"utc"for K=1,#cJ.data do local b5=cJ.data[K]local aP=ef*cJ.sampleRate;local cH=(ei-eg)/((eh-ef)*cJ.sampleRate)for X=aP,eh*cJ.sampleRate do if q"utc"-ej>5000 then ej=q"utc"sleep(0)end;b5[X]=Z(b5[X]*(cH*(X-aP)+eg),-1,1)end end;return cJ end;function C.effects.invert(cJ)a2(1,cJ)for K=1,#cJ.data do local b5=cJ.data[K]for X=1,#b5 do b5[X]=-b5[X]end end;return cJ end;function C.effects.normalize(cJ,ek,el)a2(1,cJ)ek=a(2,ek,"number","nil")or 1;a(3,el,"boolean","nil")local em;local aP,bj=q"utc",cJ.sampleRate;if not el then local a1=0;for K=1,#cJ.data do local b5=cJ.data[K]if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;for X=1,#b5 do a1=o(a1,l(b5[X]))end end;em=ek/a1 end;for K=1,#cJ.data do local b5=cJ.data[K]if el then local a1=0;for X=1,#b5 do a1=o(a1,l(b5[X]))end;em=ek/a1 end;if q"utc"-aP>3000 then aP=q"utc"sleep(0)end;for X=1,#b5 do b5[X]=Z(b5[X]*em,-1,1)end end;return cJ end;function C.effects.center(cJ)a2(1,cJ)for K=1,#cJ.data do local b5=cJ.data[K]for X=0,#b5-1,cJ.sampleRate do local en=0;local bz=n(#b5-X,cJ.sampleRate)for aR=1,bz do en=en+b5[X+aR]end;en=en/bz;for aR=1,bz do b5[X+aR]=Z(b5[X+aR]-en,-1,1)end end end;return cJ end;function C.effects.trim(cJ,eo)a2(1,cJ)eo=a(2,eo,"number","nil")or 1/65536;local b8,cI;for X=1,#cJ.data[1]do for K=1,#cJ.data do if l(cJ.data[K][X])>eo then b8=X;break end end;if b8 then break end end;for X=#cJ.data[1],1,-1 do for K=1,#cJ.data do if l(cJ.data[K][X])>eo then cI=X;break end end;if cI then break end end;local bq=v(cJ,b8/cJ.sampleRate,cI/cJ.sampleRate)cJ.data=bq.data;return cJ end;function C.effects.delay(cJ,ep,ed)a2(1,cJ)a(2,ep,"number")ed=a(3,ed,"number","nil")or 0.5;local eq=i(ep*cJ.sampleRate)for K=1,#cJ.data do local er={}local es=cJ.data[K]for X=1,#es do er[X]=es[X]end;for X=eq+1,#es do es[X]=Z(es[X]+er[X-eq]*ed,-1,1)end end;return cJ end;function C.effects.echo(cJ,ep,ed)a2(1,cJ)ep=a(2,ep,"number","nil")or 1;ed=a(3,ed,"number","nil")or 0.5;local eq=i(ep*cJ.sampleRate)for K=1,#cJ.data do local es=cJ.data[K]for X=eq+1,#es do es[X]=Z(es[X]+es[X-eq]*ed,-1,1)end end;return cJ end;local et={0,-11.73,19.31,-7.97}local eu={0,0.1313,0.2743,0.31}function C.effects.reverb(cJ,ep,ev,ew,ex)a2(1,cJ)ep=a(2,ep,"number","nil")or 100;ev=a(3,ev,"number","nil")or 0.3;ew=a(4,ew,"number","nil")or 1;ex=a(5,ex,"number","nil")or 0;for K=1,#cJ.data do local al={}local es=cJ.data[K]for _=1,4 do local ey={}local eq=i((ep+et[_])/1000*cJ.sampleRate)local ed=ev-eu[_]for X=1,n(eq,#es)do ey[X]=es[X]al[X]=(al[X]or 0)+es[X]end;for X=eq+1,#es do local b8=es[X]+ey[X-eq]*ed;ey[X]=b8;al[X]=(al[X]or 0)+b8 end end;for X=1,#al do al[X]=al[X]*ew+es[X]*ex end;local eq=i(0.08927*cJ.sampleRate)al[eq+1]=al[eq+1]-0.131*al[1]for X=eq+2,#al do al[X]=al[X]-0.131*al[X-eq]+0.131*al[X+20-eq]end;es[eq+1]=Z(al[eq+1]-0.131*al[1],-1,1)for X=eq+2,#al do es[X]=Z(al[X]-0.131*al[X-eq]+0.131*al[X+20-eq],-1,1)end end;return cJ end;function C.effects.lowpass(cJ,d7)a2(1,cJ)a(2,d7,"number")local bA=1-math.exp(-(d7/cJ.sampleRate)*2*p)for K=1,#cJ.data do local an=cJ.data[K]for X=2,#an do local bz=an[X-1]an[X]=bz+bA*(an[X]-bz)end end;return cJ end;function C.effects.highpass(cJ,d7)a2(1,cJ)a(2,d7,"number")local bA=1/(2*p*d7/cJ.sampleRate+1)for K=1,#cJ.data do local an=cJ.data[K]local ez=an[1]for X=2,#an do local eA=an[X]an[X]=bA*(an[X-1]+eA-ez)ez=eA end end;return cJ end;return C