Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Implementing C# MIDI Toolkit

Discussion in 'Editor & General Support' started by Lastair, Mar 21, 2012.

  1. Lastair

    Lastair

    Joined:
    Mar 16, 2012
    Posts:
    13
    Basically, I want to implement this in a Unity project:

    http://www.codeproject.com/Articles/6228/C-MIDI-Toolkit

    The idea is to read a MIDI file and play it back while running animations pre-generated based on the MIDI events, so I need the full functionality. Anyway, as I'm somewhat new to coding in general, I'm not exactly sure how to make it work for me. I just extracted the entire thing into my Assets folder on my first attempt, which resulted in this error that I couldn't make much sense of:
    The code seems to use DLLs, so does this mean I would need Unity Pro to make it work as a plugin? I understand it takes a bit of effort to check out something like this, and the download is behind a login wall, but I'd really appreciate it. Or if there's some fundamental reason I can't just shove in any C# code I find on the Internet, it'd also be nice if someone could shed light on that. :)

    If it's not possible without Pro (or just not possible in general), I'd really appreciate hints on any other ways to approach reading events from MIDI files and MIDI playback. Coding it on my own seems like it'd be well beyond my abilities, and the only MIDI-related thing in the Asset Store I could find ("MIDI Unified") requires the Pro license and doesn't seem to have the capabilities for playing back MIDI files or reading the event data from them directly, just reacting in real time to MIDI events being generated from other sources.

    Help would be greatly appreciated. :)
     
  2. Lastair

    Lastair

    Joined:
    Mar 16, 2012
    Posts:
    13
    I got it to work (or it looks good so far) and I figure it'd be good to share what I had to do in case someone stumbles here through a search or something. Some of this might be kind of obvious, but I'm a beginner so it wasn't perfectly clear to me. Thanks to people on the IRC channel for helping me figure this out. Anyway, in this specific case, here's some info:

    * I had to switch my Unity project API compatibility level from the NET 2.0 subset to the full NET 2.0. (Edit - Project Settings - Player, it's under Other Settings in the Inspector) My project will be only on Windows and the MIDI Toolkit itself is NET 2.0 compatible like it says on the page so there shouldn't be problems here.

    * The DLL files turned out to be C# DLLs, so they work like they do in C#, which was something I didn't fully understand. Basically they aren't "plugins" in the Unity sense, and will work without the Pro license.

    * When copying this over to my project Assets folder, I just made sure not to include the GUI stuff and other things that were obviously extraneous like the example projects, debug DLLs and the Visual Studio project files.

    Hopefully this will help if someone ends up in the same spot as me. I still have to figure the library itself out to make it do what I need it to, but running some of the example code and doing basic things, it seems to be working quite nicely. If anyone feels like there's anything to add, feel free to. :)
     
  3. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
  4. psdev

    psdev

    Joined:
    Feb 11, 2011
    Posts:
    53
    So will your solution not work on iOS? And does it require Unity Pro? I just bought the MIDI Unified from the Asset Store, but haven't tried it yet as it requires Pro and I wont be upgrading for a few months. I'd really like to get MIDI file reading (and playback with no sound, just timing to sync with real audio files) working on an iPad.

    I need to be able to load multitrack midi files and have at least the Note On Off info and timing per channel/track. And ideally play this back with the timing synced to an MP3/Wav.

    Will what you're doing work in this situation:?
    Thanks for any help :)
     
  5. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
    Last edited: Mar 29, 2012
  6. psdev

    psdev

    Joined:
    Feb 11, 2011
    Posts:
    53
    Thanks for that link Wolfos, that's good to know. :)

    Still wondering if this Midi solution requires Unity Pro though and if it does playback of midi too that I can sync to actual audio files playing.
     
  7. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
  8. Pi_3-14

    Pi_3-14

    Joined:
    May 9, 2012
    Posts:
    168
    Lastair, did you ever complete this port?

    I've had a look through the codeproject article, and it's super-high-grade code.

    I'm in need of a MIDI sequencer that takes a file as input and spits out note-on messages at the right times. I'm currently using SonicViz's port as it works out of the box (many thanks SV!). However, the original source code has the sequence and the synth glommed together; it really is impossible to separate them. And I'm using my own synth.

    I'm going to have a go at this port myself.

    If you do have something working, and you are happy to share code, it would be gratefully received!

    π

    EDIT: I have now have to go; I'm getting floored by this error:

    DllNotFoundException: winmm.dll
    Sanford.Multimedia.Timers.Timer..cctor ()
    Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Sanford.Multimedia.Timers.Timer
    Sanford.Multimedia.Midi.MidiInternalClock..ctor ()
    Sanford.Multimedia.Midi.Sequencer..ctor ()
    SequencerTest.Start () (at Assets/SequencerTest.cs:14)

    Looks like I need to get the source code for his Sanford.Multimedia.Timers .NET DLL, and wrap around whatever line is calling out into WinMM.dll.
     
    Last edited: Oct 27, 2013