Maestro - Midi Player Tool Kit for Unity Version 2.22.0
Loading...
Searching...
No Matches
MidiPlayerTK.MPTKInnerLoop

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

Public Types

enum  InnerLoopPhase { Start , Resume , Exit }
 Loop phase action sent to OnEventInnerLoop. More...

Public Member Functions

void Clear ()
 Resets inner-loop state. Enabled = false, Finished = false Start = 0, Resume = 0, End = 0 Count = 0.

Public Attributes

bool Log
 Enable log messages.
Func< InnerLoopPhase, long, long, int, bool > OnEventInnerLoop
 Callback triggered when an inner-loop event occurs. Parameters:
bool Enabled
 Enable or disable the loop. Default is false.
bool Finished
 Becomes true when the loop has finished or when OnEventInnerLoop returns false.
Can also be set to true to stop looping.
long Start
 Tick position where the loop begins when MIDI starts. The sequencer jumps immediately to this position.
If Start > Resume, the loop begins at Resume. Default is 0.
long Resume
 Tick position where the loop resumes when MidiLoad.MPTK_TickPlayer >= End. Default is 0. See also MidiFilePlayer.MPTK_RawSeek.
long End
 Tick position that triggers a restart at Resume (when MidiLoad.MPTK_TickPlayer >= End). Default is 0.
int Max
 Maximum number of loop iterations, including the first pass. When Count >= Max, the sequencer continues with MIDI events after tick End.
Set to 0 for an infinite loop. Default is 0.
int Count
 Current loop count. Default is 0.

Detailed Description

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

// 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 = FindAnyObjectByType<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;
}
}

Member Enumeration Documentation

◆ InnerLoopPhase

Loop phase action sent to OnEventInnerLoop.

Enumerator
Start 

Start the loop.

Resume 

Resume the loop.

Exit 

Exit the loop.

Member Data Documentation

◆ OnEventInnerLoop

Func<InnerLoopPhase, long, long, int, bool> MidiPlayerTK.MPTKInnerLoop.OnEventInnerLoop

Callback triggered when an inner-loop event occurs. Parameters:

  • InnerLoopPhase current loop phase
  • long current player tick (MPTK_TickPlayer)
  • long loop end tick (End)
  • long loop count (Count) Return:
  • boolean true to continue looping, false to exit.
    Note
  • This callback runs on the MIDI thread, not on the Unity thread.
  • It is not possible to call Unity APIs (except Debug.Log).
  • This runs on a managed thread context, so your script fields remain accessible.