use new action map API instead of ActionManager::get_action
[ardour.git] / scripts / _midi_rec_start.lua
1 ardour {
2         ["type"]    = "session",
3         name        = "MIDI Record Enable",
4         category    = "Example", -- "Utility"
5         license     = "MIT",
6         author      = "Ardour Lua Task Force",
7         description = [[An example script to start recording on note-on.]]
8 }
9
10 function factory ()
11         return function (n_samples)
12                 if Session:actively_recording() then return end -- when recording already, do nothing
13                 -- iterate over all MIDI ports
14                 _, t = Session:engine ():get_ports (ARDOUR.DataType.midi (), ARDOUR.PortList ())
15                 for p in t[2]:iter () do
16                         -- skip output ports
17                         if not p:receives_input () then goto next end
18                         local midiport = p:to_midiport ()
19                         -- and skip async event ports
20                         if midiport:isnil () then goto next end
21                         local mb = midiport:get_midi_buffer (n_samples) -- get the midi-data buffers
22                         local events = mb:table () -- copy event list into lua table
23                         for _,e in pairs (events) do -- iterate over all events in the midi-buffer
24                                 if (e:buffer():array()[1] & 0xf0) == 0x90 then -- note on
25                                         Session:maybe_enable_record (true) -- global record-enable from rt-context
26                                         -- maybe-enable may fail if there are no tracks or step-entry is active
27                                         -- roll transport if record-enable suceeded:
28                                         if ARDOUR.Session.RecordState.Enabled == Session:record_status() then
29                                                 Session:request_transport_speed (1.0, true) -- ...and go.
30                                         end
31                                         return
32                                 end
33                         end
34                         ::next::
35                 end
36         end
37 end