Page tree
Skip to end of metadata
Go to start of metadata
insertEvent(eventsTable, event)


Function to insert an event in the specified events table according to its PPQ position. The events table is part of a tracks table which is part of the MIDI sequence table. See MIDI Sequence Table for details.

The table.insert function from Lua's table library sorts only by index and not by PPQ position. The insertEvent function inserts the events according to its PPQ position. That's why you should use this function instead. Removing events is more straightforward and you can use Lua's table.remove function just like normal.

Available in: Controller


eventsTableThe table record referencing the events table.table
eventThe Event object to be inserted.Event


-- produce a minor scale and write it to a MIDI file

-- create MIDI sequence table
midiSequence = { tracks = { { events = {} } } }

-- initialize variables
minorScaleIntervals = { 0, 2, 3, 5, 7, 8, 10, 12 }
root = 60  -- C3
ppqPosition = 0

-- produce the events of the minor scale
for i, interval in ipairs(minorScaleIntervals) do

    local note = root + interval

    -- create note-on event
    local noteOn = Event(EventType.noteOn)
    noteOn.note = note
    noteOn.velocity = 100
    noteOn.ppqPosition = ppqPosition

    -- create note-off event
    local noteOff = Event(EventType.noteOff)
    noteOff.note = note
    noteOff.ppqPosition = ppqPosition + 1

    -- insert the events in the MIDI sequence table
    insertEvent(midiSequence.tracks[1].events, noteOn)
    insertEvent(midiSequence.tracks[1].events, noteOff)

    ppqPosition = ppqPosition + 1


-- write the MIDI sequence table as .mid file to disk
saveState = writeMidiFile ("c:/temp/test.mid", midiSequence) --[[ please set the file path to the desired
                                                                  location on your system before you run
                                                                  the script ]]

if saveState then
    print("The MIDI file was successfully written.")
    print("The MIDI file could not be written!")

1 Comment