Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: Calin Leafshade on Sun 20/02/2011 19:01:08

Title: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Sun 20/02/2011 19:01:08
Hola!

Ok so I thought I'd try and implement Fmod in AGS and here is the result

http://www.thethoughtradar.com/AGS/FMODAGS/AGSfmodbin.zip
(source is available if anyone wants it)

3D sound/reverb works. Heres some basic usage:


// room script file
FmodSound *Sample;
FmodSound *Stream;
FmodChannel *SampleChannel;
FmodChannel *StreamChannel;

function room_AfterFadeIn()
{

Sample = FmodSound.Create("footstep.mp3", eFmodNoLoop, eFmodSpatial3D, eFmodLoadAsSample); //load a footstep as a 3D sample.
Stream = FmodSound.Create("music.mp3",eFmodLoop, eFmodSpatial2D, eFmodLoadAsStream); // load some music as a 2D stream.
StreamChannel = Stream.Play(); //start the music playing
StreamChannel.Volume = 30; //lower the volume (Valid values 0 to 100, default 100).
StreamChannel.Panning = -100; //pan the music hard left (Valid values -100 to 100, default 0).
FmodSetReverbPreset(eFmodReverbRoom); //Set the reverb preset (Default eFmodReverbOff).
FmodSetReverb3DProperties(0, 0, 0, 10, 20); // Set the 3D properties. parameters are x, y, z, mindistance, maxdistance.


SetTimer(1, 37); //lets set a timer!

}

function repeatedly_execute_always(){
 
    int LR = mouse.x - 160;
    int FB = 100 - mouse.y;
    if (LR < -100) LR = -100; //calculate some positions based on mouse coordinates.
 
  if (IsTimerExpired(1)){
   
    SetTimer(1, 37);
    SampleChannel = Sample.Play() //play the footstep.
    if (c != null) c.SetPosition(LR/20, 0, FB/20); //Sets the footstep position in 3D space.
   
  }
 
  if (c != null) Label1.Text = String.Format("%d", SampleChannel.GetPosMS()); // show the position in milliseconds.
  if (IsGamePaused()) Label1.Text = "Paused!"; // All channels will pause if IsGamePaused() returns true.
 
}


A note on 3D space

The units used are metres.

The plugin uses a left-handed coordinate system which means that +X = right, +Y = up and +Z = forwards.

The 3D listener is set at the origin and all 3D coordinates should be relative to that.

(-10, 0, 10) is ten meters to the left, zero metres up and ten meters in front of the listener.
(5,0,-2) is five meters to the right, zero metres up and two metres behind the listener

How is reverb calculated


FmodSetReverb3DProperties(0, 0, 0, 10, 20); // Set the 3D properties. parameters are x, y, z, mindistance, maxdistance.


The line above sets the reverb location and area of effect.

The first three ints are coordinates which indicate the epicentre of the reverbs effect. You should usually set this to the approximate centre of your room relative to the camera (imagine you are looking at the room through a camera and that it what the screen is showing).

The 4th int sets the starting falloff distance of the reverb. This is the distance at which the reverb begins to fall off and no longer apply.
the 5th int sets the end falloff distance for the reverb. This is distance at which the reverb will no longer apply to the 3D sounds you play.

In practice you can usually set the starting falloff to be half the size of your room (the radius of the effect) and the end falloff to some value higher than that.
These values are usually only important when there are multiple reverbs in play, which i havent done yet.

Speaker configurations

The plugin supports any speaker configuration and will use whatever the user has set in windows. (tested with 2.1, 5.1, 4.1 and stereo)
3D sounds will map to the available speakers as if in 3D space and 2D sounds will map to their correct channels (so you can play 5.1 music :D)

Filenames, Types, Streams and Samples

When running the game from the debugger the plugin will try and load the files relative to the main game directory (the one with game.agf in)
When running the game as a compiled exe the plugin will load the files relative to the folder with the game exe in.

The plugin supports OGG, MP3, WAV and loads others that I haven't tested (MOD, MIDI, AIFF, WMA, CDDA (you can load a CD by passing the drive letter like "D:"))

You should load large files, like music, as streams by passing eFmodAsStream to the CreateSound function.
Small files like SFX should be loaded as samples by passing eFmodAsSample.

Things that *DON'T* work

-Reverb customisation
You can load any of the reverb presets (which should be fine for most uses) but i haven't added functions to customise them yet.
You can also only have 1 reverb at a time.

-Additional DSP
I haven't added other DSP effects like Chorus, Delay, Phaser and stuff yet but I don't really think they are that necessary for most purposes.

LICENSE!! Be aware that FMOD is only free for non-commercial projects!!

TODO
- More DSP options

I think that's everything! Have fun!
Title: Re: PLUGIN: FMOD For AGS
Post by: Sslaxx on Sun 20/02/2011 20:18:17
What would this have over the standard in-built sound system, with the exception of being able to use external sound files?
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Sun 20/02/2011 20:29:29
3D Sound,
5.1 sound
Dynamic Reverb
DSP chain (chorus, phaser, whatever)
Title: Re: PLUGIN: FMOD For AGS
Post by: Dualnames on Sun 20/02/2011 20:30:38
Bioshock and Starcraft II use this.[irony] They don't know shit. [/irony]
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Sun 20/02/2011 20:32:52
I'll admit its something of a niche thing and for most people the standard sound system is fine (with the exception of the sound stuttering which seems to be giving a lot of projects serious problems)

But if you want to do something special with the sound system for your game then why not!
Title: Re: PLUGIN: FMOD For AGS
Post by: xenogia on Sun 20/02/2011 23:01:23
I'll try this out tonight.  This will resolve my stutters within Face of Corruption under AGS.
Title: Re: PLUGIN: FMOD For AGS
Post by: LimpingFish on Sun 20/02/2011 23:57:10
This is exactly the kind of thing I can get excited about! Hooray!

Once it gets fully implemented, I'll be all over this like filth on a hobo.
Title: Re: PLUGIN: FMOD For AGS
Post by: Shane 'ProgZmax' Stevens on Mon 21/02/2011 11:10:02
I may be remembering this wrong, but I believe CJ said once, long ago, that AGS used a very early version of FMOD before they started requiring licensing fees for commercial use.

Forgive me if I'm wrong, but doesn't FMOD still require a license for using it commercially?  This is the main point of contention I've always had with it, anyway.

Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Mon 21/02/2011 11:16:49
Yes it does, as noted in the OP.

I would probably prefer to use OpenAL but it's a little out of my confidence bracket c++ wise.

FMOD is free for non-commercial use though so most AGS users would be able to get a kick out of 5.1, reverb, spatial audio and other, largely pointless features in their games.

One could argue that creating a plugin with a restrictive license is a pointless exercise but no one else seems all that eager to use the plugin API so I might as well use my limited C++ skillz to do something with it even if those skills preclude commercial titles.

Maybe my effort will prompt some of the real programmers to do something.

EDIT: I should qualify 'no one else seems all that eager' as 'very few people' and I didnt mean to belittle the work of those who *have* done stuff with the api.
Title: Re: PLUGIN: FMOD For AGS
Post by: Dualnames on Mon 21/02/2011 14:54:09
Quote from: ProgZmax on Mon 21/02/2011 11:10:02
Forgive me if I'm wrong, but doesn't FMOD still require a license for using it commercially?  This is the main point of contention I've always had with it, anyway.

http://www.fmod.org/index.php/sales

Yep, it pretty much does.
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Tue 22/02/2011 07:12:39
Save states added. Games restore correctly now.
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Thu 24/02/2011 01:47:26
Ok well the plugin is basically finished and working now. I just need to tidy some stuff up and thoroughly test it.

Would people please test this game and tell me if it errors/reverb doesnt work/whatever

http://www.thethoughtradar.com/AGS/FMODAGS/513DSoundTest.zip
Title: Re: PLUGIN: FMOD For AGS
Post by: LimpingFish on Thu 24/02/2011 02:21:39
Working fine for me. Reverb on/off, no problems.
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Thu 24/02/2011 17:06:52
Plugin released. See original post.
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Sun 27/02/2011 23:11:16
Any bug reports/feedback would be appreciated if anyone has used it.
Title: Re: PLUGIN: FMOD For AGS
Post by: Shane 'ProgZmax' Stevens on Mon 28/02/2011 23:25:48
I'm curious, Calin; does your plugin avoid the sometimes-annoying issue ags has with audio playback when skipping or transitioning cutscenes, ie, the note repetition as the audio engine stalls out briefly.  This is noticeable most recently with Gemini Rue, for instance.  I'm curious because at least for freeware titles I might use this as an alternative to avoid that issue until it's properly resolved.
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Mon 28/02/2011 23:34:31
Yes, That issue is fully resolved with the plugin (as far as i can tell)

You can avoid all the 3D sound stuff by simply creating your music/sound as a 2D sound. Then it behaves almost exactly the same as AGS's built in audio but without the stuttering.

Title: Re: PLUGIN: FMOD For AGS
Post by: bbalint85 on Wed 02/03/2011 15:31:38
Quote from: Calin Leafshade on Mon 28/02/2011 23:34:31
Yes, That issue is fully resolved with the plugin (as far as i can tell)

You can avoid all the 3D sound stuff by simply creating your music/sound as a 2D sound. Then it behaves almost exactly the same as AGS's built in audio but without the stuttering.



Wow, that's actually a pretty cool sideeffect, I'll definitely try this!  ;D

Edit: I tried this, works fine! A few things:
-the sample code above has some errors, a missing ; and c should be SampleChannel
-can you give us some list of functions? for example how can I seek in the music?
-can I provide filepaths in the function? I tried FmodSound.Create("\AudioCache\au000040.mp3".., also /, \\, and I only get an error message (error:23) maybe It should be handled somehow...

Great work by the way, I'm looking forward to use it!
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Thu 03/03/2011 05:36:48
Quote from: bbalint85 on Wed 02/03/2011 15:31:38
-the sample code above has some errors, a missing ; and c should be SampleChannel
nice spot, thanks.

Quote
-can you give us some list of functions? for example how can I seek in the music?

I think most functions above are listed except for the seek one which is something like
channel.SeekPosMS(int timeInMillisecons); (might not be exactly right)
Auto complete is your friend.

Quote
-can I provide filepaths in the function? I tried FmodSound.Create("\AudioCache\au000040.mp3".., also /, \\, and I only get an error message (error:23) maybe It should be handled somehow...

Yes error checking does need to be handled better, I will look into it. (error 23 is file not found)
the format for filenames is "Folder/File.Extension".
Note that there is no leading slash and they are forward slashes.

Also I would advise that you create your own folder for media and dont use the audiocache. The audiocache is an internal folder used in AGS and you cant guarantee that the files will remain in there with the same names.

Title: Re: PLUGIN: FMOD For AGS
Post by: bbalint85 on Tue 08/03/2011 12:31:21
Quote from: Calin Leafshade on Thu 03/03/2011 05:36:48
Yes error checking does need to be handled better, I will look into it. (error 23 is file not found)
the format for filenames is "Folder/File.Extension".
Note that there is no leading slash and they are forward slashes.

Also I would advise that you create your own folder for media and dont use the audiocache. The audiocache is an internal folder used in AGS and you cant guarantee that the files will remain in there with the same names.

Sure, it was just for testing purposes. Now everything works, thanks!
Title: Re: PLUGIN: FMOD For AGS
Post by: bbalint85 on Wed 09/03/2011 13:49:00
one more thing: Can I turn the music off somehow?
Can you implement a stop() function?
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Thu 10/03/2011 07:00:40
I'm pretty sure i implemented a channel.Pause() method. If I didnt then I am rather stupid.

Since a channel is just an instance of a playing audiofile you dont have to 'stop' it as such since the channel allocation is dynamic anyway.
Title: Re: PLUGIN: FMOD For AGS
Post by: bbalint85 on Thu 10/03/2011 07:34:52
Quote from: Calin Leafshade on Thu 10/03/2011 07:00:40
I'm pretty sure i implemented a channel.Pause() method. If I didnt then I am rather stupid.

Since a channel is just an instance of a playing audiofile you dont have to 'stop' it as such since the channel allocation is dynamic anyway.

Well, there's no pause, at least it's not public...
the functions I can see are:
-GetPosMS
-Pannnig
-SeekMS
-SetPosition
-SetReverbMinMax
-Volume

Compared to the ags audiochannel stop(or pause) and isplaying are missing...
In the sound functions(Create, Play) I was missing a Playfrom function, but SeekMS is an available workaround.
Can you look into this?

Title: Re: PLUGIN: FMOD For AGS
Post by: Ronsen on Sun 21/04/2013 13:03:41
I know this post is quite old, but I just wanted to check it out, unfortunately the links are dead...
Btw, Is this plugin obsolete/not working anymore or was it just a little experiment to use/check a different sound engine?   
Title: Re: PLUGIN: FMOD For AGS
Post by: bbalint85 on Sun 21/04/2013 16:58:19
Well we're using this plugin, and it's working, with the mentioned functionality. If you want to use it only for background music, and with large backgrounds, it's a good choice, as it solves the small stop while changing rooms problem.
Title: Re: PLUGIN: FMOD For AGS
Post by: Ronsen on Tue 30/04/2013 07:12:58
Quote
http://www.thethoughtradar.com/AGS/FMODAGS/AGSfmodbin.zip
(source is available if anyone wants it)

calin, may I have a look at the source code... please :-)
Title: Re: PLUGIN: FMOD For AGS
Post by: Calin Leafshade on Tue 30/04/2013 17:10:21
Heres the source: https://dl.dropboxusercontent.com/u/27247158/FMODAGSsrc.zip

Title: Re: PLUGIN: FMOD For AGS
Post by: Gord10 on Mon 29/07/2013 23:01:26
Could you also upload the compiled one? I'd love to use this module, but I couldn't manage compiling the source code.  :cry:
Title: Re: PLUGIN: FMOD For AGS
Post by: Knox on Thu 01/08/2013 17:58:48
I wouldn't mind the compiled one as well, the link seems to be broken. :(