Main Menu
  • Welcome to The RPG Maker Resource Kit.

[Request][Translation] True mp3 looping

Started by MrMoo, August 07, 2008, 10:03:45 PM

0 Members and 1 Guest are viewing this topic.

MrMoo

True mp3 looping
Aug 07, 2008




Summary
As most people are aware, mp3s tend to have a "pause" right before looping, and for battle music with an intro sequence that isn't supposed to be repeated, this script is necessary.
A while ago I found a Japanese script which does exactly this using the exact "Frame" of which the song starts and ends its looping. I have posted this script below and hopefully it can be used as a guideline for the English translation.

Features Desired

  • Ability to play from the start of the song, to a selected end, and reloop to a selected beggining.
  • All looping will be done with frames rather than seconds for maximum accuracy
  • All loop editing will be done with file name, so it may apply to all music used in the game.
  • PERFECT LOOPS, so no pauses.




Did you search?
Yes

Where did you search?

  • Google
  • Random japanese vx script sites
  • RMRK, RPGR, hbgames.org, etc

What did you search for?

  • VX Mp3 Looping
  • VX Music Looping
  • VX Battle Theme Looping, etc




Link to the script summary: Click Here (Translated)

Code (Perfect MP3 Looping by Cogwheel) Select
# MP3ƒ‹[ƒv‰‰'t Ver VX_1.00
# "z•zŒ³EƒTƒ|[ƒgURL
# http://members.jcom.home.ne.jp/cogwheel/

#==============================================================================
# ¡ RPG
#------------------------------------------------------------------------------
# @RPG'S'Ì,̃,ƒWƒ...[ƒ‹,Å,·B
#==============================================================================

module RPG
#==============================================================================
# ¡ BGM
#------------------------------------------------------------------------------
# @BGM ,ðŠÇ—E§Œä,·,éƒNƒ‰ƒX,Å,·B
#==============================================================================

  class BGM < AudioFile
    @@last = BGM.new
    #--------------------------------------------------------------------------
    # œ BGM ,̉‰'t
    #--------------------------------------------------------------------------
    def play
      if @name.empty?
        Audio.bgm_stop
        @@last = BGM.new
      else
        name = "Audio/BGM/"
        start = 0
        loop = 0
        step = [0, 0]
        fin = 0
        case @name
        when "satoc-boss09-2"
          type = "Battle"
          start = 0
          loop = 96040
          fin = 202680
        when "satoc-town07-2"
          type = "Dungeon"
          start = 0
          loop = 31980
          fin = 100000
        else
          type = "Normal"
        end
        if type == "Normal"
          name = @name
        else
          name = $game_audio.name?("Audio/BGM/" + @name)
        end
        $game_audio.bgm_play(name, @volume, @pitch, type, start, loop, fin)
        @@last = self
      end
    end
    #--------------------------------------------------------------------------
    # œ BGM ,̉‰'t'âŽ~
    #--------------------------------------------------------------------------
    def self.stop
      $game_audio.bgm_stop
      @@last = BGM.new
    end
    #--------------------------------------------------------------------------
    # œ BGM ,̃tƒF[ƒhƒAƒEƒg
    #--------------------------------------------------------------------------
    def self.fade(time)
      $game_audio.fadeout_set(time)
      @@last = BGM.new
    end
  end

#==============================================================================
# ¡ ME
#------------------------------------------------------------------------------
# @ME ,ðŠÇ—E§Œä,·,éƒNƒ‰ƒX,Å,·B
#==============================================================================

  class ME < AudioFile
    #--------------------------------------------------------------------------
    # œ ME ,̉‰'t
    #--------------------------------------------------------------------------
    def play
      if @name.empty?
        Audio.me_stop
      else
        Audio.me_play("Audio/ME/" + @name, @volume, @pitch)
      end
      $game_audio.me_set("Audio/ME/" + @name)
    end
    #--------------------------------------------------------------------------
    # œ ME ,̉‰'t'âŽ~
    #--------------------------------------------------------------------------
    def self.stop
      Audio.me_stop
      $game_audio.mecount_set(1)
    end
    #--------------------------------------------------------------------------
    # œ ME ,̃tƒF[ƒhƒAƒEƒg
    #--------------------------------------------------------------------------
    def self.fade(time)
      Audio.me_fade(time)
      $game_audio.mecount_set(time * Graphics.frame_rate / 1000)
    end
  end
end

#==============================================================================
# ¡ Scene_Map
#------------------------------------------------------------------------------
# @ƒ}ƒbƒv‰æ–Ê,̏ˆ—,ðs,¤ƒNƒ‰ƒX,Å,·B
#==============================================================================

class Scene_Map < Scene_Base
  #--------------------------------------------------------------------------
  # œ ƒoƒgƒ‹‰æ–Ê,Ö,̐Ø,è'Ö,¦
  #--------------------------------------------------------------------------
  def call_battle
    @spriteset.update
    Graphics.update
    $game_player.make_encounter_count
    $game_player.straighten
    $game_temp.map_bgm = RPG::BGM.last
    $game_temp.map_bgs = RPG::BGS.last
    # RPG::BGM.stop
    RPG::BGS.stop
    Sound.play_battle_start
    $game_system.battle_bgm.play
    $game_temp.next_scene = nil
    $scene = Scene_Battle.new
  end
end

#==============================================================================
# ¡ Game_Audio
#------------------------------------------------------------------------------
# @MP3,̉‰'tE,»,Ì'¼BGM,̉‰'t'S"Ê,ðŽi,éƒNƒ‰ƒX,Å,·B
# MP3,Ì"r'†ƒ‹[ƒv"™,É'Ήž,µ,Ü,·B
#==============================================================================

class Game_Audio
  #--------------------------------------------------------------------------
  # œ ƒIƒuƒWƒFƒNƒg‰Šú‰»
  #--------------------------------------------------------------------------
  def initialize
    @play = Win32API.new("winmm.dll", "mciSendString", ["p","p","l","l"], "i")
    @play.call("Close all", "\0" * 255, 255, 0)
    @type = "Normal"
    @me_count = 0
    fadein_set(0)
  end
  #--------------------------------------------------------------------------
  # œ BGM ,̉‰'t
  #     name       : BGM ƒl[ƒ€
  #     vol        : ƒ{ƒŠƒ...[ƒ€
  #     pitch      : ƒsƒbƒ`
  #     type       : BGM ƒ^ƒCƒv
  #     start      : ‰‰'tŠJŽnˆÊ'u
  #     loop       : ‰‰'tƒ‹[ƒvŽžŠJŽnˆÊ'u
  #     fin        : ‰‰'tƒ‹[ƒvI—¹ˆÊ'u
  #--------------------------------------------------------------------------
  def bgm_play(name, vol, pitch, type, start, loop, fin)
    fadein_set(0)
    if type == "Normal"
      bgm_stop(type) if @name != name
      Audio.bgm_play("Audio/BGM/" + name, vol, 100) if name != ""
    elsif @name != name
      bgm_stop(type)
      status = "\0" * 255
      @play.call("Status " + type + " mode", status, 255, 0)
      if status.unpack("A*")[0] == "paused"
        @play.call("Setaudio " + type + " volume to 0", "\0" * 255, 255, 0)
        @play.call("Play " + type, "\0" * 255, 255, 0)
        fadein_set(1000)
      else
        @play.call("Open " + name + " alias " + type + " type MPEGVideo",
          "\0" * 255, 255, 0)
        @play.call("Setaudio " + type + " volume to " +
          (vol ** 2 / 10).to_s, "\0" * 255, 255, 0)
        @play.call("Play " + type + " from " +
          start.to_s + " to " + fin.to_s, "\0" * 255, 255, 0)
      end
      if @me_count > 0
        fadein_set(0)
        @play.call("Setaudio " + type + " volume to 0", "\0" * 255, 255, 0)
      end
    else
      if @me_count > 0
        @play.call("Setaudio " + type + " volume to 0", "\0" * 255, 255, 0)
      else
        @play.call("Setaudio " + type + " volume to " +
          (vol ** 2 / 10).to_s, "\0" * 255, 255, 0)
      end
    end
    @name = name
    @type = type
    @vol = vol
    @start = start
    @loop = loop
    @fin = fin
  end
  #--------------------------------------------------------------------------
  # œ ƒtƒ@ƒCƒ‹ƒl[ƒ€,ÌŽæ"¾
  #--------------------------------------------------------------------------
  def name?(name)
    if FileTest.exist?(name + ".mp3")
      return (name + ".mp3")
    elsif FileTest.exist?(name + ".ogg")
      return (name + ".ogg")
    elsif FileTest.exist?(name + ".wav")
      return (name + ".wav")
    elsif FileTest.exist?(name + ".mid")
      return (name + ".mid")
    end
    return "name"
  end
  #--------------------------------------------------------------------------
  # œ ƒtƒF[ƒhƒCƒ"
  #--------------------------------------------------------------------------
  def fadein(time)
    fadein_set(time * 1000)
  end
  #--------------------------------------------------------------------------
  # œ ƒtƒF[ƒhƒCƒ",̐Ý'è
  #--------------------------------------------------------------------------
  def fadein_set(time)
    @fade_max = @fade_count = time * Graphics.frame_rate / 1000
  end
  #--------------------------------------------------------------------------
  # œ ƒtƒF[ƒhƒAƒEƒg,̐Ý'è
  #--------------------------------------------------------------------------
  def fadeout_set(time)
    if @type == "Normal"
      Audio.bgm_fade(time)
    else
      if @fade_count == 0
        @fade_max = time * Graphics.frame_rate / 1000
        @fade_count = - time * Graphics.frame_rate / 1000
      elsif @fade_count > 0
        @fade_max = (time * @fade_max * Graphics.frame_rate) /
          (1000 * (@fade_max - @fade_count))
        @fade_count = - time * Graphics.frame_rate / 1000
      else
        @fade_max = - (time * @fade_max * Graphics.frame_rate) /
          (1000 * @fade_count)
        @fade_count = - time * Graphics.frame_rate / 1000
      end
    end
  end
  #--------------------------------------------------------------------------
  # œ ‰‰'t,Ì'âŽ~
  #--------------------------------------------------------------------------
  def bgm_stop(type = "")
    if @type == "Dungeon" and type == "Battle"
      @play.call("Pause " + @type, "\0" * 255, 255, 0)
    else
      if @type == "Normal"
        Audio.bgm_stop
      else
        @play.call("Stop " + @type, "\0" * 255, 255, 0)
        @play.call("Close " + @type, "\0" * 255, 255, 0)
      end
    end
    @type = "Normal"
  end
  #--------------------------------------------------------------------------
  # œ ME ‰‰'t,̐Ý'è
  #--------------------------------------------------------------------------
  def me_set(name)
    # Œ»Ý,ÌBGM,ª Normal ,Å,È,¢,Ì,È,ç
    if @type != "Normal"
      # BGM ‰‰'t,Ì'âŽ~
      @play.call("Setaudio " + @type + " volume to 0", "\0" * 255, 255, 0)
    end
    # ME ‰‰'t'·,ÌŽæ"¾
    @play.call("Open " + name?(name) + " alias me type MPEGVideo",
       "\0" * 255, 255, 0)
    length = "\0" * 255
    @play.call("Status me length", length, 255, 0)
    # ME ƒJƒEƒ"ƒg,̐Ý'è
    @me_count = length.unpack("A*")[0].to_i * 60 / 1000 - 90
  end
  #--------------------------------------------------------------------------
  # œ ME ‰‰'t,Ì'¼ÚŽw'è
  #--------------------------------------------------------------------------
  def mecount_set(time)
    @me_count = time
  end
  #--------------------------------------------------------------------------
  # œ ƒtƒŒ[ƒ€XV
  #--------------------------------------------------------------------------
  def update
    if @type != "Normal"
      code = "\0" * 255
      @play.call("Status " + @type + " position", code, 255, 0)
      if code.unpack("A*")[0].to_i >= @fin
        @play.call("Seek " + @type + " to " + @loop.to_s, "\0" * 255, 255, 0)
        @play.call("Play " + @type + " to " + @fin.to_s, "\0" * 255, 255, 0)
      end
      if @fade_count > 0
        @fade_count -= 1
        @play.call("Setaudio " + @type + " volume to " +
          ((@vol * (@fade_max - @fade_count) / @fade_max) ** 2 / 10).to_s,
          "\0" * 255, 255, 0)
      elsif @fade_count < 0
        @fade_count += 1
        @play.call("Setaudio " + @type + " volume to " +
          ((- @vol * @fade_count / @fade_max) ** 2 / 10).to_s,
          "\0" * 255, 255, 0)
      end
    end
    if @me_count > 0
      @me_count -= 1
      if @type != "Normal" and @me_count == 0
        fadein_set(1000)
      end
    end
  end
end

#==============================================================================
# ¡ ƒI[ƒfƒBƒIƒNƒ‰ƒX,Ì‹­§ì¬
#==============================================================================

if $game_audio == nil
  $game_audio = Game_Audio.new
end

#==============================================================================
# ¡ Graphics
#------------------------------------------------------------------------------
# @ƒOƒ‰ƒtƒBƒbƒN'S"Ê,̏ˆ—,ðs,¤ƒNƒ‰ƒX,Å,·B
#==============================================================================

class << Graphics
  #--------------------------------------------------------------------------
  # œ ƒtƒŒ[ƒ€XV
  #--------------------------------------------------------------------------
  alias :update_audio :update unless method_defined?("update_audio")
  def update
    update_audio
    $game_audio.update
  end
end



- -

modern algebra

Well, I can't really translate it, but I could probably take a look and write up some instructions if that's what you want. It sounds like you know how to use it though.

MrMoo

I sorta figured how it works by looking at the script and horribly translated instructions.
What I really want is either a full translation of this script, or a script made from scratch similar to this script but simpler to use. I know how it works but I still can't edit it myself at my current knowledge of RGSS2.
Some instructions would be great MA.



- -