Maestro - Midi Player Tool Kit for Unity Version 2.18.2
Loading...
Searching...
No Matches
Playback Events and Callbacks

React to playback start, end, and groups of MIDI events. More...

MIDI Events

EventNotesMidiClass MidiPlayerTK.MidiFilePlayer.OnEventNotesMidi
 Method triggered for each MIDI event (or group of MIDI events) ready to be played by the MIDI synth. All these events are on same MIDI tick
. The callback method is able to directly interacts with Unity gameObject (same thread).
A List<MPTKEvent> is passed to the delegate.
EventStartMidiClass MidiPlayerTK.MidiFilePlayer.OnEventStartPlayMidi
 Define the Unity event to be triggered at the start of Midi playback.
At this moment, the MIDI file is loaded, the MIDI synth is initialised, but no MIDI event has been read yet.
This is the right time to defined some specific behaviors.
EventEndMidiClass MidiPlayerTK.MidiFilePlayer.OnEventEndPlayMidi
 Specify the Unity event that is triggered when the end of the MIDI list of events is reached.

Detailed Description

React to playback start, end, and groups of MIDI events.

Variable Documentation

◆ OnEventNotesMidi

EventNotesMidiClass MidiPlayerTK.MidiFilePlayer.OnEventNotesMidi

Method triggered for each MIDI event (or group of MIDI events) ready to be played by the MIDI synth. All these events are on same MIDI tick
. The callback method is able to directly interacts with Unity gameObject (same thread).
A List<MPTKEvent> is passed to the delegate.

Note
It's not possible to alter playing music by modifying note properties (pitch, velocity, ....) in the callback.
using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
using UnityEngine;
public class YourClass : MonoBehaviour
{
MidiFilePlayer midiFilePlayer;
void Start()
{
// Get a reference to the prefab MidiFilePlayer from the hierarchy in the scene
midiFilePlayer = FindFirstObjectByType<MidiFilePlayer>();
// Add a listener on the MIDI File Player.
// NotesToPlay will be called for each new group of notes read by the MIDI sequencer from the MIDI file.
midiFilePlayer.OnEventNotesMidi.AddListener(NotesToPlay);
}
// This method will be called by the MIDI sequencer just before the notes
// are playing by the MIDI synthesizer (if 'Send To Synth' is enabled)
public void NotesToPlay(List<MPTKEvent> mptkEvents)
{
Debug.Log("Received " + mptkEvents.Count + " MIDI Events");
// Loop on each MIDI events
foreach (MPTKEvent mptkEvent in mptkEvents)
{
// Log if event is a note on
if (mptkEvent.Command == MPTKCommand.NoteOn)
Debug.Log($"Note on Time:{mptkEvent.RealTime} millisecond Note:{mptkEvent.Value} Duration:{mptkEvent.Duration} millisecond Velocity:{mptkEvent.Velocity}");
// Uncomment to display all MIDI events
// Debug.Log(mptkEvent.ToString());
}
}
}
Represents a MIDI event used throughout MPTK. This class is central to script-based MIDI workflows in...
Definition MPTKEvent.cs:59
MPTKCommand Command
MIDI command type for this event. See MPTKCommand (NoteOn, ControlChange, PatchChange,...
Definition MPTKEvent.cs:73
Plays a MIDI file from the MidiDB. This class must be used with the prefab MidiFilePlayer....
Definition MidiFilePlayer.cs:69
EventNotesMidiClass OnEventNotesMidi
Method triggered for each MIDI event (or group of MIDI events) ready to be played by the MIDI synth....
Definition MidiFilePlayer.cs:926
Definition MidiFileEditorPlayer.cs:6
MPTKCommand
MIDI command codes. Defines the action performed by the message: note on/off, patch change,...
Definition MPTKEnum.cs:16

◆ OnEventStartPlayMidi

EventStartMidiClass MidiPlayerTK.MidiFilePlayer.OnEventStartPlayMidi

Define the Unity event to be triggered at the start of Midi playback.
At this moment, the MIDI file is loaded, the MIDI synth is initialised, but no MIDI event has been read yet.
This is the right time to defined some specific behaviors.

using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
using UnityEngine;
public class YourClass : MonoBehaviour
{
MidiFilePlayer midiFilePlayer;
void Start()
{
// Get a reference to the prefab MidiFilePlayer from the hierarchy in the scene
midiFilePlayer = FindFirstObjectByType<MidiFilePlayer>();
// Add a listener on the MIDI File Player.
// NotesToPlay will be called for each new group of notes read by the MIDI sequencer from the MIDI file.
midiFilePlayer.OnEventStartPlayMidi.AddListener(StartPlay);
}
public void StartPlay(string midiname)
{
Debug.LogFormat($"Start playing midi {midiname}");
// Disable MIDI channel 9 (generally drums)
midiFilePlayer.MPTK_ChannelEnableSet(9, false);
// Set start tick
midiFilePlayer.MPTK_TickCurrent = 500;
}
}
EventStartMidiClass OnEventStartPlayMidi
Define the Unity event to be triggered at the start of Midi playback. At this moment,...
Definition MidiFilePlayer.cs:973

◆ OnEventEndPlayMidi

EventEndMidiClass MidiPlayerTK.MidiFilePlayer.OnEventEndPlayMidi

Specify the Unity event that is triggered when the end of the MIDI list of events is reached.

Note
  • This event is triggered even if the note is still in play. In some cases this may cause unpleasant behavior.
    MPTK_ModeStopVoice defined the behavior of the MIDI player when playback is stopped or restarted.
    A good practice is to defined MPTK_ModeStopVoice = ModeStopPlay.StopWhenAllVoicesReleased.
  • By default, the end of playback of a MIDI file is not the last note. It is the last MIDI event.
    Sets MPTK_StopPlayOnLastNote to true to fire this event on the last note.
  • Set MPTK_KeepEndTrack or MPTK_KeepNoteOff to true when loading the MIDI file to synchronise the end of playback with the real end of the MIDI file.
    using MidiPlayerTK; // Add a reference to the MPTK namespace at the top of your script
    using UnityEngine;
    public class YourClass : MonoBehaviour
    {
    MidiFilePlayer midiFilePlayer;
    void Start()
    {
    // Get a reference to the prefab MidiFilePlayer from the hierarchy in the scene
    midiFilePlayer = FindFirstObjectByType<MidiFilePlayer>();
    // Add a listener on the MIDI File Player.
    // NotesToPlay will be called for each new group of notes read by the MIDI sequencer from the MIDI file.
    midiFilePlayer.OnEventEndPlayMidi.AddListener(EndPlay);
    midiFilePlayer.MPTK_ModeStopVoice = MidiFilePlayer.ModeStopPlay.StopWhenAllVoicesReleased;
    }
    public void EndPlay(string midiname, EventEndMidiEnum reason)
    {
    Debug.LogFormat($"End playing midi {midiname} reason:{reason}");
    }
    }
    ModeStopPlay
    Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the...
    Definition MidiFilePlayer.cs:211
    EventEndMidiClass OnEventEndPlayMidi
    Specify the Unity event that is triggered when the end of the MIDI list of events is reached.
    Definition MidiFilePlayer.cs:1014