API MPTK – Integrate MPTK in your script.

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

Above all, don’t be worried MPTK API is quite simple and well documented. See yourself.

Therefore, MPTK contains an API able to:

  • Playing Midi Music from midi file, see MidiFilePlayer class.
  • Playing Generated Music from algorithm, see MidiStreamPlayer and MPTKEvent class.
  • Loading a Midi file and get access to all the Midi events, see MidiFileLoader class.
  • With the Pro version, you can also:
    • Playing External Midi, see MidiExternalPlayer class.
    • Writing a Midi file, see MidiFileWriter class.
    • Defined play list with overlap and play extract of Midi, see MidiListPlayer class.
    • Reading Midi events from a Midi keyboard, see MidiInReader class.
    • Changing on fly current SoundFont or bank, see MidiPlayerGlobal class.
    • Applying effects as filter, reverb, chorus.
    • Build Scales and Chords.

General information to start:

  • First of all, you’ll need to add the following at the top of any C# script that accesses them: using MidiPlayerTK;
  • All methods and properties are prefixed with MPTK_. To clarify, don’t use the others ! They can be modified or removed without any information.
  • Some methods are virtual : also 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.

Download API documentation (PDF)

We hate huge documentation! Consequently methods and attributes inherited are not duplicated in each class.

Also, a Help file (.chm) is available with the asset.

Some examples in the package could be useful for you:

  • CatchMusic – midi music with preprocessing before playing. In short, could become a complete game in the future or for you if you have the desire 😉
  • EuclideanRhythm – how to build a rythmic box! (Pro only)
  • TestMidiFilePlayerWithScript – play midi music with a GUI, change SoundFont (Pro version)
  • TestMidiStreamSimple – very simple example for generating music. It’s useful for my regression tests but also for you 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!