Maestro - Midi Player Tool Kit for Unity Version 2.18.2
Loading...
Searching...
No Matches
Maestro Pro Extensions

Extended loading, looping, and transition features available in Maestro Pro. More...

Classes

class  MidiPlayerTK.MPTKInnerLoop
 MIDI inner-loop settings for MidiFilePlayer and MidiExternalPlayer [Pro].
See MidiFilePlayer.MPTK_InnerLoop and MidiExternalPlayer.MPTK_InnerLoop. See example:
. More...

Inner Loop

Defines looping conditions inside the MIDI sequencer [Pro]. An instance is automatically created when the MidiFilePlayer prefab is loaded.
See #MPTKInnerLoop and #MPTK_RawSeek.

MPTKInnerLoop MidiPlayerTK.MidiFilePlayer.MPTK_InnerLoop

Detailed Description

Extended loading, looping, and transition features available in Maestro Pro.

MPTKInnerLoop Workflow (Pro)

MPTKInnerLoop defines an inner loop range driven by ticks:

  • Start: initial tick when playback starts,
  • End: loop boundary tick,
  • Resume: target tick when the loop restarts.

This is useful when you want to repeat only one musical segment without restarting the whole MIDI file.

Quick Start

  1. Get MPTK_InnerLoop from your player.
  2. Set Enabled to true.
  3. Configure Start, End, and Resume.
  4. Optionally set Max (0 means infinite).

Runtime Control

  • Count tracks current loop iterations.
  • Set Finished to true to stop looping.
  • Use Clear() to reset inner-loop state.

Callback Behavior

OnEventInnerLoop is called for loop phases (Start, Resume, Exit).

Return true to keep looping, false to exit.

Warning
This callback runs on the MIDI thread. Avoid Unity API calls, except Debug.Log.
See also
MidiFilePlayer Pro Extensions
MidiPlayerTK.MidiFilePlayer.MPTK_InnerLoop
MidiPlayerTK.MidiExternalPlayer.MPTK_InnerLoop

Variable Documentation

◆ MPTK_InnerLoop

MPTKInnerLoop MidiPlayerTK.MidiFilePlayer.MPTK_InnerLoop
Version
Maestro Pro
// Full source code in TestInnerLoop.cs
// As usual with a MVP demo, focus is on the essentials:
// - no value check,
// - limited error catch,
// - no optimization,
// - limited functions
// - ...
// Start is called before the first frame update
void Start()
{
// Find a MidiFilePlayer in the scene hierarchy
// Innerloop works also with MidiExternalPlayer
// ----------------------------------------------
midiFilePlayer = FindFirstObjectByType<MidiFilePlayer>();
if (midiFilePlayer == null)
{
Debug.LogWarning("Can't find a MidiFilePlayer Prefab in the current Scene Hierarchy. Add it with the Maestro menu.");
return;
}
// The MPTK_InnerLoop attributes are cleared when the MIDI is loaded.
// To define the start condition, you need to define a callback function (here StartPlay)
// that will set the inner loop attributes when the MIDI is started.
// Note: with this demo the MPTK_InnerLoop attributes are defined in the Unity Update().
// So, defining the initial condition is not useful ... just for the demo!
midiFilePlayer.OnEventStartPlayMidi.AddListener(StartPlay);
midiFilePlayer.MPTK_Play();
}
// Event fired by MidiFilePlayer or MidiExternalPlayer instance when a midi is started.
// Useful when MPTK properties are cleared when the MIDI is loaded ... for example for MPTK_InnerLoop.
public void StartPlay(string midiname)
{
Debug.Log("Start Midi " + midiname + " Duration: " + midiFilePlayer.MPTK_Duration.TotalSeconds + " seconds");
// midiFilePlayer.MPTK_InnerLoop is instantiated during the awake phase of the MidiFilePlayer.
// You can also instantiated or manage your own references and set midiFilePlayer.MPTK_InnerLoop with your MPTKInnerLoop instance.
midiFilePlayer.MPTK_InnerLoop.Enabled = true;
// No log from MPTK for this demo, rather we prefer to use a callback to define our own.
midiFilePlayer.MPTK_InnerLoop.Log = false;
// Define C# event of type Func() for each loop phase change: Start --> Resume --> ... --> Resume --> Exit
// If return is false then looping can be exited earlier..
// It's also possible to set innerLoop.Finished to True anywhere in your script
// but the loop will not be finished until tickPlayer reaches the end of the loop.
midiFilePlayer.MPTK_InnerLoop.OnEventInnerLoop = (MPTKInnerLoop.InnerLoopPhase mode, long tickPlayer, long tickSeek, int count) =>
{
Debug.Log($"Inner Loop {mode} - MPTK_TickPlayer:{tickPlayer} --> TickSeek:{tickSeek} Count:{count}/{midiFilePlayer.MPTK_InnerLoop.Max}");
if (mode == MPTKInnerLoop.InnerLoopPhase.Exit)
// Set the value for the Unity User Interface to be able to reactivate the loop.
LoopFinished = true;
return true;
};
// Set initial inner loop attributes
SetInnerLoopParameters();
}
private void SetInnerLoopParameters()
{
// These parameters can be changed dynamically with the inspector
midiFilePlayer.MPTK_InnerLoop.Max = LoopMax;
midiFilePlayer.MPTK_InnerLoop.Start = TickStart;
midiFilePlayer.MPTK_InnerLoop.Resume = TickResume;
midiFilePlayer.MPTK_InnerLoop.End = TickEnd;
midiFilePlayer.MPTK_InnerLoop.Finished = LoopFinished;
}
// Update is called once per frame
void Update()
{
if (midiFilePlayer != null && midiFilePlayer.MPTK_MidiLoaded != null)
{
// Display current real-time tick value of the MIDI sequencer.
TickPlayer = midiFilePlayer.MPTK_MidiLoaded.MPTK_TickPlayer;
// Display tick value of the last MIDI event read by the MIDI sequencer.
TickCurrent = midiFilePlayer.MPTK_MidiLoaded.MPTK_TickCurrent;
// Display current measure and beat value of the last MIDI event read by the MIDI sequencer.
MeasurePlayer = $"{midiFilePlayer.MPTK_MidiLoaded.MPTK_CurrentMeasure}.{midiFilePlayer.MPTK_MidiLoaded.MPTK_CurrentBeat} - Last measure: {midiFilePlayer.MPTK_MidiLoaded.MPTK_MeasureLastNote}";
// Set inner loop attributes from the inspector's values.
SetInnerLoopParameters();
// These values are read from the inner loop instance and display on the UI.
loopEnabled = midiFilePlayer.MPTK_InnerLoop.Enabled;
LoopCount = midiFilePlayer.MPTK_InnerLoop.Count;
// Calculate tick position of a measure (just for a demo how to calculate tick from bar).
// So, it's easy to create loop based on measure.
Tick = MPTKSignature.MeasureToTick(midiFilePlayer.MPTK_MidiLoaded.MPTK_SignMap, Measure);
// Add quarter. Beat start at the begin of the measure (Beat = 1).
Tick += (Quarter - 1) * midiFilePlayer.MPTK_DeltaTicksPerQuarterNote;
}
}