Create, build, write, import, play MIDI by script. See full example 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 () |
Build the tracks information (MPTK_TrackStat) from the MIDI event found in MPTK_Events. MPTK_TrackStat is a dictionary with the track number as a key and the item holds some informatio 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, play MIDI by script. See full example with these scripts:
MidiPlayerTK.MPTKWriter.MPTKWriter | ( | int | deltaTicksPerQuarterNote = 240 , |
int | midiFileType = 1 , |
||
int | bpm = 120 , |
||
int | channelcount = 16 |
||
) |
Create an empty MPTKWriter with default or specific header midi value (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 1 |
bpm | Initial Beats Per Minute, default 120 |
MPTKEvent MidiPlayerTK.MPTKWriter.AddBPMChange | ( | int | track, |
long | tick, | ||
int | bpm | ||
) |
Add a tempo change to the midi stream. There is no channel in parameter because tempo change is applied to all tracks and channels.
Next note-on with milliseconds defined after the tempo change will take into account the new value of the BPM.
track | Track for this event (it's a good practive to use track 0 for this event) |
tick | Tick time for this event |
bpm | quarter per minute |
MPTKEvent MidiPlayerTK.MPTKWriter.AddChangePreset | ( | int | track, |
long | tick, | ||
int | channel, | ||
int | preset | ||
) |
Add a change preset
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 | ||
) |
Add 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 | ||
) |
Add a chord from a library of chord
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 | ||
) |
Add 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 | ||
) |
Add a note on event at an absolute time (tickFromTime count). The corresponding Noteoff is automatically created if length > 0
If an infinite note-on is added (length < 0), don't forget to add a note-off, it will never 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 | ||
) |
Add a note off event.
Must always succeed the corresponding NoteOn, obviously 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. |
Add a MPTK Midi event from a MptkEvent instance. Useful to add a raw MIDI event.
These attributs must be defined in the MptkEvent instance:
.
mptkEvent |
MPTKEvent MidiPlayerTK.MPTKWriter.AddSilence | ( | int | track, |
long | tick, | ||
int | channel, | ||
int | length | ||
) |
Add 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 | ||
) |
Add a tempo change to the midi stream in microseconds per quarter note.
There is no channel in parameter because tempo change is applied to all tracks and channels.
Next note-on with milliseconds defined after the tempo change will take into account the new value of the BPM.
track | Track for this event (it's a good practive to use track 0 for this event) |
tick | Tick time for this event |
microsecondsPerQuarterNote | Microseconds per quarter note |
Create 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 to this MIDI event |
MPTKEvent MidiPlayerTK.MPTKWriter.AddTimeSignature | ( | int | track, |
long | tick, | ||
int | numerator = 4 , |
||
int | denominator = 2 , |
||
int | ticksInMetronomeClick = 24 , |
||
int | no32ndNotesInQuarterNote = 32 |
||
) |
Create a new TimeSignatureEvent. This event is optionnal. Internal Midi sequencer assumes the default value is 4,2,24,32. No track nor channel as teampo change applied to the whole midi. More info here https://paxstellar.fr/2020/09/11/midi-timing/
track | Track for this event (it's a good practive 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 | No of 32nd Notes in Beat Click. Set to 32 for a standard value. |
MidiFile MidiPlayerTK.MPTKWriter.BuildNAudioMidi | ( | ) |
Build a NAudio midi object from the midi events. WriteToMidiDB and WriteToFile call these methods just before writing the MIDI file.
void MidiPlayerTK.MPTKWriter.CalculateTiming | ( | bool | logPerf = false , |
bool | logDebug = false |
||
) |
Calculate real time, measure and quarter position
logPerf | |
logDebug |
void MidiPlayerTK.MPTKWriter.Clear | ( | ) |
Remove all MIDI events and restore default attributs:
long MidiPlayerTK.MPTKWriter.ConvertMilliToTick | ( | float | time, |
int | indexTempo = -1 |
||
) |
Convert a time position in millisecond 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 |
||
) |
Convert an absolute tickFromTime position to a time in millisecond.
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 | ( | ) |
Build the tracks information (MPTK_TrackStat) from the MIDI event found in MPTK_Events. MPTK_TrackStat is a dictionary with the track number as a key and the item holds some informatio about the track.
MaestroException |
void MidiPlayerTK.MPTKWriter.DeleteChannel | ( | int | channel | ) |
Delete all MIDI events on this channel
channel |
void MidiPlayerTK.MPTKWriter.DeleteTrack | ( | int | track | ) |
Delete all MIDI events on this track
track |
long MidiPlayerTK.MPTKWriter.DurationMilliToTick | ( | float | time | ) |
Convert the tickFromTime duration to a real time duration in millisecond regarding the current tempo.
tick | duration in ticks |
long MidiPlayerTK.MPTKWriter.DurationTickToMilli | ( | long | tick | ) |
Convert the tickFromTime duration to a time duration in millisecond regarding 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 with version V2.9.0 Import a list of MptkEvent.
Multiple imports can be done for joining 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 beguinning |
name | Name of the MIDI created (set MPTK_MidiName). |
logDebug | Debug log. |
bool MidiPlayerTK.MPTKWriter.LoadFromFile | ( | string | filename | ) |
Load a Midi file from OS system file (could be dependant of the OS)
filename |
bool MidiPlayerTK.MPTKWriter.LoadFromMidiDB | ( | int | indexMidiDb | ) |
Create a MPTKWriter from a Midi found in MPTK MidiDB. All existing MIDI events before the load 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 | ( | ) |
Log information about the MIDI
void MidiPlayerTK.MPTKWriter.LogSignMap | ( | ) |
Log information about the signature map
void MidiPlayerTK.MPTKWriter.LogTempoMap | ( | ) |
Log information about the tempo map
void MidiPlayerTK.MPTKWriter.LogTrackStat | ( | ) |
Log information about the stat map
bool MidiPlayerTK.MPTKWriter.LogWriter | ( | ) |
Log information about the MIDI
void MidiPlayerTK.MPTKWriter.StableSortEvents | ( | bool | logPerf = false | ) |
Sort in place events in MPTK_MidiEvents by ascending tickFromTime position. First priority is applied for 'preset change' and 'meta' event for a group of events with the same position (but 'end track' are set at end of the group.
logPerf |
bool MidiPlayerTK.MPTKWriter.WriteToFile | ( | string | filename | ) |
Write Midi file to an OS folder
filename | filename of the midi file |
bool MidiPlayerTK.MPTKWriter.WriteToMidiDB | ( | string | filename | ) |
Write Midi file to MidiDB.
To be used only in edit mode not in a standalone application.
A call to AssetDatabase.Refresh() must be required after the file has been added to the resource.
filename | filename of the midi file without any folder and any extension |
int MidiPlayerTK.MPTKWriter.DeltaTicksPerQuarterNote |
Delta Ticks Per Beat Note (or DTPQN) represent the duration time in "ticks" which make up a quarter-note.
For example, with 96 a duration of an eighth-note in the file would be 48.
From a MIDI file, this value is found in the MIDI Header and remains constant for all the MIDI file.
More info here https://paxstellar.fr/2020/09/11/midi-timing/
int MidiPlayerTK.MPTKWriter.MidiFileType |
Get the MIDI file type of the loaded MIDI (0,1,2)
string MidiPlayerTK.MPTKWriter.MidiName |
Name of this MIDI.
List<MPTKEvent> MidiPlayerTK.MPTKWriter.MPTK_MidiEvents |
Get all the MIDI events created.
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 |
Count of MIDI events in the MPTK_Events
|
get |
V2.9.0 - Get the current tempo. Set with AddTempoChange.
https://en.wikipedia.org/wiki/Tempo
|
getset |
If the value is true, Text for META (e.g. lyrics) will be write 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 of UTF8 characters from an MPTK MIDI player, set the MidiFilePlayer::MPTK_ExtendedText attribut to true.
|
get |
Get current Microseconds Per Quater Note: 60 * 1000 * 1000 https://en.wikipedia.org/wiki/Tempo
The tempo in a MIDI file is given in micro seconds per quarter beat. To convert this to BPM use method #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 events found in the MIDI events created. It's not always the last sound that's played, especially if the previous event lasted longer than the last.
|
get |
Get current lenght in millisecond of a MIDI tickFromTime (related to the current tempo).
Obviously depends on the current tempo (CurrentTempo) and the #MPTK_DeltaTicksPerQuarterNote.
PulseLenght = 60000 / CurrentTempo / #MPTK_DeltaTicksPerQuarterNote
|
get |
Last tick position including the duration.
|
get |
Get the count of track. The value is available only when CreateTracksStat() has been called. There no more limit of count of track with V2.9.0