True mp3 looping
Aug 07, 2008
SummaryAs 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?YesWhere 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)# 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‚̉‰‘tE‚»‚Ì‘¼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ƒ‹[ƒvI—¹ˆÊ’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ƒŒ[ƒ€XV
#--------------------------------------------------------------------------
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ƒŒ[ƒ€XV
#--------------------------------------------------------------------------
alias :update_audio :update unless method_defined?("update_audio")
def update
update_audio
$game_audio.update
end
end