Lua: how to iterate over selected MIDI notes?

I’m trying to write a Lua script that will process selected MIDI notes – just the selected region, not the whole session or clip. I expected to be able to get a list of MIDI notes by starting with ARDOURUI.Selection, but that only works if I select an entire clip, not if I only select a portion of a clip.

The example code I’m working from (Albert Gräf’s meter.lua and umkehr.lua) uses an ARDOUR.NotePtrList obtained from ARDOUR.LuaAPI.note_list(). That method appears to be the only way to obtain a (non-empty) NotePtrList, and it takes as a parameter a MidiModel.
As far as I can tell, a MidiModel can only be obtained from a MidiRegion or a MidiSource.
When I have an entire clip selected, ARDOURUI.Selection contains a Region, and all is well. But when I select just a portion of a clip, ARDOURUI.Selection contains a Range and a Route, but not a Region.
Regions can be obtained from a RegionSelection (which isn’t available in this case) or a Playlist.
The only way I found to obtain a Playlist is from a Track or a Route, but those classes don’t appear to have starting and ending times, and represent entire tracks.

There must be a way to do this. What am I missing?

Thank you for any help you can provide.

– Ed

I believe you meant to say only the selected (time) range, not the whole session or *region.

I don’t believe there is. Lua only exposes existing C++ APIs and in Ardour all MIDI region operations use the MIDI Model.

Thanks for the feedback, Robin.

I believe you meant to say only the selected (time) range, not the whole session or *region.

Yes, thank you for the correction.

Lua only exposes existing C++ APIs and in Ardour all MIDI region operations use the MIDI Model.

I’m not sure what you mean by that; all of the API items I mentioned are listed in the Lua Bindings Class Reference section of the Ardour manual.

Also, I just verified in Ardour that I can select a handful of MIDI notes (in one track, and not all the notes) and edit them with either mouse gestures or with operations from the main menu bar. So there must be an interface for getting the selected notes. Whether it’s exposed to Lua is another question.

This is internal to a given MIDIView and not part of the general Selection Model, which for MIDI Notes is only used as cut buffer in Ardour.

This may or may not change with the introduction of the pianoroll in Ardour 10.

Meanwhile you will likely have to iterate over the entire MIDI region and only pick notes inside the given time range :thinking:

Ardour 10 ?? No pianoroll for Ardour 9 ?

oops … just off by one :slight_smile:

1 Like

Heh, just the “two hard things in computer science” jumping up to bite us again.