MIDI File – Giavapps MIDI

These functions are for advanced users only. For more info about MIDI File Format click here.

 

midi_file_write_mthd_start(FileName, Format, Tracks, Division) Creates a new MIDI File and starts writing the Header Chunk. This function returns a File index that you can use for all other functions.
FileName: file name.
Format: MIDI File Format (0, 1 or 2).
Tracks: the number of Track Chunks.
Division: unit of time for Delta Timing.

 

midi_file_write_mthd_end(File) Closes the given File.
File: file index.

 

midi_file_write_mtrk_start(File, Length) Begins writing the Track Chunk. The Length argument must be the correct size in bytes otherwise the MIDI file will be unreadable by MIDI Players.
File: file index.
Length: length (in bytes) of this Track Chunk.

 

Variable Length Value

 

Giavapps MIDI calculates and encodes all Variable Length Values for you automatically. When a larger Value needs to be specified then additional bytes are used:

Value Bytes
0 < — > 127 1
128 < — > 16383 2
16384 < — > 2097151 3
2097152 < — > 268435455 4

 

Meta Events

 

midi_file_write_mtrk_text(File, Text) Writes the Meta Event “Text”. (3 bytes + Variable Length Value + String Length)
File: file index.
Text: string with the Text.

 

midi_file_write_mtrk_copyright(File, Copyright) Writes the Meta Event “Copyright”. (3 bytes + Variable Length Value + String Length)
File: file index.
TrackName: string with the Copyright.

 

midi_file_write_mtrk_track_name(File, TrackName) Writes the Meta Event “Sequence / Track Name”. (3 bytes + Variable Length Value + String Length)
File: file index.
TrackName: string with the Track Name.

 

midi_file_write_mtrk_instrument_name(File, InstrumentName) Writes the Meta Event “Instrument Name”. (3 bytes + Variable Length Value + String Length)
File: file index.
InstrumentName: string with the Instrument Name.

 

midi_file_write_mtrk_lyric(File, Lyric) Writes the Meta Event “Lyric”. (3 bytes + Variable Length Value + String Length)
File: file index.
Lyric: string with the Lyric.

 

midi_file_write_mtrk_marker(File, Marker) Writes the Meta Event “Marker”. (3 bytes + Variable Length Value + String Length)
File: file index.
Marker: string with the Marker.

 

midi_file_write_mtrk_cue_point(File, CuePoint) Writes the Meta Event “Cue Point”. (3 bytes + Variable Length Value + String Length)
File: file index.
CuePoint: string with the Cue Point.

 

midi_file_write_mtrk_program_name(File, ProgramName) Writes the Meta Event “Program Name”. (3 bytes + Variable Length Value + String Length)
File: file index.
ProgramName: string with the Program Name.

 

midi_file_write_mtrk_device_name(File, DeviceName) Writes the Meta Event “Device Name”. (3 bytes + Variable Length Value + String Length)
File: file index.
ProgramName: string with the Device Name.

 

midi_file_write_mtrk_channel_prefix(File, Channel) Writes the Meta Event “Channel Prefix”. (5 bytes)
File: file index.
Channel: MIDI Channel (015).

 

midi_file_write_mtrk_midi_port(File, MIDIPort) Writes the Meta Event “MIDI Port”. (5 bytes)
File: file index.
MIDIPort: MIDI Port (0127).

 

midi_file_write_mtrk_end(File) Writes the Meta Event “End of Track”. (4 bytes).
File: file index.

 

midi_file_write_mtrk_tempo(File, Microseconds) Writes the Meta Event “Tempo”. (7 bytes).
File: file index.
Microseconds: tempo value (in microseconds).

 

midi_file_write_mtrk_smpte_offset(File, Hours, Minutes, Seconds, FramesPerSecond, FractionalFrames) Writes the Meta Event “SMPTE Offset”. (9 bytes).
File: file index.
Hours: hours (023).
Minutes: minutes (059).
Seconds: seconds (059).
FramesPerSecond: frames per second (24, 25, 29, 30).
FractionalFrames: fractional frames (in 100ths of a frame).

 

midi_file_write_mtrk_time_signature(File, Numerator, Denominator, TicksPerMetronomeClick, 32ndNotesPerQuarterNote) Writes the Meta Event “Time Signature”. (8 bytes).
File: file index.
Numerator: numerator of the Time Signature.
Denominator: denominator of the Time Signature as a negative power of 2 (2 = quarter note, 3 = eight note, etc.).
TicksPerMetronomeClick: number of ticks between metronome clicks.
32ndNotesPerQuarterNote: number of notated 32nd-notes in a quarter-note.

 

midi_file_write_mtrk_key_signature(File, FlatOrSharps, MajorOrMinor) Writes the Meta Event “Key Signature”. (6 bytes).
File: file index.
FlatOrSharps: number of flats (negative value) or sharps (positive value) that identifies the Key Signature (-7 = 7 flats, -1 = 1 flat, 0 = C, 1 = 1 sharp, etc).
MajorOrMinor: major (0) or minor (1) key.

 

MIDI Events

 

midi_file_write_mtrk_instrument(File, Channel, InstrumentNumber) Writes the MIDI Event “Program Change”. (3 bytes)
File: file index.
Channel: MIDI Channel (015).
InstrumentNumber: instrument number (0127).

 

midi_file_write_mtrk_channel_volume(File, Channel, Volume) Writes the MIDI Event “Control Change”. (4 bytes)
File: file index.
Channel: MIDI Channel (015).
Volume: channel volume (0127).

 

midi_file_write_mtrk_pan(File, Channel, Pan) Writes the MIDI Event “Control Change”. (4 bytes)
File: file index.
Channel: MIDI Channel (015).
Pan: panning (0127), 0 = left, 64 = center, 127 = right.

 

midi_file_write_mtrk_control_change(File, Channel, ControlNumber, Value) Writes the MIDI Event “Control Change”. (4 bytes)
File: file index.
Channel: MIDI Channel (015).
ControlNumber: MIDI Control Number (0127).
Value: value (0127).

 

midi_file_write_mtrk_note_on(File, DeltaTime, Channel, NoteNumber, Velocity) Writes the MIDI Event “Note On”. (3 bytes + Variable Length Value)
File: file index.
DeltaTime: number of tick intervals since the previous event.
Channel: MIDI Channel (015).
NoteNumber: note number (0127).
Velocity: velocity (0127).

 

midi_file_write_mtrk_note_off(File, DeltaTime, Channel, NoteNumber) Writes the MIDI Event “Note Off”. (3 bytes + Variable Length Value)
File: file index.
DeltaTime: number of tick intervals since the previous event.
Channel: MIDI Channel (015).
NoteNumber: note number (0127).

 

Code Examples

 

Creating a MIDI File in Format 0

 

filename = get_save_filename(“MIDI (*.mid)|*.mid”,“midi.mid”);
if(filename == “”){exit;}
//MIDI FILE FORMAT 0
file = midi_file_write_mthd_start(filename,0,1,96);//Header Chunk
midi_file_write_mtrk_start(file,26);//Track Chunk
midi_file_write_mtrk_track_name(file,“Track 1”);//3 bytes + 1 byte + 7 bytes
midi_file_write_mtrk_instrument(file,0,0);//3 bytes
midi_file_write_mtrk_note_on(file,0,0,60,64);//3 bytes + 1 byte
midi_file_write_mtrk_note_off(file,96,0,60);//3 bytes + 1 byte
midi_file_write_mtrk_end(file);//4 bytes
midi_file_write_mthd_end(file);

 

I am Luigi Piscopo, also known as DJ GiDeejay / Producer / Remixer in the music world. I am a Producer with a lot of experience in photo editing, video editing, audio editing, graphic design, web design, programming and promotion.

Leave a Reply

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

*