Jump to content
Sign in to follow this  
  • entries
  • comments
  • views

Use FMOD Ex to play audio in Linux, A C# Mono tutorial.

Sign in to follow this  


Since I posted this article, I was approached by the creator of FmodSharp and since he has joined our games development team (thanks Marc-Andre!). Also as the project is now fully active again, the file's I originally provided on my website are out of date, and thus instead I have provided a link to the active FmodSharp project. When looking for files to complete this tutorial, please refer to that website instead. The code is up-to date, and far more reliable than the ones I originally provided.


If anyone has visited the project I am working on with others at the moment named Akaldiroka, you will know that we have spent a great deal of time ensuring that the engine works fine on both Windows and Linux. Recently while debugging and seeing how the engine ran on other peoples computers, a problem crept up with OpenAL failing to load, causing the game to crash on windows. Also Ubuntu 10.10 (the distribution I am using) had mixed results while dealing with the JACK server.

Now from a game developers stand-point, this is a simple fix. Perhaps use some API from DirectX instead to handle audio, and not pay attention to the misbehaviours of Linux leaving it for users to fix it. This would surely solve the problem, and everyone would be happy as Larry (apart for those unfortunate enough not to have the right Linux set-up). However from a cross-platform point of view, it would also mean maintaining multiple versions of the game, which in-turn would mean allot more work (not very good for an indie developer, and possibly career destroying having a first title that didn't really work).

So digging around, there seemed to be allot of hype about FMOD, and it's abilities to work on pretty much any system you care to distribute for. Also as mono allows you to target native FMOD linux binaries, which means one piece of code suits all. On the down-side, as of yet (or as far as I could find) there is no real tutorial on how to make this work in a C#/Mono environment (though the sample for Windows should just work out of the bag), so today I will run through what is needed to get your audio playing in Linux.

- fmodapi43400linux.tar.gz (get it from http://www.fmod.org).
[s]- fmodsharp.tar.gz (get if from my site http://aimee.xpod.be/bin/fmodsharp.rar)[/s]
- FmodSharp (get it from http://gitorious.org/fmodsharp)
- MonoDevelop 2.4 (get it from http://monodevelop.com)
- An mp3 or some other type of audio file you can use to test the project with.

- Ubuntu 10.10, allthough im sure this will work with other unix based operating systems like Mint.
- {user} in terminal commands should be replaced with your logon username.

1. Extract the dependencies

OK, so the first thing we need to do is extract the tarball we downloaded from FMOD and the one I provided. For simplicity sake we will be using a folder on the desktop. So lets open up the terminal and create it:

mkdir /home/{user}/Desktop/fmodsharp

Once this is done, open fmodsharp.tar.gz, and extract the contents of that tarball to the directory you just created. Next open fmodapi43400linux.tar.gz and navigate to the internal folder '/fmodapi43400linux/api/lib/', there you will see these two files:

- libfmodex-4.34.00.so
- libfmodexL-4.34.00.so

These files are the fmod binaries, so we'll need to install them. But for now, extract these also into the desktop folder you created.

2. Install the FMOD binaries on Linux.

Now that we have the files we need on our desktop, it's time to do something with them. So lets go back into the terminal and write this command:

sudo cp /home/{user}/Desktop/fmodsharp/libfmodex* /usr/local/lib

If everything goes well, the terminal should ask you for your root password, then return with no errors. Doing this adds the libraries you need to be able to use FMOD, so this is a critical step and should not be skipped.

3. Refresh the binary cache.

This is a really simple step, but important. Even though we have copied over the libraries, Linux does not yet know they exist. So we must refresh something called ldconfig. To do this, simply run this command in your terminal:

sudo ldconfig

4. Tell Mono about FMOD

A step I almost missed when I first got this working. In order for you to be able build your project, mono need's to know where the Linux equivalent libraries are. To do this, we must edit a file. As it is protected,
use the sudo command like so:

sudo gedit /etc/mono/config

When gedit opens, you'll see alot of XML code with nodes such as dllmap etc..., this is the file Mono uses to direct DLL calls when running under linux. So in order for us to have FMOD work too, we must add it, so after the last

Now save the file, and exit gedit.

Notice that i have used the library that ends with 'L', this is the logging version of FMOD, which means you get a more verbose output when trying to solve problems. I reccomend changing this at later date when you are happy things are working right.

5. Test it!

Yes we are just about there! Let's go back to the folder we created on the desktop from FmodSharp and open the PlayFile.csprj solution in MonoDevelop.

[s]Credit here should goto the FMOD team as this solution is just a modification of the 'examples_csharp/vs2005/playsound' example found in the Win32 download of FMOD. However there have been some important changes that make this project cross-platform. Here are the fixes that have been made:

- Linux doesnt like unicode filename's, especially when it comes to FMOD, so fmod.cs has been altered to deal only with Ansii.

- createSound has had the MODE changed from MODE.HARDWARE to MODE.MPEGSEARCH to ensure the mp3 is read correctly. See the FMOD documentation for more info on this subject.

Now you will notice on line 190 that I have chosen a file called 'kandi.mp3', make sure to change this path to the mp3 you wish to play. Once you have done so, press F5 to debug the application, the window should appear like so:[/s]

[s]If it shows, then congratulations! click on the button and you should be able to hear your audio file playing. If you have any problems, feel free to email me and i'll see what I can do. For now, thanks for reading.[/s]
In the new FmodSharp, you must either specify the file in the command line as an argument, or change the SoundFile creation to use a string containing the path to the file you wish to play. If it plays, then congratulations!. If you have any problems, feel free to email me and i'll see what I can do. For now, thanks for reading.

Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!