FmodForFoxes
FmodForFoxes copied to clipboard
Cross-platform C# FMOD wrapper.
I suffer so you won't have to.
The time has come. You're finally witnessing a high-level cross-platform C# library, which makes Monogame and FMOD best friends.
In case you came here and the only thing you got so far was that hilarious doge meme, FMOD is quite powerful cross-platform audio engine, which is pretty much the only hope to have any decent audio in Monogame.
The catch is - FMOD is written in C++, and the only thing C# overlords have is a bare-bones C# wrapper. No tutorials, no setup guides. Only you and DllImport.
That's why this library exists. It does the tough part for you, and provides basic high-level interface.
Also note that even though the primary target of this library is Monogame, its sources will be very useful if you want to use FMOD in some other C#-based project.
Setup
Initial setup is a little fiddly. Here's the thing - the FMOD license prohibits me from distributing their libraries in my Nuget package - so you have to download them yourself. But fear not, I've put together a detailed guide. With pictures.
Preparations
Visit the FMOD Download page (accessing it requires registration), find the FMOD Studio API downloads and get APIs for Windows, Linux and Android. If you're going to set up all three, of course.
NOTE: Current version of the library was tested on FMOD v2.00.08. I really recommend getting it. Later versions will probably also work, but I have no guarantee.
Windows API requires installation, Linux and Android don't. You can drop them near the Windows API just to have everything in one place.
Windows & Linux
- Open your DesktopGL or SharpDX Monogame project.
- Install the NuGet package ChaiFoxes.FMODAudio.Desktop. Alternatively, you can plug repo as a submodule and reference projects directly.
- Navigate to your FMOD Windows API installation. From there navigate to
\api\core\lib
. You will see two directories:x64
andx86
. Each one will contain this:
Out of all the files you'll need only fmod.dll
from both x86
and x64
directories. Your Monogame project should already have x86
and x64
directories (if it doesn't - just create them). Copy corresponding versions of fmod.dll
into them. You should end up with it looking like this:
Make sure dll files will be copied to the output directory:
- Navigate to your FMOD Linux API installation. From there navigate to
\api\core\lib
. This time you will see four directories:
You'll need only x86
and x86_64
.
Each directory contains this:
You will only need libfmod
files - the ones without L. Copy fmod files from x86
directory to x86
directory of your project, and then copy files from x86_64
to your x64
directory. You'll end up with this:
NOTE: If you're going for Linux-only build, you can exclude fmod.dll files.
Again, make sure all the files you've just added will be copied to the output directory:
And that's it - you've gotten yourself cross-platform desktop FMOD! Dllmap for Linux is already done internally, no need to worry about that.
Android
- Open your Monogame Android project.
- Install NuGet package ChaiFoxes.FMODAudio.Android. Alternatively, you can plug repo as a submodule and reference projects directly.
- Create
libs
directory in the root of your project. - Navigate to your FMOD Android API installation. From there navigate to
\api\core\lib
. You will see this:
- Each folder contains
libfmod.so
andlibfmodL.so
. You need onlylibfmod.so
from each directory. Copy everything except.jar
file over to yourlibs
directory. Don't lose this jar, tho. We'll need it later. You'll end up with this:
- Select each
.so
file you've just copied, open their Properties and set their Build Action toAndroidNativeLibrary
.
-
Create an Android Bindings Project.
Remember that jar from earlier? Now you need to copy it into the Jars directory and make sure its Build Action is set to
EmbeddedJar
.Now reference Bindings project to your main Android project - and you're golden.
And yes. Whole separate project for a single file. Thanks, Xamarin. There is actually a way to do this without creating a Bindings project, but that's up to you to figure out. : - )
Studio setup
FMOD Studio setup process is exactly the same, but you'll need to look into studio
instead of core
directories. It's also extremely important that ALL your dlls and jar are taken form the same version. FMOD doesn't like version mixup. Foxes don't like version mixup. Nobody does.
If you still have questions, take a look at the Demos project.
Playing some tunes!
So, after you've set everything up, it's time to bop some pops, as kids say these days.
- Find a sound file and import it into the Content Pipeline.
- Select the sound file and set its Build Action to Copy.
- Include the
ChaiFoxes.FMODAudio
namespace and paste the following code into your Initialize() method:
FMODManager.Init(FMODMode.CoreAndStudio, "Content");
var sound = CoreSystem.LoadStreamedSound("test.mp3");
var channel = sound.Play();
channel.Looping = true;
- Compile and hope that you (and me) did everything right.
You can also check out the incluided Demos project.
Bait-and-switch
As you could notice, there are only two nugets - Desktop and Android. But what if you want to use FMOD in a netstandard library? Since there is no crossplatform core FMOD library, this becomes a problem, right? Well, yeah. First-party C# bindings are made in a way that makes it impossible to extract core API without rewriting literally everything from scratch. I am not willing to do that. So, instead we will use a little trick.
Bait-and switch! :0
We don't have a crossplatform library. But we can fool the project into thinking we have one. Open up your netstandard library's csproj
and add this:
<PackageReference Include="ChaiFoxes.FMODAudio.Desktop" Version="2.0.0.0" >
<PrivateAssets>All</PrivateAssets>
</PackageReference>
But wait, what's this? We are adding a desktop nuget into the crossplatform project? Why?
That's the trick - we aren't actually using it! <PrivateAssets>All</PrivateAssets>
makes it so the nuget's dll file is not copied over to the output directory. We are only using this nuget at compile-time - and then switching it to a platform-specific version, which is installed in your platform project.
Hacky? Yes. (:
But what about other platforms?
I'd like to make console versions of the library - but currently I have no ability to do so, and probably won't have for a long time. As for UWP and Apple platforms, I just don't care about them enough.
If you want to be a hero and expand the library with any of those platforms yourself - contact me and we'll figure something out. ; - )
License and legal stuffs
This library is licensed under MPL 2.0, so you can use it and its code in any shenanigans you want. Free games, commercial games, anything - no payment or royalties required. Just leave a credit. ; - )
But the show's main star is a bit different. FMOD has its own license, which is much less permissive than mine.
Demo music is provided by Agrofox and FMOD team.
Also big thanque to StinkBrigade who helped a ton in adding FMOD Studio support.
don't forget to pet your foxes