Advertisement
mikb89

[Ace] Ambient Sound v1.0

May 30th, 2012
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.42 KB | None | 0 0
  1. # Ambient Sound v. 1.0
  2. # VX Ace 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(:clear_page_settings_b4_ambisound, :clear_page_settings) unless method_defined?(:clear_page_settings_b4_ambisound)
  124. #class Game_Event#def clear_page_settings() <- aliased
  125.   def clear_page_settings
  126.     clear_page_settings_b4_ambisound
  127.     @sound_type = ""
  128.     @last_volume = 0
  129.   end
  130.   alias_method(:setup_page_settings_b4_ambisound, :setup_page_settings) unless method_defined?(:setup_page_settings_b4_ambisound)
  131. #class Game_Event#def setup_page_settings() <- aliased
  132.   def setup_page_settings
  133.     setup_page_settings_b4_ambisound
  134.     @sound_type = ""
  135.     @last_volume = 0
  136.     unless empty?
  137.       for par in @list
  138.         if [408, 108].include?(par.code) && AMBISOUND::EFFECTS.has_key?(par.parameters[0])
  139.           @sound_type = par.parameters[0]
  140.         end
  141.       end
  142.     end
  143.   end
  144.   alias_method(:updateGE_b4_ambisound, :update) unless method_defined?(:updateGE_b4_ambisound)
  145. #class Game_Event#def update() <- aliased
  146.   def update
  147.     updateGE_b4_ambisound
  148.     if (@sdelay ||= AMBISOUND::DELAY) == 0
  149.       @sdelay = nil
  150.       var = nil
  151.       if @sound_type != ""
  152.         xv = $game_map.adjust_x(@real_x) - $game_map.adjust_x($game_player.real_x)
  153.         yv = $game_map.adjust_y(@real_y) - $game_map.adjust_y($game_player.real_y)
  154.         v = Math.hypot(xv, yv)
  155.         if v <= AMBISOUND::EFFECTS[@sound_type][4]
  156.           if @ae_playing
  157.             spn = AMBISOUND::EFFECTS[@sound_type][0..3]
  158.             spn[2] *= 1 - v/AMBISOUND::EFFECTS[@sound_type][4]
  159.             met = AMBISOUND::EFFECTS[@sound_type][6]
  160.             if @last_volume != spn[2] || met
  161.               @last_volume = spn[2]
  162.               if spn[2] >= AMBISOUND::EFFECTS[@sound_type][5]
  163.                 at = get_audio(spn)
  164.                 # REQUIRE Audio Pump Up - FMOD Ex
  165.                   if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  166.                     var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  167.                     $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  168.                   end
  169.                 # END REQUIRE
  170.                 if at != nil
  171.                   if met
  172.                     AMBISOUND.method(met).call(at)
  173.                   else
  174.                     at.play
  175.                     # REQUIRE Audio Pump Up - FMOD Ex
  176.                       if $imported[:mikb89_apu_fmod] && AMBISOUND::USE_PAN && $game_variables[APU::CURRENT_CHANNEL_VARIABLE] > 0
  177.                         pan = 1.0 * case $game_player.direction
  178.                               when 2; -xv; when 8; xv
  179.                               when 4; yv; when 6; -yv; else; 0; end
  180.                         pan /= AMBISOUND::EFFECTS[@sound_type][4]
  181.                         if pan != 0
  182.                           case AMBISOUND::EFFECTS[@sound_type][0]
  183.                           when "BGM"; FMod.bgm_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  184.                           when "BGS"; FMod.bgs_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  185.                           when "ME"; FMod.me_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE])
  186.                           when "SE"; FMod.se_set_pan(pan, $game_variables[APU::CURRENT_CHANNEL_VARIABLE], 0)
  187.                           end
  188.                         end
  189.                       end
  190.                     # END REQUIRE
  191.                   end
  192.                 end
  193.                 # REQUIRE Audio Pump Up - FMOD Ex
  194.                   $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  195.                 # END REQUIRE
  196.               end
  197.             end
  198.           else
  199.             if @audiowait
  200.               @audiowait -= 1
  201.               @ae_playing = !(@audiowait = false) if @audiowait == 0
  202.             else
  203.               at = get_audio(AMBISOUND::EFFECTS[@sound_type])
  204.               # REQUIRE Audio Pump Up - FMOD Ex
  205.                 if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  206.                   var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  207.                   $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  208.                 end
  209.               # END REQUIRE
  210.               if var != nil
  211.                 # REQUIRE Audio Pump Up - FMOD Ex
  212.                   FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGM
  213.                   FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if at.class == RPG::BGS
  214.                 # END REQUIRE
  215.               else
  216.                 @ex_audio = at.class.last if [RPG::BGM, RPG::BGS].include?(at.class)
  217.               end
  218.               if @ex_audio != nil
  219.                 @ex_audio.class.fade(40*AMBISOUND::DELAY)
  220.                 @audiowait = 4
  221.               else
  222.                 @ae_playing = true
  223.               end
  224.               # REQUIRE Audio Pump Up - FMOD Ex
  225.                 $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  226.               # END REQUIRE
  227.             end
  228.           end
  229.         else
  230.           @ae_playing = false
  231.           met = AMBISOUND::EFFECTS[@sound_type][6]
  232.           # REQUIRE Audio Pump Up - FMOD Ex
  233.             if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  234.               var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  235.               $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  236.             end
  237.           # END REQUIRE
  238.           if @ex_audio
  239.             @ex_audio.replay if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
  240.             @ex_audio = nil
  241.             AMBISOUND.method(met).call(nil) if met
  242.           else
  243.             AMBISOUND.method(met).call(nil) if met
  244.             if !met && var != nil
  245.               t = AMBISOUND::EFFECTS[@sound_type][0]
  246.               # REQUIRE Audio Pump Up - FMOD Ex
  247.                 FMod::Audio.bgm_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGM"
  248.                 FMod::Audio.bgs_fade(20*AMBISOUND::DELAY, AMBISOUND::SOUND_CHANNEL) if t.upcase == "BGS"
  249.               # END REQUIRE
  250.             end
  251.           end
  252.           # REQUIRE Audio Pump Up - FMOD Ex
  253.             $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  254.           # END REQUIRE
  255.         end
  256.       elsif @ae_playing
  257.         @ae_playing = false
  258.         # REQUIRE Audio Pump Up - FMOD Ex
  259.           if $imported[:mikb89_apu_fmod] && AMBISOUND::SOUND_CHANNEL > 0
  260.             var = $game_variables[APU::CURRENT_CHANNEL_VARIABLE]
  261.             $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = AMBISOUND::SOUND_CHANNEL
  262.           end
  263.         # END REQUIRE
  264.         if @ex_audio
  265.           @ex_audio.replay if [RPG::BGM, RPG::BGS, RPG::ME].include?(@ex_audio.class)
  266.           @ex_audio = nil
  267.         end
  268.         # REQUIRE Audio Pump Up - FMOD Ex
  269.           $game_variables[APU::CURRENT_CHANNEL_VARIABLE] = var if var != nil
  270.         # END REQUIRE
  271.       end
  272.     else
  273.       @sdelay -= 1
  274.     end
  275.   end
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement