Guest User

FModEX RGSS2

a guest
May 12th, 2012
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 66.95 KB | None | 0 0
  1. #==============================================================================
  2. # ** FMOD Ex Audio
  3. #------------------------------------------------------------------------------
  4. # Script by : Hiretsukan (Kevin Gadd)
  5. # Modified by: : RPG/Cowlol (Firas Assad)
  6. # ArePeeGee (AIM name)
  7. # Last Update : September 23rd, 2008
  8. # Version : 1.5
  9. #------------------------------------------------------------------------------
  10. # A rewrite of the built-in Audio module to extend its functionality
  11. # using the FMOD Ex library (http://www.fmod.org/). In particular,
  12. # it supports several new formats and has the ability to get current
  13. # playing position and seek to a new one, and to set loop points.
  14. # Extensions to Game_System and Scene_Battle makes the memorize BGM/BGS
  15. # event command remember position as well, and map music to resume
  16. # playing from the position it stopped at before a battle, instead
  17. # of starting all over like default RMXP behavior.
  18. #------------------------------------------------------------------------------
  19. # Usage:
  20. #
  21. # You need to copy the file fmodex.dll to your game folder (folder where
  22. # your Game.exe and project file are). I've provided the DLL with the
  23. # demo, and you can also get the latest version from FMOD's official
  24. # website (http://www.fmod.org/index.php/download).
  25. #
  26. # There are three scripts that could be used, the real script is only
  27. # the first one, but the other scripts are provided for convenience:
  28. #
  29. # FModEx
  30. # This includes the core functionality and the Audio module
  31. # rewrite. It's the main script and should be placed on
  32. # top of all scripts using it. Most people would just add a
  33. # new section above the Game_System or Game_Temp section at
  34. # the very top of the script editor and paste script there.
  35. # Game_System
  36. # This script rewrites parts of the Game_System class to
  37. # allow memorize_bgm to support remembering position,
  38. # and to allow the play methods to take an extra parameter
  39. # specifying position to start playing from.
  40. # Add it anywhere under the Game_System script section.
  41. # If you don't need any of these features you don't need this
  42. # script.
  43. # Other Classes
  44. # This is really an optional script that modifies the default
  45. # battle system to memorize map BGM and position instead of
  46. # starting over after the battle. Add it anywhere under
  47. # the Scene_Battle classes.
  48. #
  49. # Aside from integration with RMXP's default scripts, you can use
  50. # this script the same way you used the old Audio class You could also
  51. # access the FMod module for more options at a lower level, such as
  52. # setting loop points (FMod.bgm_set_loop_points(first, second) in
  53. # milliseconds) and getting current BGS position (FMod.bgs_position) to
  54. # name a few.
  55. #------------------------------------------------------------------------------
  56. # Compatibility:
  57. #
  58. # This script has different levels of compatibility depending on what
  59. # scripts you use:
  60. #
  61. # FModEx
  62. # This script overwrites the Audio module, but since
  63. # few if any other scripts would do that, it's pretty
  64. # much compatible with the majority of other scripts
  65. # without modifications.
  66. # Game_System
  67. # The following methods of Game_System are redefined:
  68. # bgm_play, bgm_restore, bgs_play, bgs_restore
  69. # Any other script that redefines these methods may
  70. # not work well with this one. Most scripts wouldn't
  71. # do that, except ones dealing with audio as well.
  72. # I've marked the areas in the script where I made
  73. # changes to help you resolve any conflicts
  74. # The following methods of Game_System are aliased:
  75. # bgm_memorize, bgs_memorize
  76. # Other scripts that redefine these methods should
  77. # be placed above the Game_System script.
  78. # Other Classes
  79. # The following methods of Scene_Battle are redefined:
  80. # judge, start_phase5, update_phase5
  81. # Any other script that redefines these methods may
  82. # not work well with this one. Custom battle system
  83. # scripts may do that, so you might consider not
  84. # including the Other Classes section and just
  85. # manually add position memorizing functionality.
  86. # I've marked the areas in the script where I made
  87. # changes to help you resolve any conflicts
  88. # The following methods of Game_Temp/Scene_Map are aliased:
  89. # Game_Temp#initialize, Scene_Map #call_battle
  90. # Other scripts that redefine these methods should
  91. # be placed above the Other Classes script.
  92. #
  93. # So in other words, FModEx is the most compatible, followed by
  94. # Game_System, and finally the last and optional Other Classes.
  95. # If you use a custom battle system you probably shouldn't include
  96. # Other Classes anyway. This isn't an SDK script and I never tested
  97. # it with the SDK. It'll probably work, but refer to SDK documentation
  98. # for more information.
  99. #------------------------------------------------------------------------------
  100. # Version Info:
  101. # - Version 1.5:
  102. # - Made the Volume and Pitch paramters to Audio ME/SE playing
  103. # methods optional. (Thanks panchokoster!)
  104. # - Script now uses FMOD's software mixer instead of hardware
  105. # acceleration. This solves issues with certain sound cards.
  106. # - A message is now displayed when a file isn't found, instead
  107. # of just throwing the error number.
  108. # - Added an independent thread to handle updating Audio module,
  109. # instead of calling it in $game_system#update. This should
  110. # ensure that it's called in all scenes. (Thanks Zeriab!)
  111. # - Updated fading calculations to depend on seconds instead
  112. # of game frames.
  113. # - Version 1.4:
  114. # - Fixed a bug where file isn't found if RTP path doesn't end
  115. # with a backslash (thanks Atoa!).
  116. # - Added BGM fading in after a ME is done playing to mimic
  117. # original Audio class behavior, the volume increment when
  118. # fading is specified by constant Audio::BGM_FADE_IN_INCREMENT.
  119. # - Several minor bug fixes and minor behavior changes relating
  120. # to fading out and stopping sounds.
  121. # - Version 1.3:
  122. # - Fixed a bug with ME fading out.
  123. # - Added methods to get BGM and BGS length.
  124. # - Providing -1 as loop end point to set_loop_points methods
  125. # makes the sound file's end the loop end point.
  126. # - Changed implementation of set_loop_points a bit.
  127. # - Position of BGM/BGS to be played after fading is now
  128. # remembered instead of starting all over.
  129. # - Version 1.2:
  130. # - Fully documented the script, and fixed some bugs.
  131. # - Completely rewrote Audio module, allowing FMOD to handle
  132. # BGMs, BGSs, MEs, and SEs except of just BGMs.
  133. # - Fixed RTP reading to use registry instead of special files..
  134. # - Version 1.1:
  135. # - Added position tracking and adjusting.
  136. # - Added loop point support.
  137. # - Implemented BGM fading.
  138. # - Version 1.0:
  139. # - Hiretsukan (Kevin Gadd)'s initial release.
  140. #------------------------------------------------------------------------------
  141. # Known bugs:
  142. #
  143. # - MIDI abrupt start when seeking or restoring from position
  144. # - Found a bug or have some ideas for the next version? Please tell me!
  145. #------------------------------------------------------------------------------
  146. # Terms of Use:
  147. #
  148. # Use of this script is subject to the permissive BSD-like license below.
  149. # That basically means you could use it in any way you like as long
  150. # as you keep the following copyright and license unchanged and available,
  151. # and don't use name of copyright holder to promote products based on
  152. # this software. Note, however, that this license only applies to the
  153. # script, and not to the FMOD library. For more information about FMOD
  154. # licenses consult FMOD website: http://www.fmod.org/index.php/sales
  155. # It's free for non-commercial use, and they provide several types
  156. # of licenses for different types of developers.
  157. #
  158. # Copyright (c) 2005, Kevin Gadd
  159. # All rights reserved.
  160. #
  161. # Redistribution and use in source and binary forms, with or without
  162. # modification, are permitted provided that the following conditions are met:
  163. # * Redistributions of source code must retain the above copyright
  164. # notice, this list of conditions and the following disclaimer.
  165. # * Redistributions in binary form must reproduce the above copyright
  166. # notice, this list of conditions and the following disclaimer in the
  167. # documentation and/or other materials provided with the distribution.
  168. # * The name of the contributors may not be used to endorse or promote
  169. # products derived from this software without specific prior written
  170. # permission.
  171. #
  172. # THIS SOFTWARE IS PROVIDED BY Kevin Gadd ''AS IS'' AND ANY
  173. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  174. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  175. # DISCLAIMED. IN NO EVENT SHALL Kevin Gadd BE LIABLE FOR ANY
  176. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  177. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  178. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  179. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  180. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  181. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  182. #==============================================================================
  183.  
  184. #==============================================================================
  185. # ** FModEx
  186. #------------------------------------------------------------------------------
  187. # FMOD Ex binding by Kevin Gadd ([email protected])
  188. #==============================================================================
  189.  
  190. module FModEx
  191. #--------------------------------------------------------------------------
  192. # * Constants
  193. #--------------------------------------------------------------------------
  194. # FMOD_INITFLAGS flags
  195. FMOD_INIT_NORMAL = 0
  196. # FMOD_RESULT flags
  197. FMOD_OK = 0
  198. FMOD_ERR_CHANNEL_STOLEN = 11
  199. FMOD_ERR_FILE_NOT_FOUND = 23
  200. FMOD_ERR_INVALID_HANDLE = 36
  201. # FMOD_MODE flags
  202. FMOD_DEFAULT = 0
  203. FMOD_LOOP_OFF = 1
  204. FMOD_LOOP_NORMAL = 2
  205. FMOD_LOOP_BIDI = 4
  206. FMOD_LOOP_BITMASK = 7
  207. FMOD_2D = 8
  208. FMOD_3D = 16
  209. FMOD_HARDWARE = 32
  210. FMOD_SOFTWARE = 64
  211. FMOD_CREATESTREAM = 128
  212. FMOD_CREATESAMPLE = 256
  213. FMOD_OPENUSER = 512
  214. FMOD_OPENMEMORY = 1024
  215. FMOD_OPENRAW = 2048
  216. FMOD_OPENONLY = 4096
  217. FMOD_ACCURATETIME = 8192
  218. FMOD_MPEGSEARCH = 16384
  219. FMOD_NONBLOCKING = 32768
  220. FMOD_UNIQUE = 65536
  221. # The default mode that the script uses
  222. FMOD_DEFAULT_SOFTWARWE = FMOD_LOOP_OFF | FMOD_2D | FMOD_SOFTWARE
  223. # FMOD_CHANNELINDEX flags
  224. FMOD_CHANNEL_FREE = -1
  225. FMOD_CHANNEL_REUSE = -2
  226. # FMOD_TIMEUNIT_flags
  227. FMOD_TIMEUNIT_MS = 1
  228. FMOD_TIMEUNIT_PCM = 2
  229. # The default time unit the script uses
  230. FMOD_DEFAULT_UNIT = FMOD_TIMEUNIT_MS
  231. # Types supported by FMOD Ex
  232. FMOD_FILE_TYPES = ['ogg', 'aac', 'wma', 'mp3', 'wav', 'it', 'xm', 'mod', 's3m', 'mid', 'midi']
  233.  
  234. #============================================================================
  235. # ** DLL
  236. #----------------------------------------------------------------------------
  237. # A class that manages importing functions from the DLL
  238. #============================================================================
  239.  
  240. class DLL
  241. #--------------------------------------------------------------------------
  242. # * Public Instance Variables
  243. #--------------------------------------------------------------------------
  244. attr_accessor :filename # DLL file name for instance
  245. attr_accessor :functions # hash of functions imported (by name)
  246. #--------------------------------------------------------------------------
  247. # * Object Initialization
  248. # filename : Name of the DLL
  249. #--------------------------------------------------------------------------
  250. def initialize(filename = 'fmodex.dll')
  251. @filename = filename
  252. @functions = {}
  253. @handle = 0 # Handle to the DLL
  254. # Load specified library into the address space of game process
  255. w32_LL = Win32API.new('kernel32.dll', 'LoadLibrary', 'p', 'l')
  256. @handle = w32_LL.call(filename)
  257. # System functions:
  258. self.import('System_Create', 'p')
  259. self.import('System_Init', 'llll')
  260. self.import('System_Close', 'l')
  261. self.import('System_Release', 'l')
  262. self.import('System_CreateSound', 'lpllp')
  263. self.import('System_CreateStream', 'lpllp')
  264. self.import('System_PlaySound', 'llllp')
  265. # Sound functions:
  266. self.import('Sound_Release', 'l')
  267. self.import('Sound_GetMode', 'lp')
  268. self.import('Sound_SetMode', 'll')
  269. self.import('Sound_SetLoopPoints', 'lllll')
  270. self.import('Sound_GetLength', 'lpl')
  271. # Channel functions:
  272. self.import('Channel_Stop', 'l')
  273. self.import('Channel_IsPlaying', 'lp')
  274. self.import('Channel_GetPaused', 'lp')
  275. self.import('Channel_SetPaused', 'll')
  276. self.import('Channel_GetVolume', 'lp')
  277. self.import('Channel_SetVolume', 'll')
  278. self.import('Channel_GetPan', 'lp')
  279. self.import('Channel_SetPan', 'll')
  280. self.import('Channel_GetFrequency', 'lp')
  281. self.import('Channel_SetFrequency', 'll')
  282. self.import('Channel_GetPosition', 'lpl')
  283. self.import('Channel_SetPosition', 'lll')
  284. end
  285. #--------------------------------------------------------------------------
  286. # * Create a Win32API Object And Add it to Hashtable
  287. # name : Function name
  288. # args : Argument types (p = pointer, l = int, v = void)
  289. # returnType: Type of value returned by function
  290. #--------------------------------------------------------------------------
  291. def import(name, args = '', returnType = 'l')
  292. @functions[name] = Win32API.new(@filename, 'FMOD_' + name, args, returnType)
  293. end
  294. #--------------------------------------------------------------------------
  295. # * Get Function by Name
  296. # key : Function name
  297. #--------------------------------------------------------------------------
  298. def [](key)
  299. return @functions[key]
  300. end
  301. #--------------------------------------------------------------------------
  302. # * Call a Function With Passed Arguments
  303. # name : Function name
  304. # args : Argument to function
  305. #--------------------------------------------------------------------------
  306. def invoke(name, *args)
  307. fn = @functions[name]
  308. raise "function not imported: #{name}" if fn.nil?
  309. result = fn.call(*args)
  310. unless result == FMOD_OK or result == FMOD_ERR_CHANNEL_STOLEN or
  311. result == FMOD_ERR_FILE_NOT_FOUND
  312. raise "FMOD Ex returned error #{result}"
  313. end
  314. return result
  315. end
  316. #--------------------------------------------------------------------------
  317. # * Store Float as Binary Int Because Floats Can't be Passed Directly
  318. # f : Float to convert
  319. #--------------------------------------------------------------------------
  320. def convertFloat(f)
  321. # First pack the float in a string as a native binary float
  322. temp = [f].pack('f')
  323. # Then unpack the native binary float as an integer
  324. return unpackInt(temp)
  325. end
  326. #--------------------------------------------------------------------------
  327. # * Unpack Binary Data to Integer
  328. # s : String containing binary data
  329. #--------------------------------------------------------------------------
  330. def unpackInt(s)
  331. return s.unpack('l')[0]
  332. end
  333. #--------------------------------------------------------------------------
  334. # * Unpack Binary Data to Float
  335. # s : String containing binary data
  336. #--------------------------------------------------------------------------
  337. def unpackFloat(s)
  338. return s.unpack('f')[0]
  339. end
  340. #--------------------------------------------------------------------------
  341. # * Unpack Binary Data to Boolean
  342. # s : String containing binary data
  343. #--------------------------------------------------------------------------
  344. def unpackBool(s)
  345. return s.unpack('l')[0] != 0
  346. end
  347. end
  348.  
  349. #============================================================================
  350. # ** System
  351. #----------------------------------------------------------------------------
  352. # A class that manages an instance of FMOD::System
  353. #============================================================================
  354.  
  355. class System
  356. #--------------------------------------------------------------------------
  357. # * Public Instance Variables
  358. #--------------------------------------------------------------------------
  359. attr_accessor :fmod # Instance of DLL class (fmodex.dll)
  360. attr_accessor :handle # Handle (pointer) to System object
  361. attr_accessor :maxChannels # Maximum number of channels
  362. #--------------------------------------------------------------------------
  363. # * Object Initialization
  364. # fmod : An instance of DLL class
  365. # maxChannels : Maximum number of used channels
  366. # flags : FMOD_INITFLAGS
  367. # extraDriverData : Driver specific data
  368. #--------------------------------------------------------------------------
  369. def initialize(theDLL, maxChannels = 32, flags = FMOD_INIT_NORMAL, extraDriverData = 0)
  370. @fmod = theDLL
  371. @maxChannels = maxChannels
  372. # Create and initialize FMOD::System
  373. temp = 0.chr * 4
  374. @fmod.invoke('System_Create', temp)
  375. @handle = @fmod.unpackInt(temp)
  376. @fmod.invoke('System_Init', @handle, maxChannels, flags, extraDriverData)
  377. end
  378. #--------------------------------------------------------------------------
  379. # * Create FMOD::Sound (fully loaded into memory by default)
  380. # filename : Name of file to open
  381. # mode : FMOD_MODE flags
  382. #--------------------------------------------------------------------------
  383. def createSound(filename, mode = FMOD_DEFAULT_SOFTWARWE)
  384. # Create sound and return it
  385. temp = 0.chr * 4
  386. result = @fmod.invoke('System_CreateSound', @handle, filename, mode, 0, temp)
  387. raise "File not found: \"#{filename}\"" if result == FMOD_ERR_FILE_NOT_FOUND
  388. newSound = Sound.new(self, @fmod.unpackInt(temp))
  389. return newSound
  390. end
  391. #--------------------------------------------------------------------------
  392. # * Create Streamed FMOD::Sound (chunks loaded on demand)
  393. # filename : Name of file to open
  394. # mode : FMOD_MODE flags
  395. #--------------------------------------------------------------------------
  396. def createStream(filename, mode = FMOD_DEFAULT_SOFTWARWE)
  397. # Create sound and return it
  398. temp = 0.chr * 4
  399. result = @fmod.invoke('System_CreateStream', @handle, filename, mode, 0, temp)
  400. raise "File not found: \"#{filename}\"" if result == FMOD_ERR_FILE_NOT_FOUND
  401. newSound = Sound.new(self, @fmod.unpackInt(temp))
  402. return newSound
  403. end
  404. #--------------------------------------------------------------------------
  405. # * Close And Release System
  406. #--------------------------------------------------------------------------
  407. def dispose
  408. if (@handle > 0)
  409. @fmod.invoke('System_Close', @handle)
  410. @fmod.invoke('System_Release', @handle)
  411. @handle = 0
  412. end
  413. @fmod = nil
  414. end
  415. end
  416.  
  417. #============================================================================
  418. # ** Sound
  419. #----------------------------------------------------------------------------
  420. # A class that manages an instance of FMOD::Sound
  421. #============================================================================
  422.  
  423. class Sound
  424. #--------------------------------------------------------------------------
  425. # * Public Instance Variables
  426. #--------------------------------------------------------------------------
  427. attr_accessor :system # System that created this Sound
  428. attr_accessor :fmod # Instance of DLL class (fmodex.dll)
  429. attr_accessor :handle # Handle (pointer) to Sound object
  430. #--------------------------------------------------------------------------
  431. # * Object Initialization
  432. # theSystem : The System that created this Sound object
  433. # handle : Handle to the FMOD::Sound object
  434. #--------------------------------------------------------------------------
  435. def initialize(theSystem, theHandle)
  436. @system = theSystem
  437. @fmod = theSystem.fmod
  438. @handle = theHandle
  439. end
  440. #--------------------------------------------------------------------------
  441. # * Play Sound
  442. # paused : Start paused?
  443. # channel : Channel allocated to sound (nil for automatic)
  444. #--------------------------------------------------------------------------
  445. def play(paused = false, channel = nil)
  446. # If channel wasn't specified, let FMOD pick a free one,
  447. # otherwise use the passed channel (id from 0 to maxChannels)
  448. unless channel
  449. temp = 0.chr * 4
  450. else
  451. temp = [channel].pack('l')
  452. end
  453. @fmod.invoke('System_PlaySound', @system.handle,
  454. (channel == nil) ? FMOD_CHANNEL_FREE : FMOD_CHANNEL_REUSE,
  455. @handle,
  456. (paused == true) ? 1 : 0,
  457. temp)
  458. theChannel = @fmod.unpackInt(temp)
  459. # Create a Channel object based on returned channel
  460. newChannel = Channel.new(self, theChannel)
  461. return newChannel
  462. end
  463. #--------------------------------------------------------------------------
  464. # * Get FMOD_MODE Bits
  465. #--------------------------------------------------------------------------
  466. def mode
  467. temp = 0.chr * 4
  468. @fmod.invoke('Sound_GetMode', @handle, temp)
  469. return @fmod.unpackInt(temp)
  470. end
  471. #--------------------------------------------------------------------------
  472. # * Set FMOD_MODE Bits
  473. #--------------------------------------------------------------------------
  474. def mode=(newMode)
  475. @fmod.invoke('Sound_SetMode', @handle, newMode)
  476. end
  477. #--------------------------------------------------------------------------
  478. # * Get FMOD_LOOP_MODE
  479. #--------------------------------------------------------------------------
  480. def loopMode
  481. temp = 0.chr * 4
  482. @fmod.invoke('Sound_GetMode', @handle, temp)
  483. return @fmod.unpackInt(temp) & FMOD_LOOP_BITMASK
  484. end
  485. #--------------------------------------------------------------------------
  486. # * Set FMOD_LOOP_MODE
  487. #--------------------------------------------------------------------------
  488. def loopMode=(newMode)
  489. @fmod.invoke('Sound_SetMode', @handle, (self.mode & ~FMOD_LOOP_BITMASK) | newMode)
  490. end
  491. #--------------------------------------------------------------------------
  492. # * Return Sound Length
  493. #--------------------------------------------------------------------------
  494. def length(unit = FMOD_DEFAULT_UNIT)
  495. temp = 0.chr * 4
  496. @fmod.invoke('Sound_GetLength', @handle, temp, unit)
  497. return @fmod.unpackInt(temp)
  498. end
  499. #--------------------------------------------------------------------------
  500. # * Set Loop Points
  501. # first : Loop start point in milliseconds
  502. # second : Loop end point in milliseconds
  503. # unit : FMOD_TIMEUNIT for points
  504. #--------------------------------------------------------------------------
  505. def setLoopPoints(first, second, unit = FMOD_DEFAULT_UNIT)
  506. @fmod.invoke('Sound_SetLoopPoints', @handle, first, unit, second, unit)
  507. end
  508. #--------------------------------------------------------------------------
  509. # * Release Sound
  510. #--------------------------------------------------------------------------
  511. def dispose
  512. if (@handle > 0)
  513. @fmod.invoke('Sound_Release', @handle)
  514. @handle = 0
  515. end
  516. @fmod = nil
  517. @system = nil
  518. end
  519. end
  520.  
  521. #============================================================================
  522. # ** Channel
  523. #----------------------------------------------------------------------------
  524. # A class that represents an FMOD::Channel
  525. #============================================================================
  526.  
  527. class Channel
  528. #--------------------------------------------------------------------------
  529. # * Public Instance Variables
  530. #--------------------------------------------------------------------------
  531. attr_accessor :system # System that created the Sound
  532. attr_accessor :sound # Sound using the Channel
  533. attr_accessor :fmod # Instance of DLL class (fmodex.dll)
  534. attr_accessor :handle # Handle (pointer) to Sound object
  535. #--------------------------------------------------------------------------
  536. # * Object Initialization
  537. # theSound : The Sound using this Channel object
  538. # handle : Handle to the FMOD::Channel object
  539. #--------------------------------------------------------------------------
  540. def initialize(theSound, theHandle)
  541. @sound = theSound
  542. @system = theSound.system
  543. @fmod = theSound.system.fmod
  544. @handle = theHandle
  545. end
  546. #--------------------------------------------------------------------------
  547. # * Stop Channel and Make it Available for Other Sounds
  548. #--------------------------------------------------------------------------
  549. def stop
  550. @fmod.invoke('Channel_Stop', @handle)
  551. end
  552. #--------------------------------------------------------------------------
  553. # * Is the Channel Handle Valid?
  554. #--------------------------------------------------------------------------
  555. def valid?
  556. temp = 0.chr * 4
  557. begin
  558. result = @fmod.invoke('Channel_IsPlaying', @handle, temp)
  559. rescue
  560. if (result == FMOD_ERR_INVALID_HANDLE)
  561. return false
  562. else
  563. raise
  564. end
  565. end
  566. # If we get here then it's valid
  567. return true
  568. end
  569. #--------------------------------------------------------------------------
  570. # * Is the Channel Playing?
  571. #--------------------------------------------------------------------------
  572. def playing?
  573. temp = 0.chr * 4
  574. @fmod.invoke('Channel_IsPlaying', @handle, temp)
  575. return @fmod.unpackBool(temp)
  576. end
  577. #--------------------------------------------------------------------------
  578. # * Get Channel Volume Level (0.0 -> 1.0)
  579. #--------------------------------------------------------------------------
  580. def volume
  581. temp = 0.chr * 4
  582. @fmod.invoke('Channel_GetVolume', @handle, temp)
  583. return @fmod.unpackFloat(temp)
  584. end
  585. #--------------------------------------------------------------------------
  586. # * Set Channel Volume Level (0.0 -> 1.0)
  587. #--------------------------------------------------------------------------
  588. def volume=(newVolume)
  589. @fmod.invoke('Channel_SetVolume', @handle, @fmod.convertFloat(newVolume))
  590. end
  591. #--------------------------------------------------------------------------
  592. # * Get Channel Pan Position (-1.0 -> 1.0)
  593. #--------------------------------------------------------------------------
  594. def pan
  595. temp = 0.chr * 4
  596. @fmod.invoke('Channel_GetPan', @handle, temp)
  597. return @fmod.unpackFloat(temp)
  598. end
  599. #--------------------------------------------------------------------------
  600. # * Set Channel Pan Position (-1.0 -> 1.0)
  601. #--------------------------------------------------------------------------
  602. def pan=(newPan)
  603. @fmod.invoke('Channel_SetPan', @handle, @fmod.convertFloat(newPan))
  604. end
  605. #--------------------------------------------------------------------------
  606. # * Get Channel Frequency in HZ (Speed/Pitch)
  607. #--------------------------------------------------------------------------
  608. def frequency
  609. temp = 0.chr * 4
  610. @fmod.invoke('Channel_GetFrequency', @handle, temp)
  611. return @fmod.unpackFloat(temp)
  612. end
  613. #--------------------------------------------------------------------------
  614. # * Set Channel Frequency in HZ (Speed/Pitch)
  615. #--------------------------------------------------------------------------
  616. def frequency=(newFrequency)
  617. @fmod.invoke('Channel_SetFrequency', @handle, @fmod.convertFloat(newFrequency))
  618. end
  619. #--------------------------------------------------------------------------
  620. # * Is Channel Paused?
  621. #--------------------------------------------------------------------------
  622. def paused
  623. temp = 0.chr * 4
  624. @fmod.invoke('Channel_GetPaused', @handle, temp)
  625. return @fmod.unpackBool(temp)
  626. end
  627. #--------------------------------------------------------------------------
  628. # * Pause Channel
  629. #--------------------------------------------------------------------------
  630. def paused=(newPaused)
  631. @fmod.invoke('Channel_SetPaused', @handle, (newPaused == true) ? 1 : 0)
  632. end
  633. #--------------------------------------------------------------------------
  634. # * Get Current Playback Position
  635. # unit : FMOD_TIMEUNIT to return position in
  636. #--------------------------------------------------------------------------
  637. def position(unit = FMOD_DEFAULT_UNIT)
  638. temp = 0.chr * 4
  639. @fmod.invoke('Channel_GetPosition', @handle, temp, unit)
  640. return @fmod.unpackInt(temp)
  641. end
  642. #--------------------------------------------------------------------------
  643. # * Set Current Playback Position
  644. # newPosition : New playback position
  645. # unit : FMOD_TIMEUNIT to use when setting position
  646. #--------------------------------------------------------------------------
  647. def position=(newPosition, unit = FMOD_DEFAULT_UNIT)
  648. @fmod.invoke('Channel_SetPosition', @handle, newPosition, unit)
  649. end
  650. #--------------------------------------------------------------------------
  651. # * Dispose of Channel
  652. #--------------------------------------------------------------------------
  653. def dispose
  654. @handle = 0
  655. @sound = nil
  656. @system = nil
  657. @fmod = nil
  658. end
  659. end
  660.  
  661. end
  662.  
  663. #==============================================================================
  664. # ** FMod
  665. #------------------------------------------------------------------------------
  666. # A higher level module to access FMOD Ex
  667. #==============================================================================
  668.  
  669. module FMod
  670.  
  671. #============================================================================
  672. # ** SoundFile
  673. #----------------------------------------------------------------------------
  674. # Represents a Sound file (BGM, BGS, SE, etc.) and associated Channel
  675. #============================================================================
  676.  
  677. class SoundFile
  678. #--------------------------------------------------------------------------
  679. # * Public Instance Variables
  680. #--------------------------------------------------------------------------
  681. attr_accessor :name # File name
  682. attr_accessor :sound # FModEx::Sound object
  683. attr_accessor :channel # Channel playing sound
  684. attr_accessor :volume # Volume in RPG::AudioFile format
  685. attr_accessor :pitch # Pitch in RPG::AudioFile format
  686. attr_accessor :looping # Sound loops
  687. attr_accessor :streaming # Sound is streamed
  688. attr_accessor :length # Sound length in milliseconds
  689. #--------------------------------------------------------------------------
  690. # * Object Initialization
  691. #--------------------------------------------------------------------------
  692. def initialize(name, sound, channel, volume, pitch, looping, streaming, length)
  693. @name = name
  694. @sound = sound
  695. @channel = channel
  696. @volume = volume
  697. @pitch = pitch
  698. @looping = looping
  699. @streaming = streaming
  700. @length = length
  701. end
  702. end
  703. #--------------------------------------------------------------------------
  704. # * Instance Variables
  705. #--------------------------------------------------------------------------
  706. @fmod_dll = FModEx::DLL.new # The FMOD Ex DLL
  707. @fmod = FModEx::System.new(@fmod_dll) # The global System object
  708. @fmod_se = [] # Array of Sound Effects
  709. @rtp_folder = nil # Name of RTP folder
  710. #--------------------------------------------------------------------------
  711. # * Get Path of RTP Folder From Registry
  712. #--------------------------------------------------------------------------
  713. def self.getRTPFolder
  714. if @rtp_folder
  715. return @rtp_folder
  716. end
  717. open_key = Win32API.new('advapi32.dll', 'RegOpenKeyExA', 'LPLLP', 'L')
  718. query_value = Win32API.new('advapi32.dll', 'RegQueryValueExA', 'LPLPPP', 'L')
  719. close_key = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L')
  720. key = 0.chr * 4
  721. # Open a HKEY_LOCAL_MACHINE with KEY_READ attribute and save handle in key
  722. open_key.call(0x80000002, 'Software\Enterbrain\RGSS\RTP', 0, 0x20019, key)
  723. key = @fmod_dll.unpackInt(key)
  724. type = 0.chr * 4
  725. size = 0.chr * 4
  726. # Query to get string size
  727. query_value.call(key, 'Standard', 0, type, 0, size)
  728. data = ' ' * @fmod_dll.unpackInt(size)
  729. # Query the string value itself using size
  730. query_value.call(key, 'Standard', 0, type, data, size)
  731. @rtp_folder = data.chop
  732. close_key.call(key)
  733. # Make sure the directory ends with a backslash
  734. @rtp_folder += "\\" if @rtp_folder[-1].chr != "\\"
  735. return @rtp_folder
  736. end
  737. #--------------------------------------------------------------------------
  738. # * Return Proper File Name (With Extensions)
  739. # name : Name of the file
  740. # extensions : Extensions to add to file name
  741. #--------------------------------------------------------------------------
  742. def self.checkExtensions(name, extensions)
  743. if FileTest.exist?(name)
  744. return name
  745. end
  746. # Add extension if needed
  747. extensions.each do |ext|
  748. if FileTest.exist?(name + '.' + ext)
  749. return name + '.' + ext
  750. end
  751. end
  752. # File doesn't exist
  753. return name
  754. end
  755. #--------------------------------------------------------------------------
  756. # * Get Valid File Name
  757. # name : Name of the file
  758. #--------------------------------------------------------------------------
  759. def self.selectBGMFilename(name)
  760. name = name.gsub("/", "\\")
  761. # See if file exists in game folder
  762. localname = self.checkExtensions(name, FModEx::FMOD_FILE_TYPES)
  763. # See if file exists in RTP
  764. commonname = self.checkExtensions(getRTPFolder + name, FModEx::FMOD_FILE_TYPES)
  765. if FileTest.exist?(localname)
  766. return localname
  767. end
  768. if FileTest.exist?(commonname)
  769. return commonname
  770. end
  771. # An invalid name was provided
  772. return name
  773. end
  774. #--------------------------------------------------------------------------
  775. # * Play a Sound File Then Return it
  776. # name : Name of the file
  777. # volume : Channel volume
  778. # pitch : Channel frequency
  779. # position : Starting position in milliseconds
  780. # looping : Does the sound loop?
  781. # streaming : Stream sound or load whole thing to memory?
  782. #--------------------------------------------------------------------------
  783. def self.play(name, volume, pitch, position, looping, streaming)
  784. # Get a valid file name
  785. filename = self.selectBGMFilename(name)
  786. # Create Sound or Stream and set initial values
  787. sound = streaming ? @fmod.createStream(filename) : @fmod.createSound(filename)
  788. sound.loopMode = looping ? FModEx::FMOD_LOOP_NORMAL : FModEx::FMOD_LOOP_OFF
  789. channel = sound.play
  790. volume = volume * 1.0
  791. pitch = pitch * 1.0
  792. file_length = sound.length(FModEx::FMOD_DEFAULT_UNIT)
  793. sound_file = SoundFile.new(filename, sound, channel, volume,
  794. pitch, looping, streaming, file_length)
  795. sound_file.channel.volume = volume / 100.0
  796. sound_file.channel.frequency = sound_file.channel.frequency * pitch / 100
  797. sound_file.channel.position = position
  798. return sound_file
  799. end
  800. #--------------------------------------------------------------------------
  801. # * Stop and Dispose of Sound File
  802. #--------------------------------------------------------------------------
  803. def self.stop(sound_file)
  804. unless sound_file and sound_file.channel
  805. return
  806. end
  807. # Stop channel, then clear variables and dispose of bgm
  808. sound_file.channel.stop
  809. sound_file.channel = nil
  810. sound_file.sound.dispose
  811. end
  812. #--------------------------------------------------------------------------
  813. # * Return Length in Milliseconds
  814. #--------------------------------------------------------------------------
  815. def self.get_length(sound_file, unit = FModEx::FMOD_DEFAULT_UNIT)
  816. return sound_file.length(unit)
  817. end
  818. #--------------------------------------------------------------------------
  819. # * Check if Another Sound File is Playing
  820. #--------------------------------------------------------------------------
  821. def self.already_playing?(sound_file, name, position = 0)
  822. # Get a valid file name
  823. filename = self.selectBGMFilename(name)
  824. if (sound_file)
  825. # If the same sound file is already playing don't play it again
  826. if (sound_file.name == filename and position == 0)
  827. return true
  828. end
  829. # If another sound file is playing, stop it
  830. if sound_file.channel
  831. self.stop(sound_file)
  832. end
  833. end
  834. # No sound file is playing or it was already stopped
  835. return false
  836. end
  837. #--------------------------------------------------------------------------
  838. # * Check if Sound File is Playing
  839. #--------------------------------------------------------------------------
  840. def self.playing?(sound_file)
  841. unless sound_file and sound_file.channel
  842. return false
  843. end
  844. return sound_file.channel.playing?
  845. end
  846. #--------------------------------------------------------------------------
  847. # * Get Current Sound File Playing Position
  848. #--------------------------------------------------------------------------
  849. def self.get_position(sound_file)
  850. unless sound_file and sound_file.channel
  851. return 0
  852. end
  853. return sound_file.channel.position
  854. end
  855. #--------------------------------------------------------------------------
  856. # * Seek to a New Sound File Playing Position
  857. #--------------------------------------------------------------------------
  858. def self.set_position(sound_file, new_pos)
  859. unless sound_file and sound_file.channel
  860. return
  861. end
  862. sound_file.channel.position = new_pos
  863. end
  864. #--------------------------------------------------------------------------
  865. # * Get Current Sound File Volume
  866. #--------------------------------------------------------------------------
  867. def self.get_volume(sound_file)
  868. unless sound_file
  869. return 0
  870. end
  871. return sound_file.volume
  872. end
  873. #--------------------------------------------------------------------------
  874. # * Set Sound File Volume
  875. #--------------------------------------------------------------------------
  876. def self.set_volume(sound_file, volume)
  877. unless sound_file and sound_file.channel
  878. return
  879. end
  880. sound_file.volume = volume * 1.0
  881. sound_file.channel.volume = volume / 100.0
  882. end
  883. #--------------------------------------------------------------------------
  884. # * Set Loop Points
  885. # first : Loop start point in milliseconds
  886. # second : Loop end point in milliseconds (-1 for file end)
  887. # unit : FMOD_TIMEUNIT for points
  888. #--------------------------------------------------------------------------
  889. def self.set_loop_points(sound_file, first, second, unit = FModEx::FMOD_DEFAULT_UNIT)
  890. unless sound_file and sound_file.channel
  891. return
  892. end
  893. # If second is -1 then set loop end to the file end
  894. if second == -1
  895. second = sound_file.length - 1
  896. end
  897. # Set loop points and reflush stream buffer
  898. sound_file.channel.sound.setLoopPoints(first, second, unit)
  899. sound_file.channel.position = sound_file.channel.position
  900. return sound_file
  901. end
  902. #--------------------------------------------------------------------------
  903. # * Play BGM (or ME)
  904. # name : Name of the file
  905. # volume : Channel volume
  906. # pitch : Channel frequency
  907. # position : Starting position in milliseconds
  908. # looping : Does the BGM loop?
  909. #--------------------------------------------------------------------------
  910. def self.bgm_play(name, volume, pitch, position = 0, looping = true)
  911. return if self.already_playing?(@fmod_bgm, name, position)
  912. # Now play the new BGM as a stream
  913. @fmod_bgm = self.play(name, volume, pitch, position, looping, true)
  914. end
  915. #--------------------------------------------------------------------------
  916. # * Stop and Dispose of BGM
  917. #--------------------------------------------------------------------------
  918. def self.bgm_stop
  919. self.stop(@fmod_bgm)
  920. @fmod_bgm = nil
  921. end
  922. #--------------------------------------------------------------------------
  923. # * Return BGM Length in Milliseconds
  924. #--------------------------------------------------------------------------
  925. def self.bgm_length(sound_file)
  926. self.get_length(@fmod_bgm)
  927. end
  928. #--------------------------------------------------------------------------
  929. # * Check if a BGM is Playing
  930. #--------------------------------------------------------------------------
  931. def self.bgm_playing?
  932. return self.playing?(@fmod_bgm)
  933. end
  934. #--------------------------------------------------------------------------
  935. # * Get Current BGM Playing Position
  936. #--------------------------------------------------------------------------
  937. def self.bgm_position
  938. return self.get_position(@fmod_bgm)
  939. end
  940. #--------------------------------------------------------------------------
  941. # * Seek to New BGM Playing Position
  942. #--------------------------------------------------------------------------
  943. def self.bgm_position=(new_pos)
  944. self.set_position(@fmod_bgm, new_pos)
  945. end
  946. #--------------------------------------------------------------------------
  947. # * Get Current BGM Volume
  948. #--------------------------------------------------------------------------
  949. def self.bgm_volume
  950. return self.get_volume(@fmod_bgm)
  951. end
  952. #--------------------------------------------------------------------------
  953. # * Set BGM Volume
  954. #--------------------------------------------------------------------------
  955. def self.bgm_volume=(volume)
  956. self.set_volume(@fmod_bgm, volume)
  957. end
  958. #--------------------------------------------------------------------------
  959. # * Set Loop Points
  960. # first : Loop start point in milliseconds
  961. # second : Loop end point in milliseconds
  962. # unit : FMOD_TIMEUNIT for points
  963. #--------------------------------------------------------------------------
  964. def self.bgm_set_loop_points(first, second, unit = FModEx::FMOD_DEFAULT_UNIT)
  965. @fmod_bgm = self.set_loop_points(@fmod_bgm, first, second, unit)
  966. end
  967. #--------------------------------------------------------------------------
  968. # * Play BGS
  969. # name : Name of the file
  970. # volume : Channel volume
  971. # pitch : Channel frequency
  972. # position : Starting position in milliseconds
  973. # looping : Does the BGS loop?
  974. #--------------------------------------------------------------------------
  975. def self.bgs_play(name, volume, pitch, position = 0, looping = true)
  976. return if self.already_playing?(@fmod_bgs, name, position)
  977. # Now play the new BGS as a stream
  978. @fmod_bgs = self.play(name, volume, pitch, position, looping, true)
  979. end
  980. #--------------------------------------------------------------------------
  981. # * Stop and Dispose of BGS
  982. #--------------------------------------------------------------------------
  983. def self.bgs_stop
  984. self.stop(@fmod_bgs)
  985. @fmod_bgs = nil
  986. end
  987. #--------------------------------------------------------------------------
  988. # * Return BGS Length in Milliseconds
  989. #--------------------------------------------------------------------------
  990. def self.bgm_length(sound_file)
  991. self.get_length(@fmod_bgs)
  992. end
  993. #--------------------------------------------------------------------------
  994. # * Check if a BGS is Playing
  995. #--------------------------------------------------------------------------
  996. def self.bgs_playing?
  997. return self.playing?(@fmod_bgs)
  998. end
  999. #--------------------------------------------------------------------------
  1000. # * Get Current BGS Playing Position
  1001. #--------------------------------------------------------------------------
  1002. def self.bgs_position
  1003. return self.get_position(@fmod_bgs)
  1004. end
  1005. #--------------------------------------------------------------------------
  1006. # * Seek to New BGS Playing Position
  1007. #--------------------------------------------------------------------------
  1008. def self.bgs_position=(new_pos)
  1009. self.set_position(@fmod_bgs, new_pos)
  1010. end
  1011. #--------------------------------------------------------------------------
  1012. # * Get Current BGS Volume
  1013. #--------------------------------------------------------------------------
  1014. def self.bgs_volume
  1015. return self.get_volume(@fmod_bgs)
  1016. end
  1017. #--------------------------------------------------------------------------
  1018. # * Set BGS Volume
  1019. #--------------------------------------------------------------------------
  1020. def self.bgs_volume=(volume)
  1021. self.set_volume(@fmod_bgs, volume)
  1022. end
  1023. #--------------------------------------------------------------------------
  1024. # * Set Loop Points
  1025. # first : Loop start point in milliseconds
  1026. # second : Loop end point in milliseconds
  1027. # unit : FMOD_TIMEUNIT for points
  1028. #--------------------------------------------------------------------------
  1029. def self.bgs_set_loop_points(first, second, unit = FModEx::FMOD_DEFAULT_UNIT)
  1030. @fmod_bgs = self.set_loop_points(@fmod_bgs, first, second, unit)
  1031. end
  1032. #--------------------------------------------------------------------------
  1033. # * Play SE
  1034. # name : Name of the file
  1035. # volume : Channel volume
  1036. # pitch : Channel frequency
  1037. #--------------------------------------------------------------------------
  1038. def self.se_play(name, volume, pitch)
  1039. if @fmod_se.size > @fmod.maxChannels
  1040. se = @fmod_se.shift
  1041. self.stop(se)
  1042. end
  1043. # Load SE into memory and play it
  1044. @fmod_se << self.play(name, volume, pitch, 0, false, false)
  1045. end
  1046. #--------------------------------------------------------------------------
  1047. # * Stop and Dispose of all SEs
  1048. #--------------------------------------------------------------------------
  1049. def self.se_stop
  1050. for se in @fmod_se
  1051. self.stop(se)
  1052. end
  1053. @fmod_se.clear
  1054. end
  1055. #--------------------------------------------------------------------------
  1056. # * Get Rid of Non-Playing SEs
  1057. #--------------------------------------------------------------------------
  1058. def self.se_clean
  1059. for se in @fmod_se
  1060. unless self.playing?(se)
  1061. self.stop(se)
  1062. @fmod_se.delete(se)
  1063. end
  1064. end
  1065. end
  1066. #--------------------------------------------------------------------------
  1067. # * Check if There's Some SE in SE Array
  1068. #--------------------------------------------------------------------------
  1069. def self.se_list_empty?
  1070. return @fmod_se.empty?
  1071. end
  1072. #--------------------------------------------------------------------------
  1073. # * Dispose of Everything
  1074. #--------------------------------------------------------------------------
  1075. def self.dispose
  1076. self.bgm_stop
  1077. self.bgs_stop
  1078. self.se_stop
  1079. @fmod.dispose
  1080. end
  1081. end
  1082.  
  1083. #==============================================================================
  1084. # ** Audio
  1085. #------------------------------------------------------------------------------
  1086. # The module that carries out music and sound processing.
  1087. #==============================================================================
  1088.  
  1089. module Audio
  1090. #--------------------------------------------------------------------------
  1091. # * Constants
  1092. #--------------------------------------------------------------------------
  1093. BGM_FADE_IN_INCREMENT = 5 # BGM volume incremented 0.2 seconds
  1094. #--------------------------------------------------------------------------
  1095. # * Instance Variables
  1096. #--------------------------------------------------------------------------
  1097. @bgm_fading_out = false # BGM started fading out
  1098. @bgm_fade_decrement = 0.0 # BGM volume decremented each update
  1099. @bgs_fading_out = false # BGS started fading out
  1100. @bgs_fade_decrement = 0.0 # BGS volume decremented each update
  1101. @me_fading_out = false # ME started fading out
  1102. @me_fade_decrement = 0.0 # ME volume decremented each update
  1103. @me_playing = false # Is some ME playing?
  1104. @playing_bgm = nil # BGM currently being played
  1105. @next_bgm = nil # The BGM to be played after fading out
  1106. @next_bgm_position = 0 # Starting position of next bgm
  1107. @next_bgs = nil # The BGS to be played after fading out
  1108. @next_bgs_position = 0 # Starting position of next bgm
  1109. @next_me = nil # The ME to be played after fading
  1110. #--------------------------------------------------------------------------
  1111. # * Starts BGM Playback
  1112. # name : Name of the file
  1113. # volume : Channel volume
  1114. # pitch : Channel frequency
  1115. # position : Starting position in milliseconds
  1116. #--------------------------------------------------------------------------
  1117. def Audio.bgm_play(filename, volume = 100, pitch = 100, position = 0,
  1118. fade_in = false)
  1119. if @bgm_fading_out and !fade_in
  1120. @next_bgm = RPG::AudioFile.new(filename, volume, pitch)
  1121. @next_bgm_position = position
  1122. return
  1123. end
  1124. start_volume = volume
  1125. if fade_in
  1126. @bgm_target_volume = volume unless @bgm_fading_in
  1127. @bgm_fading_in = true
  1128. start_volume = 0
  1129. end
  1130. @bgm_fading_out = false
  1131. # If a ME is playing we wait until it's over before playing BGM
  1132. unless @me_playing
  1133. FMod::bgm_play(filename, start_volume, pitch, position)
  1134. end
  1135. @playing_bgm = RPG::AudioFile.new(filename, volume, pitch)
  1136. @memorized_bgm = @playing_bgm
  1137. @memorized_bgm_position = position
  1138. end
  1139. #--------------------------------------------------------------------------
  1140. # * Stops BGM Playback
  1141. #--------------------------------------------------------------------------
  1142. def Audio.bgm_stop
  1143. @memorized_bgm = nil
  1144. @playing_bgm = nil
  1145. @bgm_fading_in = false
  1146. @bgm_fading_out = false
  1147. # MEs are internally BGMs, but are stopped with me_stop instead
  1148. if @me_playing
  1149. return
  1150. end
  1151. FMod::bgm_stop
  1152. end
  1153. #--------------------------------------------------------------------------
  1154. # * Starts BGM fadeout.
  1155. # time : Length of the fadeout in milliseconds.
  1156. #--------------------------------------------------------------------------
  1157. def Audio.bgm_fade(time)
  1158. return if @me_playing or !FMod::bgm_playing?
  1159. @bgm_fading_out = true
  1160. time = time / 1000
  1161. @bgm_fade_decrement = FMod::bgm_volume / (time * 5)
  1162. end
  1163. #--------------------------------------------------------------------------
  1164. # * Starts BGS Playback
  1165. # name : Name of the file
  1166. # volume : Channel volume
  1167. # pitch : Channel frequency
  1168. # position : Starting position in milliseconds
  1169. #--------------------------------------------------------------------------
  1170. def Audio.bgs_play(filename, volume = 100, pitch = 100, position = 0)
  1171. if @bgs_fading_out
  1172. @next_bgs = RPG::AudioFile.new(filename, volume, pitch)
  1173. @next_bgs_position = position
  1174. return
  1175. end
  1176. FMod::bgs_play(filename, volume, pitch, position)
  1177. end
  1178. #--------------------------------------------------------------------------
  1179. # * Stops BGS Playback
  1180. #--------------------------------------------------------------------------
  1181. def Audio.bgs_stop
  1182. FMod::bgs_stop
  1183. @bgs_fading_out = false
  1184. end
  1185. #--------------------------------------------------------------------------
  1186. # * Starts BGS fadeout.
  1187. # time : Length of the fadeout in milliseconds.
  1188. #--------------------------------------------------------------------------
  1189. def Audio.bgs_fade(time)
  1190. return unless FMod::bgs_playing?
  1191. @bgs_fading_out = true
  1192. time = time / 1000
  1193. @bgs_fade_decrement = FMod::bgs_volume / (time * 5)
  1194. end
  1195. #--------------------------------------------------------------------------
  1196. # * Starts ME Playback
  1197. # name : Name of the file
  1198. # volume : Channel volume
  1199. # pitch : Channel frequency
  1200. #--------------------------------------------------------------------------
  1201. def Audio.me_play(filename, volume = 100, pitch = 100)
  1202. if @me_fading_out
  1203. @next_me = RPG::AudioFile.new(filename, volume, pitch)
  1204. return
  1205. end
  1206. if @bgm_fading_out
  1207. self.bgm_stop
  1208. end
  1209. # Memorize playing bgm
  1210. if @playing_bgm and !@me_playing
  1211. bgm = @playing_bgm
  1212. @playing_bgm = RPG::AudioFile.new(bgm.name, FMod::bgm_volume, bgm.pitch)
  1213. @memorized_bgm = @playing_bgm
  1214. @memorized_bgm_position = FMod::bgm_position
  1215. end
  1216. @me_playing = true
  1217. FMod::bgm_play(filename, volume, pitch, 0, false)
  1218. end
  1219. #--------------------------------------------------------------------------
  1220. # * Stops ME Playback
  1221. #--------------------------------------------------------------------------
  1222. def Audio.me_stop
  1223. return unless @me_playing
  1224. @me_playing = false
  1225. @me_fading_out = false
  1226. # Play memorized bgm, fading in
  1227. if @memorized_bgm and !@bgm_fading_out
  1228. bgm = @memorized_bgm
  1229. self.bgm_play(bgm.name, bgm.volume, bgm.pitch, @memorized_bgm_position, true)
  1230. else
  1231. self.bgm_stop
  1232. end
  1233. end
  1234. #--------------------------------------------------------------------------
  1235. # * Starts ME fadeout.
  1236. # time : Length of the fadeout in milliseconds.
  1237. #--------------------------------------------------------------------------
  1238. def Audio.me_fade(time)
  1239. return unless FMod::bgm_playing?
  1240. @me_fading_out = true
  1241. time = time / 1000
  1242. @bgm_fade_decrement = FMod::bgm_volume / (time * 5)
  1243. end
  1244. #--------------------------------------------------------------------------
  1245. # * Starts SE Playback
  1246. # name : Name of the file
  1247. # volume : Channel volume
  1248. # pitch : Channel frequency
  1249. #--------------------------------------------------------------------------
  1250. def Audio.se_play(filename, volume = 100, pitch = 100)
  1251. FMod::se_play(filename, volume, pitch)
  1252. end
  1253. #--------------------------------------------------------------------------
  1254. # * Stops SE Playback
  1255. #--------------------------------------------------------------------------
  1256. def Audio.se_stop
  1257. FMod::se_stop
  1258. end
  1259. #--------------------------------------------------------------------------
  1260. # * Update ME Playback, SE Disposal and Fading, Called Each Frame
  1261. #--------------------------------------------------------------------------
  1262. def Audio.update
  1263. # Stop ME when it's over (and continue playing BGM)
  1264. if @me_playing
  1265. unless FMod::bgm_playing?
  1266. self.me_stop
  1267. end
  1268. end
  1269. # Remove any finished SEs
  1270. unless FMod::se_list_empty?
  1271. FMod::se_clean
  1272. end
  1273. if @bgm_fading_in
  1274. # Stop fading when target is reached, otherwise increase volume
  1275. if FMod::bgm_volume >= @bgm_target_volume
  1276. @bgm_fading_in = false
  1277. else
  1278. current_volume = FMod::bgm_volume + BGM_FADE_IN_INCREMENT
  1279. FMod::bgm_volume = current_volume
  1280. end
  1281. end
  1282. if FMod::bgm_playing? and @bgm_fading_out and
  1283. !@me_playing
  1284. if FMod::bgm_volume <= 0
  1285. @bgm_fading_out = false
  1286. self.bgm_stop
  1287. # If another BGM played while fading out, play it (most recent)
  1288. if @next_bgm
  1289. self.bgm_play(@next_bgm.name, @next_bgm.volume,
  1290. @next_bgm.pitch, @next_bgm_position)
  1291. @next_bgm = nil
  1292. end
  1293. else
  1294. current_volume = FMod::bgm_volume - @bgm_fade_decrement
  1295. FMod::bgm_volume = current_volume
  1296. end
  1297. end
  1298. if FMod::bgs_playing? and @bgs_fading_out
  1299. if FMod::bgs_volume <= 0
  1300. @bgs_fading_out = false
  1301. self.bgs_stop
  1302. # If another BGS played while fading out, play it (most recent)
  1303. if @next_bgs
  1304. self.bgs_play(@next_bgs.name, @next_bgs.volume,
  1305. @next_bgs.pitch, @next_bgs_position)
  1306. @next_bgs = nil
  1307. end
  1308. else
  1309. current_volume = FMod::bgs_volume - @bgs_fade_decrement
  1310. FMod::bgs_volume = current_volume
  1311. end
  1312. end
  1313. if FMod::bgm_playing? and @me_fading_out
  1314. if FMod::bgm_volume <= 0
  1315. # If another ME played while fading out, play it (most recent)
  1316. if @next_me
  1317. self.me_play(@next_me.name, @next_me.volume, @next_me.pitch)
  1318. @next_me = nil
  1319. else
  1320. @me_fading_out = false
  1321. self.me_stop
  1322. end
  1323. else
  1324. current_volume = FMod::bgm_volume - @bgm_fade_decrement
  1325. FMod::bgm_volume = current_volume
  1326. end
  1327. end
  1328. end
  1329. end
  1330.  
  1331. # Create an endless loop to update Audio module
  1332. Thread.new do
  1333. loop do
  1334. sleep 0.2
  1335. Audio.update
  1336. end
  1337. end
  1338.  
  1339. #==============================================================================
  1340. # ** Game_System
  1341. #------------------------------------------------------------------------------
  1342. # This class handles data surrounding the system. Backround music, etc.
  1343. # is managed here as well. Refer to "$game_system" for the instance of
  1344. # this class.
  1345. #==============================================================================
  1346.  
  1347. class Game_System
  1348. #--------------------------------------------------------------------------
  1349. # * Play Background Music
  1350. # bgm : background music to be played
  1351. #--------------------------------------------------------------------------
  1352. ############################################################################
  1353. # ADDED pos (POSITION) AS PARAMETER TO METHOD
  1354. ############################################################################
  1355. def bgm_play(bgm, pos = 0)
  1356. @playing_bgm = bgm
  1357. if bgm != nil and bgm.name != ""
  1358. Audio.bgm_play("Audio/BGM/" + bgm.name, bgm.volume, bgm.pitch, pos)
  1359. ############################################################################
  1360. else
  1361. Audio.bgm_stop
  1362. end
  1363. Graphics.frame_reset
  1364. end
  1365. #--------------------------------------------------------------------------
  1366. # * Memorize Background Music
  1367. #--------------------------------------------------------------------------
  1368. alias :fmodex_old_system_bgm_memorize :bgm_memorize unless $@
  1369. def bgm_memorize
  1370. fmodex_old_system_bgm_memorize
  1371. @bgm_memorized_position = FMod::bgm_position
  1372. end
  1373. #--------------------------------------------------------------------------
  1374. # * Restore Background Music
  1375. #--------------------------------------------------------------------------
  1376. def bgm_restore
  1377. ############################################################################
  1378. # ADDED @bgm_memorized_position AS PARAMETER TO METHOD
  1379. ############################################################################
  1380. bgm_play(@memorized_bgm, @bgm_memorized_position)
  1381. ############################################################################
  1382. end
  1383. #--------------------------------------------------------------------------
  1384. # * Play Background Sound
  1385. # bgs : background sound to be played
  1386. #--------------------------------------------------------------------------
  1387. ############################################################################
  1388. # ADDED pos (POSITION) AS PARAMETER TO METHOD
  1389. ############################################################################
  1390. def bgs_play(bgs, pos = 0)
  1391. @playing_bgs = bgs
  1392. if bgs != nil and bgs.name != ""
  1393. Audio.bgs_play("Audio/BGS/" + bgs.name, bgs.volume, bgs.pitch, pos)
  1394. ############################################################################
  1395. else
  1396. Audio.bgs_stop
  1397. end
  1398. Graphics.frame_reset
  1399. end
  1400. #--------------------------------------------------------------------------
  1401. # * Stop Background Sound
  1402. #--------------------------------------------------------------------------
  1403. def bgs_stop
  1404. @playing_bgs = nil
  1405. Audio.bgs_stop
  1406. end
  1407. #--------------------------------------------------------------------------
  1408. # * Memorize Background Sound
  1409. #--------------------------------------------------------------------------
  1410. alias :fmodex_old_system_bgs_memorize :bgs_memorize unless $@
  1411. def bgs_memorize
  1412. fmodex_old_system_bgs_memorize
  1413. @bgs_memorized_position = FMod::bgs_position
  1414. end
  1415. #--------------------------------------------------------------------------
  1416. # * Restore Background Sound
  1417. #--------------------------------------------------------------------------
  1418. def bgs_restore
  1419. ############################################################################
  1420. # ADDED @bgs_memorized_position AS PARAMETER TO METHOD
  1421. ############################################################################
  1422. bgs_play(@memorized_bgs, @bgs_memorized_position)
  1423. ############################################################################
  1424. end
  1425. end
  1426.  
  1427. class Game_Temp
  1428. attr_accessor :map_bgm_pos
  1429. alias :fmodex_old_temp_initialize :initialize unless $@
  1430. def initialize
  1431. fmodex_old_temp_initialize
  1432. @map_bgm_pos = 0
  1433. end
  1434. end
  1435.  
  1436. class Scene_Map
  1437. alias :fmodex_old_map_call_battle :call_battle unless $@
  1438. def call_battle
  1439. $game_temp.map_bgm_pos = FMod.bgm_position
  1440. fmodex_old_map_call_battle
  1441. end
  1442. end
  1443.  
  1444. class Scene_Battle
  1445. def judge
  1446. # If all dead determinant is true, or number of members in party is 0
  1447. if $game_party.all_dead? or $game_party.actors.size == 0
  1448. # If possible to lose
  1449. if $game_temp.battle_can_lose
  1450. ############################################################################
  1451. # ADDED $game_temp.map_bgm_pos AS 2ND PARAMETE TO bgm_play
  1452. ############################################################################
  1453. # Return to BGM before battle starts
  1454. $game_system.bgm_play($game_temp.map_bgm, $game_temp.map_bgm_pos)
  1455. ############################################################################
  1456. # Battle ends
  1457. battle_end(2)
  1458. # Return true
  1459. return true
  1460. end
  1461. # Set game over flag
  1462. $game_temp.gameover = true
  1463. # Return true
  1464. return true
  1465. end
  1466. # Return false if even 1 enemy exists
  1467. for enemy in $game_troop.enemies
  1468. if enemy.exist?
  1469. return false
  1470. end
  1471. end
  1472. # Start after battle phase (win)
  1473. start_phase5
  1474. # Return true
  1475. return true
  1476. end
  1477.  
  1478. def start_phase5
  1479. # Shift to phase 5
  1480. @phase = 5
  1481. ############################################################################
  1482. # ADDED $game_temp.map_bgm_pos AS 2ND PARAMETE TO bgm_play
  1483. # AND SWITCHED PLAY ORDER SO BGM IS PLAYED FIRST
  1484. ############################################################################
  1485. # Return to BGM before battle started
  1486. $game_system.bgm_play($game_temp.map_bgm, $game_temp.map_bgm_pos)
  1487. # Play battle end ME
  1488. $game_system.me_play($game_system.battle_end_me)
  1489. ############################################################################
  1490. # Initialize EXP, amount of gold, and treasure
  1491. exp = 0
  1492. gold = 0
  1493. treasures = []
  1494. # Loop
  1495. for enemy in $game_troop.enemies
  1496. # If enemy is not hidden
  1497. unless enemy.hidden
  1498. # Add EXP and amount of gold obtained
  1499. exp += enemy.exp
  1500. gold += enemy.gold
  1501. # Determine if treasure appears
  1502. if rand(100) < enemy.treasure_prob
  1503. if enemy.item_id > 0
  1504. treasures.push($data_items[enemy.item_id])
  1505. end
  1506. if enemy.weapon_id > 0
  1507. treasures.push($data_weapons[enemy.weapon_id])
  1508. end
  1509. if enemy.armor_id > 0
  1510. treasures.push($data_armors[enemy.armor_id])
  1511. end
  1512. end
  1513. end
  1514. end
  1515. # Treasure is limited to a maximum of 6 items
  1516. treasures = treasures[0..5]
  1517. # Obtaining EXP
  1518. for i in 0...$game_party.actors.size
  1519. actor = $game_party.actors[i]
  1520. if actor.cant_get_exp? == false
  1521. last_level = actor.level
  1522. actor.exp += exp
  1523. if actor.level > last_level
  1524. @status_window.level_up(i)
  1525. end
  1526. end
  1527. end
  1528. # Obtaining gold
  1529. $game_party.gain_gold(gold)
  1530. # Obtaining treasure
  1531. for item in treasures
  1532. case item
  1533. when RPG::Item
  1534. $game_party.gain_item(item.id, 1)
  1535. when RPG::Weapon
  1536. $game_party.gain_weapon(item.id, 1)
  1537. when RPG::Armor
  1538. $game_party.gain_armor(item.id, 1)
  1539. end
  1540. end
  1541. # Make battle result window
  1542. @result_window = Window_BattleResult.new(exp, gold, treasures)
  1543. # Set wait count
  1544. @phase5_wait_count = 100
  1545. end
  1546.  
  1547.  
  1548. def update_phase5
  1549. # If wait count is larger than 0
  1550. if @phase5_wait_count > 0
  1551. # Decrease wait count
  1552. @phase5_wait_count -= 1
  1553. # If wait count reaches 0
  1554. if @phase5_wait_count == 0
  1555. # Show result window
  1556. @result_window.visible = true
  1557. # Clear main phase flag
  1558. $game_temp.battle_main_phase = false
  1559. # Refresh status window
  1560. @status_window.refresh
  1561. end
  1562. return
  1563. end
  1564. # If C button was pressed
  1565. if Input.trigger?(Input::C)
  1566. ############################################################################
  1567. # REMOVED $game_system.play_bgm BECAUSE IT'S DONE AUTOMATICALLY
  1568. # WHEN ME IS DONE PLAYING
  1569. ############################################################################
  1570. ############################################################################
  1571. # Battle ends
  1572. battle_end(0)
  1573. end
  1574. end
  1575. end
Advertisement
Add Comment
Please, Sign In to add comment