mirror of
https://github.com/infinitefusion/infinitefusion-e18.git
synced 2025-12-11 07:04:59 +00:00
game data
This commit is contained in:
171
Data/Scripts_backup/008_Audio/001_Audio.rb
Normal file
171
Data/Scripts_backup/008_Audio/001_Audio.rb
Normal file
@@ -0,0 +1,171 @@
|
||||
#####################################
|
||||
# Needed because RGSS doesn't call at_exit procs on exit
|
||||
# Exit is not called when game is reset (using F12)
|
||||
$AtExitProcs=[] if !$AtExitProcs
|
||||
|
||||
def exit(code=0)
|
||||
for p in $AtExitProcs
|
||||
p.call
|
||||
end
|
||||
raise SystemExit.new(code)
|
||||
end
|
||||
|
||||
def at_exit(&block)
|
||||
$AtExitProcs.push(Proc.new(&block))
|
||||
end
|
||||
|
||||
#===============================================================================
|
||||
# Methods that determine the duration of an audio file.
|
||||
#===============================================================================
|
||||
def getOggPage(file)
|
||||
fgetdw = proc { |file|
|
||||
(file.eof? ? 0 : (file.read(4).unpack("V")[0] || 0))
|
||||
}
|
||||
dw = fgetdw.call(file)
|
||||
return nil if dw != 0x5367674F
|
||||
header = file.read(22)
|
||||
bodysize = 0
|
||||
hdrbodysize = (file.read(1)[0].ord rescue 0)
|
||||
hdrbodysize.times do
|
||||
bodysize += (file.read(1)[0].ord rescue 0)
|
||||
end
|
||||
ret = [header, file.pos, bodysize, file.pos + bodysize]
|
||||
return ret
|
||||
end
|
||||
|
||||
# internal function
|
||||
def oggfiletime(file)
|
||||
fgetdw = proc { |file|
|
||||
(file.eof? ? 0 : (file.read(4).unpack("V")[0] || 0))
|
||||
}
|
||||
pages = []
|
||||
page = nil
|
||||
loop do
|
||||
page = getOggPage(file)
|
||||
break if !page
|
||||
pages.push(page)
|
||||
file.pos = page[3]
|
||||
end
|
||||
return -1 if pages.length == 0
|
||||
curserial = nil
|
||||
i = -1
|
||||
pcmlengths = []
|
||||
rates = []
|
||||
for page in pages
|
||||
header = page[0]
|
||||
serial = header[10, 4].unpack("V")
|
||||
frame = header[2, 8].unpack("C*")
|
||||
frameno = frame[7]
|
||||
frameno = (frameno << 8) | frame[6]
|
||||
frameno = (frameno << 8) | frame[5]
|
||||
frameno = (frameno << 8) | frame[4]
|
||||
frameno = (frameno << 8) | frame[3]
|
||||
frameno = (frameno << 8) | frame[2]
|
||||
frameno = (frameno << 8) | frame[1]
|
||||
frameno = (frameno << 8) | frame[0]
|
||||
if serial != curserial
|
||||
curserial = serial
|
||||
file.pos = page[1]
|
||||
packtype = (file.read(1)[0].ord rescue 0)
|
||||
string = file.read(6)
|
||||
return -1 if string != "vorbis"
|
||||
return -1 if packtype != 1
|
||||
i += 1
|
||||
version = fgetdw.call(file)
|
||||
return -1 if version != 0
|
||||
rates[i] = fgetdw.call(file)
|
||||
end
|
||||
pcmlengths[i] = frameno
|
||||
end
|
||||
ret = 0.0
|
||||
for i in 0...pcmlengths.length
|
||||
ret += pcmlengths[i].to_f / rates[i].to_f
|
||||
end
|
||||
return ret * 256.0
|
||||
end
|
||||
|
||||
# Gets the length of an audio file in seconds. Supports WAV, MP3, and OGG files.
|
||||
def getPlayTime(filename)
|
||||
if safeExists?(filename)
|
||||
return [getPlayTime2(filename), 0].max
|
||||
elsif safeExists?(filename + ".wav")
|
||||
return [getPlayTime2(filename + ".wav"), 0].max
|
||||
elsif safeExists?(filename + ".mp3")
|
||||
return [getPlayTime2(filename + ".mp3"), 0].max
|
||||
elsif safeExists?(filename + ".ogg")
|
||||
return [getPlayTime2(filename + ".ogg"), 0].max
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
def getPlayTime2(filename)
|
||||
return -1 if !safeExists?(filename)
|
||||
time = -1
|
||||
fgetdw = proc { |file|
|
||||
(file.eof? ? 0 : (file.read(4).unpack("V")[0] || 0))
|
||||
}
|
||||
fgetw = proc { |file|
|
||||
(file.eof? ? 0 : (file.read(2).unpack("v")[0] || 0))
|
||||
}
|
||||
File.open(filename, "rb") { |file|
|
||||
file.pos = 0
|
||||
fdw = fgetdw.call(file)
|
||||
if fdw == 0x46464952 # "RIFF"
|
||||
filesize = fgetdw.call(file)
|
||||
wave = fgetdw.call(file)
|
||||
return -1 if wave != 0x45564157 # "WAVE"
|
||||
fmt = fgetdw.call(file)
|
||||
return -1 if fmt != 0x20746d66 # "fmt "
|
||||
fmtsize = fgetdw.call(file)
|
||||
format = fgetw.call(file)
|
||||
channels = fgetw.call(file)
|
||||
rate = fgetdw.call(file)
|
||||
bytessec = fgetdw.call(file)
|
||||
return -1 if bytessec == 0
|
||||
bytessample = fgetw.call(file)
|
||||
bitssample = fgetw.call(file)
|
||||
data = fgetdw.call(file)
|
||||
return -1 if data != 0x61746164 # "data"
|
||||
datasize = fgetdw.call(file)
|
||||
time = (datasize*1.0)/bytessec
|
||||
return time
|
||||
elsif fdw == 0x5367674F # "OggS"
|
||||
file.pos = 0
|
||||
time = oggfiletime(file)
|
||||
return time
|
||||
end
|
||||
file.pos = 0
|
||||
# Find the length of an MP3 file
|
||||
while true
|
||||
rstr = ""
|
||||
ateof = false
|
||||
while !file.eof?
|
||||
if (file.read(1)[0] rescue 0) == 0xFF
|
||||
begin
|
||||
rstr = file.read(3)
|
||||
rescue
|
||||
ateof = true
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
break if ateof || !rstr || rstr.length != 3
|
||||
if rstr[0] == 0xFB
|
||||
t = rstr[1] >> 4
|
||||
next if t == 0 || t == 15
|
||||
freqs = [44100, 22050, 11025, 48000]
|
||||
bitrates = [32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320]
|
||||
bitrate = bitrates[t]
|
||||
t = (rstr[1] >> 2) & 3
|
||||
freq = freqs[t]
|
||||
t = (rstr[1] >> 1) & 1
|
||||
filesize = FileTest.size(filename)
|
||||
frameLength = ((144000 * bitrate) / freq) + t
|
||||
numFrames = filesize / (frameLength + 4)
|
||||
time = (numFrames * 1152.0 / freq)
|
||||
break
|
||||
end
|
||||
end
|
||||
}
|
||||
return time
|
||||
end
|
||||
292
Data/Scripts_backup/008_Audio/002_Audio_Play.rb
Normal file
292
Data/Scripts_backup/008_Audio/002_Audio_Play.rb
Normal file
@@ -0,0 +1,292 @@
|
||||
def pbStringToAudioFile(str)
|
||||
if str[/^(.*)\:\s*(\d+)\s*\:\s*(\d+)\s*$/] # Of the format "XXX: ###: ###"
|
||||
file = $1
|
||||
volume = $2.to_i
|
||||
pitch = $3.to_i
|
||||
return RPG::AudioFile.new(file,volume,pitch)
|
||||
elsif str[/^(.*)\:\s*(\d+)\s*$/] # Of the format "XXX: ###"
|
||||
file = $1
|
||||
volume = $2.to_i
|
||||
return RPG::AudioFile.new(file,volume,100)
|
||||
else
|
||||
return RPG::AudioFile.new(str,100,100)
|
||||
end
|
||||
end
|
||||
|
||||
# Converts an object to an audio file.
|
||||
# str -- Either a string showing the filename or an RPG::AudioFile object.
|
||||
# Possible formats for _str_:
|
||||
# filename volume and pitch 100
|
||||
# filename:volume pitch 100
|
||||
# filename:volume:pitch
|
||||
# volume -- Volume of the file, up to 100
|
||||
# pitch -- Pitch of the file, normally 100
|
||||
def pbResolveAudioFile(str,volume=nil,pitch=nil)
|
||||
if str.is_a?(String)
|
||||
str = pbStringToAudioFile(str)
|
||||
str.volume = volume || 100
|
||||
str.pitch = pitch || 100
|
||||
end
|
||||
if str.is_a?(RPG::AudioFile)
|
||||
if volume || pitch
|
||||
return RPG::AudioFile.new(str.name,volume || str.volume || 100 ,
|
||||
pitch || str.pitch || 100)
|
||||
else
|
||||
return str
|
||||
end
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
# Plays a BGM file.
|
||||
# param -- Either a string showing the filename
|
||||
# (relative to Audio/BGM/) or an RPG::AudioFile object.
|
||||
# Possible formats for _param_:
|
||||
# filename volume and pitch 100
|
||||
# filename:volume pitch 100
|
||||
# filename:volume:pitch
|
||||
# volume -- Volume of the file, up to 100
|
||||
# pitch -- Pitch of the file, normally 100
|
||||
def pbBGMPlay(param,volume=nil,pitch=nil)
|
||||
return if !param
|
||||
param=pbResolveAudioFile(param,volume,pitch)
|
||||
if param.name && param.name!=""
|
||||
if $game_system && $game_system.respond_to?("bgm_play")
|
||||
$game_system.bgm_play(param)
|
||||
return
|
||||
elsif (RPG.const_defined?(:BGM) rescue false)
|
||||
b=RPG::BGM.new(param.name,param.volume,param.pitch)
|
||||
if b && b.respond_to?("play")
|
||||
b.play
|
||||
return
|
||||
end
|
||||
end
|
||||
Audio.bgm_play(canonicalize("Audio/BGM/"+param.name),param.volume,param.pitch)
|
||||
end
|
||||
end
|
||||
|
||||
# Fades out or stops BGM playback. 'x' is the time in seconds to fade out.
|
||||
def pbBGMFade(x=0.0); pbBGMStop(x);end
|
||||
|
||||
# Fades out or stops BGM playback. 'x' is the time in seconds to fade out.
|
||||
def pbBGMStop(timeInSeconds=0.0)
|
||||
if $game_system && timeInSeconds>0.0 && $game_system.respond_to?("bgm_fade")
|
||||
$game_system.bgm_fade(timeInSeconds)
|
||||
return
|
||||
elsif $game_system && $game_system.respond_to?("bgm_stop")
|
||||
$game_system.bgm_stop
|
||||
return
|
||||
elsif (RPG.const_defined?(:BGM) rescue false)
|
||||
begin
|
||||
(timeInSeconds>0.0) ? RPG::BGM.fade((timeInSeconds*1000).floor) : RPG::BGM.stop
|
||||
return
|
||||
rescue
|
||||
end
|
||||
end
|
||||
(timeInSeconds>0.0) ? Audio.bgm_fade((timeInSeconds*1000).floor) : Audio.bgm_stop
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
# Plays an ME file.
|
||||
# param -- Either a string showing the filename
|
||||
# (relative to Audio/ME/) or an RPG::AudioFile object.
|
||||
# Possible formats for _param_:
|
||||
# filename volume and pitch 100
|
||||
# filename:volume pitch 100
|
||||
# filename:volume:pitch
|
||||
# volume -- Volume of the file, up to 100
|
||||
# pitch -- Pitch of the file, normally 100
|
||||
def pbMEPlay(param,volume=nil,pitch=nil)
|
||||
return if !param
|
||||
param=pbResolveAudioFile(param,volume,pitch)
|
||||
if param.name && param.name!=""
|
||||
if $game_system && $game_system.respond_to?("me_play")
|
||||
$game_system.me_play(param)
|
||||
return
|
||||
elsif (RPG.const_defined?(:ME) rescue false)
|
||||
b=RPG::ME.new(param.name,param.volume,param.pitch)
|
||||
if b && b.respond_to?("play")
|
||||
b.play; return
|
||||
end
|
||||
end
|
||||
Audio.me_play(canonicalize("Audio/ME/"+param.name),param.volume,param.pitch)
|
||||
end
|
||||
end
|
||||
|
||||
# Fades out or stops ME playback. 'x' is the time in seconds to fade out.
|
||||
def pbMEFade(x=0.0); pbMEStop(x);end
|
||||
|
||||
# Fades out or stops ME playback. 'x' is the time in seconds to fade out.
|
||||
def pbMEStop(timeInSeconds=0.0)
|
||||
if $game_system && timeInSeconds>0.0 && $game_system.respond_to?("me_fade")
|
||||
$game_system.me_fade(timeInSeconds)
|
||||
return
|
||||
elsif $game_system && $game_system.respond_to?("me_stop")
|
||||
$game_system.me_stop(nil)
|
||||
return
|
||||
elsif (RPG.const_defined?(:ME) rescue false)
|
||||
begin
|
||||
(timeInSeconds>0.0) ? RPG::ME.fade((timeInSeconds*1000).floor) : RPG::ME.stop
|
||||
return
|
||||
rescue
|
||||
end
|
||||
end
|
||||
(timeInSeconds>0.0) ? Audio.me_fade((timeInSeconds*1000).floor) : Audio.me_stop
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
# Plays a BGS file.
|
||||
# param -- Either a string showing the filename
|
||||
# (relative to Audio/BGS/) or an RPG::AudioFile object.
|
||||
# Possible formats for _param_:
|
||||
# filename volume and pitch 100
|
||||
# filename:volume pitch 100
|
||||
# filename:volume:pitch
|
||||
# volume -- Volume of the file, up to 100
|
||||
# pitch -- Pitch of the file, normally 100
|
||||
def pbBGSPlay(param,volume=nil,pitch=nil)
|
||||
return if !param
|
||||
param=pbResolveAudioFile(param,volume,pitch)
|
||||
if param.name && param.name!=""
|
||||
if $game_system && $game_system.respond_to?("bgs_play")
|
||||
$game_system.bgs_play(param)
|
||||
return
|
||||
elsif (RPG.const_defined?(:BGS) rescue false)
|
||||
b=RPG::BGS.new(param.name,param.volume,param.pitch)
|
||||
if b && b.respond_to?("play")
|
||||
b.play; return
|
||||
end
|
||||
end
|
||||
Audio.bgs_play(canonicalize("Audio/BGS/"+param.name),param.volume,param.pitch)
|
||||
end
|
||||
end
|
||||
|
||||
# Fades out or stops BGS playback. 'x' is the time in seconds to fade out.
|
||||
def pbBGSFade(x=0.0); pbBGSStop(x);end
|
||||
|
||||
# Fades out or stops BGS playback. 'x' is the time in seconds to fade out.
|
||||
def pbBGSStop(timeInSeconds=0.0)
|
||||
if $game_system && timeInSeconds>0.0 && $game_system.respond_to?("bgs_fade")
|
||||
$game_system.bgs_fade(timeInSeconds)
|
||||
return
|
||||
elsif $game_system && $game_system.respond_to?("bgs_play")
|
||||
$game_system.bgs_play(nil)
|
||||
return
|
||||
elsif (RPG.const_defined?(:BGS) rescue false)
|
||||
begin
|
||||
(timeInSeconds>0.0) ? RPG::BGS.fade((timeInSeconds*1000).floor) : RPG::BGS.stop
|
||||
return
|
||||
rescue
|
||||
end
|
||||
end
|
||||
(timeInSeconds>0.0) ? Audio.bgs_fade((timeInSeconds*1000).floor) : Audio.bgs_stop
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
# Plays an SE file.
|
||||
# param -- Either a string showing the filename
|
||||
# (relative to Audio/SE/) or an RPG::AudioFile object.
|
||||
# Possible formats for _param_:
|
||||
# filename volume and pitch 100
|
||||
# filename:volume pitch 100
|
||||
# filename:volume:pitch
|
||||
# volume -- Volume of the file, up to 100
|
||||
# pitch -- Pitch of the file, normally 100
|
||||
def pbSEPlay(param,volume=nil,pitch=nil)
|
||||
return if !param
|
||||
param = pbResolveAudioFile(param,volume,pitch)
|
||||
if param.name && param.name!=""
|
||||
if $game_system && $game_system.respond_to?("se_play")
|
||||
$game_system.se_play(param)
|
||||
return
|
||||
end
|
||||
if (RPG.const_defined?(:SE) rescue false)
|
||||
b = RPG::SE.new(param.name,param.volume,param.pitch)
|
||||
if b && b.respond_to?("play")
|
||||
b.play
|
||||
return
|
||||
end
|
||||
end
|
||||
Audio.se_play(canonicalize("Audio/SE/"+param.name),param.volume,param.pitch)
|
||||
end
|
||||
end
|
||||
|
||||
# Stops SE playback.
|
||||
def pbSEFade(x=0.0); pbSEStop(x);end
|
||||
|
||||
# Stops SE playback.
|
||||
def pbSEStop(_timeInSeconds=0.0)
|
||||
if $game_system
|
||||
$game_system.se_stop
|
||||
elsif (RPG.const_defined?(:SE) rescue false)
|
||||
RPG::SE.stop rescue nil
|
||||
else
|
||||
Audio.se_stop
|
||||
end
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
# Plays a sound effect that plays when the player moves the cursor.
|
||||
def pbPlayCursorSE
|
||||
if $data_system && $data_system.respond_to?("cursor_se") &&
|
||||
$data_system.cursor_se && $data_system.cursor_se.name!=""
|
||||
pbSEPlay($data_system.cursor_se)
|
||||
elsif $data_system && $data_system.respond_to?("sounds") &&
|
||||
$data_system.sounds && $data_system.sounds[0] && $data_system.sounds[0].name!=""
|
||||
pbSEPlay($data_system.sounds[0])
|
||||
elsif FileTest.audio_exist?("Audio/SE/GUI sel cursor")
|
||||
pbSEPlay("GUI sel cursor",80)
|
||||
end
|
||||
end
|
||||
|
||||
# Plays a sound effect that plays when a decision is confirmed or a choice is made.
|
||||
def pbPlayDecisionSE
|
||||
if $data_system && $data_system.respond_to?("decision_se") &&
|
||||
$data_system.decision_se && $data_system.decision_se.name!=""
|
||||
pbSEPlay($data_system.decision_se)
|
||||
elsif $data_system && $data_system.respond_to?("sounds") &&
|
||||
$data_system.sounds && $data_system.sounds[1] && $data_system.sounds[1].name!=""
|
||||
pbSEPlay($data_system.sounds[1])
|
||||
elsif FileTest.audio_exist?("Audio/SE/GUI sel decision")
|
||||
pbSEPlay("GUI sel decision",80)
|
||||
end
|
||||
end
|
||||
|
||||
# Plays a sound effect that plays when a choice is canceled.
|
||||
def pbPlayCancelSE
|
||||
if $data_system && $data_system.respond_to?("cancel_se") &&
|
||||
$data_system.cancel_se && $data_system.cancel_se.name!=""
|
||||
pbSEPlay($data_system.cancel_se)
|
||||
elsif $data_system && $data_system.respond_to?("sounds") &&
|
||||
$data_system.sounds && $data_system.sounds[2] && $data_system.sounds[2].name!=""
|
||||
pbSEPlay($data_system.sounds[2])
|
||||
elsif FileTest.audio_exist?("Audio/SE/GUI sel cancel")
|
||||
pbSEPlay("GUI sel cancel",80)
|
||||
end
|
||||
end
|
||||
|
||||
# Plays a buzzer sound effect.
|
||||
def pbPlayBuzzerSE
|
||||
if $data_system && $data_system.respond_to?("buzzer_se") &&
|
||||
$data_system.buzzer_se && $data_system.buzzer_se.name!=""
|
||||
pbSEPlay($data_system.buzzer_se)
|
||||
elsif $data_system && $data_system.respond_to?("sounds") &&
|
||||
$data_system.sounds && $data_system.sounds[3] && $data_system.sounds[3].name!=""
|
||||
pbSEPlay($data_system.sounds[3])
|
||||
elsif FileTest.audio_exist?("Audio/SE/GUI sel buzzer")
|
||||
pbSEPlay("GUI sel buzzer",80)
|
||||
end
|
||||
end
|
||||
|
||||
# Plays a sound effect that plays when the player moves the cursor.
|
||||
def pbPlayCloseMenuSE
|
||||
if FileTest.audio_exist?("Audio/SE/GUI menu close")
|
||||
pbSEPlay("GUI menu close",80)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user