FMOD Ex Audio Module Rewrite

FMOD Ex Audio
Version: 1.51

Introduction
A few years ago, Hiretsukan (Kevin Gadd) wrote an FMOD binding script for RGSS, which allowed RPG Maker XP to use the FMOD audio library for BGM playback, allowing access to many more file formats (including streamed OGG, AAC, WMA, IT, XM, MOD, etc.). It was a great script that could be easily extended. You could find his original script here. Still, the script was limited; it was focused on playing BGMs, and didn't support Music or Sound Effects, it also didn't support fading. Furthermore, some more features were needed such as remembering BGM position after battle and with the BGM/BGS memorize event command, and setting loop points for files other than MIDIs. With that in mind, I started modifying the script to add more and more features. The bulk of the script is still Hiretsukan's, but it's now much more accessible; I used it to rewrite the built-in Audio module. I also fully documented the script, fixed some bugs, and added position and loop point support.

Features
  • Aside from MIDIs, WAVs, and MP3s, the script supports many new formats: Streaming OGG, AAC, WMA, IT, XM, MOD, S3M.
  • Several layers of complexity with the highest being generally identical to the built-in Audio module and the lowest allowing direct access to FMOD's interface.
  • Remembers position of BGM with BGM/BGS memorize event command and after battle instead of starting over.
  • Ability to set loop points to any file type.
  • Source code provided and well-documented, so you could easily extend it farther to harness the power of FMOD.

Screenshots

It's an Audio script, so just try demo.

Demo

DEMO LINK

Check the script called 'FmodEx *' at the top of the script sections list for instructions on how to use it (also check 'Game_System *' and 'Other Classes *' at the bottom for the other classes)

Script

Just download the demo.

Instructions

Directly from demo:
  1.  
  2. # You need to copy the file fmodex.dll to your game folder (folder where
  3. # your Game.exe and project file are). I've provided the DLL with the
  4. # demo, and you can also get the latest version from FMOD's official
  5. # website (http://www.fmod.org/index.php/download).
  6. #
  7. # There are three scripts that could be used, the real script is only
  8. # the first one, but the other scripts are provided for convenience:
  9. #
  10. # FModEx
  11. #           This includes the core functionality and the Audio module
  12. #           rewrite. It's the main script and should be placed on
  13. #           top of all scripts using it. Most people would just add a
  14. #           new section above the Game_System or Game_Temp section at
  15. #           the very top of the script editor and paste script there.
  16. # Game_System
  17. #           This script rewrites parts of the Game_System class to
  18. #           allow memorize_bgm to support remembering position,
  19. #           and to allow the play methods to take an extra parameter
  20. #           specifying position to start playing from. The last
  21. #           addition is updating the Audio class every frame.
  22. #           Add it anywhere under the Game_System script section.
  23. #           If you don't need any of these features you don't need this
  24. #           script, but you'll still need to at least call the Audio
  25. #           update methods yourself.
  26. # Other Classes
  27. #           This is really an optional script that modifies the default
  28. #           battle system to memorize map BGM and position instead of
  29. #           starting over after the battle. Add it anywhere under
  30. #           the Scene_Battle classes.
  31. #
  32. # Aside from integration with RMXP's default scripts, you can use
  33. # this script the same way you used the old Audio class You could also
  34. # access the FMod module for more options at a lower level, such as
  35. # setting loop points (FMod.bgm_set_loop_points(first, second) in
  36. # milliseconds) and getting current BGS position (FMod.bgs_position) to
  37. # name a few.
  38.  
Expand to see the code.


FAQ

Feel free to ask any questions

Compatibility

  1.  
  2. # This script has different levels of compatibility depending on what
  3. # scripts you use:
  4. #
  5. # FModEx
  6. #           This script overwrites the Audio module, but since
  7. #           few if any other scripts would do that, it's pretty
  8. #           much compatible with the majority of other scripts
  9. #           without modifications.
  10. # Game_System
  11. #           The following methods of Game_System are redefined:
  12. #             bgm_play, bgm_restore, bgs_play, bgs_restore
  13. #           Any other script that redefines these methods may
  14. #           not work well with this one. Most scripts wouldn't
  15. #           do that, except ones dealing with audio as well.
  16. #           I've marked the areas in the script where I made
  17. #           changes to help you resolve any conflicts
  18. #           The following methods of Game_System are aliased:
  19. #             bgm_memorize, bgs_memorize, update
  20. #           Other scripts that redefine these methods should
  21. #           be placed above the Game_System script.
  22. # Other Classes
  23. #           The following methods of Scene_Battle are redefined:
  24. #             judge, start_phase5, update_phase5
  25. #           Any other script that redefines these methods may
  26. #           not work well with this one. Custom battle system
  27. #           scripts may do that, so you might consider not
  28. #           including the Other Classes section and just
  29. #           manually add position memorizing functionality.
  30. #           I've marked the areas in the script where I made
  31. #           changes to help you resolve any conflicts
  32. #           The following methods of Game_Temp/Scene_Map are aliased:
  33. #             Game_Temp#initialize, Scene_Map #call_battle
  34. #           Other scripts that redefine these methods should
  35. #           be placed above the Other Classes script.
  36. #
  37. # So in other words, FModEx is the most compatible, followed by
  38. # Game_System, and finally the last and optional Other Classes.
  39. # If you use a custom battle system you probably shouldn't include
  40. # Other Classes anyway. This isn't an SDK script and I never tested
  41. # it with the SDK. It'll probably work, but refer to SDK documentation
  42. # for more information.
  43.  
Expand to see the code.


Credits and Thanks

- Thanks to Hiretsukan (Kevin Gadd) for the original binding. If you'd like to credit it anyone, it's him!


Terms and Conditions

  1.  
  2. #   Use of this script is subject to the permissive BSD-like license below.
  3. #   That basically means you could use it in any way you like as long
  4. #   as you keep the following copyright and license unchanged and available,
  5. #   and don't use name of copyright holder to promote products based on
  6. #   this software. Note, however, that this license only applies to the
  7. #   script, and not to the FMOD library. For more information about FMOD
  8. #   licenses consult FMOD website: http://www.fmod.org/index.php/sales
  9. #   It's free for non-commercial use, and they provide several types
  10. #   of licenses for different types of developers.
  11. #
  12. # Copyright (c) 2005, Kevin Gadd
  13. # All rights reserved.
  14. #
  15. # Redistribution and use in source and binary forms, with or without
  16. # modification, are permitted provided that the following conditions are met:
  17. #     * Redistributions of source code must retain the above copyright
  18. #       notice, this list of conditions and the following disclaimer.
  19. #     * Redistributions in binary form must reproduce the above copyright
  20. #       notice, this list of conditions and the following disclaimer in the
  21. #       documentation and/or other materials provided with the distribution.
  22. #     * The name of the contributors may not be used to endorse or promote
  23. #       products derived from this software without specific prior written
  24. #       permission.
  25. #
  26. # THIS SOFTWARE IS PROVIDED BY Kevin Gadd ''AS IS'' AND ANY
  27. # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. # DISCLAIMED. IN NO EVENT SHALL Kevin Gadd BE LIABLE FOR ANY
  30. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.  
Expand to see the code.
Hmm very useful indeed, I don't know if its possible stop the music when the window is inactive and resume it once the window is active again this is useful for making intro that sync with music or other DDR mock up mini games.  Other than that very nice script.
Very interesting! Does the FMOD audio library support Ogg looping? I recall that RMXP's limitations in that vein were a fairly major roadblock for me, since I'm a man who likes if at all possible to have an into to his songs, so if this drawback has been eliminated then I'm pretty much beside myself with joy right now.
Mimi Chan wrote:
Hmm very useful indeed, I don't know if its possible stop the music when the window is inactive and resume it once the window is active again this is useful for making intro that sync with music or other DDR mock up mini games.  Other than that very nice script.


Unfortunately, I doubt there's a simple way to do something like that. It'd require knowing when the game window loses focus, but since the Ruby interpreter is paused I'm not sure how you could detect that. If someone knows how to achieve something like that I wouldn't mind implementing it, but I personally don't have a clue.

Dr. Winston O'Boogie wrote:
Very interesting! Does the FMOD audio library support Ogg looping? I recall that RMXP's limitations in that vein were a fairly major roadblock for me, since I'm a man who likes if at all possible to have an into to his songs, so if this drawback has been eliminated then I'm pretty much beside myself with joy right now.


It does! The cool thing about FMOD is that if it works for one file format, it probably works for all other formats. BGM and BGS OGGs should loop by default. If you meant setting loop points and not just looping in general (for intros, etc.), then the answer is also yes (see instructions), but for now it's done manually. Future versions of the script will automatically read some tag to get the loop start and end values (like LOOPSTART and LOOPLENGTH OGG tags in VX).
Crikey, somebody actually got around to a proper rewrite of the Audio class. I believe something like this has been desired since the .net days! Good work, sir, good work. Actually makes the Memorise BGM command somewhat more useful...

I believe the only thing remaining is convincing the Audio module to use the base windows instruments for playing MIDI files (seeing as how they can sound totally different in Media Player compared to RMXP~ heck, one song I tried to use in RMXP once just ended up with the drumbeat...whilst it was a full piece before...haha...)
Hmm, but I don't how RMXP can pause everything (animation, sprite, etc.) when the window is inactive (with exception of the audio) maybe a script that detects when every script running is suddenly paused?  Hmm I cant explain well since I have zero script knowledge T.T
That's really interresting. I've tested a couple of Audio rewrites before but wasn't really happy with them.
I really like the "No more restarting BGM after battle" feature, I've been looking for this for a long time.
I'll take a look at this one as soon as possible.

Thanks for sharing it!
I just uploaded a new version of the script, it fixes some bugs and adds some minor stuff.

Quote:
I believe the only thing remaining is convincing the Audio module to use the base windows instruments for playing MIDI files (seeing as how they can sound totally different in Media Player compared to RMXP~ heck, one song I tried to use in RMXP once just ended up with the drumbeat...whilst it was a full piece before...haha...)

Problem with the MIDIs and sound card instrument (not Windows) is that they are different on every PC, they might be pretty terrible on some PCs and great on others. If it sounds bad on RMXP you could be pretty sure it might sound that bad or worse on certain hardware. RMXP and FMOD standardize the way MIDIs are played across all platforms, so you can be sure it'd sound the same way on player's PC. If you really want the sound card instruments, you'll probably have to use a different API (Windows or Winamp or something), or figure out a way to supply your own sound font for FMOD instead of the default one. Alternatively, you could use another file format such as OGG or tracker formats.

Quote:
Hmm, but I don't how RMXP can pause everything (animation, sprite, etc.) when the window is inactive (with exception of the audio) maybe a script that detects when every script running is suddenly paused?  Hmm I cant explain well since I have zero script knowledge T.T

Yeah but the script itself would be paused, so it can't detect that until after it's unpaused, and it'd be useless in that case. It's really not that simple. :(

Thanks for your replies~
I've had a hard time working to undestand poccil's audio module, in 2 days i was working Hard on it, i finished it today, and then i come and see this xD
I don't know if I thank you for this, or if i get angry for coming with this excactly after i finished my system...

Since your system is better, i'll thak you! =D
One would think that there would be a boolean status for whether or not the window was minimized, same as it is when its fullscreen.  That just doesn't make sense.
when i tested the demo i've got this error:

Line 293 'RuntimeErro' FMOD Ex returned error 23

I removed the line that caused the error
        raise "FMOD Ex returned error #{result}"
Expand to see the code.

and i noticed one thing: only sounds in the game folder worked.
Musics on the RTP folder didn't work.

Is that normal?

EDIT.: i was looking the script, and it's not normal that the script can't detect my RTP.
And there's a strange sound before de BGM restarts after the victory ME
Too bad =/
Excuse me, will it be possible to rewrite audio module using BASS library like this?  I was always curious, and haven't seen one yet(though there's script which uses winamp plugin to play BGM).
Totti wrote:
One would think that there would be a boolean status for whether or not the window was minimized, same as it is when its fullscreen.  That just doesn't make sense.

What usually happens is that Windows sends a message to the application telling it the window was minimized or lost focus, an application could choose to handle that message. It seems that when getting such a message, RMXP pauses the RGSS interpreter (and in effect pausing Graphics), but since sound is usually handled by a separate thread or by Windows, it's not paused. It has to do with the way the engine is designed internally, which is why there isn't a simple way to check if it happened, and even if you could, it'd have to be from a running program and not the paused RMXP.

Quote:
When i tested the demo i've got this error:

Line 293 'RuntimeErro' FMOD Ex returned error 23

I removed the line that caused the error
Code:

        raise "FMOD Ex returned error #{result}"

and i noticed one thing: only sounds in the game folder worked.
Musics on the RTP folder didn't work.

Is that normal?

EDIT.: i was looking the script, and it's not normal that the script can't detect my RTP.
And there's a strange sound before de BGM restarts after the victory ME
Too bad =/

Error 23 means the file wasn't found, so you were right about it not detecting your RTP. I'd like to know the following things:
1. Do you use the standard RTP? (and not your own or some pirated RMXP version RTP)
2. From the start menu, either select Run in older Windows versions or type directly into the search box in Vista: regedit. With the Registry Editor open, browse to this location: HKEY_LOCAL_MACHINE\SOFTWARE\Enterbrain\RGSS\RTP (or anything similar) and tell me the values you see there -or better yet, take a screenshot.
3. Open the .ini file in the Game.exe directory and tell me the RTP values it lists.

As for the sound you hear when resuming BGMs, it seems to be an FMOD problem with setting the position in a MIDI file. I'll look more into it, but I doubt it'd be solved soon... it's a pretty minor annoyance as far as I'm concerned.

Landarma wrote:
Excuse me, will it be possible to rewrite audio module using BASS library like this?  I was always curious, and haven't seen one yet(though there's script which uses winamp plugin to play BGM).

Well, you'd have to change the base classes to correspond to that library's structure, and figure out the DLL interface. In other words, it's possible, but you'll need to do some scripting of your own (or find someone to do it). The basics are the same though, whether you're using FMOD or Winamp or some other library: you map the interface of that library to RGSS, and then call the functions you need. Is there anything particular you require from BASS library that isn't in FMOD Ex?
I use an pirated vesion =X
since we don't have an oficial portuguese version of RM. T-T

here's the registry image
http://i258.photobucket.com/albums/hh25 ... Img4-9.png[/img]

RTP1=Standard
RTP2=
RTP3=
I see. I think the problem is that I'm assuming that the RTP path ends with a \, after which I add the midi file name. For example:

C:\Program Files\Common Files\Enterbrain\RGSS\Standard\ + filename.mid = C:\Program Files\Common Files\Enterbrain\RGSS\Standard\filename.mid <- a valid file name

D:\Atoa\RPG Maker XP\RGSS-RTP + filename.mid = D:\Atoa\RPG Maker XP\RGSS-RTPfilename.mid <- not a valid file name!

I'll need to check for a \ at the end of the path and add one if it's not present. I'll fix it in the next version. Thanks for your help. :)
This really is an awesome script.  Thanks a bunch.
I just uploaded a new version that mainly deals with the issues raised by Atoa (RTP problem and BGM fading in after ME finishes playing). It also fixes several bugs and introduces some minor behavioral changes. Please download the new version and report any new bugs you might encounter.
Cowlol wrote:
Landarma wrote:
Excuse me, will it be possible to rewrite audio module using BASS library like this?  I was always curious, and haven't seen one yet(though there's script which uses winamp plugin to play BGM).

Well, you'd have to change the base classes to correspond to that library's structure, and figure out the DLL interface. In other words, it's possible, but you'll need to do some scripting of your own (or find someone to do it). The basics are the same though, whether you're using FMOD or Winamp or some other library: you map the interface of that library to RGSS, and then call the functions you need. Is there anything particular you require from BASS library that isn't in FMOD Ex?

I'm no programmer, but I can assume what BASS library does a bit.  Frankly, what I want is .mo3 support. 
Its just great now!

There's only a small (almost insignificant) bug, a small "tick" before Victory ME, but nothing really bad.

now i will study how to use the loop funcition =D
great script!, but I got an error in battle (even in the demo) it says 'FMOD Ex returned error 58' it seems to be related to the animation_process_timing method, I got the error just defending with all the battlers a few times.
there are also argument errors with some scripts when they say something like Audio.se_play('se_name'),
but you can fix it just changing the line 1257 of your script to
'def Audio.se_play(filename, volume = 100, pitch = 100) ', right?
Reminds me of chrono trigger.. good job! :thumb:
panchokoster wrote:
great script!, but I got an error in battle (even in the demo) it says 'FMOD Ex returned error 58' it seems to be related to the animation_process_timing method, I got the error just defending with all the battlers a few times.
there are also argument errors with some scripts when they say something like Audio.se_play('se_name'),
but you can fix it just changing the line 1257 of your script to
'def Audio.se_play(filename, volume = 100, pitch = 100) ', right?


Thanks for your input. I fixed the argument error with se_play and me_play. As for the other error, it's a problem with your sound card or driver. I've changed the script to use a software mixer instead of relying on the sound card itself, which should solve most hardware problems. I updated the first post with a new version of the script with these changes, so could you download the new version and see if you still get that error?
I went into the skills menu and mashed directions and the bgm cut out. I made it start playing again by talking to one of the people, went back into skills and did it again, and got an error.

Script 'FmodEx *' line 312: RuntimeError occured.
FMOD Ex returned error 36

Looks like a great script though, should add a lot of functionality.
Do any of the formats supported so far work with sound fonts, or do you plan to get it working with any that do?
Hmm...
It seems the problem is that Scene_Menu doesn't call $game_system.update (which calls Audio.update to stop and release any finished Sound Effects), and since SEs aren't cleaned probably you end up with more SEs than could be handled. One way to solve that is to add a $game_system.update to Scene_Menu's (and every other menu) update method:

Quote:
  def update
    $game_system.update
    @command_window.update
......
......


I don't think this is the best way for the script to handle that though. I'll try to fix it in the next version (maybe clean up each time you play an SE, or even running Audio.update in a separate thread.. I'm not sure)

I think FMOD Ex can handle sound fonts, I don't know much about sound fonts and I have other priorities at the moment. Maybe you could go to FMOD's website, download the documentation, study the script, and try to add sound font on your own!
thanks, works perfect now
BwdYeti wrote:
I went into the skills menu and mashed directions and the bgm cut out. I made it start playing again by talking to one of the people, went back into skills and did it again, and got an error.

Script 'FmodEx *' line 312: RuntimeError occured.
FMOD Ex returned error 36


Ok, I updated a new version that should fix that. Could you try it and see if you still get that problem?

I suggest everyone download and use the new version. If you're already using it in a project don't forget to overwrite my old Game_System script section (the one at the bottom of script editor list in demo) with the new one as well as the FModEx script section as they were both modified.
mmm, here you can find a bgm called 'Battle - Small002'
http://www.hbgames.org/forums/index.php?topic=23605.msg%25msg_id%25
i really like it because,with the defaut audio script, it has a little intro and then it starts the loop.(you can hear it with the 'sound test')
but now with the script it loops from the begining to end, can this feature be recovered?
Well, not directly. RMXP default Audio class recognizes control change 111 event in a MIDI and uses it as a loop start point. I'm not sure how to do that with FMOD, though I do plan on supporting something similar for OGGs (using tags). For now you can do it manually; Right after you play the BGM, add this script FMod::bgm_set_loop_points(start, -1) where start is the start of the part after the intro (and you can change the -1 to specify end of loop too, -1 means the end of the loop is the end of the BGM) in milliseconds (1 second = 1000 milliseconds), so if you figure out where exactly the loop start happens you could emulate that feature.

I'm not sure if FMOD has direct access to MIDI events but I'll look into it. The more manual alternative should suffice for now, at least.
The ability to resume music tracks from an arbitrary point sounds NUMBER ONE!

How much work might I expect to do to make this compatible with VX? (keeping in mind I'm very fluent with scripting) I wouldn't expect much, but I don't have access to the source from where I am right now.
Hi. First: Great script!

Cowlol wrote:
Well, not directly. RMXP default Audio class recognizes control change 111 event in a MIDI and uses it as a loop start point. I'm not sure how to do that with FMOD, though I do plan on supporting something similar for OGGs (using tags).


I've been doing an audio replacement for RM2k a while ago, and the main problem about the midis is not only the controller thing but also that it skips the beginning of any Midi in which no "note on"-events are found. I haven't found anything that was able to reproduce that behavior, and I also took a look at FMOD. But that maybe only means that I'm too stupid ;)

For this Audio replacement, I wrote a tool which allowed you to find, preview and set a starting point for loops in MP3, WMA and OGG files. This info was saved in the comment tag of each format, prepended by "DisharmonyLoop:". But I think it was saved in centiseconds, not microseconds. If the user chose to not play the file looped, the comment tag was set to "DisharmonyLoop:no loop". Maybe I could recycle this tool to help you a little.

I would be very happy if you were able to get midi playback right the same way the Maker is doing it.

(This post editor is highly irritating.)

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=55486