MidiPlayerTK.MidiFileLoader

This class, associated with the prefab MidiFileLoader is useful for loading all or a part of the Midi events from a Midi file.
No sequencer, no synthetizer, no music playing capabilities, just loading and decoding a Midi file to the more easy class MPTKEvent.
Midi can be loaded from the MidiDB list (see Unity menu MPTK / Midi Player Setup) or from a folder on the desktop (Pro). More...

Inherits MonoBehaviour, and MonoBehaviour.

Public Member Functions

bool MPTK_Load (byte[] midiBytesToLoad=null)
 Load the midi file defined with MPTK_MidiName or MPTK_MidiIndex or from a array of bytes More...
 
bool MPTK_Load (string filePath)
 Load a Midi file from a local desktop file.
Example of path for Mac "/Users/xxx/Desktop/WellTempered.mid"
Example of path for Windows "C:\Users\xxx\Desktop\BIM\Sound\Midi\DreamOn.mid"
More...
 
void MPTK_Next ()
 Read next Midi from the list of midi defined in MPTK (see Unity menu Midi) More...
 
MPTKEvent.EnumLength MPTK_NoteLength (MPTKEvent note)
 Return note length as https://en.wikipedia.org/wiki/Note_value More...
 
void MPTK_Previous ()
 Read previous Midi from the list of midi defined in MPTK (see Unity menu Midi) More...
 
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 More...
 
bool MPTK_SearchMidiToPlay (string name)
 [MPTK PRO] - Find a Midi in the Unity resources folder MidiDB which contains the name (case sensitive)
Tips: Add Midi files to your project with the Unity menu MPTK or add it directly in the ressource folder and open Midi File Setup to automatically integrate Midi in MPTK. More...
 

Public Attributes

int MPTK_DeltaTicksPerQuarterNote
 From Midi Header: Delta Ticks Per Quarter Note.
Represent the duration time in "ticks" which make up a quarter-note.
For instance, if 96, then a duration of an eighth-note in the file would be 48. More...
 
TimeSpan MPTK_Duration
 Duration of the midi. More...
 
double MPTK_InitialTempo
 Initial tempo read in the Midi. More...
 
bool MPTK_KeepNoteOff
 Should keep note off event Events ? More...
 
int MPTK_KeySigMajorMinor
 From KeySignature event: Specifies the scale of the MIDI file. More...
 
int MPTK_KeySigSharpsFlats
 From KeySignature event: Values between -7 and 7 and specifies the key signature in terms of number of flats (if negative) or sharps (if positive) https://www.recordingblogs.com/wiki/midi-key-signature-meta-message More...
 
bool MPTK_LogEvents
 Log midi events More...
 
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/ More...
 
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/ More...
 
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/ More...
 
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/ More...
 
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. More...
 
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/ More...
 
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/ More...
 
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/ More...
 
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. More...
 

Properties

float MPTK_DurationMS [get]
 Duration (milliseconds) of the midi. More...
 
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 ressource folder and open Midi File Setup to automatically integrate Midi in MPTK.
return -1 if not found More...
 
MidiLoad MPTK_MidiLoaded [get]
 Get detailed information about the midi loaded. More...
 
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 ressource folder and open Midi File Setup to automatically integrate Midi in MPTK. More...
 

Detailed Description

This class, associated with the prefab MidiFileLoader is useful for loading all or a part of the Midi events from a Midi file.
No sequencer, no synthetizer, no music playing capabilities, just loading and decoding a Midi file to the more easy class MPTKEvent.
Midi can be loaded from the MidiDB list (see Unity menu MPTK / Midi Player Setup) or from a folder on the desktop (Pro).

// Examples of script. See TestMidiFileLoad.cs for a more detailed usage.
// Need a reference to the Prefab (to be set in the hierarchy)
MidiFileLoader MidiLoader;
// Defined index (from the Midi list defined in MPTK)
MidiLoader.MPTK_MidiIndex = midiindex;
// Load Midi event from the MidiDB
MidiLoader.MPTK_Load();
// Get the list of events from start to end (in ticks)
List<MPTKEvent> events = MidiLoader.MPTK_ReadMidiEvents(StartTicks, EndTicks);
// Load Midi event from the MidiDB
MidiLoader.MPTK_Load("C:\Users\User\Desktop\BIM\Sound\Midi\DreamOn.mid");
// Get the list of events from ticks 100 to ticks 200
List<MPTKEvent> events = MidiLoader.MPTK_ReadMidiEvents(100, 200);

Member Function Documentation

◆ MPTK_Load() [1/2]

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

Load the midi file defined with MPTK_MidiName or MPTK_MidiIndex or from a array of bytes

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

◆ MPTK_Load() [2/2]

bool MidiPlayerTK.MidiFileLoader.MPTK_Load ( string  filePath)

Load a Midi file from a local desktop file.
Example of path for Mac "/Users/xxx/Desktop/WellTempered.mid"
Example of path for Windows "C:\Users\xxx\Desktop\BIM\Sound\Midi\DreamOn.mid"

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

◆ MPTK_Next()

void MidiPlayerTK.MidiFileLoader.MPTK_Next ( )

Read next Midi from the list of midi defined in MPTK (see Unity menu Midi)

◆ 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_Previous()

void MidiPlayerTK.MidiFileLoader.MPTK_Previous ( )

Read previous Midi from the list of midi defined in MPTK (see Unity menu Midi)

◆ 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()
{
MidiFileLoader loader = FindObjectOfType<MidiFileLoader>();
if (loader == null)
{
Debug.LogWarning("Can't find a MidiFileLoader in the Hierarchy. Add a prefab MidiFileLoader.");
return;
}
// Index of the midi in the Midi DB (find it with 'Midi File Setup' from the menu MPTK)
loader.MPTK_MidiIndex = 10;
// Open and load the Midi
loader.MPTK_Load();
// Read midi event to a List<>
List<MPTKEvent> mptkEvents = loader.MPTK_ReadMidiEvents();
// Loop on each Midi events
foreach (MPTKEvent mptkEvent in mptkEvents)
{
// Log if event is a note on
if (mptkEvent.Command == MPTKCommand.NoteOn)
Debug.Log($"Note on Time:{mptkEvent.RealTime} millisecond Note:{mptkEvent.Value} Duration:{mptkEvent.Duration} millisecond Velocity:{mptkEvent.Velocity}");
// Uncomment to display all Midi events
//Debug.Log(mptkEvent.ToString());
}
}
MPTKCommand
MIDI command codes. Defined the action to be done with the message: note on/off, change instrument,...
Definition: MPTKEvent.cs:15

See full example in TestMidiFileLoad.cs

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

◆ MPTK_SearchMidiToPlay()

bool MidiPlayerTK.MidiFileLoader.MPTK_SearchMidiToPlay ( string  name)

[MPTK PRO] - Find a Midi in the Unity resources folder MidiDB which contains the name (case sensitive)
Tips: Add Midi files to your project with the Unity menu MPTK or add it directly in the ressource folder and open Midi File Setup to automatically integrate Midi in MPTK.

// Find the first Midi file name in MidiDB which contains "Adagio"
midiFilePlayer.MPTK_SearchMidiToPlay("Adagio");
// And play it
midiFilePlayer.MPTK_Play();
Parameters
namecase sensitive part of a midi file name
Returns
true if found else false

Member Data Documentation

◆ MPTK_DeltaTicksPerQuarterNote

int MidiPlayerTK.MidiFileLoader.MPTK_DeltaTicksPerQuarterNote

From Midi Header: Delta Ticks Per Quarter Note.
Represent the duration time in "ticks" which make up a quarter-note.
For instance, if 96, then a duration of an eighth-note in the file would be 48.

◆ MPTK_Duration

TimeSpan MidiPlayerTK.MidiFileLoader.MPTK_Duration

Duration of the midi.

◆ MPTK_InitialTempo

double MidiPlayerTK.MidiFileLoader.MPTK_InitialTempo

Initial tempo read in the Midi.

◆ MPTK_KeepNoteOff

bool MidiPlayerTK.MidiFileLoader.MPTK_KeepNoteOff

Should keep note off event Events ?

◆ MPTK_KeySigMajorMinor

int MidiPlayerTK.MidiFileLoader.MPTK_KeySigMajorMinor

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

◆ MPTK_KeySigSharpsFlats

int MidiPlayerTK.MidiFileLoader.MPTK_KeySigSharpsFlats

From KeySignature event: Values between -7 and 7 and specifies the key signature in terms of number of flats (if negative) or sharps (if positive) https://www.recordingblogs.com/wiki/midi-key-signature-meta-message

◆ MPTK_LogEvents

bool MidiPlayerTK.MidiFileLoader.MPTK_LogEvents

Log midi events

◆ MPTK_MicrosecondsPerQuarterNote

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_No32ndNotesInQuarterNote

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_NumberBeatsMeasure

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_NumberQuarterBeat

int MidiPlayerTK.MidiFileLoader.MPTK_NumberQuarterBeat

From TimeSignature event: number of quarter notes in a beat.
Equal 2 Power TimeSigDenominator.
https://paxstellar.fr/2020/09/11/midi-timing/

◆ MPTK_TickLast

long MidiPlayerTK.MidiFileLoader.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.

◆ MPTK_TicksInMetronomeClick

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_TimeSigDenominator

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_TimeSigNumerator

int MidiPlayerTK.MidiFileLoader.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/

◆ MPTK_TrackCount

int MidiPlayerTK.MidiFileLoader.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.

Property Documentation

◆ MPTK_DurationMS

float MidiPlayerTK.MidiFileLoader.MPTK_DurationMS
get

Duration (milliseconds) of the midi.

◆ 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 ressource folder and open Midi File Setup to automatically integrate Midi in MPTK.
return -1 if not found

midiFileLoader.MPTK_MidiIndex = 1;
Parameters
index

◆ MPTK_MidiLoaded

MidiLoad MidiPlayerTK.MidiFileLoader.MPTK_MidiLoaded
get

Get detailed information about the midi loaded.

◆ MPTK_MidiName

string MidiPlayerTK.MidiFileLoader.MPTK_MidiName
getset

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 ressource folder and open Midi File Setup to automatically integrate Midi in MPTK.