Prefab MidiFilePlayer V2

MidiFilePlayer is a Prefab that you can use to play a Midi  files in your application. See Quick Start to understand how to add a MidiFilePlayer in your application.

Also, you can use MidiFilePlayer gameobject in your script to create a more complex interaction. See an example with source code at the end of this page.

Basics parameters

Select your midi in the Inspector and run your application, the Midi is playing . It’s as simple as that ! See below the specific parameters that can be applied.

  • Select Midi open a popup with all your Midi available in your project. Select one Midi.
  • Volume set the default volume of the playing.
  • Pause with distance if checked, automatically pause the play if distance between the gameobject MidiFilePlayer and the AudioListener is greater than MaxDistance.
  • MaxDistance distance for the pause with distance function.
  • Transpose transpose the music by half tone.
  • Play At Startup if checked, start playing when your application starts.
  • Send To Synth if not checked, Midi file is read but no Midi events are send to the synthesizer. Nevertheless, The Unity event
    OnEventNotesMidi is fired.
  • Loop if checked, restart playing automatically when the end of the Midi file is reached.

Parameters when running

  • Time / Position current time position of the playing Midi and total length. The value can change if a tempo change is received from the Midi sequencer. The slider can be used to change this position when playing.
  • Ticks / Position current ticks position of the playing Midi and total length. The value is independant of the tempo change. The slider can be used to change this position when playing.
  • Play / Pause / Stop / Restart, change the current playing status.
  • Previous / Next load previous or next Midi in the MPTK Midi list.

Foldout Midi Parameters

  • Quantization define quantization to remove some imprecision in tempo. With quantization, notes are plays on beats or on fractions of beats.
  • Speed to play the Midi file. with 1 play at the tempo defined in the Midi file.
  • Tempo Change enable tempo change from the Midi.
  • Drum Preset Change enable control change preset for drum. Disabled by default.
  • Keep Notes Off notes off are not used to play the Midi (duration is used). If you have the need to get Note Off (for example to write a consistent Midi file), check this option.
  • Log Midi Events check to have a log for each Midi events when playing.

Foldout Events

Fires actions in your script in relation with the Midi Player. These actios can be defined with the inspector or by script with MPTK API.

  • On Event Start Play Midi: start playing the Midi file. Receives the Midi name in parameter as a string.
  • On Event Notes Midi: when a group of notes is ready to be played by the sequencer. Receives a list of MptkEvent in the parameter.
  • On Event End Play Midi: end playing the Midi file. Receives the Midi name in parameter as a string and the reason of the end.

Foldout Midi Info

Display information from the META chunks of the midi file. Enabled only when playing a Midi which contains META data.

Foldout Synth Parameters

The midi synthesizer is based on the fluidsynth. It’s an adaption on this well known synth from C to C# for Unity. Some parameters can be changed.

  • Play Only First Wave: patches often play two waves (or more) for rendering a sound. Enabled this check to play only the sirst wave found.
  • Weak Device: to avoid heavy CPU consumption, enable this check to use a very light synth. The most part of Control Change events will not be processed.
  • Pan Change: if checked, panomarics (left/right) change from the Midi events are used. Don’t check if left/right is managed by your application.
  • Apply Modulator: enable or disable synth modulator.
  • Apply Mod LFO: enable or disable LFO modulator (amplitude LFO).
  • Apply Vib LFO: enable or disable LFO vibrator (pitch LFO).
  • Release Time Minimum: release time is a parameter set by patches within SoudFont. A minumum can be set with this parameters. A lower value than 50ms could produce bad sound.
  • Apply Filter: low pass filter is a parameter set by patches within SoudFont. You can change the default value.
  • Offset Filter Freq.: offset on the SF frequency set for this preset. 1000 seems a good value with the Unity filter …
  • Apply Reverb: reverb is an effect set by patches within SoudFont. You can change the default value.
  • Supersede Reverb: if greater than 0, this parameter supersedes the value found in SF. 0:low dry signal 1: high dry signal.
  • Apply Chorus: chorus is an effect set by patches within SoudFont. You can change the default value.
  • Supersede Chorus: if greater than 0, this parameter supersedes the value found in SF. 0:low original signal 1: high original signal.
  • Log Waves display a log for each waves selected for each notes.
  • On Event Synth Awake: fired just at the beginning of the Awake() of the synth gameobject.
  • On Event Synth Started: fired at the end of the Start() of the synth gameobject.

Foldout Show Default Editor

Some parameters, mainly for the synth, are not defined in the dedicated inspector. You can change some values for experimentation or debug … at your own risks!

Integration of MidiFilePlayer in your script

See TestMidiFilePlayerScripting.cs for the whole example.

using MidiPlayerTK;
...
MidiFilePlayer mfp = FindObjectOfType<MidiFilePlayer>();

// Set event by script
mfp.OnEventEndPlayMidi.AddListener(EndPlay);
mfp.OnEventNotesMidi.AddListener(MidiReadEvents);

// Event fired when a midi is ended
public void EndPlay(string midiname, EventEndMidiEnum reason)
{
   Debug.LogFormat("End playing {0} reason:{1}", midiname, reason);
}

// Event fired by MidiFilePlayer when midi notes are available
public void MidiReadEvents(List<MPTKEvent> events)
{
   foreach (MPTKEvent midievent in events)
      ...
}

void OnGUI()
{
   ...
   mfp.MPTK_Play();
   midiFilePlayer.MPTK_Position = position;
   ...
}