MusIT

Prefab MidiExternalPlay

Overview

MidiExternalPlay is a prefab included in the Pro version that can play MIDI music from a MIDI file, which is not part of the MPTK-defined MIDI database which is a read-only resource created during the build process.

You app will be able to play a MIDI:

  • From a file in a folder anywhere on the device.
  • From a web site with a URL.
  • From a byte array (by script).

Some points:

  • This prefab is available with MPTK Pro.
  • Using this prefab does not require any scripting. All tasks can be accomplished through the inspector.

API

For more advanced requirements in your application, an API is usually available. A full example with source code can be found at the bottom of this page.

Important note: The MidiExternalPlay class inherits from both the MidiFilePlayer and MidiSynth classes. Consequently, all attributes and methods from these classes are accessible through MidiExternalPlay.

Below are some quick examples:

Play from a MIDI on a web site:

C#
midiExternalPlayer.MPTK_MidiName = "http://www.midiworld.com/midis/other/c2/bolero.mid";
midiExternalPlayer.MPTK_Play();   

Play from a byte array:

C#
using (Stream fsMidi = new FileStream("C:\xxx\Midi\DreamOn.mid", FileMode.Open, FileAccess.Read))
{
   byte[] data = new byte[fsMidi.Length];
   fsMidi.Read(data, 0, (int)fsMidi.Length);
   midiExternalPlayer.MPTK_Play(data);
}

Play Loop inside an external MIDI

When calling MPTK_Play, the MIDI player resets some settings, it’s mandatory to change these settings according to your needs when the MIDI is ready to play. Use the OnEventStartPlayMidi to do this.

C#

MidiExternalPlayer midiExternalPlayer;
private void Start()
{
   midiExternalPlayer = FindObjectOfType<MidiExternalPlayer>();
   if (midiExternalPlayer != null)
   {
     // Event trigger when midi file start playing   
      midiExternalPlayer.OnEventStartPlayMidi.AddListener(StartPlay);
      midiExternalPlayer.MPTK_MidiName = yourUri;
      midiExternalPlayer.MPTK_Play();
   }
}
public void StartPlay(string midiname)
{
    Debug.Log("Start Midi " + midiname);
    Debug.Log("Duration: " +  midiExternalPlayer.MPTK_Duration.TotalSeconds); 
    midiExternalPlayer.MPTK_InnerLoop.Enabled = true;
    midiExternalPlayer.MPTK_InnerLoop.Start = 10000;
    midiExternalPlayer.MPTK_InnerLoop.Resume = 10000;
    midiExternalPlayer.MPTK_InnerLoop.End = 10200;
    midiExternalPlayer.MPTK_InnerLoop.Log = true;
}

Add the prefab in your scene

Before scripting with MidiExternalPlay, it is necessary to add a MidiExternalPlay prefab to your scene.

Add prefab MidiExternalPlay to your scene
Your scene after adding the prefab

Inspector parameters

When running:

Access all properties in the inspector
  • Midi URL or file path Example of path regarding the OS:
    • Windows file://C:/Users/Thierry/Desktop/Midi/WishYouWereHere.mid
    • Mac file:///Users/thierry/Desktop/Nirvana.mid
    • Web: http://www.midiworld.com/midis/other/bach/bwv1060b.mid
    • Or use the browser icon (on left) to select a file from your desktop.

Others Foldout

The inspector inherits of all properties of the MidiFilePlayer inspector, see below for a detailed explanation of others properties .

Available Demo

From Maestro / Load Demonstration
Demo wiith MidiExternalPlay prefab

Integration of MidiExternalPlayer in your script

See the TestMidiExternalPlayer.cs source code and the associated events in the TestExternalMidiPlay scene’s GameObjects canvas for the full example.

Get MPTK from the Unity store

If you like Midi Player Tool Kit, please leave a review on the Asset Store. It’s very appreciated!!!

Maestro MPTK on ChatGPT!

From different MPTK documentation sources DarkSky42 has created a custom LLM based on ChatGPT. You are now able to ask all the questions you want and get a good level of response, request code example, verify your source code …

Contact

If you have questions, please don’t hesitate to contact us via the dedicated Unity forum or our Discord  channel.

Reach the Discord archive by topic.

We are always happy to discuss your projects!

Add MIDI Music With 3 Clicks for Free

Sound Spatialisation, MPTK is ready for Virtual Reality [Pro]

Sound Spatialisation, MPTK is ready for Virtual Reality [free]

Midi Synth : Real Time Voice Effect Change

Euclidean Rhythm demo

The Deezer playlist that helped me create Maestro