MusIT

Prefab MidiFilePlayer

MidiFilePlayer is a Prefab useful for playing a MIDI file in your application. See Quick Start to understand how to add a MidiFilePlayer in your application.

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

Tips:

  • Play once MIDI file at a time.
  • Several prefabs MidiFileplayer can be added to the scene and can play simultaneously.
  • All the parameters and the functions of the prefab are also available with an API for integration in your script. See here.

Main parameters

Maestro MIDI File Player settings when not running

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 apply.

  • Select Midi: open a popup with all your MIDI file available in your project. Select one MIDI to play. Before, you have to add MIDI files to the local MidiDB. See method
    Free version comes with a set of MIDI files for demonstration purpose, obviously you can delete all. See MPTK Midi Setup for setting MIDI files DB. For selecting a MIDI from script, See method MPTK_MidiIndex or MPTK_MidiName or MPTK_SearchMidiToPlay (Pro).
  • Volume: set the default volume of the playing. See properties MPTK_Volume.
  • Transpose: transpose the music by half tones. See properties MPTK_Transpose
  • Channel Exception: transpose will not affect this channel. Generally, we don’t want to transpose channel 9 which contains drums hit: each note play a different drum, transpose will change the drum played! See method MPTK_TransExcludedChannel.
  • Play At Startup: if checked, start playing when your application starts.
  • Start Play From First Note: search for the first note in the MIDI and plays it immediately. Useful to avoid latency at start. See properties MPTK_StartPlayAtFirstNote.
  • Pause When Focus Loss: Pause the MIDI playing when the application lost the focus, go in background or application is suspended. See properties MPTK_PauseOnFocusLoss.
  • Send To Synth: if not checked, MIDI file is read by the internal Midi sequencer but no MIDI event is sent to the internal MIDI synthesizer. Nevertheless, all MIDI events are triggering the event OnEventNotesMidi, so you can processing MIDI events in your script without playing it. See properties MPTK_DirectSendToPlayer.
  • Loop On Midi: if check, restart playing automatically when the end of the MIDI file is reached. The MIDI file is not reload. See properties MPTK_Loop.

Parameters when running

Maestro MIDI File Player settings when running
  • Real Time: Real time from the start of the playing. Change tempo or speed have no impact except for MIDI pause. See method MPTK_RealTime.
  • MIDI Time / Position: current time position of the playing Midi and total length. The time can change if a tempo change is received from the Midi sequencer. The slider can be use to change this position when playing. See properties MPTK_Position and MPTK_Duration.
  • Ticks / Position: current tick position of the playing Midi and total length. The value is independent of the tempo change. The slider can be use to change this position when playing. See properties MPTK_TickCurrent and MPTK_TickLast.
  • Play / Pause / Stop / Restart, change the current playing status. See method MPTK_Play, MPTK_Pause, MPTK_Stop and MPTK_RePlay.
  • Previous / Next load previous or next Midi in the MPTK Midi list. See method MPTK_Previous, MPTK_Next and MPTK_PlayNextOrPrevious.

Foldout Spatialization

First of all, you have to setup Spatialization in Unity See here. Then look at these dedicated settings in the prefab inspector:

MPTK Inspector For Spatialization Parameters

Define a distance for the audio spatializer
  • Spatialization: when checked, Unity spatializer is enabled. It’s not necessary to install a spatializer plug-ins to get some simple spatial effects but you will get a warning message from Unity:

    Audio source failed to initialize audio spatializer. An audio spatializer is specified in the audio project settings, but the associated plugin was not found or initialized properly. Please make sure that the selected spatializer is compatible with the target.

When MPTK_Spatialize is true some parameters of the AudioSource are set:

AudioSource.maxDistance = MPTK_MaxDistance
AudioSource.spatialBlend = 1
AudioSource.spatialize = true
AudioSource.spatializePostEffects = true.
See properties MPTK_Spatialize.


Warning: Without a spatializer plug-ins, you will still have these effects: volume depending on the distance and left/right panning (but limited to panning specified in the SoundFont).

  • Max Distance: distance for pausing the Midi reader. Maestro MPTK will pause the MIDI reader if the distance between the gameobject (which holds this prefab) and the AudioListener is greater than Max Distance. See MPTK_MaxDistance.

Tips:

  • To take advantage of this effect, it’s better to listen with an headphone.
  • Do not mix up “stereo” and “spatialization” effects. The stereo information (pan) comes up from the SoundFont properties. For example: left right panning for a piano is depending on the key position.
    These two effects could counteracted each other. The “pan” properties can be disabled within the foldout “Show Synth Parameters” in inspector. See here Modulator / Pan Change.
  • For enjoying full spatialization, don’t forget to setup Unity spatialization See here!
Define a spatializer in the Unity Audio Settings

Look also at the dedicated prefab MidiSpatializer (Pro version) able to assign different 3d sound positions by midi channels, by tracks, by instruments. Fly through the music. Perfect with a virtual headsets!!!

Foldout Midi Parameters

Maestro MIDI parameters
  • Quantization: define quantization to remove some imprecision in tempo. With quantization, notes are plays on beats or on fractions of beats. See MPTK_Quantization.
  • Speed: to play the Midi file. with 1 play at the tempo defined in the Midi file. See MPTK_Speed.
  • Tempo Change: enable tempo change from the Midi. See MPTK_EnableChangeTempo.
  • Drum Preset Change: enable control change preset for drum. Disabled by default. See MPTK_EnablePresetDrum.
  • 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 or process these events in your code), check this option. See MPTK_KeepNoteOff.
  • Log Midi Events: check to have a log for each Midi events when playing. See MPTK_LogEvents.

Foldout Events

Maestro Unity Events

Fires actions in your script in relation with the Midi Player. These actions can be define 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. See OnEventStartPlayMidi.
  • On Event Notes Midi: this event is triggered when a group of MIDI events is ready to be played by the sequencer. A list of MPTKEvent is passed in the parameter. See OnEventNotesMidi.
  • 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. See OnEventEndPlayMidi.

Foldout Midi Info

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

Maestro MIDI Info

Foldout Synth Parameters

The midi synthesizer is based on fluidsynth. It’s an adaption on this well known synth from C to C# for Unity.

Audio Parameters

Maestro MIDI Synthesizer parameters
  • Core Player: if checked, the synthesizer is working on a system thread apart from the main Unity thread: accuracy and latency are better. On top of that, synthesizer is based on a full migration of fluidsynth: sound is better.
    When Core Player is not checked, the legacy mode is enabled (available since the V1). Many AudioSource are used to play each sample and the synth is simplified. Warning, the legacy mode is not compatible with all the MPTK functions. It will be removed with the next major version.
  • Rate Synth Output: defined the rate of the FMOD component in Unity. High rate enhance the quality of the sound but with cost on performance (linear impact). It’s a global parameter for your application. The default value can be also defined with Project Setting/Audio.
  • Buffer Synth Size: defined the size of the FMOD buffer. Small buffer decrease the latency and enhance the accurate of the playing but with a little cost on performance. It’s a global parameter for your application. The default value can be also defined with Project Setting/Audio.
  • Interpolation Method: convert the wave sample to the synth format.
    • None: no interpolation. Just take the sample, which is closest to the playback pointer. Very efficient but questionable quality.
    • Linear: Straight line interpolation. A good balancing. Cost performance: 17% from none interpolation.
    • 4th order: Even Better ! Cost performance: 30% (editor mode, to be tested with ILCPP)
    • 7th order: Even Better ! Cost performance: 65% (editor mode, to be tested with ILCPP)

Tip: Changing audio parameters on iOS is disabled because the hardware don’t like changing the default value. Seems to be 24kHz.

Tip: See performance information here

Modulator

Maestro MIDI Modulator settings
  • Log Samples: log to console information about each samples for each notes: sample name, instrument.
  • Pan Change: panoramic setting (stereo) are processed with specific Midi events (Control Change 08).
    But, the pan information comes up from the SoundFont properties. For example: left right panning for a piano is depending on the key position.
    Uncheck Pan Change if left/right is managed by your application or you have enabled Spatialization.
  • Apply Modulator: apply real time modulator as Pitch Bend and Control Change.
  • Apply Mod LFO: apply LFO modulator (amplitude LFO).
  • Apply Vib LFO: apply LFO vibrator (pitch LFO).
  • Release Same Note: the default behavior of a Midi player is to release a note when the same note is played again. A physical keyboard can’t plays two keys at the same time! It’s possible to change this behavior by unchecking this checkbox.
  • Kill By Exclusive Class: the default behavior of a Midi player is to released a note when a note of the same class is played in the same preset. Class is defined in the SoundFont. Generally, this function is used by drum kit. It’s possible to change this behavior by unchecking this checkbox.

SoundFont Effects [Pro]

Important: by default these effects are not enabled in the prefab. You have to enable them from the inspector (or by script) and enjoy a better sound.

See here for more information.

Foldout Show Default Editor

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

There is also some information about time processing of Midi and Synth. To activate statistics, add these Define Symbols (see here how to):

  • DEBUG_PERF_AUDIO
  • DEBUG_PERF_MIDI
  • DEBUG_STATUS_STAT

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; ... }
Code language: PHP (php)