MidiPlayerTK.MPTKInnerLoop

MIDI inner loop setting available for MidiFilePlayer and MidiExternalPlayer [Pro].
Look at MidiFilePlayer.MPTK_InnerLoop and MidiExternalPlayer.MPTK_InnerLoop. See Example:
More...

Public Types

enum  InnerLoopPhase { Start , Resume , Exit }
 

Public Member Functions

void Clear ()
 

Public Attributes

int Count
 
bool Enabled
 
long End
 
bool Finished
 
bool Log
 
int Max
 
Func< InnerLoopPhase, long, long, int, bool > OnEventInnerLoop
 
long Resume
 
long Start
 

Detailed Description

MIDI inner loop setting available for MidiFilePlayer and MidiExternalPlayer [Pro].
Look at 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 = FindObjectOfType<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;
}
}
long Start
Definition: MPTKInnerLoop.cs:73

Member Enumeration Documentation

◆ InnerLoopPhase

Loop phase action sent to OnEventInnerLoop

Enumerator
Start 

Start the loop

Resume 

Resume the loop

Exit 

Exit the loop

Member Function Documentation

◆ Clear()

void MidiPlayerTK.MPTKInnerLoop.Clear ( )

Clear Inner Loop attributes. Enabled = false, Finished = false Start = 0, Resume = 0, End = 0 Count = 0

Member Data Documentation

◆ Count

int MidiPlayerTK.MPTKInnerLoop.Count

Current loop count. Default is 0.

◆ Enabled

bool MidiPlayerTK.MPTKInnerLoop.Enabled

Enable or disable the loop. Default is false.

◆ End

long MidiPlayerTK.MPTKInnerLoop.End

Tick position to trigger the loop restart to the Resume position (when MidiLoad.MPTK_TickPlayer >= to End). Default is 0.

◆ Finished

bool MidiPlayerTK.MPTKInnerLoop.Finished

Becomes true when the loop is finished or OnEventInnerLoop returns false.
Can also be set to true to stop looping.

◆ Log

bool MidiPlayerTK.MPTKInnerLoop.Log

Enable logging message

◆ Max

int MidiPlayerTK.MPTKInnerLoop.Max

Maximum iteration for the loop including the first. When Count >= Max the MIDI sequencer continue to the next MIDI events AFTER TICK #eND.
Set to 0 for infinite loop. Default is 0.

◆ OnEventInnerLoop

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

Unity event triggered when a loop occurs. parameters:

  • InnerLoopPhase current loop phase
  • long current tick player (MPTK_TickPlayer)
  • long tick target (End)
  • long loop count (Count). return:
  • boolean true continue looping, false exit loop.
    Note
  • this action is done from the MIDI thread, not from the Unity thread.
  • It's not possible to call Unity API (only Debug.Log).
  • it's a managed thread, so all variables from your script are visible.

◆ Resume

long MidiPlayerTK.MPTKInnerLoop.Resume

Tick position to resume the loop when MidiLoad.MPTK_TickPlayer >= to End. Default is 0. See also MidiFilePlayer.MPTK_RawSeek

◆ Start

long MidiPlayerTK.MPTKInnerLoop.Start

Tick position where the loop begin when the MIDI start. The MIDI sequencer go immediately to this position.
if Start > Resume the loop will begin at Resume position. Default is 0.