Unity MIDI

I'm looking to accomplish two MIDI related tasks in Unity FREE.

1) get all note-on messages from a midi file
2) get signals from a MIDI keyboard into unity

Task 1

Here are the contenders:

csharpsynthproject PORT by SonicViz http://forum.unity3d.com/threads/130104-UnitySynth-full-Xplatform-midi-synth

This port works, just need to set project settings so that unity uses the full .NET 2.0, rather than the subset

csharpsynthproject http://csharpsynthproject.codeplex.com/

It's impossible to separate out the sequencer from the synth. You need to create a synth and feed it into the constructor for the sequencer. The sequencer looks to the synth for note timing information. Also data being pulled from the synth seems to trigger data getting generated in the sequencer i.e. if I stop pulling the WAV output from the synth I stop getting note-on messages from the sequencer. I think I would have to figure out the entire functionality to a high level of detail in order to prise these two components from one another.

NOTE: csharpsynthproject actually itself ports NAudio. As you can see further down the list I've managed to do this port directly, And that's my preferred solution.

(OLD VERSiON http://code.google.com/p/csharpsynth/ Marek is one of the two principal contributors to this project. However I can't see any way to download the source code for that project, I am guessing public code access has been retracted. It doesn't seem to have been touched since spring 2012.)

+++Unity ASSET STORE: MIDI Synth / MIDI Unified by Marek Ledvina http://www.pinterest.com/foriero/unity3d-plugins/
Marek has put a couple of MIDI assets on the store

MIDI Synth is another port of csharpsynthproject with no source code and no documentation. SonicViz's port gives a cleaner Demo Project, AND of course contains source code, which means that if you want to fiddle with the engine you can actually dig into the source code and figure out what is going on… /how/ to call these API methods that have been exposed!

MIDI Unified: SonicViz tells me Marek is porting the Sanford Toolkit here! MIDI Unified costs 100 bucks and requires unity PRO, because it uses an OSX plug-in to get MIDI in from an external source.

(There is a time of writing only one other MIDI asset on the store, simpleMIDI, and that only works on Windows — so clearly the author is wrapping some framework that uses WinAPI calls)

C# MIDI Toolkit (by Leslie Sanford) http://www.codeproject.com/Articles/6228/C-MIDI-Toolkit

At first glance this looks perfect — the author has put a lot of effort into repeatedly restructuring the code base.
It supports MIDI file I/O and sequencing.
However, it's very advanced structurally, which makes it difficult (for me, at any rate) to understand.
I've attempted porting it to Unity, and ran into a serious problem: it uses one of his own .NET DLLs which invokes some WinAPI DLL ("winmm.dll") function — a high resolution timer. Now the source code for this DLL is also available as one of his CodeProject articles ( http://www.codeproject.com/Articles/5501/The-Multimedia-Timer-for-the-NET-Framework ) and it's not going to be trivial to rewrite this library wrapping Windows specific API calls. And even when that's done, there will probably be other API calls that need wrapping. So, a lot of work. Onwards…

MIDI.DOT.NET (by Tom Lokovic) https://code.google.com/p/midi-dot-net/

This project also uses P/Invokes — i.e. it invokes platform specific API calls. so that's a no.

MIDI.NET (by Marc Jacobi aka ObiwanJacobi) http://midinet.codeplex.com/

(Yes, it does kind of have the same name as the other one)
Has sequencer, seems to handle streams, author says he may be up for implementing RTP MIDI here http://stackoverflow.com/questions/19612790/c-net-implementation-of-rtp-midi

'MIDI.NET allows any .NET developer to access the power of MIDI without doing P/Invokes.'

NAudio (by Mark Heath) https://naudio.codeplex.com/

Good clean well maintained code. I managed a port within a couple of hours.
This gives me exactly the level of access I'm after; I throw in a MIDI file and get back an array of ordered MIDI events.
This means it is not involving timers, and hence no platform specific stuff.
^ great tutorial on cracking open a MIDI file.
Also this framework seems to have a minimal dependency chain so I should be able to just pull out a few files

Someone else has done a Unity port here: http://code.google.com/p/naudio-with-unity3d/
But I'm happier my own port — just bringing over the minimum number of files I need. I get a sense of well-being from having the source available…

Task 2

PureMIDI (by Silenter) http://puremidi.codeplex.com/

Author writes "There are some goals which I would like to achieve: … Create module for transfer midi streams over TCP/IP "


OSC (Open Sound Control) appears to be an alternative to MIDI. Apparently it is better but it hasn't taken off very well.



So, if I can get my MIDI keyboard / OS X to pipe OSC to localhost, I should be able to use one of these solutions

MIDI Onto Network

How to get MIDI in from an external source, into unity FREE?
http://www.nerds.de/en/ipmidi.html lets me get MIDI onto the Internet connection, so I can use that and send it to localhost


in theory I should be able to pick it up from unity over UDP
it should be arriving in this format: RTP MIDI http://www.cs.berkeley.edu/~lazzaro/rtpmidi/




so I have 2 problems
1. how to actually pick up the UDP stream from within unity
2. how to implement RTP — I am trying to find some C#/JS implementation



http://winrtpmidi.codeplex.com/ <— this looks like it could be an easy port

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License