![]() |
Maestro - Midi Player Tool Kit for Unity Version 2.19.0
|
Create, build, write, import, and play MIDI from script. See full examples in: More...
Tempo and Time Conversion | |
Tempo state, tempo/signature maps, and tick/millisecond conversion helpers. This section defines the timing model used by the writer. It includes DTPQN, current tempo, cached tempo/signature maps, and helper methods to convert absolute positions and durations between ticks and milliseconds. | |
| int | DeltaTicksPerQuarterNote |
| Delta Ticks Per Quarter Note (DTPQN) represents the number of ticks in one quarter note. For example, with 96 ticks per quarter note, an eighth note has a duration of 48 ticks. In a MIDI file, this value is found in the MIDI header and remains constant for the entire file. More information: https://paxstellar.fr/2020/09/11/midi-timing/ . | |
| List< MPTKTempo > | MPTK_TempoMap |
| List of tempo segments derived from MPTK_MidiEvents. Rebuild with MPTKTempo.CalculateMap when events change. See example: | |
| List< MPTKSignature > | MPTK_SignMap |
| List of time-signature segments derived from MPTK_MidiEvents. Rebuild with MPTKSignature.CalculateMap when events change. See example: | |
| int | MicrosecondsPerQuaterNote [get] |
| Gets the current tempo value in microseconds per quarter note: 60 * 1000 * 1000 / BPM. . | |
| double | CurrentTempo [get] |
| Gets the current tempo in BPM. Updated by AddTempoChange and AddBPMChange. https://en.wikipedia.org/wiki/Tempo. | |
| float | PulseLenght [get] |
| Gets the current duration, in milliseconds, of one MIDI tick (relative to the current tempo). . | |
| float | ConvertTickToMilli (long tick, int indexTempo=-1) |
| Converts an absolute tick position to time in milliseconds. . | |
| long | ConvertMilliToTick (float time, int indexTempo=-1) |
| Converts a time position in milliseconds to an absolute tick position. . | |
| long | DurationTickToMilli (long tick) |
| Converts a tick duration to a millisecond duration using the current tempo. . | |
| long | DurationMilliToTick (float time) |
| Converts a millisecond duration to a tick duration using the current tempo. . | |
Sequence State and Event Collections | |
Core state for the current MIDI sequence and generated event list. This section stores global sequence metadata (name, MIDI format, channels), accessors for the generated event list, and convenience properties such as last event and last tick position. | |
| int | ChannelCount = 16 |
| Gets the MIDI file type of the loaded MIDI (0, 1, or 2). | |
| int | MidiFileType |
| Gets the MIDI file type of the loaded MIDI (0, 1, or 2). | |
| string | MidiName |
| Name of this MIDI sequence. | |
| List< MPTKEvent > | MPTK_MidiEvents |
| Gets all created MIDI events. | |
| int | TrackCount [get] |
| Gets the number of tracks. This value is available only when CreateTracksStat() has been called. There is no longer a track-count limit as of V2.9.0. | |
| MPTKEvent | MPTK_LastEvent [get] |
| Last MIDI event in the created MIDI events list. It is not always the last sound played, especially if a previous event lasts longer. | |
| long | TickLast [get] |
| Last tick position including event duration. | |
| int | CountEvent [get] |
| Number of MIDI events in MPTK_MidiEvents. | |
MIDI Event Creation API | |
Add musical, control, and meta events to the sequence. This section is the main authoring API for building MIDI content: notes, chords, program/controller changes, pitch wheel, tempo/signature, and text meta events. | |
| bool | ExtendedText [get, set] |
| If true, META text (for example lyrics) is written with UTF-8 encoding. Default is false. Standard MIDI text meta events are ASCII. With this extension, you can also store and display non-ASCII characters (Korean, Chinese, Japanese, accented Latin characters, and more). To enable reading UTF8 characters from an MPTK MIDI player, set MidiFilePlayer.MPTK_ExtendedText to true. . | |
| MPTKEvent | AddRawEvent (MPTKEvent mptkEvent) |
| Adds an event directly from an MPTKEvent instance. . | |
| MPTKEvent | AddNote (int track, long tick, int channel, int note, int velocity, int length) |
| Adds a NoteOn event at an absolute tick position. A corresponding NoteOff is generated automatically when length is greater than 0. If length is less than 0, no automatic NoteOff is created. | |
| MPTKEvent | AddSilence (int track, long tick, int channel, int length) |
| Adds a silence placeholder. . | |
| MPTKEvent | AddOff (int track, long tick, int channel, int note) |
| Adds a note-off event. It must always follow the corresponding NoteOn, on the same channel. | |
| void | AddChordFromScale (int track, long tick, int channel, MPTKScaleLib scale, MPTKChordBuilder chord) |
| Adds a chord built from a scale range. | |
| void | AddChordFromLib (int track, long tick, int channel, MPTKChordName chordName, MPTKChordBuilder chord) |
| Adds a chord from a chord library. | |
| MPTKEvent | AddChangePreset (int track, long tick, int channel, int preset) |
| Adds a preset change. | |
| MPTKEvent | AddChannelAfterTouch (int track, long tick, int channel, int afterTouchPressure) |
| Adds a Channel After-Touch event. | |
| MPTKEvent | AddControlChange (int track, long tick, int channel, MPTKController controller, int controllerValue) |
| Creates a general Control Change event (CC). | |
| MPTKEvent | AddPitchWheelChange (int track, long tick, int channel, float pitchWheel) |
| Creates a Pitch Wheel event from a normalized value. pitchWheel: | |
| MPTKEvent | AddBPMChange (int track, long tick, int bpm) |
| Adds a tempo change to the MIDI stream. There is no channel parameter because a tempo change is applied to all tracks and channels. Subsequent duration/time conversions use the new BPM value. | |
| MPTKEvent | AddTempoChange (int track, long tick, int microsecondsPerQuarterNote) |
| Adds a tempo change to the MIDI stream in microseconds per quarter note. There is no channel parameter because a tempo change is applied to all tracks and channels. Subsequent duration/time conversions use the new tempo value. | |
| MPTKEvent | AddTimeSignature (int track, long tick, int numerator=4, int denominator=2, int ticksInMetronomeClick=24, int no32ndNotesInQuarterNote=32) |
| Creates a TimeSignature meta event (optional). The internal sequencer default is 4,2,24,32. No channel is required because time signature applies globally. More information: https://paxstellar.fr/2020/09/11/midi-timing/. | |
| MPTKEvent | AddText (int track, long tick, MPTKMeta typeMeta, string text) |
| Creates a MIDI text event. | |
Initialization and Import | |
Initialize, reset, and merge event lists into the writer. Includes constructor defaults, full reset logic, and list import/merge behavior with DTPQN conversion and post-import timing recalculation. | |
| MPTKWriter (int deltaTicksPerQuarterNote=240, int midiFileType=1, int bpm=120, int channelcount=16) | |
| Creates an empty MPTKWriter with default or custom MIDI header values. Default: . | |
| void | Clear () |
| Removes all MIDI events and restores default attributes: | |
| bool | ImportFromEventsList (List< MPTKEvent > midiEventsToInsert, int deltaTicksPerQuarterNote, long position=-1, string name=null, bool logPerf=false, bool logDebug=false) |
| Imports a list of MPTKEvent instances. . | |
Loading Existing MIDI Content | |
Load sequence data from file system or MidiDB. These methods replace the current in-memory event list with events loaded from an external source and restore timing header values such as DTPQN. | |
| bool | LoadFromFile (string filename) |
| Loads a MIDI file from the OS file system (behavior may depend on the OS). | |
| bool | LoadFromMidiDB (int indexMidiDb) |
| Loads a MIDI from MPTK MidiDB into this writer. Existing events are replaced. If you add events afterward, sort the list before writing. | |
Track Statistics | |
Build and maintain per-track counters derived from event content. Track statistics are used by export/diagnostic operations to summarize event distribution by track (count, notes, preset changes). | |
| Dictionary< long, MPTKStat > | CreateTracksStat () |
| Builds track statistics (MPTK_TrackStat) from events in MPTK_MidiEvents. The dictionary key is the track number, and each value contains aggregated counters. | |
Event List Maintenance and Timing Recalculation | |
Modify event collections and recompute sorted/timed state. Use these methods to delete subsets of events, apply stable sorting, and rebuild real-time/measure/beat values after content changes. | |
| void | DeleteChannel (int channel) |
| Deletes all MIDI events on this channel. | |
| void | DeleteTrack (int track) |
| Deletes all MIDI events on this track. | |
| void | StableSortEvents (bool logPerf=false) |
| Sorts events in MPTK_MidiEvents in place by ascending tickFromTime position. Priority is applied for 'bank change' and 'preset change' within a group of events at the same position (but 'end track' is placed at the end of the group). | |
| void | CalculateTiming (bool logPerf=false, bool logDebug=false) |
| Calculates real-time position, measure, and beat for each event. | |
Export and Diagnostics | |
Export MIDI files and inspect generated output/logs. This section converts MPTK events to NAudio structures, writes files to disk or MidiDB, and provides detailed logging helpers for validation. | |
| bool | WriteToFile (string filename) |
| Writes a MIDI file to an OS folder. | |
| bool | WriteToMidiDB (string filename) |
| Writes a MIDI file to MidiDB. To be used only in Edit mode, not in a standalone application. A call to AssetDatabase.Refresh() is required after the file has been added to the Resources folder. | |
| MidiFile | BuildNAudioMidi () |
| Builds an NAudio MidiFile object from MPTK_MidiEvents. WriteToMidiDB and WriteToFile call this method before exporting. | |
| bool | LogWriter () |
| Logs writer state and MIDI events. | |
| void | LogTempoMap () |
| Logs information about the tempo map. | |
| void | LogSignMap () |
| Logs information about the signature map. | |
| void | LogTrackStat () |
| Logs information about track statistics. | |
| bool | LogRaw () |
| Logs raw NAudio MIDI events built from the current writer state. | |
Create, build, write, import, and play MIDI from script. See full examples in:
| MidiPlayerTK.MPTKWriter.MPTKWriter | ( | int | deltaTicksPerQuarterNote = 240, |
| int | midiFileType = 1, | ||
| int | bpm = 120, | ||
| int | channelcount = 16 ) |
Creates an empty MPTKWriter with default or custom MIDI header values.
Default:
.
| deltaTicksPerQuarterNote | Delta ticks per quarter note, default is 240. See DeltaTicksPerQuarterNote. |
| midiFileType | Type of MIDI format. Must be 0 or 1. Default is 1. |
| bpm | Initial tempo in beats per minute. Default is 120. |
| void MidiPlayerTK.MPTKWriter.Clear | ( | ) |
Removes all MIDI events and restores default attributes:
| bool MidiPlayerTK.MPTKWriter.ImportFromEventsList | ( | List< MPTKEvent > | midiEventsToInsert, |
| int | deltaTicksPerQuarterNote, | ||
| long | position = -1, | ||
| string | name = null, | ||
| bool | logPerf = false, | ||
| bool | logDebug = false ) |
Imports a list of MPTKEvent instances.
.
Multiple imports can be chained to merge events from different sources.
| midiEventsToInsert | List of MPTKEvent instances to insert. |
| deltaTicksPerQuarterNote | DTPQN value of the source events. |
| position | Tick position for insertion: -1 to append, 0 for beginning, or any tick within the sequence. |
| name | Optional sequence name (sets MidiName). |
| logDebug | Enable debug logs. |
| float MidiPlayerTK.MPTKWriter.ConvertTickToMilli | ( | long | tick, |
| int | indexTempo = -1 ) |
Converts an absolute tick position to time in milliseconds.
.
| tick | Absolute tick position. |
| indexTempo | Optional index in MPTK_TempoMap for this position. If -1, the map is recalculated and the segment is searched automatically. |
| long MidiPlayerTK.MPTKWriter.ConvertMilliToTick | ( | float | time, |
| int | indexTempo = -1 ) |
Converts a time position in milliseconds to an absolute tick position.
.
| time | Time position in milliseconds |
| indexTempo | Optional index in MPTK_TempoMap for this position. If -1, the map is recalculated and the segment is searched automatically. |
| long MidiPlayerTK.MPTKWriter.DurationTickToMilli | ( | long | tick | ) |
Converts a tick duration to a millisecond duration using the current tempo.
.
| tick | Duration in ticks. |
| long MidiPlayerTK.MPTKWriter.DurationMilliToTick | ( | float | time | ) |
Converts a millisecond duration to a tick duration using the current tempo.
.
| time | Duration in milliseconds. |
| bool MidiPlayerTK.MPTKWriter.LoadFromFile | ( | string | filename | ) |
Loads a MIDI file from the OS file system (behavior may depend on the OS).
| filename | Full path to the MIDI file. |
| bool MidiPlayerTK.MPTKWriter.LoadFromMidiDB | ( | int | indexMidiDb | ) |
Loads a MIDI from MPTK MidiDB into this writer. Existing events are replaced. If you add events afterward, sort the list before writing.
| indexMidiDb | Index in the MidiDB list. |
| Dictionary< long, MPTKStat > MidiPlayerTK.MPTKWriter.CreateTracksStat | ( | ) |
Builds track statistics (MPTK_TrackStat) from events in MPTK_MidiEvents. The dictionary key is the track number, and each value contains aggregated counters.
| MaestroException |
| void MidiPlayerTK.MPTKWriter.DeleteChannel | ( | int | channel | ) |
Deletes all MIDI events on this channel.
| channel | Channel to remove (0-15). |
| void MidiPlayerTK.MPTKWriter.DeleteTrack | ( | int | track | ) |
Deletes all MIDI events on this track.
| track | Track index to remove. |
| void MidiPlayerTK.MPTKWriter.StableSortEvents | ( | bool | logPerf = false | ) |
Sorts events in MPTK_MidiEvents in place by ascending tickFromTime position. Priority is applied for 'bank change' and 'preset change' within a group of events at the same position (but 'end track' is placed at the end of the group).
| logPerf | Enable performance logging. |
| bool MidiPlayerTK.MPTKWriter.WriteToFile | ( | string | filename | ) |
Writes a MIDI file to an OS folder.
| filename | Output file path. |
| bool MidiPlayerTK.MPTKWriter.WriteToMidiDB | ( | string | filename | ) |
Writes a MIDI file to MidiDB.
To be used only in Edit mode, not in a standalone application.
A call to AssetDatabase.Refresh() is required after the file has been added to the Resources folder.
| filename | Filename of the MIDI file, without folder or extension. |
| List<MPTKTempo> MidiPlayerTK.MPTKWriter.MPTK_TempoMap |
List of tempo segments derived from MPTK_MidiEvents. Rebuild with MPTKTempo.CalculateMap when events change.
See example:
| List<MPTKSignature> MidiPlayerTK.MPTKWriter.MPTK_SignMap |
List of time-signature segments derived from MPTK_MidiEvents. Rebuild with MPTKSignature.CalculateMap when events change.
See example:
| List<MPTKEvent> MidiPlayerTK.MPTKWriter.MPTK_MidiEvents |
Gets all created MIDI events.
|
get |
Gets the current tempo value in microseconds per quarter note: 60 * 1000 * 1000 / BPM.
.
Tempo in a MIDI file is stored in microseconds per quarter note. To convert this to BPM, use #MPTKEvent.QuarterPerMicroSecond2BeatPerMinute.
This value can change while generating MIDI when AddTempoChange is called.
More information: https://paxstellar.fr/2020/09/11/midi-timing/
|
get |
Gets the current duration, in milliseconds, of one MIDI tick (relative to the current tempo).
.
This depends on CurrentTempo and DeltaTicksPerQuarterNote.
PulseLength = 60000 / CurrentTempo / DeltaTicksPerQuarterNote
|
get |
Last MIDI event in the created MIDI events list. It is not always the last sound played, especially if a previous event lasts longer.
|
get |
Last tick position including event duration.