MidiPlayerTK.MidiLoad

Base class for loading a Midi file. It is not recommended to instanciate directly this class.
This class is used by MidiFilePlayer, MidiListPlayer, MidiFileWrite2, MidiFileLoader (see members MPTK_MidiLoaded of these classes).
It is better to use the prefab and class MidiFileLoader if you only want to load a Midi file. More...

Public Member Functions

double MPTK_ConvertTickToTime (long tick)
 
long MPTK_ConvertTimeToTick (double time)
 
bool MPTK_Load (byte[] datamidi, bool strict=false)
 
bool MPTK_Load (int index, bool strict=false)
 
bool MPTK_Load (MidiFileWriter2 mfw2)
 
bool MPTK_Load (string midiname, bool strict=false)
 
bool MPTK_LoadFile (string filename, bool strict=false)
 
List< MPTKEventMPTK_ReadMidiEvents (long fromTicks=0, long toTicks=long.MaxValue)
 
MPTKEvent MPTK_SearchEventFromTime (double time)
 
long MPTK_SearchTickFromTime (double time)
 

Public Attributes

int MPTK_DeltaTicksPerQuarterNote
 
TimeSpan MPTK_Duration
 
float MPTK_DurationMS
 
bool MPTK_EnableChangeTempo
 
double MPTK_InitialTempo
 
bool MPTK_KeepEndTrack
 
int MPTK_KeySigMajorMinor
 
int MPTK_KeySigSharpsFlats
 
MPTKEvent MPTK_LastEventPlayed
 
float MPTK_LoadTime
 
int MPTK_MicrosecondsPerQuarterNote
 
int MPTK_No32ndNotesInQuarterNote
 
int MPTK_NumberBeatsMeasure
 
int MPTK_NumberQuarterBeat
 
double MPTK_PositionFirstNote
 
double MPTK_PositionLastNote
 
long MPTK_TickCurrent
 
long MPTK_TickFirstNote
 
long MPTK_TickLast
 
long MPTK_TickLastNote
 
int MPTK_TicksInMetronomeClick
 
int MPTK_TimeSigDenominator
 
int MPTK_TimeSigNumerator
 
int MPTK_TrackCount
 

Properties

double MPTK_CurrentTempo [get, set]
 

Detailed Description

Base class for loading a Midi file. It is not recommended to instanciate directly this class.
This class is used by MidiFilePlayer, MidiListPlayer, MidiFileWrite2, MidiFileLoader (see members MPTK_MidiLoaded of these classes).
It is better to use the prefab and class MidiFileLoader if you only want to load a Midi file.

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);
Base class for loading a Midi file. It is not recommended to instanciate directly this class....
Definition: MidiLoad.cs:19
TimeSpan MPTK_Duration
Definition: MidiLoad.cs:72
bool MPTK_Load(int index, bool strict=false)
Definition: MidiLoad.cs:303
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 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

◆ 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 expressed in millisecond.
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

Real duration expressed in TimeSpan of the full midi from the first event (tick=0) to the last event.
If MPTK_KeepEndTrack is false, the MIDI events End Track are not considered to calculate this time.
The tempo changes are taken into account if MPTK_EnableChangeTempo is set to true before loading the MIDI.

◆ MPTK_DurationMS

float MidiPlayerTK.MidiLoad.MPTK_DurationMS

Real duration expressed in milliseconds of the full midi from the first event (tick=0) to the last event.
If MPTK_KeepEndTrack is false, the MIDI events End Track are not considered to calculate this time.
The tempo changes are taken into account if MPTK_EnableChangeTempo is set to true before loading the MIDI.

◆ MPTK_EnableChangeTempo

bool MidiPlayerTK.MidiLoad.MPTK_EnableChangeTempo

Should accept change tempo from MIDI Events ?

◆ MPTK_InitialTempo

double MidiPlayerTK.MidiLoad.MPTK_InitialTempo

Initial tempo found in the MIDI.

◆ MPTK_KeepEndTrack

bool MidiPlayerTK.MidiLoad.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.

◆ 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

Current MIDI event read when the MIDI sequencer is playing the MIDI. See MPTK_TickCurrent.

◆ MPTK_LoadTime

float MidiPlayerTK.MidiLoad.MPTK_LoadTime

Time takne expressed in millisecond for loading the MIDI file.

◆ 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

Real time position in millisecond for the first note-on found.
Most MIDI don't start playing a note immediately. There is often a delay.
Use this attribute to known the real time wich it will start.
See also MPTK_TickFirstNote

◆ MPTK_PositionLastNote

double MidiPlayerTK.MidiLoad.MPTK_PositionLastNote

Real time position in millisecond for the last note-on found in the MIDI.
There is often other MIDI events after the last note-on: for example event track-end.
Use this attribute to known the real time when all sound will be stop.
See also the MPTK_DurationMS which provides the full time of all MIDI events including track-end, control at the beginning and at the end, ....
See also MPTK_TickLastNote

◆ MPTK_TickCurrent

long MidiPlayerTK.MidiLoad.MPTK_TickCurrent

Set or get the current tick position when the MIDI sequencer is playing the MIDI.
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 MidiFilePlayer::MPTK_Play() reset the start position to 0.
Other possibility to change the position in the Midi is to use the property MidiFilePlayer::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-on found.
Most MIDI don't start playing a note immediately. There is often a delay.
Use this attribute to known the tick position where the will start to play a sound.
See also MPTK_PositionFirstNote

◆ MPTK_TickLast

long MidiPlayerTK.MidiLoad.MPTK_TickLast

Tick position of the last MIDI event found.

◆ MPTK_TickLastNote

long MidiPlayerTK.MidiLoad.MPTK_TickLastNote

Tick position for the last note-on found.
There is often other MIDI events after the last note-on: for example event track-end.
Use this attribute to known the tick position time when all sound will be stop.
See also the MPTK_PositionLastNote which provides the last tich of the MIDI.

◆ 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 in the MIDI file

Property Documentation

◆ MPTK_CurrentTempo

double MidiPlayerTK.MidiLoad.MPTK_CurrentTempo
getset

Get or change the current tempo played by the internal MIDI sequencer.
Can be used only when the MIDI is playing.
Warning: changing the current tempo when playing has no impact on the calculated duration of the MIDI.