Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Getting SE play length

Wichu

Member

Is there any way to get the play length of a SE, or other .wav file? I want to get one to loop; however, the SE file is different depending on a variable. Since there are hundreds of different SE files playable, making an array with their lengths is not an option.
 

Wichu

Member

Thanks. Unfortunately, I'd rather not use that, since it doesn't support MIDI looping by default like RMXP's Audio module (i.e. using control 111 to loop). However, it's given me an idea; I'll use the FMOD Ex script to record the play lengths of each one in a file, then add the contents of the file as a constant in my game.

EDIT: Looking through poccil's Pokémon starter kit, I found an interesting script.
Code:
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
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top