Update LuaDialog scripts
[ardour.git] / scripts / midi_cc_to_automation.lua
1 ardour { ["type"] = "EditorAction", name = "MIDI CC to Plugin Automation",
2         license     = "MIT",
3         author      = "Ardour Team",
4         description = [[Parse a given MIDI control changes (CC) from all selected MIDI regions and convert them into plugin parameter automation]]
5 }
6
7 function factory () return function ()
8         -- find target parameters
9         local targets = {}
10         local have_entries = false
11         for r in Session:get_routes():iter() do -- for every track/bus
12                 local i = 0
13                 while 1 do -- iterate over all plugins on the route
14                         local proc = r:nth_plugin (i)
15                         if proc:isnil () then break end
16                         local plug = proc:to_insert ():plugin (0)
17                         local n = 0
18                         for j = 0, plug:parameter_count () - 1 do -- iterate over all plugin parameters
19                                 if plug:parameter_is_control(j) then
20                                         if plug:parameter_is_input(j) then
21                                                 local nn = n
22                                                 if not targets [r:name ()] then targets [r:name ()] = {} end
23                                                 if not targets [r:name ()][proc:display_name ()] then targets [r:name ()][proc:display_name ()] = {} end
24                                                 targets [r:name ()][proc:display_name ()][plug:parameter_label(j)] = function () return {["p"] = proc, ["n"] = nn} end
25                                                 have_entries = true
26                                         end
27                                         n = n + 1
28                                 end
29                         end
30                         i = i + 1
31                 end
32         end
33
34         -- bail out if there are no parameters
35         if not have_entries then
36                 LuaDialog.Message ("CC to Plugin Automation", "No Plugins found", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
37                 collectgarbage ()
38                 return
39         end
40
41         -- create a dialog, ask user which MIDI-CC to map and to what parameter
42         local dialog_options = {
43                 { type = "heading", title = "MIDI CC Source", align = "left" },
44                 { type = "number", key = "channel", title = "Channel",  min = 1, max = 16, step = 1, digits = 0 },
45                 { type = "number", key = "ccparam", title = "CC Parameter",  min = 0, max = 127, step = 1, digits = 0 },
46                 { type = "heading", title = "Target Track and Plugin", align = "left"},
47                 { type = "dropdown", key = "param", title = "Target Parameter", values = targets }
48         }
49         local od = LuaDialog.Dialog ("Select Taget", dialog_options)
50         local rv = od:run()
51
52         if not rv then
53                 od = nil collectgarbage ()
54                 return
55         end
56
57         -- parse user response
58
59         local midi_channel = rv["channel"] - 1
60         local cc_param = rv["ccparam"]
61         local pp = rv["param"]()
62         local al, _, pd = ARDOUR.LuaAPI.plugin_automation (pp["p"], pp["n"])
63         od = nil collectgarbage ()
64         assert (al)
65         assert (midi_channel >= 0 and midi_channel < 16)
66         assert (cc_param >= 0 and cc_param < 128)
67
68         -- all systems go
69         local add_undo = false
70         Session:begin_reversible_command ("CC to Automation")
71         local before = al:get_state()
72         al:clear_list ()
73
74         -- for all selected MIDI regions
75         local sel = Editor:get_selection ()
76         for r in sel.regions:regionlist ():iter () do
77                 local mr = r:to_midiregion ()
78                 if mr:isnil () then goto next end
79
80                 local bfc = ARDOUR.DoubleBeatsFramesConverter (Session:tempo_map (), r:position ())
81                 local ec = mr:control (Evoral.Parameter (ARDOUR.AutomationType.MidiCCAutomation, midi_channel, cc_param), false)
82                 if ec:isnil () then goto next end
83                 if ec:list ():events ():size() == 0 then goto next end
84
85                 for av in ec:list ():events ():iter () do
86                         local val = pd.lower + (pd.upper - pd.lower) * av.value / 127
87                         al:add (bfc:to (av.when), val, false, true)
88                         add_undo = true
89                 end
90                 ::next::
91         end
92
93         -- save undo
94         if add_undo then
95                 local after = al:get_state()
96                 Session:add_command (al:memento_command(before, after))
97                 Session:commit_reversible_command (nil)
98         else
99                 Session:abort_reversible_command ()
100                 LuaDialog.Message ("CC to Plugin Automation", "No data was converted. Was a MIDI-region selected?", LuaDialog.MessageType.Info, LuaDialog.ButtonType.Close):run()
101                 collectgarbage ()
102         end
103 end end
104
105
106 function icon (params) return function (ctx, width, height, fg)
107         local txt = Cairo.PangoLayout (ctx, "ArdourMono ".. math.ceil(width * .45) .. "px")
108         txt:set_text ("CC\nPA")
109         local tw, th = txt:get_pixel_size ()
110         ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (fg))
111         ctx:move_to (.5 * (width - tw), .5 * (height - th))
112         txt:show_in_cairo_context (ctx)
113 end end