Advertisement
mikb89

[VX] Ambient Sound v1.0

May 30th, 2012
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.08 KB | None | 0 0
  1. # Ambient Sound v. 1.0
  2. # VX version
  3. # by mikb89
  4.  
  5. # Details:
  6. #  RPG Maker audio possibilities, suck. I'll talk everywhere of the Audio Pump
  7. #   Up - FMOD Ex script (a script that uses FMod library to extend the audio
  8. #   system) because adding it will add a lot of possibilities. This script
  9. #   however works better also by itself.
  10. #  What THIS script does I think is clear. You can set any event to play a sound
  11. #   if near the player, varying volume, pitch, etc.
  12. #  You can also add effects, like the one of the chicken I wrote below, that
  13. #   change as you want, by writing the code.
  14. #  To use the script you have to configure HERE the effects, then write the name
  15. #   in a comment line, wherever you want in an event page.
  16.  
  17. # Configurations:
  18. module AMBISOUND
  19.   # Settings if you use Audio Pump Up - FMOD Ex.
  20.     SOUND_CHANNEL = 3
  21.      # The channel to use for every sound. Set 0 or less to deactivate.
  22.     USE_PAN = true # Want to hear sounds like they are stereo?
  23.      # e.g. if a sound is coming from left, you will hear it in the left speaker.
  24.     # End of APU configurations.
  25.  
  26.   DELAY = 10
  27.    # Avoid updating the audio too many times per second. Larger the number, less
  28.    #  the lag.
  29.   EFFECTS = {
  30.   #"name"    => ["TYPE", "filename", max_vol, pitch, range, vol_min, :def]
  31.    "radio"   => ["BGM",  "Airship",  100,     100,   8,     10,      nil], # <- add a , until it's the last effect
  32.    "radioLow"=> ["BGM",  "Airship",  70,      100,   2,     0,       nil],
  33.    "chicken" => ["SE",   "Chicken",  80,      80,    3,     0,       :chicken],
  34.    "tremolo" => ["BGS",  "Wind",     80,      100,   5,     0,       :tremolo]
  35.   }
  36.   # Explanation:
  37.   # "name" is what you have to write in the event comment to recall the effect.
  38.   # "TYPE" is one of "BGM", "BGS", "ME", "SE".
  39.   # "filename" is the name of the file you want to hear.
  40.   # max_volume is the max volume you can hear the sound.
  41.   #  i.e. when pg is over the event.
  42.   # pitch is the sound pitch. Standard for BGM/BGS is 100, for ME/SE is 80.
  43.   # range is the distance between which you can hear the sound.
  44.   # volume_min is the minimum volume to reach for start playing. If player is in
  45.   #  range, but the actual volume would be less than this value, it won't play,
  46.   #  letting the previous audio (bgm or bgs) fade for a smoother effect.
  47.   # :def let you create a function that process the sound file. You can either
  48.   #  choose one of the defaults or add your own if you know how to script.
  49.  
  50.   # Example defs
  51.   def self.chicken(sound) # trying to emulate a chicken lol
  52.     return unless sound # sound is nil when passed for stop the sound. we just
  53.      # return because there's no need for a se.
  54.     if (@last_chick ||= 0) >= 4+rand(6) # avoid to play the sound everytime
  55.       @last_chick = 0 # we have to use
  56.       pitchange = rand(20)-10 # casually change pitch
  57.       sound.pitch += pitchange
  58.       volchange = rand(1) == 0 ? rand(30)-10 : sound.volume*-1 # another avoid (volume 0)
  59.       sound.volume += volchange
  60.       sound.play # we HAVE to call this to play the sound
  61.     else
  62.       @last_chick += 1
  63.     end
  64.   end
  65.  
  66.   def self.tremolo(sound) # this example requires the Audio Pump Up - FMOD Ex
  67.     return if !$imported || !$imported[:mikb89_apu_fmod]
  68.     unless sound # we stop the sound
  69.       return unless @trem_chan # channel is nil???
  70.       FMod.bgs_stop(@trem_chan) # stop
  71.       return # we stop. really.
  72.     end
  73.     return if (@last_tremolo_volume ||= sound.volume) == sound.volume
  74.     @last_tremolo_volume = sound.volume # set the last volume
  75.      # we avoid to play the same bgs at the same volume (this is by default for
  76.      #  bgm and bgs where there aren't specified defs)
  77.     cv = APU::CURRENT_CHANNEL_VARIABLE # channel variable from the APU settings
  78.     var = @trem_chan = $game_variables[cv] # get the current channel
  79.     $game_variables[cv] = @trem_chan = APU::MAX_CHANNELS - 1 if var < 1
  80.     add_eff = true # tremolo effect already added? we'll see...
  81.      # if the current channel is 0 we use the last one, then:
  82.     sound.play # first play the BGS (see APU documentation)
  83.     for e in 0...FMod.bgs_DSP(@trem_chan)
  84.       t = FModEx::FMOD_DSP_TYPES[FMod.bgs_getType(@trem_chan, e)]
  85.       if t.downcase == "tremolo" # if tremolo exists
  86.         add_eff = false # add nothing
  87.         break
  88.       end
  89.     end
  90.     FMod::Audio.bgs_add_effect("tremolo", @trem_chan) if add_eff
  91.      # and then add the DSP effect if needed
  92.     $game_variables[cv] = var if var < 1 # restore the channel
  93.   end
  94. end
  95.  
  96. #Codename: ambisound
  97.  
  98. ($imported ||= {})[:mikb89_ambisound] = true
  99.  
  100. # License:
  101. # - You can ask me to include support for other scripts as long as these scripts
  102. #   use the $imported[script] = true;
  103. # - You can modify and even repost my scripts, after having received a response
  104. #   by me. For reposting it, anyway, you must have done heavy edit or porting,
  105. #   you can't do a post with the script as is;
  106. # - You can use my scripts for whatever you want, from free to open to
  107. #   commercial games. I'd appreciate by the way if you let me know about what
  108. #   you're doing;
  109. # - You must credit me, if you use this script or part of it.
  110.  
  111. class Game_Event
  112. #class Game_Event#def get_audio(spn)
  113.   def get_audio(spn)
  114.     audio_eff = (spn[1] == "" ||
  115.                   spn[2] <= 0) ? nil :
  116.                   case spn[0]
  117.                   when "BGM"; RPG::BGM.new(spn[1], spn[2], spn[3])
  118.                   when "BGS"; RPG::BGS.new(spn[1], spn[2], spn[3])
  119.                   when "ME"; RPG::ME.new(spn[1], spn[2], spn[3])
  120.                   when "SE"; RPG::SE.new(spn[1], spn[2], spn[3])
  121.                   end
  122.   end
  123.   alias_method(:setup_b4_ambisound, :setup) unless method_defined?(:setup_b4_ambisound)
  124. #class Game_Event#def setup(new_page) <- aliased
  125.   def setup(new_page)
  126.     setup_b4_ambisound(new_page)
  127.     @sound_type = ""
  128.     @last_volume = 0
  129.     if @list != nil
  130.       for par in @list
  131.         if [408, 108].include?(par.code) && AMBISOUND::EFFECTS.has_key?(par.parameters[0])
  132.           @sound_type = par.parameters[0]
  133.         end
  134.       end
  135.     end
  136.   end
  137.   alias_method(:updateGE_b4_ambisound, :update) unless method_defined?(:updateGE_b4_ambisound)
  138. #class Game_Event#def update() <- aliased
  139.   def update
  140.     updateGE_b4_ambisound
  141.     if (@sdelay ||= AMBISOUND::DELAY) == 0
  142.       @sdelay = nil
  143.       var = nil
  144.       if @sound_type != ""
  145.         xv = $game_map.adjust_x(@real_x) - $game_map.adjust_x($game_player.real_x)
  146.         yv = $game_map.adjust_y(@real_y) - $game_map.adjust_y($game_player.real_y)
  147.         v = Math.hypot(xv/256.0, yv/256.0)
  148.         if v <= AMBISOUND::EFFECTS[@sound_type][4]
  149.           if @ae_playing
  150.             spn = AMBISOUND::EFFECTS[@sound_type][0..3]
  151.             spn[2] *= 1 - v/AMBISOUND::EFFECTS[@sound_type][4]
  152.             met = AMBISOUND::EFFECTS[@sound_type][6]
  153.             if @last_volume != spn[2] || met
  154.               @last_volume = spn[2]
  155.               if spn[2] >= AMBISOUND::EFFECTS[@sound_type][5]
  156.                 at = get_audio(spn)
  157.                 # REQUIRE Audio Pump Up - FMOD Ex
  158.                   if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  159.                     var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  160.                     $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  161.                   end
  162.                 # END REQUIRE
  163.                 if at != nil
  164.                   if met
  165.                     AMBISOUND.method(met).call(at)
  166.                   else
  167.                     at.play
  168.                     # REQUIRE Audio Pump Up - FMOD Ex
  169.                       if $imported[:mikb89_apu_fmod] && AMBISOUND::USE_PAN && $game_variables[APU::CURRENT_CHANNEL_VARIABLE] > 0
  170.                         pan = 1.0 * case $game_player.direction
  171.                               when 2; -xv; when 8; xv
  172.                               when 4; yv; when 6; -yv; else; 0; end
  173.                         pan /= AMBISOUND::EFFECTS[@sound_type][4]
  174.                         if pan != 0
  175.                           case AMBISOUND::EFFECTS[@sound_type][0]
  176.                           when "BGM"; FMod.bgm_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  177.                           when "BGS"; FMod.bgs_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  178.                           when "ME"; FMod.me_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  179.                           when "SE"; FMod.se_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE], 0)
  180.                           end
  181.                         end
  182.                       end
  183.                     # END REQUIRE
  184.                   end
  185.                 end
  186.                 # REQUIRE Audio Pump Up - FMOD Ex
  187.                   $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  188.                 # END REQUIRE
  189.               end
  190.             end
  191.           else
  192.             if @audiowait
  193.               @audiowait -= 1
  194.               @ae_playing = !(@audiowait = false) if @audiowait == 0
  195.             else
  196.               at = get_audio(AMBISOUND::EFFECTS[@sound_type])
  197.               # REQUIRE Audio Pump Up - FMOD Ex
  198.                 if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  199.                   var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  200.                   $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  201.                 end
  202.               # END REQUIRE
  203.               if var != nil
  204.                 # REQUIRE Audio Pump Up - FMOD Ex
  205.                   FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGM
  206.                   FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGS
  207.                 # END REQUIRE
  208.               else
  209.                 @ex_audio = at.class.last if [RPG::BGM, RPG::BGS].include?(at.class)
  210.               end
  211.               if @ex_audio != nil
  212.                 @ex_audio.class.fade(40*AMBISOUND::DELAY)
  213.                 @audiowait = 4
  214.               else
  215.                 @ae_playing = true
  216.               end
  217.               # REQUIRE Audio Pump Up - FMOD Ex
  218.                 $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  219.               # END REQUIRE
  220.             end
  221.           end
  222.         else
  223.           @ae_playing = false
  224.           met = AMBISOUND::EFFECTS[@sound_type][6]
  225.           # REQUIRE Audio Pump Up - FMOD Ex
  226.             if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  227.               var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  228.               $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  229.             end
  230.           # END REQUIRE
  231.           if @ex_audio
  232.             @ex_audio.play if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
  233.             @ex_audio = nil
  234.             AMBISOUND.method(met).call(nil) if met
  235.           else
  236.             AMBISOUND.method(met).call(nil) if met
  237.             if !met && var != nil
  238.               t = AMBISOUND::EFFECTS[@sound_type][0]
  239.               # REQUIRE Audio Pump Up - FMOD Ex
  240.                 FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGM"
  241.                 FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGS"
  242.               # END REQUIRE
  243.             end
  244.           end
  245.           # REQUIRE Audio Pump Up - FMOD Ex
  246.             $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  247.           # END REQUIRE
  248.         end
  249.       elsif @ae_playing
  250.         @ae_playing = false
  251.         # REQUIRE Audio Pump Up - FMOD Ex
  252.           if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  253.             var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  254.             $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  255.           end
  256.         # END REQUIRE
  257.         if @ex_audio
  258.           @ex_audio.play if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
  259.           @ex_audio = nil
  260.         end
  261.         # REQUIRE Audio Pump Up - FMOD Ex
  262.           $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  263.         # END REQUIRE
  264.       end
  265.     else
  266.       @sdelay -= 1
  267.     end
  268.   end
  269. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement