Jump to content

Recommended Posts

Ambient Sound 1.0

mikb89

 

Introduction

With this script you can set any event to play something if near the player, varying volume, pitch, etc.

You can also add effects that change audio as you want, by writing the code.

To use the script you have to configure effects inside the script, then write the effect name in a comment line, wherever you want in an event page.

 

Features

  • positional audio system;
  • let you create your own effects;
  • can be integrated with Audio Pump Up script.

 

Screenshots

It's an audio system.

 

How to Use

Copy the script under Modules, then configure the effects.

 

Demo

Multilanguage demo v. 1.0 (1.33 MB)

http://www.mediafire.com/?alkn9m7g22gb023

 

Script

 

# Ambient Sound v. 1.0
# VX Ace 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(:clear_page_settings_b4_ambisound, :clear_page_settings) unless method_defined?(:clear_page_settings_b4_ambisound)
#class Game_Event#def clear_page_settings() <- aliased
 def clear_page_settings
   clear_page_settings_b4_ambisound
   @sound_type = ""
   @last_volume = 0
 end
 alias_method(:setup_page_settings_b4_ambisound, :setup_page_settings) unless method_defined?(:setup_page_settings_b4_ambisound)
#class Game_Event#def setup_page_settings() <- aliased
 def setup_page_settings
   setup_page_settings_b4_ambisound
   @sound_type = ""
   @last_volume = 0
   unless empty?
     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, yv)
       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.replay 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.replay 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

Also visible on Pastebin.

 

FAQ

N/A

 

Credit and Thanks

N/A

 

Author's Notes

I suggest to use with this other script.

Share this post


Link to post
Share on other sites

This is a very nice script, good work! (:

Only problem I'm having is that the sound effect updates for every event, even if the sound effect played is the same, thus giving off a 'chunky' effect; meaning that I can't make ambient sounds for say, a waterfall. ):

Edited by decadence

Share this post


Link to post
Share on other sites

I like this script but I don't understand how to use it.  How do I make my own custom sounds?  I want to play a sound for a bird flapping its wings when it's near the character, how can I set that up?  This example only uses default sounds too, so it leaves me even more confused on how to set sounds manually from the SE folder. 

 

I'm pretty sure this script is what I need, but it's really vague on how to use unless one is familiar with Ruby scripting editing/customizing.  Any help would be appreciated. 

 

Thank you for your time reading.

Edited by Spearnear

Share this post


Link to post
Share on other sites

Could author or someone else change this script so you could in comment override settings (except def and type) in colon-separated list?

 

Let me explain.

 

You could put just name of sound you want to play, but you could also put it in format like name:filename:max_vol:pitch:range:min_vol, like chicken:OtherChicken:80:80:3:0, in this case overriding file name. This way you could have some effects defined (in def functions), and you'd only change few parameters needed, so you would rarely need to go into script editor.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×