MPTK: API to integrate Midi In Your Script

MPTK contains an API to play Midi Music (see MidiFilePlayer class) or Generated Music (see MidiStreamPlayer and MPTKNote) in your application.

General information:

  • Add this using in your C# : 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 have the benefit of the futures releases.

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 😉
  • InifintyMusic – a start of a generated music application, Could become a complete application 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.

 

Class MidiPlayerTK.MidiPlayerGlobal

Global class with static methods and properties. It’s a singleton class : only one instance is kept when playing. Template MidiFilePlayer and MidiStreamPlayer already contains a MidiPlayerGlobal component, but when playing only one instance is kept in these Player components.

Static Public Member Functions
  • int MPTK_FindMidi (string name)
    Find a Midi file from name or partial name.
  • float MPTK_DistanceToListener(Transform trf)
    Helper to calculate distance with the AudioListener and any Transform in your project.
  • void MPTK_SelectSoundFont(string name) [NEW 1.6]
    Change current Soundfont on fly.
Static Public Events
  • UnityEvent OnEventPresetLoaded
    Event triggered when MPTK is ready to play.
Static Properties
  • ReadOnlyCollection<string> MPTK_ListDrum  [get] [NEW 1.6]
    Get the list of waves available for drum.
  • ReadOnlyCollection<string> MPTK_ListMidi [get]
    Get list of midi files available.
  • ReadOnlyCollection<string> MPTK_ListPreset  [get] [NEW 1.6]
    Get the list of presets available.
  • List<string> MPTK_ListSoundFont  [get] [NEW 1.6]
    Get the list of Soundfont(s) available.

 

Class MidiPlayerTK.MidiFilePlayer

Play a selected midi file. A set of Midi file must be defined with Midi Player Setup (menu tools) before using these methods.

Public Member Functions
  • void MPTK_ClearAllSound(bool destroyAudioSource = false, float p_timeToRelease = -1f)
    Stop smoothly all sounds playing . Action is run in a coroutine, so method return immediately.

    • destroyAudioSource: set to True if you want to destroy all Audio Source.
    • p_timeToRelease : by default, the sound decreases in 0.1 second to avoid unpleasant noise. From version 1.7, you can defined another delay.
  • void MPTK_Play ()
    Play the current midi file selected with MPTK_MidiName or with  MPTK_MidiIndex.
  • void MPTK_Stop ()
    Stop playing current Midi file.
  • void MPTK_RePlay ()
    Restart playing the current midi file.
  • void MPTK_ReSyncTime()
    In case of delay in the application, resync is usefull to avoid huge midi events play at the same time.
  • void MPTK_Pause (float timeToPauseMS = -1f)
    Pause the current playing indefinitely or for a delay set in milliseconds.
  • void MPTK_Next ()
    Play next Midi in list.
  • void MPTK_Previous ()
    Play previous Midi in list.
Public Events
  • UnityEvent OnEventStartPlayMidi
    Define unity event to trigger at start of playing a Midi file.
  • ListNotesEvent OnEventNotesMidi [NEW 1.7]
    Define unity event to trigger when Midi events are available from the current playing Midi file.
void Start()
{
   midiFilePlayer.OnEventNotesMidi = new MidiFilePlayer.ListNotesEvent();
   midiFilePlayer.OnEventNotesMidi.AddListener(NotesToPlay); 
}
public void NotesToPlay(List<MidiNote> notes)
{ 
   midiFilePlayer.MPTK_PlayNotes(notes);
}
  • UnityEvent OnEventEndPlayMidi
    Define unity event to trigger at end. Not triggered when MPTK_Stop() is called.
Properties
  • TimeSpan MPTK_DirectSendToPlayer [get, set] [NEW 1.7]
    If true (default) then when Midi events are read they are sent automatically to the midi player. Set to false if you want to process events before playing. OnEventNotesMidi can be used to process midi data before playing. See “Demo CatchMusic” for example of use.
  • TimeSpan MPTK_Duration [get]
    Get duration of current Midi according the current MPTK_Speed.
  • bool MPTK_EnableChangeTempo [get, set]
    Should change tempo from Midi Events ?
  • bool MPTK_IsPaused [get]
    Is Midi file playing is paused ?
  • bool MPTK_IsPlaying [get]
    Is Midi file is playing ?
  • bool MPTK_Loop [get, set]
    Should automatically restart playing current Midi when Midi reach the end ?
  • bool MPTK_LogEvents [get, set]
    Log midi events.
  • bool MPTK_LogWaves [get, set]
    Log information on selected wave for each notes.
  • float MPTK_MaxDistance [get, set]
    MaxDistance to use for MPTK_PauseOnDistance. See example of use in “TestMidiFilePlayerMulti”.
  • int MPTK_MidiIndex [get, set]
    Index Midi to play or playing. return -1 if not found.
  • string MPTK_MidiName [get, set]
    Midi name to play. Must exists in MidiPlayerGlobal.MPTK_ListMidis. List of Midi files must be defined with Midi Player Setup (menu tools) and much contains that name.
  • bool MPTK_PauseOnDistance [get, set]
    Should the Midi playing must be paused if distance between AudioListener and MidiFilePlayer is greater than MaxDistance ? See example of use in “TestMidiFilePlayerMulti”.
  • bool MPTK_PlayOnStart [get, set]
    Should the Midi start playing when application start ?
  • TimeSpan MPTK_PlayTime [get]
    Time from the start of playing the current midi.
  • float MPTK_Position [get, set]
    Position to play from 0 to length time of midi playing (in millisecond).
  • float MPTK_PulseLenght [get]
    Lenght in millisecond of a quarter.
  • int MPTK_Quantization [get, set]
    Level of quantization : 0 = none to 5 = 64th Note.
  • float MPTK_Speed [get, set]
    Speed of playing. Between 0.1 (10%) to 5.0 (500%). Set to 1 for normal speed.
  • double MPTK_Tempo [get]
    Get default tempo defined in Midi file or modified with Speed. Return QuarterPerMinuteValue similar to BPM (Beat Per Measure).
  • int MPTK_Transpose [get, set]
    Transpose note from -24 to 24.
  • float MPTK_Volume [get, set]
    Set the volume of midi playing. Must be >=0 and <= 1.

Class MidiPlayerTK.MidiStreamPlayer

Play notes generated by script

Public Member Functions
  • void MPTK_ClearAllSound(bool destroyAudioSource = false, float p_timeToRelease = -1f)
    Stop smoothly all sounds playing . Action is run in a coroutine, so method return immediately.

    • destroyAudioSource: set to True if you want to destroy all Audio Source.
    • p_timeToRelease : by default, the sound decreases in 0.1 second to avoid unpleasant noise. From version 1.7, you can defined another delay.
  • void MPTK_Play(List<MPTKNote> notes)
    Play a list of notes with a thread (Coroutine). Return immediately. If you want to be able to stop note, use rather the Methods Play and Stop of MPTKNote class.
Properties
  • bool MPTK_LogWaves = false
    Log information on selected wave for each notes.
  • float MPTK_MaxDistance [get, set]
    MaxDistance to use for MPTK_PauseOnDistance. See example of use in “TestMidiFilePlayerMulti”.
  • bool MPTK_PauseOnDistance [get, set]
    Should the Midi playing must be paused if distance between AudioListener and MidiFilePlayer is greater than MaxDistance ? See example of use in “TestMidiFilePlayerMulti”
  • int MPTK_Transpose [get, set]
    Transpose note from -24 to 24.
  • float MPTK_Volume [get, set]
    Set of Volume of midi playing. Must be >=0 and <= 1

 

Class MidiPlayerTK.MPTKNote

public class MPTKNote
{
   /// <summary>
   /// Note between 0 and 127
   /// </summary>
   public int Note;

   /// <summary>
   /// Velocity between 0 and 127
   /// </summary>
   public int Velocity;

   /// <summary>
   /// Duration of the note in millisecond
   /// </summary>
   public double Duration;

   /// <summary>
   /// Delay before playing the note in millisecond
   /// Used to short delay inferior to 1 second. From 0 to 1000
   /// </summary>
   public float Delay;

   /// <summary>
   /// Patch (preset) to play with this note. Between 0 and 127. 
   /// Forced to 0 if Drum is True.
   /// </summary>
   public int Patch;

   /// <summary>
   /// True if not is a drum hit
   /// </summary>
   public bool Drum;
}

 /// <summary>
/// Play a note which is stoppable. 
/// A release delay (typically 0.1 second) will be apply before the effective
/// stop of the sound. No effect on Drum Kit sound. - [New 1.7]
/// </summary>
/// <param name="streamPlayer">A MidiStreamPlayer component</param>
public void Play(MidiStreamPlayer streamPlayer)

/// <summary>
/// Stop the note. A release delay (typically 0.1 second) will be apply 
/// before the stop of the sound. No effect on Drum Kit sound. - [New 1.7]
/// </summary>
public void Stop()

Have Fun !


Leave a Reply

Your email address will not be published. Required fields are marked *