Advertisement
neonblack

Cross Fade Audio

Jun 9th, 2012
874
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 12.66 KB | None | 0 0
  1. ###--------------------------------------------------------------------------###
  2. #  Fade-across Audio script                                                    #
  3. #  Version 1.0                                                                 #
  4. #                                                                              #
  5. #      Credits:                                                                #
  6. #  Original code by: Neonblack                                                 #
  7. #  Modified by:                                                                #
  8. #                                                                              #
  9. #  This work is licensed under the Creative Commons Attribution-NonCommercial  #
  10. #  3.0 Unported License. To view a copy of this license, visit                 #
  11. #  http://creativecommons.org/licenses/by-nc/3.0/.                             #
  12. #  Permissions beyond the scope of this license are available at               #
  13. #  http://cphouseset.wordpress.com/liscense-and-terms-of-use/.                 #
  14. #                                                                              #
  15. #      Contact:                                                                #
  16. #  NeonBlack - neonblack23@live.com (e-mail) or "neonblack23" on skype         #
  17. ###--------------------------------------------------------------------------###
  18.  
  19. ###--------------------------------------------------------------------------###
  20. #      Revision information:                                                   #
  21. #  V1.0 - 6.4.2012                                                             #
  22. #   Wrote and debugged main script                                             #
  23. #  Beta - Dunno When                                                           #
  24. #   Tested idea with dummy script                                              #
  25. ###--------------------------------------------------------------------------###
  26.  
  27. ###--------------------------------------------------------------------------###
  28. #      Compatibility:                                                          #
  29. #  Alias       - Audio: bgm_play, bgm_stop, bgm_fade, bgm_pos, bgs_play,       #
  30. #                       bgs_stop, bgs_fade, bgs_pos, me_play                   #
  31. #  New Objects - Audio: check_fades, stop_fades, cross_bgm, cross_bgs          #
  32. ###--------------------------------------------------------------------------###
  33.  
  34. ###--------------------------------------------------------------------------###
  35. #      Instructions:                                                           #
  36. #  Place this script in the "Materials" section of the scripts above main.     #
  37. #  This script is pretty much plug and play with a few options available to    #
  38. #  change below.  To start the "cross-fade audio", simply enable the switch    #
  39. #  you defined in the settings.  Note that this script uses the bgs            #
  40. #  (background sound) to allow the cross fade to work, so you cannot play a    #
  41. #  bgs while the switch is turned on.  Also note that playing a ME (music      #
  42. #  effect) of any type will disable the cross-fade, so it will need to be      #
  43. #  re-enabled afterwards.  It is recommended that you start playing a music    #
  44. #  before enabling cross-fade since it will cause the newly played bgm to      #
  45. #  cross fade either from the old bgm or from nothing.  Also note that this    #
  46. #  script only works with .OGG and .WAV files.                                 #
  47. #      Short Instructions:                                                     #
  48. #  1. Turn switch on to enable.                                                #
  49. #  2. Turn the switch off to diable.                                           #
  50. #  3. Playing a ME disables cross-fade.                                        #
  51. #  4. Only use .OGG and .WAV files.                                            #
  52. #                                                                              #
  53. #      Special Note:                                                           #
  54. #  Okay, at the expense of sounding like I don't know what I'm doing (which I  #
  55. #  can't say I entirely do) there are some issues with the script which I am   #
  56. #  unable to resolve (and most likely will never be able to unless someone     #
  57. #  decides to rip apart the "audio" module so I can find out how it actually   #
  58. #  works).  The first issue involves stereo tracks and the position offset     #
  59. #  (see below).  For some reason, a stereo track that has been offset and      #
  60. #  played as a "bgs" will have a split second of static at the end and I'm     #
  61. #  not entirely sure why.  The simple solution is to just now have any stereo  #
  62. #  tracks (just use mono).  Of course if you have this issue with mono tracks  #
  63. #  PLEASE LET ME KNOW!!  I will do what I can to solve the issue then.  The    #
  64. #  second issue is also with the position tracking and has nothing to do with  #
  65. #  anything I did at all but is actually a "bug" that vxa already has but      #
  66. #  which would have been otherwise impossible to detect.  When trying to       #
  67. #  determine the "position" of the current song, the value is actually only    #
  68. #  updated every 8~12 frames.  This means no matter how much I try to modify   #
  69. #  what I can see, there will always be a slight variance of roughly 150       #
  70. #  milliseconds.  This is barely noticable and in my opinion no big deal.  I   #
  71. #  no plans to try to fix this (once again, unless someone rips apart the      #
  72. #  audio module).  If there are any other issues noted with the script         #
  73. #  besides these two, please contact me with details (and a demo with the      #
  74. #  issue or a video or something else).                                        #
  75. ###--------------------------------------------------------------------------###
  76.  
  77. ###--------------------------------------------------------------------------###
  78. #      Config:                                                                 #
  79. #  These are the default values used by several of the functions in the        #
  80. #  script.  You may change these values as you find your game requires in      #
  81. #  order to give the player a better playing experience based on your game.    #
  82. #                                                                              #
  83. module CP          # Do not edit                                               #
  84. module FADE_AUDIO  #  these two lines.                                         #
  85. #                                                                              #
  86. ###-----                                                                -----###
  87. # This is the switch used by the script to determine if cross fading is being  #
  88. # used or not.                                                                 #
  89. SWITCH = 11 # Default = 11                                                     #
  90. #                                                                              #
  91. # This determines what to do if the program tries to play a bms while cross-   #
  92. # fade is enabled.  If this is set to TRUE it will treat it the same as        #
  93. # bgm_play.  If this is set to FALSE it does nothing.                          #
  94. DO_BGM = false # Default = false                                               #
  95. #                                                                              #
  96. # Okay, this one is kind of odd.  When a new bgm is called and the script      #
  97. # tries to play it from the old position, it doesn't play from exactly the     #
  98. # same position.  This value actually tells the script to play the new song    #
  99. # from a position a little later in the song to account for discrepancy,       #
  100. # however, even then it is not exact (see above).  If you are using music      #
  101. # with a different bitrate than the demo, this may need to be adjusted.  This  #
  102. # value is (I believe) in bytes, not any actual length of time.                #
  103. OFFSET = 26650 # Default = 26650                                               #
  104. #                                                                              #
  105. #                                                                              #
  106. end # Don't edit                                                               #
  107. end #  either of these.                                                        #
  108. ###--------------------------------------------------------------------------###
  109.  
  110.  
  111. ###--------------------------------------------------------------------------###
  112. #  The following lines are the actual core code of the script.  While you are  #
  113. #  certainly invited to look, modifying it may result in undesirable results.  #
  114. #  Modify at your own risk!                                                    #
  115. ###--------------------------------------------------------------------------###
  116.  
  117. $imported = {} if $imported == nil
  118. $imported["CP_CROSSFADE"] = true
  119.  
  120. module Audio
  121.   class << self
  122.     alias cp_bgm_play bgm_play unless $@  ## Initialize alias
  123.     alias cp_bgs_play bgs_play unless $@
  124.     alias cp_bgm_stop bgm_stop unless $@
  125.     alias cp_bgs_stop bgs_stop unless $@
  126.     alias cp_bgm_fade bgm_fade unless $@
  127.     alias cp_bgs_fade bgs_fade unless $@
  128.     alias cp_bgm_pos bgm_pos unless $@
  129.     alias cp_bgs_pos bgs_pos unless $@
  130.     alias cp_me_play me_play unless $@
  131.    
  132.     $fades = false if $fades == nil    ## Initialize variables
  133.     $switch = false if $switch == nil
  134.    
  135.     def bgm_play(*args)  ## Creates the new bgm_play
  136.       check_fades
  137.       if $fades
  138.         if $switch
  139.           spot = cross_bgs# + CP::FADE_AUDIO::OFFSET
  140.           @bgm = args
  141.           @bgs = nil
  142.           cp_bgm_play(args[0],args[1],args[2],spot)
  143.           cp_bgs_fade(1000)
  144.         else
  145.           spot = cross_bgm# + CP::FADE_AUDIO::OFFSET
  146.           @bgs = args
  147.           @bgm = nil
  148.           cp_bgs_play(args[0],args[1],args[2],spot)
  149.           cp_bgm_fade(1000)
  150.         end
  151.         $switch = $switch ? false : true
  152.       else  ## Plays the bgm like normal if disabled.
  153.         unless $switch
  154.           cp_bgm_play(*args)
  155.           @bgm = args
  156.         else
  157.           cp_bgs_play(*args)
  158.           @bgs = args
  159.         end
  160.       end
  161.     end
  162.    
  163.     def check_fades  ## New method that checks the related switch
  164.       return if $game_switches == nil
  165.       $fades = $game_switches[CP::FADE_AUDIO::SWITCH]
  166.     end
  167.    
  168.     def bgs_play(*args)  ## Changes method for ambient sounds
  169.       check_fades
  170.       if $fades
  171.         bgm_play(*args) if CP::FADE_AUDIO::DO_BGM
  172.       else
  173.         unless $switch
  174.           cp_bgs_play(*args)
  175.           @bgs = args
  176.         else
  177.           cp_bgm_play(*args)
  178.           @bgm = args
  179.         end
  180.       end
  181.     end
  182.        
  183.     def me_play(*args)  ## Resets BGM and BGS when playing a ME
  184.       stop_fades
  185.       unless $switch
  186.         cp_me_play(*args)
  187.       else
  188.         temp = @bgs
  189.         spot = cp_bgs_pos
  190.         cp_bgs_stop
  191.         cp_bgs_play(@bgm[0],@bgm[1],@bgm[2],0) unless @bgm == nil
  192.         @bgs = @bgm
  193.         cp_bgm_stop
  194.         cp_bgm_play(temp[0],temp[1],temp[2],spot) unless temp == nil
  195.         @bgm = temp
  196.         cp_me_play(*args)
  197.         $switch = false
  198.       end
  199.     end
  200.    
  201.     def stop_fades  ## Reverts to normal audio
  202.       return if $game_switches == nil
  203.       $fades = false
  204.       $game_switches[CP::FADE_AUDIO::SWITCH] = false
  205.     end
  206.    
  207.     def bgm_stop      ## Ungodly number of aliased methods here
  208.       unless $switch
  209.         cp_bgm_stop
  210.         @bgm = nil
  211.       else
  212.         cp_bgs_stop
  213.         @bgs = nil
  214.       end
  215.     end
  216.    
  217.     def bgs_stop
  218.       unless $switch
  219.         cp_bgs_stop
  220.         @bgs = nil
  221.       else
  222.         cp_bgm_stop
  223.         @bgm = nil
  224.       end
  225.     end
  226.    
  227.     def bgm_fade(time)
  228.       unless $switch
  229.         cp_bgm_fade(time)
  230.         @bgm = nil
  231.       else
  232.         cp_bgs_fade(time)
  233.         @bgs = nil
  234.       end
  235.     end
  236.    
  237.     def bgs_fade(time)
  238.       unless $switch
  239.         cp_bgs_fade(time)
  240.         @bgs = nil
  241.       else
  242.         cp_bgm_fade(time)
  243.         @bgm = nil
  244.       end
  245.     end
  246.    
  247.     def bgm_pos
  248.       unless $switch
  249.         cp_bgm_pos
  250.       else
  251.         cp_bgs_pos
  252.       end
  253.     end
  254.    
  255.     def bgs_pos
  256.       unless $switch
  257.         cp_bgs_pos
  258.       else
  259.         cp_bgm_pos
  260.       end
  261.     end
  262.    
  263.     def cross_bgm
  264.       i = cp_bgm_pos
  265.       i += CP::FADE_AUDIO::OFFSET
  266.       return i
  267.     end
  268.    
  269.     def cross_bgs
  270.       i = cp_bgs_pos
  271.       i += CP::FADE_AUDIO::OFFSET
  272.       return i
  273.     end
  274.   end
  275. end
  276.  
  277.  
  278. ###--------------------------------------------------------------------------###
  279. #  End of script.                                                              #
  280. ###--------------------------------------------------------------------------###
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement