Create, build, write, import, and play MIDI by script. See full examples with these scripts: More...
Public Member Functions | |
| MPTKWriter (int deltaTicksPerQuarterNote=240, int midiFileType=1, int bpm=120, int channelcount=16) | |
| MPTKEvent | AddBPMChange (int track, long tick, int bpm) |
| MPTKEvent | AddChangePreset (int track, long tick, int channel, int preset) |
| MPTKEvent | AddChannelAfterTouch (int track, long tick, int channel, int afterTouchPressure) |
| void | AddChordFromLib (int track, long tick, int channel, MPTKChordName chordName, MPTKChordBuilder chord) |
| void | AddChordFromScale (int track, long tick, int channel, MPTKScaleLib scale, MPTKChordBuilder chord) |
| MPTKEvent | AddControlChange (int track, long tick, int channel, MPTKController controller, int controllerValue) |
| MPTKEvent | AddNote (int track, long tick, int channel, int note, int velocity, int length) |
| MPTKEvent | AddOff (int track, long tick, int channel, int note) |
| MPTKEvent | AddPitchWheelChange (int track, long tick, int channel, float pitchWheel) |
| MPTKEvent | AddRawEvent (MPTKEvent mptkEvent) |
| MPTKEvent | AddSilence (int track, long tick, int channel, int length) |
| MPTKEvent | AddTempoChange (int track, long tick, int microsecondsPerQuarterNote) |
| MPTKEvent | AddText (int track, long tick, MPTKMeta typeMeta, string text) |
| MPTKEvent | AddTimeSignature (int track, long tick, int numerator=4, int denominator=2, int ticksInMetronomeClick=24, int no32ndNotesInQuarterNote=32) |
| MidiFile | BuildNAudioMidi () |
| void | CalculateTiming (bool logPerf=false, bool logDebug=false) |
| void | Clear () |
| long | ConvertMilliToTick (float time, int indexTempo=-1) |
| float | ConvertTickToMilli (long tick, int indexTempo=-1) |
| Dictionary< long, MPTKStat > | CreateTracksStat () |
Builds track information (MPTK_TrackStat) from MIDI events found in MPTK_Events. MPTK_TrackStat is a dictionary with the track number as the key, and each item holds information about the track. More... | |
| void | DeleteChannel (int channel) |
| void | DeleteTrack (int track) |
| long | DurationMilliToTick (float time) |
| long | DurationTickToMilli (long tick) |
| bool | ImportFromEventsList (List< MPTKEvent > midiEventsToInsert, int deltaTicksPerQuarterNote, long position=-1, string name=null, bool logPerf=false, bool logDebug=false) |
| bool | LoadFromFile (string filename) |
| bool | LoadFromMidiDB (int indexMidiDb) |
| bool | LogRaw () |
| void | LogSignMap () |
| void | LogTempoMap () |
| void | LogTrackStat () |
| bool | LogWriter () |
| void | StableSortEvents (bool logPerf=false) |
| bool | WriteToFile (string filename) |
| bool | WriteToMidiDB (string filename) |
Public Attributes | |
| int | DeltaTicksPerQuarterNote |
| int | MidiFileType |
| string | MidiName |
| List< MPTKEvent > | MPTK_MidiEvents |
| List< MPTKSignature > | MPTK_SignMap |
| List< MPTKTempo > | MPTK_TempoMap |
Properties | |
| int | CountEvent [get] |
| double | CurrentTempo [get] |
| bool | ExtendedText [get, set] |
| int | MicrosecondsPerQuaterNote [get] |
| MPTKEvent | MPTK_LastEvent [get] |
| float | PulseLenght [get] |
| long | TickLast [get] |
| int | TrackCount [get] |
Create, build, write, import, and play MIDI by script. See full examples with these scripts:
| MidiPlayerTK.MPTKWriter.MPTKWriter | ( | int | deltaTicksPerQuarterNote = 240, |
| int | midiFileType = 1, |
||
| int | bpm = 120, |
||
| int | channelcount = 16 |
||
| ) |
Creates an empty MPTKWriter with default or specific MIDI header values (for advanced use).
Default:
| deltaTicksPerQuarterNote | Delta Ticks Per Beat Note, default is 240. See #MPTK_DeltaTicksPerQuarterNote. |
| midiFileType | Type of MIDI format. Must be 0 or 1. Default is 1. |
| bpm | Initial Beats Per Minute, default 120 |
| MPTKEvent MidiPlayerTK.MPTKWriter.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.
The next note-on with milliseconds defined after the tempo change will take the new BPM value into account.
| track | Track for this event (it is good practice to use track 0 for this event). |
| tick | Tick time for this event |
| bpm | Quarters per minute. |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddChangePreset | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| int | preset | ||
| ) |
Adds a preset change.
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| preset | Preset (program/patch) must be in the range 0-127 |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddChannelAfterTouch | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| int | afterTouchPressure | ||
| ) |
Adds a Channel After-Touch event.
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| afterTouchPressure | After-touch pressure from 0 to 127 |
| void MidiPlayerTK.MPTKWriter.AddChordFromLib | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| MPTKChordName | chordName, | ||
| MPTKChordBuilder | chord | ||
| ) |
Adds a chord from a chord library.
| track | Track for this event (do not add to track 0) |
| tick | |
| channel | |
| chordName | Name of the chord See MPTKChordName |
| chord | See MPTKChordBuilder |
| void MidiPlayerTK.MPTKWriter.AddChordFromScale | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| MPTKScaleLib | scale, | ||
| MPTKChordBuilder | chord | ||
| ) |
Adds a chord from a range.
| track | Track for this event (do not add to track 0) |
| tick | |
| channel | |
| scale | See MPTKScaleLib |
| chord | See MPTKChordBuilder |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddControlChange | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| MPTKController | controller, | ||
| int | controllerValue | ||
| ) |
Creates a general Control Change event (CC).
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| controller | The MIDI Controller. See MPTKController |
| controllerValue | Controller value |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddNote | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| int | note, | ||
| int | velocity, | ||
| int | length | ||
| ) |
Adds a note-on event at an absolute time (tickFromTime count). The corresponding note-off is created automatically if length > 0.
If an infinite note-on is added (length < 0), do not forget to add a note-off; it will never be created automatically.
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| note | Note must be in the range 0-127 |
| velocity | Velocity must be in the range 0-127. |
| length | Duration in tickFromTime. No automatic noteoff is added if duration is < 0, need to be added with MPTK_AddOff |
| MPTKEvent MidiPlayerTK.MPTKWriter.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.
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| note | Note must be in the range 0-127 |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddPitchWheelChange | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| float | pitchWheel | ||
| ) |
Creates a control change event (CC) for the pitch (Pitch Wheel)
pitchWheel:
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event |
| channel | Channel must be in the range 0-15 |
| pitchWheel | Normalized Pitch Wheel Value. Range 0 to 1. V2.88.2 range normalized from 0 to 1. |
Adds an MPTK MIDI event from an MptkEvent instance. Useful for adding a raw MIDI event.
These attributes must be defined in the MptkEvent instance:
| mptkEvent |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddSilence | ( | int | track, |
| long | tick, | ||
| int | channel, | ||
| int | length | ||
| ) |
Adds a silence.
| track | Track for this event (do not add to track 0) |
| tick | Tick time for this event. |
| channel | Channel must be in the range 0-15 |
| length | Duration in tickFromTime. |
| MPTKEvent MidiPlayerTK.MPTKWriter.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.
The next note-on with milliseconds defined after the tempo change will take the new BPM value into account.
| track | Track for this event (it is good practice to use track 0 for this event). |
| tick | Tick time for this event |
| microsecondsPerQuarterNote | Microseconds per quarter note |
Creates a MIDI text event.
| track | Track for this event (it's a good practice to use track 0 for this event) |
| tick | Absolute time of this event |
| typeMeta | MetaEvent type |
| text | The text associated with this MIDI event. |
| MPTKEvent MidiPlayerTK.MPTKWriter.AddTimeSignature | ( | int | track, |
| long | tick, | ||
| int | numerator = 4, |
||
| int | denominator = 2, |
||
| int | ticksInMetronomeClick = 24, |
||
| int | no32ndNotesInQuarterNote = 32 |
||
| ) |
Creates a new TimeSignatureEvent. This event is optional. The internal MIDI sequencer assumes the default value is 4,2,24,32. No track or channel is needed because the time signature applies to the whole MIDI. More information: https://paxstellar.fr/2020/09/11/midi-timing/
| track | Track for this event (it is good practice to use track 0 for this event). |
| tick | Time at which to create this event |
| numerator | Numerator, beats per measure. Will be MPTKSignature.NumberBeatsMeasure in MPTK_SignMap |
| denominator | Denominator, beat unit: 1 means 2, 2 means 4 (crochet), 3 means 8 (quaver), 4 means 16, ... |
| ticksInMetronomeClick | Ticks in Metronome Click. Set to 24 for a standard value. |
| no32ndNotesInQuarterNote | Number of 32nd notes in a beat click. Set to 32 for a standard value. |
| MidiFile MidiPlayerTK.MPTKWriter.BuildNAudioMidi | ( | ) |
Builds an NAudio MIDI object from the MIDI events. WriteToMidiDB and WriteToFile call this method just before writing the MIDI file.
| void MidiPlayerTK.MPTKWriter.CalculateTiming | ( | bool | logPerf = false, |
| bool | logDebug = false |
||
| ) |
Calculates real time, measure, and quarter position.
| logPerf | |
| logDebug |
| void MidiPlayerTK.MPTKWriter.Clear | ( | ) |
Removes all MIDI events and restores default attributes:
| long MidiPlayerTK.MPTKWriter.ConvertMilliToTick | ( | float | time, |
| int | indexTempo = -1 |
||
| ) |
Converts a time position in milliseconds to an absolute tickFromTime position.
| time | Time position in milliseconds |
| indexTempo | Index in MPTK_TempoMap for this position. Optional, if not defined or -1, MPTK_TempoMap will be recalculated and the segment for this position will be searched |
| float MidiPlayerTK.MPTKWriter.ConvertTickToMilli | ( | long | tick, |
| int | indexTempo = -1 |
||
| ) |
Converts an absolute tickFromTime position to a time in milliseconds.
| tick | Absolute tickFromTime position |
| indexTempo | Index in MPTK_TempoMap for this position. Optional, if not defined or -1, MPTK_TempoMap will be recalculated and the segment for this position will be searched |
| Dictionary< long, MPTKStat > MidiPlayerTK.MPTKWriter.CreateTracksStat | ( | ) |
Builds track information (MPTK_TrackStat) from MIDI events found in MPTK_Events. MPTK_TrackStat is a dictionary with the track number as the key, and each item holds information about the track.
| MaestroException |
| void MidiPlayerTK.MPTKWriter.DeleteChannel | ( | int | channel | ) |
Deletes all MIDI events on this channel.
| channel |
| void MidiPlayerTK.MPTKWriter.DeleteTrack | ( | int | track | ) |
Deletes all MIDI events on this track.
| track |
| long MidiPlayerTK.MPTKWriter.DurationMilliToTick | ( | float | time | ) |
Converts a time duration in milliseconds to a tickFromTime duration using the current tempo.
| time | Duration in milliseconds. |
| long MidiPlayerTK.MPTKWriter.DurationTickToMilli | ( | long | tick | ) |
Converts a tickFromTime duration to a time duration in milliseconds using the current tempo.
| tick | Duration in ticks. |
| bool MidiPlayerTK.MPTKWriter.ImportFromEventsList | ( | List< MPTKEvent > | midiEventsToInsert, |
| int | deltaTicksPerQuarterNote, | ||
| long | position = -1, |
||
| string | name = null, |
||
| bool | logPerf = false, |
||
| bool | logDebug = false |
||
| ) |
New in version V2.9.0: imports a list of MptkEvent.
Multiple imports can be performed to join MIDI events from different sources.
| midiEventsToInsert | List of MptkEvent to insert |
| deltaTicksPerQuarterNote | It's the DTPQN of the MIDI events to insert.
|
| position | tickFromTime position to insert: -1 to append, 0 at the beginning. |
| name | Name of the MIDI created (set MPTK_MidiName). |
| logDebug | Debug log. |
| bool MidiPlayerTK.MPTKWriter.LoadFromFile | ( | string | filename | ) |
Loads a MIDI file from the OS file system (behavior may depend on the OS).
| filename |
| bool MidiPlayerTK.MPTKWriter.LoadFromMidiDB | ( | int | indexMidiDb | ) |
Creates an MPTKWriter from a MIDI found in MPTK MidiDB. All existing MIDI events before loading will be lost. If you add some MIDI events after loading, don't forget to sort the MIDI events after. See example.
| indexMidiDb |
| bool MidiPlayerTK.MPTKWriter.LogRaw | ( | ) |
Logs information about the MIDI.
| void MidiPlayerTK.MPTKWriter.LogSignMap | ( | ) |
Logs information about the signature map.
| void MidiPlayerTK.MPTKWriter.LogTempoMap | ( | ) |
Logs information about the tempo map.
| void MidiPlayerTK.MPTKWriter.LogTrackStat | ( | ) |
Logs information about the stat map.
| bool MidiPlayerTK.MPTKWriter.LogWriter | ( | ) |
Logs information about the MIDI.
| 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 |
| bool MidiPlayerTK.MPTKWriter.WriteToFile | ( | string | filename | ) |
Writes a MIDI file to an OS folder.
| filename | Filename of the MIDI file. |
| 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. |
| int MidiPlayerTK.MPTKWriter.DeltaTicksPerQuarterNote |
Delta Ticks Per Beat Note (or DTPQN) represents the duration in "ticks" that makes up a quarter note.
For example, with 96 a duration of an eighth-note in the file would be 48.
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/
| int MidiPlayerTK.MPTKWriter.MidiFileType |
Gets the MIDI file type of the loaded MIDI (0, 1, or 2).
| string MidiPlayerTK.MPTKWriter.MidiName |
Name of this MIDI.
| List<MPTKEvent> MidiPlayerTK.MPTKWriter.MPTK_MidiEvents |
Gets all created MIDI events.
| List<MPTKSignature> MidiPlayerTK.MPTKWriter.MPTK_SignMap |
List of signature changes found in MPTK_MidiEvents. Must be updated with MPTKSignature.MPTK_CalculateMap.
See example:
| List<MPTKTempo> MidiPlayerTK.MPTKWriter.MPTK_TempoMap |
List of tempo changes found in MPTK_MidiEvents. Must be updated with MPTKTempo.MPTK_CalculateMap.
See example:
|
get |
Number of MIDI events in MPTK_Events.
|
get |
V2.9.0 - Get the current tempo. Set with AddTempoChange.
https://en.wikipedia.org/wiki/Tempo
|
getset |
If true, META text (e.g. lyrics) will be written 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 ;-)
To enable reading UTF8 characters from an MPTK MIDI player, set MidiFilePlayer::MPTK_ExtendedText to true.
|
get |
Gets the current microseconds per quarter note: 60 * 1000 * 1000 https://en.wikipedia.org/wiki/Tempo
Tempo in a MIDI file is given in microseconds per quarter note. To convert this to BPM, use #QuarterPerMicroSecond2BeatPerMinute.
This value can change during the generation when AddTempoChange is called.
See here for more information https://paxstellar.fr/2020/09/11/midi-timing/
|
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 |
Gets the current length in milliseconds of one MIDI tickFromTime (relative to the current tempo).
Obviously depends on the current tempo (CurrentTempo) and the #MPTK_DeltaTicksPerQuarterNote.
PulseLength = 60000 / CurrentTempo / #MPTK_DeltaTicksPerQuarterNote
|
get |
Last tick position including the duration.
|
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.