This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given.
Introduction
Ever want to have better audio but to still have loop functionality? This script will allow you to configure by song name a start/begin loop/end loop position that will allow you to achive looping regardless the file format. Be it MID, MP3, OGG, or WAV.
The Script
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.
#==============================================================================
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 "035-Dungeon01"
type = "Dungeon"
start = 0
loop = 2000 #restart location
fin = 10000 #return to loop when
when "003-Battle03"
type = "Battle"
start = 0
loop = 1000
fin = 15000
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
Instructions
At line 35 you start "case @name", each when statement below there should contain the file name of the song you are trying to play. (Note: All song files must be in the Audio\BGM folder!)
When a song is specified "When 035-Dungeon01", then you can define 4 things: type, start, loop, fin
Type:
Type is the type of song it is. This generally doesn't matter UNLESS you are calling a "battle" during a "dungeon". When a "Battle" is called from a "Dungeon", the "Dungeon" is paused and will later be resumed when called again.
Start:
This is the location to start the song. If you want to skip the first 10 seconds, then you are more than welcome to do so.
Loop:
This will be the location in which the song resumes when FIN in reached.
Fin:
This is the "end of song" or "end of loop". When reached, this will "rewind" the song to "loop" position. If this position is exceeded because the game is inactive, it will loop when the song is complete.
If there is not a "When *" statement for the song, then it will use the default audio system and looping. Also be sure that file names specified here must contain no SPACES: 106 IGNITION = BAD, 106_IGNITION = Good
Also note that if your Windows Media Player supports the audio type, then you can update this script to use that audio type as well. Just update the "def name?" method to search for that file extension.
Note:
All time measurements are done in 1/1000 sec, so 1000 would be 1 second. Whenever the song "rewinds" it has about a .2 second rewind time.