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

MIDI loading/parsing services and scan helpers. More...

Classes

class  MidiPlayerTK.MidiFileLoader
 @Warning - will be soon deprecated, please rather use MidiFilePlayer prefab which have the same features + eventually MIDI sequencer and MIDI synth! Exemple for just loading a MIDI with MidiFilePlayer:
More...
class  MidiPlayerTK.MidiLoad
 Base class for loading a MIDI file and reading MIDI event in a MIDI sequencer. It's not possible to instanciate directly this class.
Rather, use MidiFilePlayer to load a MIDI This class is used by MidiFilePlayer, MidiListPlayer, MidiFileWrite2, MidiFileLoader (see members MPTK_MidiLoaded of these classes).
. More...

Functions

bool MidiPlayerTK.MidiFileLoader.MPTK_SearchMidiToPlay (string name)
 Finds a MIDI in the Unity Resources folder MidiDB whose name contains the specified text (case-sensitive).
Beware: the name of this method is not appropriate because MidiFileLoader cannot play MIDI. Use MidiFilePlayer instead.
It is kept only for compatibility and may be removed in a future major version.
.
void MidiPlayerTK.MidiLoad.MPTK_CalculateTiming ()
 Calculate all timing in the MIDI file:
MPTKEvent MidiPlayerTK.MidiLoad.MPTK_FindLastNote ()
 Find the last MIDI event note-on.
static int MidiPlayerTK.MidiLoad.MPTK_SearchEventFromTick (List< MPTKEvent > midiEvents, long tickSearched)
 Search for a MIDI event from a tick position. v2.9.0
.
bool MidiPlayerTK.MidiLoad.MPTK_LoadFile (string filename, bool strict=false)
 Load MIDI file from a local file (Moved to PRO since version 2.89.5).

Detailed Description

MIDI loading/parsing services and scan helpers.

Function Documentation

◆ MPTK_SearchMidiToPlay()

bool MidiPlayerTK.MidiFileLoader.MPTK_SearchMidiToPlay ( string name)

Finds a MIDI in the Unity Resources folder MidiDB whose name contains the specified text (case-sensitive).
Beware: the name of this method is not appropriate because MidiFileLoader cannot play MIDI. Use MidiFilePlayer instead.
It is kept only for compatibility and may be removed in a future major version.
.

Version
Maestro Pro
Note
  • Add MIDI files to your project with the Unity menu MPTK, or add them directly to the resource folder and open MIDI File Setup to integrate them automatically in MPTK.
// Find the first MIDI file name in MidiDB which contains "Adagio"
midiLoadPlayer.MPTK_SearchMidiToPlay("Adagio");
Parameters
nameCase-sensitive part of a MIDI file name.
Returns
true if found; otherwise, false

◆ MPTK_CalculateTiming()

void MidiPlayerTK.MidiLoad.MPTK_CalculateTiming ( )

Calculate all timing in the MIDI file:

  • Tempo Map
  • Signature Map
  • For each events: Realtime, Measure, Beat
private void OnGUI_ModifyMidiAndPlay()
{
HelperDemo.GUI_Horizontal(HelperDemo.Zone.BEGIN, myStyle.BacgDemosLight);
HelperDemo.GUI_Indent(widthIndent);
HelperDemo.GUI_Vertical(HelperDemo.Zone.BEGIN, myStyle.BacgDemosLight);
GUILayout.Label("It's possible to change the MIDI events before playing. This demo loads the selected MIDI, adds some notes and plays the MIDI without reloading it. Result not guaranteed!", myStyle.TitleLabel3);
countNoteToInsert = (int)HelperDemo.GUI_Slider("Count notes to insert:", (float)countNoteToInsert, 1, 100,
alignCaptionRight: false, enableButton: true, widthCaption: 170, widthSlider: 250, widthLabelValue: 50);
tickPositionToInsert = (long)HelperDemo.GUI_Slider("Tick position to insert:", (long)tickPositionToInsert, 0, (long)midiFilePlayer.MPTK_TickLast,
alignCaptionRight: false, enableButton: true, widthCaption: 170, widthSlider: 250, widthLabelValue: 50);
long quarterPosition = tickPositionToInsert / midiFilePlayer.MPTK_DeltaTicksPerQuarterNote;
long newQuarter = (long)HelperDemo.GUI_Slider("Tick position by quarter:", (long)quarterPosition, 0, (long)midiFilePlayer.MPTK_TickLast / midiFilePlayer.MPTK_DeltaTicksPerQuarterNote,
alignCaptionRight: false, enableButton: true, widthCaption: 170, widthSlider: 250, widthLabelValue: 50);
if (newQuarter != quarterPosition)
{
quarterPosition = newQuarter;
tickPositionToInsert = quarterPosition * midiFilePlayer.MPTK_DeltaTicksPerQuarterNote;
}
channelToInsert = (int)HelperDemo.GUI_Slider("Channel to insert:", channelToInsert, 0, 15,
alignCaptionRight: false, enableButton: true, widthCaption: 170, widthSlider: 250, widthLabelValue: 50);
HelperDemo.GUI_Horizontal(HelperDemo.Zone.BEGIN, null, GUILayout.Width(500));
clearNote = GUILayout.Toggle(clearNote, "Clear MIDI list before inserting");
GUILayout.Space(10);
randomNote = GUILayout.Toggle(randomNote, "Random Note");
GUILayout.Space(10);
randomDuration = GUILayout.Toggle(randomDuration, "Random Duration");
GUILayout.Space(10);
calculateTiming = GUILayout.Toggle(calculateTiming, "Timing Recalculation");
HelperDemo.GUI_Horizontal(HelperDemo.Zone.END);
HelperDemo.GUI_Horizontal(HelperDemo.Zone.BEGIN);
if (GUILayout.Button("Insert And Play", GUILayout.Width(120)))
{
#if MPTK_PRO
// Better to stop playing.
midiFilePlayer.MPTK_Stop();
// There is no need of note-off events.
midiFilePlayer.MPTK_KeepNoteOff = false;
// MPTK_MidiName must contains the name of the MIDI to load.
if (midiFilePlayer.MPTK_Load() != null)
{
Debug.Log($"Duration: {midiFilePlayer.MPTK_Duration.TotalSeconds} seconds");
Debug.Log($"Count MIDI Events: {midiFilePlayer.MPTK_MidiEvents.Count}");
if (clearNote)
midiFilePlayer.MPTK_MidiEvents.Clear();
// Insert weird notes in this beautiful MIDI!
// ------------------------------------------
long tickToInsert = tickPositionToInsert;
for (int insertNote = 0; insertNote < countNoteToInsert; insertNote++)
{
int note;
if (randomNote)
note = UnityEngine.Random.Range(50, 73); // Random notes between 48 (C4) and 72 (C6)
else
note = 60 + insertNote % 12; // Hust a remap of notes!
int eightCount; // How many eight duration to generate
if (randomDuration)
eightCount = UnityEngine.Random.Range(0, 9); // max 8, so a whole note
else
eightCount = 2; // a quarter
int tickDuration = eightCount * midiFilePlayer.MPTK_DeltaTicksPerQuarterNote;
// Add a note
midiFilePlayer.MPTK_MidiEvents.Insert(0,
new MPTKEvent()
{
Channel = channelToInsert,
Command = MPTKCommand.NoteOn,
Value = note,
Length = tickDuration,
Duration = (long)(tickDuration * midiFilePlayer.MPTK_Pulse), // Transform ticks to millisecond
Tick = tickToInsert,
});
// Add a text
midiFilePlayer.MPTK_MidiEvents.Insert(0,
new MPTKEvent()
{
Command = MPTKCommand.MetaEvent,
Meta = MPTKMeta.TextEvent,
Info = $"Add a weird note {HelperNoteLabel.LabelFromMidi(note)}",
Tick = tickToInsert,
});
// Move to the next insert, add length of note added.
tickToInsert += tickDuration;
}
// New events has been inserted, MIDI events list must be sorted by tick value.
// ---------------------------------------------------------------------------
midiFilePlayer.MPTK_SortEvents();
if (calculateTiming)
{
// Timing recalculation is not useful for all use cases.
// Avoid if possible because this takes time and realloc data.
midiFilePlayer.midiLoaded.MPTK_CalculateTiming();
}
// Then play the event list modified (not guaranteed to be the hit of the year!)
// ----------------------------------------------------------------------------------
midiFilePlayer.MPTK_Play(alreadyLoaded: true);
}
#else
Debug.LogWarning("MIDI preload and alter MIDI events are available only with the PRO version");
#endif
}
HelperDemo.LinkTo("https://mptkapi.paxstellar.com/d7/deb/class_midi_player_t_k_1_1_midi_file_player.html#a7c1b1b1efab0022731f69e5161952c59");
if (GUILayout.Button("View MIDI events", GUILayout.Width(120)))
{
midiFilePlayer.MPTK_MidiEvents.ForEach(midi => Debug.Log(midi));
}
HelperDemo.GUI_Horizontal(HelperDemo.Zone.END);
HelperDemo.GUI_Vertical(HelperDemo.Zone.END);
HelperDemo.GUI_Horizontal(HelperDemo.Zone.END);
}

◆ MPTK_FindLastNote()

MPTKEvent MidiPlayerTK.MidiLoad.MPTK_FindLastNote ( )

Find the last MIDI event note-on.

Version
2.12.1
Returns
return the last MPTKEvent note-on found or null if not found

◆ MPTK_SearchEventFromTick()

int MidiPlayerTK.MidiLoad.MPTK_SearchEventFromTick ( List< MPTKEvent > midiEvents,
long tickSearched )
static

Search for a MIDI event from a tick position. v2.9.0
.

Parameters
tickSearchedtick position
Returns
MPTKEvent or null

◆ MPTK_LoadFile()

bool MidiPlayerTK.MidiLoad.MPTK_LoadFile ( string filename,
bool strict = false )

Load MIDI file from a local file (Moved to PRO since version 2.89.5).

Parameters
filenameMidi path and filename to load (OS dependant)
strictif true the MIDI must strictely respect the midi norm
Returns