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

@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...

Public Member Functions

bool MPTK_Load (byte[] midiBytesToLoad=null)
 Loads the midi file defined with MPTK_MidiName or MPTK_MidiIndex or from a array of bytes.
Look at MPTK_MidiLoaded for detailed information about the MIDI loaded. MPTK_MidiEvents will contains all MIDI events loaded.
List< MPTKEventMPTK_ReadMidiEvents (long fromTicks=0, long toTicks=long.MaxValue)
 Read the list of midi events available in the Midi file from a ticks position to an end position into a List of MPTKEvent.
void MPTK_Next ()
 Read next Midi from the list of midi defined in MPTK (see Unity menu Midi).
void MPTK_Previous ()
 Read previous Midi from the list of midi defined in MPTK (see Unity menu Midi).
MPTKEvent.EnumLength MPTK_NoteLength (MPTKEvent note)
 Return note length as https://en.wikipedia.org/wiki/Note_value.
bool MPTK_Load (string filePath)
 Loads a MIDI file from a local desktop file. See MPTK_MidiLoaded for detailed information about the loaded MIDI.
Example of path for Mac "/Users/xxx/Desktop/WellTempered.mid"
Example of path for Windows "C:\Users\xxx\Desktop\BIM\Sound\Midi\DreamOn.mid"
.
bool 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.
.

Public Attributes

bool MPTK_LogLoadEvents
 If true display in console all midi events loaded. v2.9.0
Sets to true could increase greatly the load time. To be used only for debug.
bool MPTK_KeepNoteOff
 A MIDI file is a kind of keyboard simulation: in general, a key pressed generates a 'note-on' and a key release generates a 'note-off'.
But there is an other possibility in a MIDI file: create a 'note-on' with a velocity=0 which must act as a 'midi-off'
By default, MPTK create only one MPTK event with the command NoteOn and a duration.
But in some cases, you could want to keep the note-off events if they exist in the MIDI file.
Sets to false if there is no need (could greatly increases the MIDI list events).
Sets to true to keep 'note-off' events.
bool MPTK_KeepEndTrack
 When set to true, meta MIDI event End Track are keep. Default is false.
If set to true, the duration of the MIDI taken into account the End Track Event.
double MPTK_InitialTempo
 Initial tempo read in the Midi.
TimeSpan MPTK_Duration
 Duration of the midi.
long MPTK_TickLast
 Last tick position in Midi: Time of the last midi event in sequence expressed in number of "ticks".
MPTK_TickLast / MPTK_DeltaTicksPerQuarterNote equal the duration time of a quarter-note regardless the defined tempo.
int MPTK_NumberBeatsMeasure
 From TimeSignature event: The numerator counts the number of beats in a measure.
For example a numerator of 4 means that each bar contains four beats.
This is important to know because usually the first beat of each bar has extra emphasis.
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_NumberQuarterBeat
 From TimeSignature event: number of quarter notes in a beat.
Equal 2 Power TimeSigDenominator.
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_TimeSigNumerator
 From TimeSignature event: The numerator counts the number of beats in a measure.
For example a numerator of 4 means that each bar contains four beats.
This is important to know because usually the first beat of each bar has extra emphasis.
In MIDI the denominator value is stored in a special format. i.e. the real denominator = 2 ^ MPTK_TimeSigNumerator
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_TimeSigDenominator
 From TimeSignature event: The denominator specifies the number of quarter notes in a beat.
2 represents a quarter-note,
3 represents an eighth-note, etc.
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_KeySigSharpsFlats
 From KeySignature event: Values between -7 and 7 and specifies the key signature in terms of number of flast (if negative) or sharps (if positive) https://www.recordingblogs.com/wiki/midi-key-signature-meta-message.
int MPTK_KeySigMajorMinor
 From KeySignature event: Specifies the scale of the MIDI file.
int MPTK_TicksInMetronomeClick
 From TimeSignature event: The standard MIDI clock ticks every 24 times every quarter note (crotchet)
So a MPTK_TicksInMetronomeClick value of 24 would mean that the metronome clicks once every quarter note.
A MPTK_TicksInMetronomeClick value of 6 would mean that the metronome clicks once every 1/8th of a note (quaver).
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_No32ndNotesInQuarterNote
 From TimeSignature event: This value specifies the number of 1/32nds of a note happen every MIDI quarter note.
It is usually 8 which means that a quarter note happens every quarter note.
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_MicrosecondsPerQuarterNote
 From the SetTempo event: The tempo is given in micro seconds per quarter beat.
To convert this to BPM we needs to use the following equation:BPM = 60,000,000/[tt tt tt]
Warning: this value can change during the playing when a change tempo event is find.
https://paxstellar.fr/2020/09/11/midi-timing/.
int MPTK_DeltaTicksPerQuarterNote
 Delta Ticks Per Beat Note (or DTPQN) represent the duration time in "ticks" which make up a quarter-note.
For example, with 96 a duration of an eighth-note in the file would be 48.
From a MIDI file, this value is found in the MIDI Header and remains constant for all the MIDI file.
More info here https://paxstellar.fr/2020/09/11/midi-timing/
.
int MPTK_TrackCount
 Count of track read in the Midi file.
Not to be confused with channel. A track can contains midi events for different channel.

Properties

string MPTK_MidiName [get, set]
 Midi name to load. Use the exact name defined in Unity resources folder MidiDB without any path or extension. Tips: Add Midi files to your project with the Unity menu MPTK or add it directly in the resource folder and open Midi File Setup to automatically integrate Midi in MPTK.
int MPTK_MidiIndex [get, set]
 Index Midi. Find the Index of Midi file from the popup in MidiFileLoader inspector.
Tips: Add Midi files to your project with the Unity menu MPTK or add it directly in the resource folder and open Midi File Setup to automatically integrate Midi in MPTK.
return -1 if not found.
float MPTK_DurationMS [get]
 Duration (milliseconds) of the midi.
List< MPTKEventMPTK_MidiEvents [get]
 Gets all the MPTK MIDI events available in the midi file. New v2.9.0
.
MidiLoad MPTK_MidiLoaded [get]
 Gets detailed information about the midi loaded.

Detailed Description

@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:

private void TheMostSimpleDemoForMidiLoader()
{
// A MidiFileLoader prefab must be added to the hierarchy with the editor (see menu MPTK)
MidiFilePlayer mfp = FindFirstObjectByType<MidiFilePlayer>();
if (mfp == null)
{
Debug.LogWarning("Can't find a MidiFilePlayer Prefab in the current Scene Hierarchy. Add it with the Maestro menu.");
return;
}
// Index of the midi in the MidiDB (find it with 'Midi File Setup' from the menu MPTK)
mfp.MPTK_MidiIndex = MidiIndex;
// Open and load the Midi
mfp.MPTK_Load();
// Loop on each MIDI events
foreach (MPTKEvent mptkEvent in mfp.MPTK_MidiEvents)
{
// Log if event is a note on
if (mptkEvent.Command == MPTKCommand.NoteOn)
Debug.Log($"Note On at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Note:{mptkEvent.Value} Duration:{mptkEvent.Duration} millisecond Velocity:{mptkEvent.Velocity}");
else if (mptkEvent.Command == MPTKCommand.PatchChange)
Debug.Log($"Preset Change at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Preset:{mptkEvent.Value}");
else if (mptkEvent.Command == MPTKCommand.ControlChange)
{
if (mptkEvent.Controller == MPTKController.BankSelectMsb)
Debug.Log($"Bank Change at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Bank:{mptkEvent.Value}");
}
// Uncomment to display all MIDI events
//Debug.Log(mptkEvent.ToString());
}
}

Member Function Documentation

◆ MPTK_Load() [1/2]

bool MidiPlayerTK.MidiFileLoader.MPTK_Load ( byte[] midiBytesToLoad = null)

Loads the midi file defined with MPTK_MidiName or MPTK_MidiIndex or from a array of bytes.
Look at MPTK_MidiLoaded for detailed information about the MIDI loaded. MPTK_MidiEvents will contains all MIDI events loaded.

Parameters
midiBytesToLoadbyte arry from a midi stream
Returns
true if loading succeed/returns>

◆ MPTK_ReadMidiEvents()

List< MPTKEvent > MidiPlayerTK.MidiFileLoader.MPTK_ReadMidiEvents ( long fromTicks = 0,
long toTicks = long::MaxValue )

Read the list of midi events available in the Midi file from a ticks position to an end position into a List of MPTKEvent.

private void TheMostSimpleDemoForMidiLoader()
{
// A MidiFileLoader prefab must be added to the hierarchy with the editor (see menu MPTK)
MidiFilePlayer mfp = FindFirstObjectByType<MidiFilePlayer>();
if (mfp == null)
{
Debug.LogWarning("Can't find a MidiFilePlayer Prefab in the current Scene Hierarchy. Add it with the Maestro menu.");
return;
}
// Index of the midi in the MidiDB (find it with 'Midi File Setup' from the menu MPTK)
mfp.MPTK_MidiIndex = MidiIndex;
// Open and load the Midi
mfp.MPTK_Load();
// Loop on each MIDI events
foreach (MPTKEvent mptkEvent in mfp.MPTK_MidiEvents)
{
// Log if event is a note on
if (mptkEvent.Command == MPTKCommand.NoteOn)
Debug.Log($"Note On at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Note:{mptkEvent.Value} Duration:{mptkEvent.Duration} millisecond Velocity:{mptkEvent.Velocity}");
else if (mptkEvent.Command == MPTKCommand.PatchChange)
Debug.Log($"Preset Change at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Preset:{mptkEvent.Value}");
else if (mptkEvent.Command == MPTKCommand.ControlChange)
{
if (mptkEvent.Controller == MPTKController.BankSelectMsb)
Debug.Log($"Bank Change at {mptkEvent.RealTime} millisecond Channel:{mptkEvent.Channel} Bank:{mptkEvent.Value}");
}
// Uncomment to display all MIDI events
//Debug.Log(mptkEvent.ToString());
}
}

See full example in TestMidiFileLoad.cs

Parameters
fromTicksticks start, default from start
toTicksticks end, default to the end
Returns

◆ MPTK_NoteLength()

MPTKEvent.EnumLength MidiPlayerTK.MidiFileLoader.MPTK_NoteLength ( MPTKEvent note)

Return note length as https://en.wikipedia.org/wiki/Note_value.

Parameters
note
Returns
MPTKEvent.EnumLength

◆ MPTK_Load() [2/2]

bool MidiPlayerTK.MidiFileLoader.MPTK_Load ( string filePath)

Loads a MIDI file from a local desktop file. See MPTK_MidiLoaded for detailed information about the loaded MIDI.
Example of path for Mac "/Users/xxx/Desktop/WellTempered.mid"
Example of path for Windows "C:\Users\xxx\Desktop\BIM\Sound\Midi\DreamOn.mid"
.

Version
Maestro Pro
Parameters
filePathExample for Windows: filePath= "C:\Users\xxx\Desktop\BIM\Sound\Midi\DreamOn.mid"
Returns
true if loading succeeds

Member Data Documentation

◆ MPTK_KeySigMajorMinor

int MidiPlayerTK.MidiFileLoader.MPTK_KeySigMajorMinor

From KeySignature event: Specifies the scale of the MIDI file.

Property Documentation

◆ MPTK_MidiIndex

int MidiPlayerTK.MidiFileLoader.MPTK_MidiIndex
getset

Index Midi. Find the Index of Midi file from the popup in MidiFileLoader inspector.
Tips: Add Midi files to your project with the Unity menu MPTK or add it directly in the resource folder and open Midi File Setup to automatically integrate Midi in MPTK.
return -1 if not found.

midiFilePlayer.MPTK_MidiIndex = 1;
Parameters
index