tags:

views:

5904

answers:

5

I'm trying to rebuild an old metronome application that was originally written using MFC in C++ to be written in .NET using C#. One of the issues I'm running into is playing the midi files that are used to represent the metronome "clicks".

I've found a few articles online about playing MIDI in .NET, but most of them seem to rely on custom libraries that someone has cobbled together and made available. I'm not averse to using these, but I'd rather understand for myself how this is being done, since it seems like it should be a mostly trivial exercise.

So, am I missing something? Or is it just difficult to use MIDI inside of a .NET application?

+1  A: 

I can't claim to know much about it, but I don't think it's that straightforward - Carl Franklin of DotNetRocks fame has done a fair bit with it - have you seen his DNRTV?

Whisk
+5  A: 

I think you'll need to p/invoke out to the windows api to be able to play midi files from .net.

I thought this codeproject article did quite a good job on explaining how to do this: vb.net article to play midi files

To rewrite this is c# you'd need the following import statement for mciSendString:

[DllImport("winmm.dll")] 
static extern Int32 mciSendString(String command, StringBuilder buffer, 
                                  Int32 bufferSize, IntPtr hwndCallback);

Hope this is of some help - good luck!

John Sibly
+5  A: 

I'm working on a C# MIDI application at the moment, and the others are right - you need to use p/invoke for this. I'm rolling my own as that seemed more appropriate for the application (I only need a small subset of MIDI functionality), but for your purposes the C# MIDI Toolkit might be a better fit. It is at least the best .NET MIDI library I found, and I searched extensively before starting the project.

odd parity
Leslie's MIDI Toolkit is definitely the most comprehensive C# solution to playing, recording MIDI. I have used it for a very complex project and it worked well.
andynormancx
A: 

For completeness, cross platform, and file format support I would use FMOD.

titanae
A: 

System.Media.SoundPlayer is a good, simple way of playing WAV files. WAV files have some advantages over MIDI, one of them being that you can control precisely what each instrument sounds like (rather than relying on the computer's built-in synthesizer).

MusiGenesis