using System;
using System.Runtime.InteropServices;
namespace Midi
{
///
/// A minimalist MIDI-IN class for Windows
///
public class MidiIn
{
[DllImport("winmm.dll")]
private static extern int midiInOpen(ref int handle, int deviceId, MidiInProc proc, int instance, int flags);
[DllImport("winmm.dll")]
private static extern int midiInClose(int handle);
[DllImport("winmm.dll")]
private static extern int midiInStart(int handle);
private delegate void MidiInProc(int handle, int msg, int instance, int param1, int param2);
private MidiInProc messageHandler = null;
private const int CALLBACK_FUNCTION = 0x30000;
private const int MIM_DATA = 0x3C3;
private const int MIM_ERROR = 0x3C5;
private const int MIM_LONGDATA = 0x3C4;
private int handle = 0;
private int deviceId = 0;
public MidiIn() { }
public void Start()
{
messageHandler = new MidiInProc(OnMessage);
midiInOpen(ref handle, deviceId, messageHandler, 0, CALLBACK_FUNCTION);
midiInStart(handle);
}
public void Stop()
{
midiInClose(handle);
}
private void OnMessage(int handle, int msg, int instance, int param1, int param2)
{
if(msg == MIM_DATA || msg == MIM_ERROR || msg == MIM_LONGDATA)
{
Console.WriteLine("MIDI Message: "+msg+" "+param1+" "+param2);
//TODO handle the message here
}
}
}
}