module Audio
  class Mp3Data
    attr_accessor :title
    attr_accessor :artist
    attr_accessor :album
    attr_accessor :year
    attr_accessor :comments
    attr_accessor :genre
    attr_accessor :track
  end
  def initialize
    @title = ""
    [url=home.php?mod=space&uid=1953775]@artist[/url] = ""
    @album = ""
    @year = ""
    @comments = ""
    @genre = ""
    @track = ""
  end
  def self.get_playing_bgm_info_mp3_id3v2
    filename = "Audio/BGM/" + $game_system.playing_bgm.name + ".mp3"
    file = File.open(filename, "r")
    info = Mp3Data.new
    buffer = file.read(10)
    if buffer[0...3] == "ID3"
      size = buffer[6...10]
      total_size = (size[0]&0x7F) * 0x200000 + (size[1]&0x7F) * 0x4000 + (size[2]&0x7F) * 0x80 +(size[3]&0x7F)
      while total_size > 0
        buffer = file.read(10)
        break if buffer.nil?
        total_size -= 10
        tag = buffer[0...4]
        size = buffer[4...8]
        current_size = size[0] * 0x1000000 + size[1]* 0x10000 + size[2] * 0x100 + size[3]
        break if current_size == 0
        buffer = file.read(current_size)
        total_size -= current_size
        case tag
        when "TIT2"
          info.title = buffer.gsub(/[^0-9A-Za-z_ ]+/, "")
        when "TPE1"
          info.artist = buffer.gsub(/[^0-9A-Za-z_ ]+/, "")
        when "TYER"
          info.year = buffer.gsub(/[^0-9]+/, "")
        when "TRCK"
          info.track = buffer.gsub(/[^0-9\/]/, "")
        when "TALB"
          info.album = buffer.gsub(/[^0-9A-Za-z_ ]+/, "")
        when "TCON"
          info.genre = buffer.gsub(/[^0-9A-Za-z_ ]+/, "")
        when "COMM"
          info.comments = buffer.gsub(/[^0-9A-Za-z_ ]+/, "")
        end
      end
    end
    file.close
    return info
  end
end