Lua script for writing a note to a Midi track


I would like to create a midi track and write a midi note in the new created track with a Lua script.
Does anyone know an example script, where I could see how a midi track is created and how to manipulate a midi track?

Thanks for any advice

For the second part, the audio-to-midi script can be of some help, that does however depend on an existing track and region (demo: )

To create a new MIDI track, you’re looking for Session:new_midi_track. There are a couple of scripts using that, best example is probably

There is a missing link: Creating an empty midi-region to write notes into.

The API for handing Music Time (Bar, Beat) is in flux and will change with Ardour6, so a lot of the MIDI methods have not been exposed to scripting, yet. In particular MIDI region scripting API is only bare-bones at this point in time.

Editing MIDI is done by creating a diff erence command and then apply it. Something like

local midi_model = midi_region:model()
local notelist = ARDOUR.LuaAPI.note_list (midi_model)
local midi_command = midi_model:new_note_diff_command ("Description of Change")
for note in notelist:iter () do
  local newnote = ARDOUR.LuaAPI.new_noteptr (note:channel(), note:time (), note:length (), note:note(), note:velocity())
  midi_command:remove (note)
  midi_command:add (newnote)
midi_model:apply_command (Session, midi_command)

Hi Robin,

this is exactly what I needed.

Thank you very much.