Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ambient Sound v. 1.0
- # VX version
- # by mikb89
- # Details:
- # RPG Maker audio possibilities, suck. I'll talk everywhere of the Audio Pump
- # Up - FMOD Ex script (a script that uses FMod library to extend the audio
- # system) because adding it will add a lot of possibilities. This script
- # however works better also by itself.
- # What THIS script does I think is clear. You can set any event to play a sound
- # if near the player, varying volume, pitch, etc.
- # You can also add effects, like the one of the chicken I wrote below, that
- # change as you want, by writing the code.
- # To use the script you have to configure HERE the effects, then write the name
- # in a comment line, wherever you want in an event page.
- # Configurations:
- module AMBISOUND
- # Settings if you use Audio Pump Up - FMOD Ex.
- SOUND_CHANNEL = 3
- # The channel to use for every sound. Set 0 or less to deactivate.
- USE_PAN = true # Want to hear sounds like they are stereo?
- # e.g. if a sound is coming from left, you will hear it in the left speaker.
- # End of APU configurations.
- DELAY = 10
- # Avoid updating the audio too many times per second. Larger the number, less
- # the lag.
- EFFECTS = {
- #"name" => ["TYPE", "filename", max_vol, pitch, range, vol_min, :def]
- "radio" => ["BGM", "Airship", 100, 100, 8, 10, nil], # <- add a , until it's the last effect
- "radioLow"=> ["BGM", "Airship", 70, 100, 2, 0, nil],
- "chicken" => ["SE", "Chicken", 80, 80, 3, 0, :chicken],
- "tremolo" => ["BGS", "Wind", 80, 100, 5, 0, :tremolo]
- }
- # Explanation:
- # "name" is what you have to write in the event comment to recall the effect.
- # "TYPE" is one of "BGM", "BGS", "ME", "SE".
- # "filename" is the name of the file you want to hear.
- # max_volume is the max volume you can hear the sound.
- # i.e. when pg is over the event.
- # pitch is the sound pitch. Standard for BGM/BGS is 100, for ME/SE is 80.
- # range is the distance between which you can hear the sound.
- # volume_min is the minimum volume to reach for start playing. If player is in
- # range, but the actual volume would be less than this value, it won't play,
- # letting the previous audio (bgm or bgs) fade for a smoother effect.
- # :def let you create a function that process the sound file. You can either
- # choose one of the defaults or add your own if you know how to script.
- # Example defs
- def self.chicken(sound) # trying to emulate a chicken lol
- return unless sound # sound is nil when passed for stop the sound. we just
- # return because there's no need for a se.
- if (@last_chick ||= 0) >= 4+rand(6) # avoid to play the sound everytime
- @last_chick = 0 # we have to use
- pitchange = rand(20)-10 # casually change pitch
- sound.pitch += pitchange
- volchange = rand(1) == 0 ? rand(30)-10 : sound.volume*-1 # another avoid (volume 0)
- sound.volume += volchange
- sound.play # we HAVE to call this to play the sound
- else
- @last_chick += 1
- end
- end
- def self.tremolo(sound) # this example requires the Audio Pump Up - FMOD Ex
- return if !$imported || !$imported[:mikb89_apu_fmod]
- unless sound # we stop the sound
- return unless @trem_chan # channel is nil???
- FMod.bgs_stop(@trem_chan) # stop
- return # we stop. really.
- end
- return if (@last_tremolo_volume ||= sound.volume) == sound.volume
- @last_tremolo_volume = sound.volume # set the last volume
- # we avoid to play the same bgs at the same volume (this is by default for
- # bgm and bgs where there aren't specified defs)
- cv = APU::CURRENT_CHANNEL_VARIABLE # channel variable from the APU settings
- var = @trem_chan = $game_variables[cv] # get the current channel
- $game_variables[cv] = @trem_chan = APU::MAX_CHANNELS - 1 if var < 1
- add_eff = true # tremolo effect already added? we'll see...
- # if the current channel is 0 we use the last one, then:
- sound.play # first play the BGS (see APU documentation)
- for e in 0...FMod.bgs_DSP(@trem_chan)
- t = FModEx::FMOD_DSP_TYPES[FMod.bgs_getType(@trem_chan, e)]
- if t.downcase == "tremolo" # if tremolo exists
- add_eff = false # add nothing
- break
- end
- end
- FMod::Audio.bgs_add_effect("tremolo", @trem_chan) if add_eff
- # and then add the DSP effect if needed
- $game_variables[cv] = var if var < 1 # restore the channel
- end
- end
- #Codename: ambisound
- ($imported ||= {})[:mikb89_ambisound] = true
- # License:
- # - You can ask me to include support for other scripts as long as these scripts
- # use the $imported[script] = true;
- # - You can modify and even repost my scripts, after having received a response
- # by me. For reposting it, anyway, you must have done heavy edit or porting,
- # you can't do a post with the script as is;
- # - You can use my scripts for whatever you want, from free to open to
- # commercial games. I'd appreciate by the way if you let me know about what
- # you're doing;
- # - You must credit me, if you use this script or part of it.
- class Game_Event
- #class Game_Event#def get_audio(spn)
- def get_audio(spn)
- audio_eff = (spn[1] == "" ||
- spn[2] <= 0) ? nil :
- case spn[0]
- when "BGM"; RPG::BGM.new(spn[1], spn[2], spn[3])
- when "BGS"; RPG::BGS.new(spn[1], spn[2], spn[3])
- when "ME"; RPG::ME.new(spn[1], spn[2], spn[3])
- when "SE"; RPG::SE.new(spn[1], spn[2], spn[3])
- end
- end
- alias_method(:setup_b4_ambisound, :setup) unless method_defined?(:setup_b4_ambisound)
- #class Game_Event#def setup(new_page) <- aliased
- def setup(new_page)
- setup_b4_ambisound(new_page)
- @sound_type = ""
- @last_volume = 0
- if @list != nil
- for par in @list
- if [408, 108].include?(par.code) && AMBISOUND::EFFECTS.has_key?(par.parameters[0])
- @sound_type = par.parameters[0]
- end
- end
- end
- end
- alias_method(:updateGE_b4_ambisound, :update) unless method_defined?(:updateGE_b4_ambisound)
- #class Game_Event#def update() <- aliased
- def update
- updateGE_b4_ambisound
- if (@sdelay ||= AMBISOUND::DELAY) == 0
- @sdelay = nil
- var = nil
- if @sound_type != ""
- xv = $game_map.adjust_x(@real_x) - $game_map.adjust_x($game_player.real_x)
- yv = $game_map.adjust_y(@real_y) - $game_map.adjust_y($game_player.real_y)
- v = Math.hypot(xv/256.0, yv/256.0)
- if v <= AMBISOUND::EFFECTS[@sound_type][4]
- if @ae_playing
- spn = AMBISOUND::EFFECTS[@sound_type][0..3]
- spn[2] *= 1 - v/AMBISOUND::EFFECTS[@sound_type][4]
- met = AMBISOUND::EFFECTS[@sound_type][6]
- if @last_volume != spn[2] || met
- @last_volume = spn[2]
- if spn[2] >= AMBISOUND::EFFECTS[@sound_type][5]
- at = get_audio(spn)
- # REQUIRE Audio Pump Up - FMOD Ex
- if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
- var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
- end
- # END REQUIRE
- if at != nil
- if met
- AMBISOUND.method(met).call(at)
- else
- at.play
- # REQUIRE Audio Pump Up - FMOD Ex
- if $imported[:mikb89_apu_fmod] && AMBISOUND::USE_PAN && $game_variables[APU::CURRENT_CHANNEL_VARIABLE] > 0
- pan = 1.0 * case $game_player.direction
- when 2; -xv; when 8; xv
- when 4; yv; when 6; -yv; else; 0; end
- pan /= AMBISOUND::EFFECTS[@sound_type][4]
- if pan != 0
- case AMBISOUND::EFFECTS[@sound_type][0]
- when "BGM"; FMod.bgm_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
- when "BGS"; FMod.bgs_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
- when "ME"; FMod.me_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
- when "SE"; FMod.se_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE], 0)
- end
- end
- end
- # END REQUIRE
- end
- end
- # REQUIRE Audio Pump Up - FMOD Ex
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
- # END REQUIRE
- end
- end
- else
- if @audiowait
- @audiowait -= 1
- @ae_playing = !(@audiowait = false) if @audiowait == 0
- else
- at = get_audio(AMBISOUND::EFFECTS[@sound_type])
- # REQUIRE Audio Pump Up - FMOD Ex
- if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
- var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
- end
- # END REQUIRE
- if var != nil
- # REQUIRE Audio Pump Up - FMOD Ex
- FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGM
- FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGS
- # END REQUIRE
- else
- @ex_audio = at.class.last if [RPG::BGM, RPG::BGS].include?(at.class)
- end
- if @ex_audio != nil
- @ex_audio.class.fade(40*AMBISOUND::DELAY)
- @audiowait = 4
- else
- @ae_playing = true
- end
- # REQUIRE Audio Pump Up - FMOD Ex
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
- # END REQUIRE
- end
- end
- else
- @ae_playing = false
- met = AMBISOUND::EFFECTS[@sound_type][6]
- # REQUIRE Audio Pump Up - FMOD Ex
- if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
- var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
- end
- # END REQUIRE
- if @ex_audio
- @ex_audio.play if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
- @ex_audio = nil
- AMBISOUND.method(met).call(nil) if met
- else
- AMBISOUND.method(met).call(nil) if met
- if !met && var != nil
- t = AMBISOUND::EFFECTS[@sound_type][0]
- # REQUIRE Audio Pump Up - FMOD Ex
- FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGM"
- FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGS"
- # END REQUIRE
- end
- end
- # REQUIRE Audio Pump Up - FMOD Ex
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
- # END REQUIRE
- end
- elsif @ae_playing
- @ae_playing = false
- # REQUIRE Audio Pump Up - FMOD Ex
- if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
- var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
- end
- # END REQUIRE
- if @ex_audio
- @ex_audio.play if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
- @ex_audio = nil
- end
- # REQUIRE Audio Pump Up - FMOD Ex
- $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
- # END REQUIRE
- end
- else
- @sdelay -= 1
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement