Skip to content

Instantly share code, notes, and snippets.

@ShawInnes
Created August 20, 2016 06:13
Show Gist options
  • Save ShawInnes/f2e4c0070f6fcf984770694c03b4b65c to your computer and use it in GitHub Desktop.
Save ShawInnes/f2e4c0070f6fcf984770694c03b4b65c to your computer and use it in GitHub Desktop.
Mac CoreMidi CSharp Example (APC40)
using System;
using System.Runtime.InteropServices;
using AppKit;
using CoreMidi;
using Foundation;
namespace WireCastMidi
{
public partial class ViewController : NSViewController
{
MidiClient client;
MidiPort outputPort, inputPort;
MidiEndpoint sendEndpoint;
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Do any additional setup after loading the view.
Midi.Restart();
SetupMidi();
}
public override void ViewWillDisappear()
{
client.Dispose();
base.ViewWillDisappear();
}
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
// Update the view, if already loaded.
}
}
void SetupMidi()
{
client = new MidiClient("CoreMidiSample MIDI CLient");
client.ObjectAdded += delegate (object sender, ObjectAddedOrRemovedEventArgs e)
{
};
client.ObjectAdded += delegate
{
ReloadDevices();
};
client.ObjectRemoved += delegate
{
ReloadDevices();
};
client.PropertyChanged += delegate (object sender, ObjectPropertyChangedEventArgs e)
{
Console.WriteLine("Changed {0}", e.PropertyName);
};
client.ThruConnectionsChanged += delegate
{
Console.WriteLine("Thru connections changed");
};
client.SerialPortOwnerChanged += delegate
{
Console.WriteLine("Serial port changed");
};
outputPort = client.CreateOutputPort("CoreMidiSample Output Port");
inputPort = client.CreateInputPort("CoreMidiSample Input Port");
inputPort.MessageReceived += delegate (object sender, MidiPacketsEventArgs e)
{
HandleMessageReceived(e);
};
ConnectExistingDevices();
}
void HandleMessageReceived(MidiPacketsEventArgs e)
{
foreach (MidiPacket mPacket in e.Packets)
{
var midiData = new byte[mPacket.Length];
Marshal.Copy(mPacket.Bytes, midiData, 0, mPacket.Length);
MidiMessage message = new MidiMessage();
message.ParseRawData(midiData);
message.TimeStamp = mPacket.TimeStamp;
Console.WriteLine("Midi Message:D {0}", message.ToString());
if (message.MessageType == MidiMessageType.NoteOn)
{
// 0x9<chan> will turn on
// 0x8<chan> will turn off
/*
* 0=off,
* 1=green,
* 2=green blink,
* 3=red,
* 4=red blink,
* 5=yellow,
* 6=yellow blink
*/
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x90, (byte)(53 + message.MidiChannel), 1 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x91, 53, 2 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x92, 53, 3 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x93, 53, 4 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x94, 53, 5 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x95, 53, 6 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x96, 53, 1 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x97, 53, 2 }) });
}
else if (message.MessageType == MidiMessageType.NoteOff)
{
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x80, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x81, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x82, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x83, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x84, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x85, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x86, 53, 127 }) });
outputPort.Send(sendEndpoint, new MidiPacket[] { new MidiPacket(0, new byte[] { 0x87, 53, 127 }) });
}
}
}
void ReloadDevices()
{
BeginInvokeOnMainThread(delegate
{
Console.WriteLine("Reload Devices");
});
}
void ConnectExistingDevices()
{
for (int i = 0; i < Midi.SourceCount; i++)
{
var ep = MidiEndpoint.GetSource(i);
Console.WriteLine("Connecting {0}", ep.DisplayName);
var code = inputPort.ConnectSource(ep);
if (code != MidiError.Ok)
Console.WriteLine("Failed to connect");
}
for (int i = 0; i < Midi.DestinationCount; i++)
{
sendEndpoint = MidiEndpoint.GetDestination(i);
Console.WriteLine("Connecting {0}", sendEndpoint.DisplayName);
var code = outputPort.ConnectSource(sendEndpoint);
if (code != MidiError.Ok)
Console.WriteLine("Failed to connect");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment