MidiPlayerTK.MidiLoad

Base class for loading a Midi file.
Internal used by MidiFilePlayer, MidiListPlayer, MidiFileWrite2, MidiFileLoader.
It is better to use the prefab and class MidiFileLoader if you want to only load a Midi file. More...

Public Member Functions

double MPTK_ConvertTickToTime (long tick)
 Convert the tick duration to a real time duration in millisecond regarding the current tempo. More...
 
long MPTK_ConvertTimeToTick (double time)
 Convert a real time duration in millisecond to a number of tick regarding the current tempo. More...
 
bool MPTK_Load (byte[] datamidi, bool strict=false)
 Load Midi from an array of bytes More...
 
bool MPTK_Load (int index, bool strict=false)
 Load Midi from midi MPTK referential (Unity resource).
The index of the Midi file can be found in the windo "Midi File Setup". Display with menu MPTK / Midi File Setup More...
 
bool MPTK_Load (MidiFileWriter2 mfw2)
 Load Midi from a MidiFileWriter2 object More...
 
bool MPTK_Load (string midiname, bool strict=false)
 Load Midi from a Midi file from Unity resources. The Midi file must be present in Unity MidiDB ressource folder. More...
 
bool MPTK_LoadFile (string filename, bool strict=false)
 Load Midi from a local file More...
 
List< MPTKEventMPTK_ReadMidiEvents (long fromTicks=0, long toTicks=long.MaxValue)
 Read the list of midi events available in the Midi from a ticks position to an end position. More...
 
MPTKEvent MPTK_SearchEventFromTime (double time)
 Search for a Midi event from a time position with millisecond precision.
So time=12.3 and time=12.9 will find the same event.
More...
 
long MPTK_SearchTickFromTime (double time)
 Search a tick position in the current midi from a position in millisecond.
Warning: this method loop on the whole midi to find the position.
Could be CPU costly but this method take care of the tempo change in the Midi.
Use MPTK_ConvertTimeToTick if there is no tempo change in the midi. More...
 

Public Attributes

int MPTK_DeltaTicksPerQuarterNote
 Read 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 ticks. Also named Division. More...
 
TimeSpan MPTK_Duration
 Duration (TimeSpan) of the midi. More...
 
float MPTK_DurationMS
 Duration (milliseconds) of the midi. More...
 
double MPTK_InitialTempo
 Initial tempo found in the Midi. 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...
 
MPTKEvent MPTK_LastEventPlayed
 Last Midi event read from the list More...
 
float MPTK_LoadTime
 Time to load the current midi file (millisecond) More...
 
int MPTK_MicrosecondsPerQuarterNote
 Read 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...
 
double MPTK_PositionFirstNote
 Position in millisecond for the first note found More...
 
long MPTK_TickCurrent
 
long MPTK_TickFirstNote
 Tick position for the first note found 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 More...
 

Properties

double MPTK_CurrentTempo [get]
 Current tempo played by the internal Midi sequencer. More...
 

Detailed Description

Base class for loading a Midi file.
Internal used by MidiFilePlayer, MidiListPlayer, MidiFileWrite2, MidiFileLoader.
It is better to use the prefab and class MidiFileLoader if you want to only load a Midi file.

Class for loading a Midi file. No sequencer, no synthetizer, so music playing capabilities. Usefull to load all the Midi events from a Midi and process, transform, write them to what you want.

Member Function Documentation

◆ MPTK_ConvertTickToTime()

double MidiPlayerTK.MidiLoad.MPTK_ConvertTickToTime ( long  tick)

Convert the tick duration to a real time duration in millisecond regarding the current tempo.

Parameters
tickduration in ticks
Returns
duration in milliseconds

◆ MPTK_ConvertTimeToTick()

long MidiPlayerTK.MidiLoad.MPTK_ConvertTimeToTick ( double  time)

Convert a real time duration in millisecond to a number of tick regarding the current tempo.

Parameters
timeduration in milliseconds
Returns
duration in ticks

◆ MPTK_Load() [1/4]

bool MidiPlayerTK.MidiLoad.MPTK_Load ( byte[]  datamidi,
bool  strict = false 
)

Load Midi from an array of bytes

Parameters
datamidibyte arry midi
strictIf true will error on non-paired note events, default:false
Returns
true if loaded

◆ MPTK_Load() [2/4]

bool MidiPlayerTK.MidiLoad.MPTK_Load ( int  index,
bool  strict = false 
)

Load Midi from midi MPTK referential (Unity resource).
The index of the Midi file can be found in the windo "Midi File Setup". Display with menu MPTK / Midi File Setup

public MidiLoad MidiLoaded;
// .....
MidiLoaded = new MidiLoad();
MidiLoaded.MPTK_Load(14) // index for "Beattles - Michelle"
Debug.Log("Duration:" + MidiLoaded.MPTK_Duration);
Parameters
index
strictIf true will error on non-paired note events, default:false
Returns
true if loaded

◆ MPTK_Load() [3/4]

bool MidiPlayerTK.MidiLoad.MPTK_Load ( MidiFileWriter2  mfw2)

Load Midi from a MidiFileWriter2 object

Parameters
mfw2MidiFileWriter2 object
Returns
true if loaded

◆ MPTK_Load() [4/4]

bool MidiPlayerTK.MidiLoad.MPTK_Load ( string  midiname,
bool  strict = false 
)

Load Midi from a Midi file from Unity resources. The Midi file must be present in Unity MidiDB ressource folder.

public MidiLoad MidiLoaded;
// .....
MidiLoaded = new MidiLoad();
MidiLoaded.MPTK_Load("Beattles - Michelle")
Debug.Log("Duration:" + MidiLoaded.MPTK_Duration);
Parameters
midinameMidi file name without path and extension
strictif true, check strict compliance with the Midi norm
Returns
true if loaded

◆ MPTK_LoadFile()

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

Load Midi from a local file

Parameters
filenameMidi path and filename to load
strictif true struct respect of the midi norm is checked
Returns

◆ MPTK_ReadMidiEvents()

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

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

Parameters
fromTicksticks start, default 0
toTicksticks end, default end of Midi file
Returns

◆ MPTK_SearchEventFromTime()

MPTKEvent MidiPlayerTK.MidiLoad.MPTK_SearchEventFromTime ( double  time)

Search for a Midi event from a time position with millisecond precision.
So time=12.3 and time=12.9 will find the same event.

Parameters
timeposition in milliseconds
Returns
MPTKEvent or null

◆ MPTK_SearchTickFromTime()

long MidiPlayerTK.MidiLoad.MPTK_SearchTickFromTime ( double  time)

Search a tick position in the current midi from a position in millisecond.
Warning: this method loop on the whole midi to find the position.
Could be CPU costly but this method take care of the tempo change in the Midi.
Use MPTK_ConvertTimeToTick if there is no tempo change in the midi.

Parameters
timeposition in milliseconds
Returns
position in ticks

Member Data Documentation

◆ MPTK_DeltaTicksPerQuarterNote

int MidiPlayerTK.MidiLoad.MPTK_DeltaTicksPerQuarterNote

Read 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 ticks. Also named Division.

◆ MPTK_Duration

TimeSpan MidiPlayerTK.MidiLoad.MPTK_Duration

Duration (TimeSpan) of the midi.

◆ MPTK_DurationMS

float MidiPlayerTK.MidiLoad.MPTK_DurationMS

Duration (milliseconds) of the midi.

◆ MPTK_InitialTempo

double MidiPlayerTK.MidiLoad.MPTK_InitialTempo

Initial tempo found in the Midi.

◆ MPTK_KeySigMajorMinor

int MidiPlayerTK.MidiLoad.MPTK_KeySigMajorMinor

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

◆ MPTK_KeySigSharpsFlats

int MidiPlayerTK.MidiLoad.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_LastEventPlayed

MPTKEvent MidiPlayerTK.MidiLoad.MPTK_LastEventPlayed

Last Midi event read from the list

◆ MPTK_LoadTime

float MidiPlayerTK.MidiLoad.MPTK_LoadTime

Time to load the current midi file (millisecond)

◆ MPTK_MicrosecondsPerQuarterNote

int MidiPlayerTK.MidiLoad.MPTK_MicrosecondsPerQuarterNote

Read 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.MidiLoad.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.MidiLoad.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.MidiLoad.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_PositionFirstNote

double MidiPlayerTK.MidiLoad.MPTK_PositionFirstNote

Position in millisecond for the first note found

◆ MPTK_TickCurrent

long MidiPlayerTK.MidiLoad.MPTK_TickCurrent

Set or get the current tick position in the Midi when playing.
Midi tick is an easy way to identify a position in a song independently of the time which could vary with tempo change.
The count of ticks for a quarter is constant all along a Midi: see properties MPTK_DeltaTicksPerQuarterNote.
Example: with a time signature of 4/4 the ticks length of a bar is 4 * MPTK_DeltaTicksPerQuarterNote.
Warning: if you want to set the start position, set MPTK_TickCurrent inside the processing of the event OnEventStartPlayMidi
because MPTK_Play() reset the start position to 0.
Other possibility to change the position in the Midi is to use the property MPTK_Position: set or get the position in milliseconds
but tempo change event will impact also this time. More info here https://paxstellar.fr/2020/09/11/midi-timing/

◆ MPTK_TickFirstNote

long MidiPlayerTK.MidiLoad.MPTK_TickFirstNote

Tick position for the first note found

◆ MPTK_TickLast

long MidiPlayerTK.MidiLoad.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.MidiLoad.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.MidiLoad.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.MidiLoad.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.MidiLoad.MPTK_TrackCount

Count of track read in the Midi file

Property Documentation

◆ MPTK_CurrentTempo

double MidiPlayerTK.MidiLoad.MPTK_CurrentTempo
get

Current tempo played by the internal Midi sequencer.