API MPTK V2 – Integrate MPTK in your script.

One of the strengths of MPTK is to be able playing music with no line of script. But, for more complex needs, you will have to write some Unity scripts.

This is a case where the MPTK API is useful. Don’t be worried MPTK API is quite simple and well documented. See yourself.

MPTK contains an API to:

  • Play Midi Music from midi file, see MidiFilePlayer class.
  • Play Generated Music from algorithm, see MidiStreamPlayer and MPTKEvent class.
  • Load a Midi file and get access to all the Midi events, see MidiFileLoader class.
  • With the Pro version, you can also:
    • Play External Midi, see MidiExternalPlayer class.
    • Write a Midi file, see MidiFileWriter class.
    • Defined play list with overlap and play extract of Midi, see MidiListPlayer class.
    • Read Midi events from a Midi keyboard, see MidiInReader class.
    • Change on fly current SoundFont or bank, see MidiPlayerGlobal class.

General information to start:

  • Add this namespace in your C# script : using MidiPlayerTK;
  • All methods and properties are prefixed with MPTK_
  • All methods are virtual : it’s better to encapsulate MPTK class with your own class than modifying MPTK source code if you want to keep the benefit of the future releases.
  • Avoid using methods or attributes not documented, they could change without any notification.

Some examples in the package could be useful for you:

  • CatchMusic – midi music with preprocessing before playing. Could become a complete game in the future or for you if you have the desire 😉
  • TestMidiFilePlayerWithScript – play midi music with a GUI, change SoundFont (Pro version)
  • TestMidiStreamSimple – generated music, very simple. More for my regression tests but useful to quickly understand the two methods available to generate music.

Simple example code with MidiExternalPlayer prefab (Pro version):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using MidiPlayerTK;

public class SimplePlayer : MonoBehaviour
    /// <summary>
    /// MPTK component able to play a Midi file from an external source. 
    /// Add a PreFab MidiExternalPlayer your scene.
    /// </summary>
    public MidiExternalPlayer midiExternalPlayer;

    private void Start()
        // Find the Midi external component 
        if (midiExternalPlayer == null)
            // Not found, try to find in the scene.
            Debug.Log("No midiExternalPlayer defined with the editor inspector, try to find one");
            MidiExternalPlayer fp = FindObjectOfType<MidiExternalPlayer>();
            if (fp == null)
                Debug.Log("Can't find a MidiExternalPlayer in the scene. No music will be played");
                midiExternalPlayer = fp;

        if (midiExternalPlayer != null)
            // Event trigger for each group of notes read from midi file
            if (!midiExternalPlayer.OnEventNotesMidi.HasEvent())
                // Set event by script
                Debug.Log("OnEventNotesMidi defined by script");
                Debug.Log("OnEventNotesMidi defined by Hierarchy in Unity editor");

    /// <summary>
    /// Event fired by MidiFilePlayer when Midi notes are available
    /// from the MPTK sequencer.
    /// Try this in the inputfield: http://www.midiworld.com/midis/other/c2/bolero.mid   
    /// </summary>
    public void ReadNotes(List<MPTKEvent> notes)
        Debug.Log("Notes count: " + notes.Count);
        foreach (MidiPlayerTK.MPTKEvent midiEvent in notes)
            if (midiEvent.Command == MPTKCommand.NoteOn)
                Debug.Log(" Channel:" + midiEvent.Channel +
                          " Note:" + midiEvent.Value + 
                          " Velocity:" + midiEvent.Velocity + 
                          " Duration:" + midiEvent.Duration + " ms");

    /// <summary>
    /// Other method: play is fired from the a button or an input field
    /// The URL is used to load the midi file from a web site.
    /// </summary>
    /// <param name="uri">uri or path to the midi file</param>
    public void Play(string uri)
        Debug.Log("Play from script:" + uri);
        midiExternalPlayer.MPTK_MidiName = uri;

Have Fun!