Making music with C#, P/Invoke, InteropServices and FMOD

I've been trying to give a glimse of how one could make a media player, but I'm not the kind of person who gives the whole answer until the absolute end. This post will be no different from the others. I'm going to present an outline as to making a media player using the FMOD library. The beaty of this library is that you can use it cross-platform so this code will also work for Linux in Mono (Linux version of C#)
Download the FMOD Library for Win32, for Windows CE, Windows CE small, Linux or MacOS
And inside you will find the reference manual, which is the place to start to get familiar with the functionallity of the library. In this little post I will focus on using C#, but as you might have guessed one can utilize every known language to programme against the FMOD library. On a quick notice; the FMOD library is primarily used in game development, but works just fine as a mediaplayer library. It is legal to use the FMOD library, without paying anything, just as long as you don't start selling your code as a product.
Now, the FMOD library is written in C++, and this means that it is written in unmanaged code contra the C# language which is a mannaged language. Therefore we need to have a look at the C# - System.Runtime.InteropServices; where we will find the DllImport attribute, which will call the unmanaged code from the C++ FMOD library (There is very much more going on in the CLI to call unmanaged code, but I will leave this for later post). As far as managed code is concerned, unmanaged code is invoked merely by invoking a method with an associated DllImport attribute.
[DllImport("My_CPP_Library.dll", EntryPoint="Function_in_CPP_Library", CallingConvention=CallingConvention.Winapi)]
public static extern Boolean Self_chosen_function_name(The parameters from the Function_in_CPP_Library);
Here's what I did to get the functionallity up and running:
[DllImport("fmod.dll", EntryPoint="FSOUND_Init",
CallingConvention=CallingConvention.Winapi)]
public static extern Boolean fmod_Init(int mixRate, int maxSoftwareChannel, int flags);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_Open",
CallingConvention=CallingConvention.Winapi)]
public static extern IntPtr fmod_Open(string data, int mode, int offset, int lenght);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_Play",
CallingConvention=CallingConvention.Winapi)]
public static extern int fmod_Play(int channel, IntPtr fstream);
[DllImport("fmod.dll", EntryPoint="FSOUND_SetVolume",
CallingConvention=CallingConvention.Winapi)]
public static extern int fmod_Setvolume(IntPtr fstream, int volume);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_GetLength",
CallingConvention=CallingConvention.Winapi)]
public static extern int fmod_getLenght(IntPtr fstream);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_GetPosition",
CallingConvention=CallingConvention.Winapi)]
public static extern UInt32 fmod_getPos(IntPtr fstream);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_SetPosition",
CallingConvention=CallingConvention.Winapi)]
public static extern Boolean fmod_setPos(IntPtr fstream, UInt32 pos);
[DllImport("fmod.dll", EntryPoint="FSOUND_Stream_Stop",
CallingConvention=CallingConvention.Winapi)]
public static extern Boolean fmod_stop(IntPtr fstream);
[DllImport("fmod.dll", EntryPoint="FSOUND_Close",
CallingConvention=CallingConvention.Winapi)]
public static extern void fmod_close();
Function (and therefore names) defined in the FMOD library.
The function names that I define, and the names that I will use in my C# code.
And now off to test if it works!
public static void Main()
{
//initialize
fmod_Init(44100, 16, 0);
//load the mp3 into a soundhandle that we can use
IntPtr soundHandle = fmod_Open("C:\\Path\\To\\Song\\gone.mp3", 0 , 0, 0);
//Set the volume on the song fmod_Setvolume(soundHandle, 255);
//Play the song
fmod_Play(0, soundHandle);
Application.Init();
//Create the Window
Window myWin = new Window("My Player");
myWin.Resize(200,200);
Button playButton = new Button("Play");
myWin.Add(playButton);
//Show Everything
myWin.ShowAll();
myWin.DeleteEvent += new DeleteEventHandler(myWin_DeleteEvent);
Application.Run();
//Stop the song
fmod_stop(soundHandle);
//Close the stream
fmod_close();
}
public static void myWin_DeleteEvent(object o, DeleteEventArgs args)
{
Application.Quit();
args.RetVal = true;
}
And that's actually about it! In this example I've tried to use GTK+ so that the GUI part will work for both Windows and Linux.
I'll let you fiddle around with it a while before showing the full code including the ID3Tag etc. You're more than welcome to send me a mail or comment and I will respond as soon as possible.
Currently rated 3.4 by 5 people
- Currently 3.4/5 Stars.
- 1
- 2
- 3
- 4
- 5