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.

Music looping

Is it possible to make a music file loop from somewhere in the midst of the file while keeping the start,

For example,

1          2                                            3
|-------|--------------------------------|

The song begins at 1, plays to 2 which carries on until 3 and then, rather than looping back to 1, it loops to 2.

I did see this in someones request, which I attempted, but the only way I ould think of it working would be to have 1-2 as a seperate ME.

Is there any method, program or file extension I can use to keep the file as a whole and retain the looping style above?

I would greatly appreciate it if anyone knew how, I have tried and failed again and again, but I don't know what to do.

Thanks in advance, to anyone who know how I can do this.
 
XP: Only MIDIs can do this in XP.  The MIDI synthesizer that XP uses has a special control value reserved to a loop point.  This is the "b06f value 111" event.  You can add the loop event with any MIDI editing program that lets you manually add in events (ie Anvil)

VX: VX supports the MIDI looping that XP does, but it also supports looping Ogg Vorbis files.  .ogg files are like the free version of MP3 files, so there's the bonus of no royalties to use them.  Looping an .ogg file is more complicated than looping a MIDI, so I made a tutorial: http://rmxp.org/forums/index.php?topic=46547.0


Hope that helps you
 
Hey, don't give up.  I found a script that does exactly what you want!

For instructions on how to use this script & a demo, go to the original post.
Code:
#==============================================================================
# Modified Audio MP3 loop v1.0 for RMXP
# Original Script by Cogwheel (http://members.jcom.home.ne.jp/cogwheel/)
# Must provide credit to GubiD and Cogwheel for this script.
#==============================================================================


#==============================================================================
# â–  RPG
#------------------------------------------------------------------------------
# Module RPG(The Game)
#==============================================================================

module RPG
#==============================================================================
# â–  BGM
#------------------------------------------------------------------------------
# BGM Audio File additional methods
#==============================================================================

  class AudioFile
    #--------------------------------------------------------------------------
    # ● Play audio file
    #--------------------------------------------------------------------------
    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 "whatever song"
          type = "Dungeon"
          start = 0
          loop = 6704
          fin = 97109
        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 の演奏停止
    #--------------------------------------------------------------------------
    def stop
      $game_audio.bgm_stop
    end
    #--------------------------------------------------------------------------
    # ● BGM のフェードアウト
    #--------------------------------------------------------------------------
    def fade(time)
      $game_audio.fadeout_set(time)  
    end
  end
end

class Game_Audio
  #--------------------------------------------------------------------------
  # ● Initialize method
  #--------------------------------------------------------------------------
  def initialize
    @play = Win32API.new("winmm.dll", "mciSendString", ["p","p","l","l"], "i")
    @play.call("Close all", "\\" * 255, 255, 0)
    @type = "Normal"
    @me_count = 0
    fadein_set(0)
  end
  #--------------------------------------------------------------------------
  # ● BGM の演奏
  #     name       : BGM filename
  #     vol        : volume
  #     pitch      : tone level
  #     type       : battle/dungeon/normal, dungeon pauses when battle is called
  #     start      : start position
  #     loop       : loop position
  #     fin        : finish position
  #--------------------------------------------------------------------------
  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 = "\\" * 255
      @play.call("Status " + type + " mode", status, 255, 0)
      if status.unpack("A*")[0] == "paused"
        @play.call("Setaudio " + type + " volume to 0", "\\" * 255, 255, 0)
        @play.call("Play " + type, "\\" * 255, 255, 0)
        fadein_set(1000)
      else
        @play.call("Open \"" + name + "\" alias " + type + " type MPEGVideo",
          "\\" * 255, 255, 0)
        @play.call("Setaudio " + type + " volume to " +
          (vol ** 2 / 10).to_s, "\\" * 255, 255, 0)
        @play.call("Play " + type + " from " +
          start.to_s + " to " + fin.to_s, "\\" * 255, 255, 0)
      end
      if @me_count > 0
        fadein_set(0)
        @play.call("Setaudio " + type + " volume to 0", "\\" * 255, 255, 0)
      end
    else
      if @me_count > 0
        @play.call("Setaudio " + type + " volume to 0", "\\" * 255, 255, 0)
      else
        @play.call("Setaudio " + type + " volume to " +
          (vol ** 2 / 10).to_s, "\\" * 255, 255, 0)
      end
    end
    @name = name
    @type = type
    @vol = vol
    @start = start
    @loop = loop
    @fin = fin
  end
  #--------------------------------------------------------------------------
  # ● get filename extension
  #--------------------------------------------------------------------------
  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
  #--------------------------------------------------------------------------
  # ● fadein time
  #--------------------------------------------------------------------------
  def fadein(time)
    fadein_set(time * 1000)
  end
  #--------------------------------------------------------------------------
  # ● fadein Set counter
  #--------------------------------------------------------------------------
  def fadein_set(time)
    @fade_max = @fade_count = time * Graphics.frame_rate / 1000
  end
  #--------------------------------------------------------------------------
  # ● Fadeout time
  #--------------------------------------------------------------------------
  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
  #--------------------------------------------------------------------------
  # ● stop BGM
  #--------------------------------------------------------------------------
  def bgm_stop(type = "")
    if @type == "Dungeon" and type == "Battle"
      @play.call("Pause " + @type, "\\" * 255, 255, 0)
    else
      if @type == "Normal"
        Audio.bgm_stop
      else
        @play.call("Stop " + @type, "\\" * 255, 255, 0)
        @play.call("Close " + @type, "\\" * 255, 255, 0)
      end
    end
    @type = "Normal"
  end
  
  #--------------------------------------------------------------------------
  # ● Update process
  #--------------------------------------------------------------------------
  def update
    if @type != "Normal"
      code = "\\" * 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, "\\" * 255, 255, 0)
        @play.call("Play " + @type + " to " + @fin.to_s, "\\" * 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,
          "\\" * 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,
          "\\" * 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

#==============================================================================
# â–  initialize new audio module
#==============================================================================

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

#==============================================================================
# â–  Graphics
#------------------------------------------------------------------------------
# When graphics are updated.. update audio.
#==============================================================================

class << Graphics
  alias :update_audio :update unless method_defined?("update_audio")
  def update
    update_audio
    $game_audio.update
  end
end
#==============================================================================
# â–  System
#------------------------------------------------------------------------------
# Update bgm_play and bgm_Stop methods to use new audio module.
#==============================================================================

class Game_System
  def bgm_play(bgm)
    bgm.play unless bgm == nil
    if bgm == nil
      bgm_stop
    end
    @playing_bgm = bgm
  end
  def bgm_stop
    @playing_bgm.stop unless @playing_bgm == nil
    @playing_bgm = nil
    Audio.bgm_stop
  end
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