Play a MIDI file from the MidiDB. This class must be used with the prefab MidiFilePlayer.
See "Midi File Setup" in the Unity menu MPTK for adding MIDI in MidiDB.
More...
Public Types | |
enum | ModeStopPlay { StopNoWaiting , StopWhenAllVoicesReleased , StopWhenAllVoicesEnded } |
Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true. See also MPTK_ModeStopVoice More... | |
Public Types inherited from MidiPlayerTK.MidiSynth | |
enum | ModeSpatializer { Channel , Track } |
Public Member Functions | |
MidiLoad | MPTK_Load () |
MidiLoad | MPTK_Load (string uri) |
void | MPTK_Next () |
MPTKEvent.EnumLength | MPTK_NoteLength (MPTKEvent note) |
void | MPTK_Pause (float timeToPauseMS=-1f) |
virtual void | MPTK_Play (bool alreadyLoaded=false) |
void | MPTK_Play (byte[] data) |
virtual void | MPTK_Play (float delayRampUp, float startDelay=0) |
void | MPTK_Play (MPTKWriter mfw2, float delayRampUp=0f, float fromPosition=0, float toPosition=0, long fromTick=0, long toTick=0, bool timePosition=true) |
void | MPTK_PlayNextOrPrevious (int offset) |
void | MPTK_Previous () |
List< MPTKEvent > | MPTK_ReadMidiEvents (long fromTicks=0, long toTicks=long.MaxValue) |
void | MPTK_RePlay () |
void | MPTK_ResetTranspose () |
Force all notes to return to their original values before transposing. Useful when looping on a MIDI with a transpose value different than 0. When returning to 0 (no transpose) the note value can be reset to their original value. More... | |
bool | MPTK_SearchMidiToPlay (string searchPartOfName) |
void | MPTK_SortEvents () |
void | MPTK_Stop (bool stopAllSound=true, float wait=0f) |
virtual void | MPTK_Stop (float delayRampDown, float stopDelay=0) |
void | MPTK_SwitchMidiWithDelay (int index, string name, float volume, float delayToStopMillisecond, float delayToStartMillisecond) |
void | MPTK_UnPause () |
Public Member Functions inherited from MidiPlayerTK.MidiSynth | |
void | MPTK_ClearAllSound (bool destroyAudioSource=false, int _idSession=-1) |
void | MPTK_DebugActiveVoice () |
void | MPTK_InitSynth (int channelCount=16, bool preserveChannelInfo=false) |
void | MPTK_PlayDirectEvent (MPTKEvent midiEvent, bool playNoteOff=true) |
void | MPTK_ResetStat () |
void | MPTK_StartSequencerMidi () |
void | MPTK_StartSynth () |
void | MPTK_StopDirectEvent (MPTKEvent midiEvent) |
void | MPTK_StopSynth () |
IEnumerator | MPTK_WaitAllNotesOff (int _idSession=-1) |
delegate void | OnAudioFrameStartHandler (double synthTime) |
Properties | |
MPTKChannels | MPTK_Channels [get] |
Description and list of MIDI Channels associated to the MIDI synth. Each MIDI synth has 16 channels that carry all the relevant MIDI information. More... | |
string | MPTK_Copyright [get] |
int | MPTK_DeltaTicksPerQuarterNote [get] |
TimeSpan | MPTK_Duration [get] |
float | MPTK_DurationMS [get] |
bool | MPTK_ExtendedText [get, set] |
bool | MPTK_IsPaused [get] |
bool | MPTK_IsPlaying [get] |
bool | MPTK_KeepEndTrack [get, set] |
bool | MPTK_KeepNoteOff [get, set] |
MPTKEvent | MPTK_LastEventPlayed [get] |
bool | MPTK_Loop [get, set] |
bool | MPTK_MidiAutoRestart [get, set] |
List< MPTKEvent > | MPTK_MidiEvents [get] |
int | MPTK_MidiIndex [get, set] |
MidiLoad | MPTK_MidiLoaded [get] |
virtual string | MPTK_MidiName [get, set] |
bool | MPTK_PlayOnStart [get, set] |
TimeSpan | MPTK_PlayTime [get] |
double | MPTK_Position [get, set] |
double | MPTK_PositionFirstNote [get] |
double | MPTK_PositionLastNote [get] |
string | MPTK_ProgramName [get] |
double | MPTK_Pulse [get] |
int | MPTK_Quantization [get, set] |
bool | MPTK_RawSeek [get, set] |
double | MPTK_RealTime [get] |
string | MPTK_SequenceTrackName [get] |
float | MPTK_Speed [get, set] |
double | MPTK_Tempo [get, set] |
string | MPTK_TextEvent [get] |
long | MPTK_TickCurrent [get, set] |
long | MPTK_TickFirstNote [get] |
long | MPTK_TickLast [get] |
long | MPTK_TickLastNote [get] |
int | MPTK_TrackCount [get] |
string | MPTK_TrackInstrumentName [get] |
Properties inherited from MidiPlayerTK.MidiSynth | |
int | MPTK_IndexSynthBuffSize [get, set] |
int | MPTK_IndexSynthRate [get, set] |
string | MPTK_InstrumentPlayed [get] |
bool | MPTK_IsSpatialSynthMaster [get] |
bool | MPTK_KeepPlayingNonLooped [get, set] |
float | MPTK_MaxDistance [get, set] |
bool | MPTK_PauseOnDistance [get, set] |
bool | MPTK_Spatialize [get, set] |
int | MPTK_SpatialSynthIndex [get] |
int | MPTK_SynthRate [get, set] |
int | MPTK_ThreadMidiPriority [get, set] |
string | MPTK_TrackName [get] |
int | MPTK_TransExcludedChannel [get, set] |
int | MPTK_Transpose [get, set] |
float | MPTK_Volume [get, set] |
Additional Inherited Members | |
Static Public Attributes inherited from MidiPlayerTK.MidiSynth | |
const float | FLUID_CHORUS_DEFAULT_DEPTH = 4.25f |
const float | FLUID_CHORUS_DEFAULT_LEVEL = 0.6f |
const int | FLUID_CHORUS_DEFAULT_N = 3 |
const float | FLUID_CHORUS_DEFAULT_SPEED = 0.2f |
const float | FLUID_CHORUS_DEFAULT_WIDTH = 10f |
const float | FLUID_REVERB_DEFAULT_DAMP = 0.3f |
const float | FLUID_REVERB_DEFAULT_LEVEL = 0.7f |
const float | FLUID_REVERB_DEFAULT_ROOMSIZE = 0.5f |
const float | FLUID_REVERB_DEFAULT_WIDTH = 0.8f |
static List< MidiFilePlayer > | SpatialSynths |
Events inherited from MidiPlayerTK.MidiSynth | |
OnAudioFrameStartHandler | OnAudioFrameStart |
Play a MIDI file from the MidiDB. This class must be used with the prefab MidiFilePlayer.
See "Midi File Setup" in the Unity menu MPTK for adding MIDI in MidiDB.
There is no need to writing a script. For a simple usage, all the job can be done in the prefab inspector.
For more information see here https://paxstellar.fr/midi-file-player-detailed-view-2/
But for specific interactions, this class can be useful. Some use cases:
Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true. See also MPTK_ModeStopVoice
MidiLoad MidiPlayerTK.MidiFilePlayer.MPTK_Load | ( | ) |
Load the midi file defined with MPTK_MidiName or MPTK_MidiIndex. It's an optional action before playing a midi file with MPTK_Play()
Use this method to get all MIDI events before start playing.
MidiLoad MidiPlayerTK.MidiFilePlayer.MPTK_Load | ( | string | uri | ) |
Load a MIDI file from a local desktop file. Example of path:
uri | uri or path to the midi file |
void MidiPlayerTK.MidiFilePlayer.MPTK_Next | ( | ) |
Play next MIDI from the list of midi defined in MPTK (see Unity menu Midi)
MPTKEvent.EnumLength MidiPlayerTK.MidiFilePlayer.MPTK_NoteLength | ( | MPTKEvent | note | ) |
Return note length as https://en.wikipedia.org/wiki/Note_value
note |
void MidiPlayerTK.MidiFilePlayer.MPTK_Pause | ( | float | timeToPauseMS = -1f | ) |
Pause the current playing
timeToPauseMS | time to pause in milliseconds. default or < 0 : indefinitely |
|
virtual |
Play the midi file defined with MPTK_MidiName or MPTK_MidiIndex. In the most part of the case, just call midiFilePlayer.MPTK_Play() in your script. But sometimes, you want to apply some changes on the MIDI file before playing it. The script example bellow describes how to load a MIDI file, apply some changes and play it. Thank to the parameter 'alreadyLoaded'. When true, the MIDI has been already loaded with MPTK_Load()
alreadyLoaded | true: the MIDI has already been loaded (see MPTK_Load() v2.9.0 |
Reimplemented in MidiPlayerTK.MidiExternalPlayer.
void MidiPlayerTK.MidiFilePlayer.MPTK_Play | ( | byte[] | data | ) |
Play the midi file from a byte array.
Look at MPTK_StatusLastMidiLoaded to get status.
|
virtual |
Play the midi file defined with MPTK_MidiName or MPTK_MidiIndex with ramp-up to the volume defined with MPTK_Volume.
The time to get a MIDI playing at full MPTK_Volume is delayRampUp + startDelay.
A delayed start can also be set.
delayRampUp | ramp-up delay in milliseconds to get the default volume |
startDelay | delayed start in milliseconds V2.89.1 |
void MidiPlayerTK.MidiFilePlayer.MPTK_Play | ( | MPTKWriter | mfw2, |
float | delayRampUp = 0f , |
||
float | fromPosition = 0 , |
||
float | toPosition = 0 , |
||
long | fromTick = 0 , |
||
long | toTick = 0 , |
||
bool | timePosition = true |
||
) |
Play the midi from a MidiFileWriter2 object
mfw2 | aMidiFileWriter2 object |
delayRampUp |
void MidiPlayerTK.MidiFilePlayer.MPTK_PlayNextOrPrevious | ( | int | offset | ) |
Play next or previous Midi from the MidiDB list.
offset | Forward or backward count in the list. 1:the next, -1:the previous |
void MidiPlayerTK.MidiFilePlayer.MPTK_Previous | ( | ) |
Play previous MIDI from the list of midi defined in MPTK (see Unity menu Midi)
List< MPTKEvent > MidiPlayerTK.MidiFilePlayer.MPTK_ReadMidiEvents | ( | long | fromTicks = 0 , |
long | toTicks = long.MaxValue |
||
) |
Read the list of midi events available in the MIDI from a ticks tick to an end tick.
fromTicks | ticks start, default 0 |
toTicks | ticks end, default end of MIDI file |
void MidiPlayerTK.MidiFilePlayer.MPTK_RePlay | ( | ) |
Restart playing of the current midi file
void MidiPlayerTK.MidiFilePlayer.MPTK_ResetTranspose | ( | ) |
Force all notes to return to their original values before transposing.
Useful when looping on a MIDI with a transpose value different than 0. When returning to 0 (no transpose) the note value can be reset to their original value.
bool MidiPlayerTK.MidiFilePlayer.MPTK_SearchMidiToPlay | ( | string | searchPartOfName | ) |
Find a Midi in the Unity resources folder MidiDB which contains searchPartOfName in it's name (case sensitive).
Set MPTK_MidiIndex and MPTK_MidiName if the MIDI has been found
searchPartOfName | Part of MIDI name to search in MIDI list (case sensitive) |
void MidiPlayerTK.MidiFilePlayer.MPTK_SortEvents | ( | ) |
MIDI list events must be sorted by ticks before playing. It's mandatory if the list is modified.
void MidiPlayerTK.MidiFilePlayer.MPTK_Stop | ( | bool | stopAllSound = true , |
float | wait = 0f |
||
) |
Stops MIDI playback and cancels all sounds. This operation is performed in the background, so MIDI may actually stop after this method returns.
stopAllSound | Set to true to stop all sounds (default), otherwise currently playing notes will continue until they finish. |
wait | If greater than 0, waits until MIDI playback is fully stopped or the specified wait time (in milliseconds) is reached. Otherwise, returns immediately. |
|
virtual |
Stop playing within a delay. After the stop delay (0 by default), the volume decrease until the playing is stopped.
The time to get a real MIDI stop is delayRampDown + stopDelay.
delayRampDown | decrease time in millisconds |
stopDelay | delayed stop in milliseconds V2.89.1 |
void MidiPlayerTK.MidiFilePlayer.MPTK_SwitchMidiWithDelay | ( | int | index, |
string | name, | ||
float | volume, | ||
float | delayToStopMillisecond, | ||
float | delayToStartMillisecond | ||
) |
Switch playing between two MIDIs with ramp-up.
This method is useful for an integration with Bolt: main MIDI parameters are defined in one call.
index | Index of the MIDI to play. Index is used only if name parameter is null or empty. |
name | Name of the MIDI to play. Can be part of the MIDI Name. If set, this parameter has the priority over index parameter. |
volume | Volume of the MIDI. -1 to not change the default volume |
delayToStopMillisecond | Delay to stop the current MIDI playing (with volume decrease) or delay before playing the MIDI if no MIDI is playing |
delayToStartMillisecond | Delay to get the MIDI at full volume (ramp-up volume) |
void MidiPlayerTK.MidiFilePlayer.MPTK_UnPause | ( | ) |
UnPause the current playing
MPTKInnerLoop MidiPlayerTK.MidiFilePlayer.MPTK_InnerLoop |
Defined looping condition inside the MIDI Sequencer [Pro]. Instance is automatically created when the MidiFilePlayer prefab is loaded/n See class #MPTKInnerLoop and MPTK_RawSeek
bool MidiPlayerTK.MidiFilePlayer.MPTK_LogLoadEvents |
If true display in console all midi events when a MIDI file is loaded.
ModeStopPlay MidiPlayerTK.MidiFilePlayer.MPTK_ModeStopVoice |
Defined the behavior of the MIDI player when playback is stopped with MPTK_Stop or restarted when the last MIDI events is reached and MPTK_MidiAutoRestart is set to true.
Code example:
bool MidiPlayerTK.MidiFilePlayer.MPTK_PauseOnFocusLoss |
Should the MIDI playing must be paused when the application lost the focus?
bool MidiPlayerTK.MidiFilePlayer.MPTK_StartPlayAtFirstNote |
If the value is true, MIDI playing will begin at the first note found in the MIDI.
Obviously, all previous events are processed, but at the same tick as the first note-on.
Often, the first note is not set at the beginning of the MIDI file (which is tick 0), alSo there is a delay before playing the first note.
This setting is useful to start playing sound immediately. Works also when looping.
LoadingStatusMidiEnum MidiPlayerTK.MidiFilePlayer.MPTK_StatusLastMidiLoaded |
Status of the last midi loaded. The status is updated in a coroutine, so the status can change at each frame.
bool MidiPlayerTK.MidiFilePlayer.MPTK_StopPlayOnLastNote |
By default, the end of a MIDI file is not the last note. It is the last MIDI event.
If this value is true, MIDI playback will stop at the last note found in the MIDI file
and the OnEventEndPlay will be triggered at the lats note.
EventEndMidiClass MidiPlayerTK.MidiFilePlayer.OnEventEndPlayMidi |
Specify the Unity event that is triggered when the end of the MIDI list of events is reached.
EventNotesMidiClass MidiPlayerTK.MidiFilePlayer.OnEventNotesMidi |
Method triggered for each MIDI event (or group of MIDI events) ready to be played by the MIDI synth. All these events are on same MIDI tick
. The callback method is able to directly interacts with Unity gameObject (same thread).
A List<MPTKEvent> is passed to the delegate.
EventStartMidiClass MidiPlayerTK.MidiFilePlayer.OnEventStartPlayMidi |
Define the Unity event to be triggered at the start of Midi playback.
At this moment, the MIDI file is loaded, the MIDI synth is initialised, but no MIDI event has been read yet.
This is the right time to defined some specific behaviors.
|
get |
Description and list of MIDI Channels associated to the MIDI synth.
Each MIDI synth has 16 channels that carry all the relevant MIDI information.
They serve to distinguish between instruments and provide independent control over each one.
By transmitting MIDI messages on their respective channels, you can alter the instrument, volume, pitch, and other parameters.
Within the Maestro Midi Player Toolkit, MIDI channels are designated numerically from 0 to 15. Notably, channel 9 is set aside specifically for drum sounds.
|
get |
Get Copyright if defined in the MIDI file with MIDI MetaEventType = Copyright
See detail here https://ccrma.stanford.edu/~craig/14q/midifile/MidiFileFormat.html
Can be used only when the MIDI is playing.
|
get |
Delta Ticks Per Beat Note. Indicate 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 info here https://paxstellar.fr/2020/09/11/midi-timing/
|
get |
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.
|
get |
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.
|
getset |
If the value is true, text read from Text META (e.g. lyrics) will be read with UTF8 encoding. The default is false.
The MIDI standard only allows ASCII characters for this META, but with this extension you will be able to read and display
characters like Korean, Chinese, Japanese and even French accented letters ;-)
|
get |
Is MIDI file playing is paused ?
|
get |
Is MIDI file is playing ?
|
getset |
When set to true, meta MIDI event End Track are keep. Default is false.
If set to true, the End Track Event are taken into account for calculate the full duration of the MIDI.
See MPTK_DurationMS.
|
getset |
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 wich 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.
Set to false if there is no need (could greatly increases the MIDI list events).
Set to true to keep 'note-off' events.
|
get |
Last MIDI event played by the MIDI sequencer
|
getset |
|
getset |
When the value is true, the current MIDI playing is restarted when it reaches the end of the MIDI file or #MPTK_MidiLoaded.MPTK_TickEnd.
|
get |
Get a list of all the MPTK MIDI events that are available in the MIDI.
|
getset |
Select a MIDI file to play by its Index from the MIDIDB.
The Index of a MIDI file is displayed in the popup from the MidiFilePlayer inspector and in the window "Midi File Setup" from the MPTK menu in the editor.
index | Index of the MIDI, start from 0 |
|
get |
Get detailed information about the MIDI playing. This readonly properties is available only when a MIDI has been loaded.
|
getset |
Select a MIDI from the MIDIDB to play by its name.
Use the exact name as seen in the MIDI setup windows (Unity menu MPTK/ without any path or extension.
Tips: Add MIDI files to your project with the Unity menu MPTK.
|
getset |
Whether the MIDI playback starts when the application starts?
|
get |
Real time in TimeSpan format from the beginning of play. It's an access to the MIDI timer used by the MIDI sequencer.
|
getset |
Set or get the current position in the MIDI in milliseconds.
When a new position is set, the corresponding tick in the MIDI event list is searched for by comparing it with the MPTKEvent.RealTime value, and the current MIDI playback is moved to this new tick position.
Here, more information about Midi Timing https://paxstellar.fr/2020/09/11/midi-timing/
|
get |
Real time tick 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
|
get |
Real time tick 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
|
get |
Get Program track name if defined in the MIDI file with MIDI MetaEventType = ProgramName
See detail here https://ccrma.stanford.edu/~craig/14q/midifile/MidiFileFormat.html
Can be used only when the MIDI is playing.
|
get |
Lenght in millisecond of a MIDI tick. The pulse length is the minimum time in millisecond between two MIDI events.
It's like a definition of graphical resolution but for MIDI: the MIDI sequencer will not be able to play two separate MIDI events in a time below this value.
|
getset |
Level of quantization :
|
getset |
This parameter controls how the playback position is adjusted within a MIDI file. By default (false), when the playback position is changed, all events except "note-on" are replayed from the start of the MIDI file up to the new position. This ensures the synthesizer is correctly updated with the appropriate context (tempo, selected instruments, controllers, etc.).
If set to true, the playback position is updated directly; consequently, the current context is preserved.
While this approach can lead to unintended (or amusing!) effects in some MIDI files, it allows for much faster position changes.
|
get |
Real time in milliseconds from the beginning of play. It's an access to the MIDI timer used by the MIDI sequencer.
|
get |
Get sequence track name if defined in the MIDI file with MIDI MetaEventType = SequenceTrackName
See detail here https://ccrma.stanford.edu/~craig/14q/midifile/MidiFileFormat.html
Can be used only when the MIDI is playing.
|
getset |
Percentage of the default playback speed. Range 0.1 (10% of the current BPM) to 10 (1000%). Default is 1 for normal speed.
Speed also applied to the duration of the sample played at voice level (often multiple voices are played for one note).
|
getset |
Get or change the current tempo played by the internal MIDI sequencer (independent from MPTK_Speed).
Return QuarterPerMinuteValue similar to BPM (Beat Per Measure).
|
get |
Get Text if defined in the MIDI file with MIDI MetaEventType = TextEvent
See detail here https://ccrma.stanford.edu/~craig/14q/midifile/MidiFileFormat.html
Can be used only when the MIDI is playing.
|
getset |
Get the tick value of the last MIDI event played.
Set the tick value of the next MIDI event to played.
MIDI tick is an easy way to identify a position in a song independently of the time which could vary with tempo change event.
The count of ticks by quarter is constant all along a MIDI, it's a properties of the whole MIDI. see MPTK_DeltaTicksPerQuarterNote.
With a time signature of 4/4 the ticks length of a bar is 4 * MPTK_DeltaTicksPerQuarterNote.
Here, more information about Midi Timing https://paxstellar.fr/2020/09/11/midi-timing/
|
get |
Tick tick 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 tick where the will start to play a sound.
See also MPTK_PositionFirstNote
|
get |
Last tick tick in Midi: it's the value of the tick for the last MIDI event in sequence expressed in number of "ticks".
|
get |
Tick tick 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 tick time when all sound will be stop.
See also the MPTK_PositionLastNote which provides the last time of the MIDI.
|
get |
Count of track read in the MIDI file
|
get |
Get Instrument track name if defined in the MIDI file with MIDI MetaEventType = TrackInstrumentName
See detail here https://ccrma.stanford.edu/~craig/14q/midifile/MidiFileFormat.html
Can be used only when the MIDI is playing.