def getOggPage(file)
pos=file.pos
dw=file.fgetdw
return nil if dw!=0x5367674F
header=file.read(22)
bodysize=0
hdrbodysize=file.fgetb
hdrbodysize.times do
bodysize+=file.fgetb
end
ret=[header,file.pos,bodysize,file.pos+bodysize]
return ret
end
def oggfiletime(file)
pages=[]
page=nil
begin
page=getOggPage(file)
if page
pages.push(page)
file.pos=page[3]
end
end while page
if pages.length==0
return -1
end
curserial=nil
i=-1
pcmlengths=[]
rates=[]
for page in pages
header=page[0]
serial=header[10,4].unpack("V")
frame=header[2,8].unpack("C*")
frameno=frame[7]
frameno=(frameno<<8)|frame[6]
frameno=(frameno<<8)|frame[5]
frameno=(frameno<<8)|frame[4]
frameno=(frameno<<8)|frame[3]
frameno=(frameno<<8)|frame[2]
frameno=(frameno<<8)|frame[1]
frameno=(frameno<<8)|frame[0]
if serial!=curserial
curserial=serial
file.pos=page[1]
packtype=file.fgetb
string=file.read(6)
if string!="vorbis"
return -1
end
if packtype!=1
return -1
end
i+=1
version=file.fgetdw
if version!=0
return -1
end
channels=file.fgetb
rates[i]=file.fgetdw
end
pcmlengths[i]=frameno
end
ret=0.0
for i in 0...pcmlengths.length
ret+=pcmlengths[i]*1.0/rates[i]
end
return ret
end
def getPlayTime(filename)
time=-1
File.open(filename,"rb"){|file|
file.pos=0
fdw=file.fgetdw
if fdw==0x46464952 # "RIFF"
filesize=file.fgetdw
wave=file.fgetdw
if wave!=0x45564157 # "WAVE"
return -1
end
fmt=file.fgetdw
if fmt!=0x20746d66 # "fmt "
return -1
end
fmtsize=file.fgetdw
format=file.fgetw
channels=file.fgetw
rate=file.fgetdw
bytessec=file.fgetdw
if bytessec==0
return -1
end
bytessample=file.fgetw
bitssample=file.fgetw
data=file.fgetdw
if data!=0x61746164 # "data"
return -1
end
datasize=file.fgetdw
time=(datasize*1.0)/bytessec
elsif fdw==0x5367674F # "OggS"
file.pos=0
time=oggfiletime(file)
end
}
return time
end