-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrd8_midi_splitter.lua
More file actions
224 lines (168 loc) · 8.89 KB
/
rd8_midi_splitter.lua
File metadata and controls
224 lines (168 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
ardour {
["type"] = "EditorAction",
name = "RD-8 MIDI Splitter",
license = "MIT",
author = "dotted_pi",
description = [[This script exports user-selected RD-8 instruments and filters selected regions of an 'RD8_MIDI_Master' track into a unique MIDI track per instrument.]]
}
function factory () return function ()
------------------------------------------------------------------------------------
--set up the RD-8 class containing all instruments with their midi notes and names--
------------------------------------------------------------------------------------
local rd8 = {
{inst = "kick", note = 36, name = "Kick"},
{inst = "snare", note = 40, name = "Snare"},
{inst = "low_ct", note = 45, name = "Low Conga/Tom"},
{inst = "mid_ct", note = 47, name = "Mid Conga/Tom"},
{inst = "high_ct", note = 50, name = "High Conga/Tom"},
{inst = "cl_rs", note = 37, name = "Claves/Rim Shot"},
{inst = "clap", note = 39, name = "Maracas/Claps"},
{inst = "cowbell", note = 56, name = "Cowbell"},
{inst = "cymbal", note = 51, name = "Cymbal"},
{inst = "openhat", note = 46, name = "Open Hat"},
{inst = "closedhat", note = 42, name = "Closed Hat"},
}
--------------------------------------------------------------------------------------
--search for "RD8_MIDI_Master" track and select the first instance if multiple exist--
--------------------------------------------------------------------------------------
local rd8_midi_master_found = false
local rd8_track_name = "RD8_MIDI_Master" --the name of the MIDI Master track to match
local rd8_master_region_ids = {}
local n_rd8_regions = 0
for track in Session:get_tracks():iter() do
if track:data_type():to_string() == "midi" then --iterate over all MIDI tracks in the session
if (not rd8_midi_master_found) and (string.find(track:name(), rd8_track_name)) then --check if valid RD8_MIDI_Master exists
rd8_midi_master_found = true
rd8["mastertrack"] = track:to_track():to_midi_track() --select the first valid option
for reg in rd8["mastertrack"]:playlist():region_list():iter() do
rd8_master_region_ids[reg:to_stateful():id():to_s()] = true
n_rd8_regions = n_rd8_regions + 1
end
end
--check if the MIDI track is an instrument MIDI track as created by the script (and get its ID)
for rd8_inst_id, rd8_inst in ipairs(rd8) do
if string.find(track:name(),"RD8_MIDI_"..rd8_inst["inst"]) then
rd8[rd8_inst_id]["MIDI_track_found"] = true
rd8[rd8_inst_id]["MIDI_track_id"] = track:to_stateful():id()
end
end
end
end
if not rd8_midi_master_found then --return if no track is found
LuaDialog.Message ("Setup Error", "No valid 'RD8_MIDI_Master' track could be found!", LuaDialog.MessageType.Error, LuaDialog.ButtonType.Close):run()
goto script_end
end
--------------------------------------------------------------------------
--get number (and ID) of selected regions of the 'RD8_MIDI_Master' track--
--------------------------------------------------------------------------
local sel_region_ids = {}
local filtered_region_ids = {}
local dialog_options = {}
local continue_on_full_track = false
for r in Editor:get_selection().regions:regionlist():iter() do
sel_region_ids[r:to_stateful():id():to_s()] = true
end
for reg_id in pairs(rd8_master_region_ids) do
filtered_region_ids[reg_id] = sel_region_ids[reg_id]
end
local n_valid_master_regions = 0
for _ in pairs(filtered_region_ids) do
n_valid_master_regions = n_valid_master_regions + 1
end
--handle the case of no valid selected regions
if (n_valid_master_regions == 0) and not (n_rd8_regions == 0) then
table.insert(dialog_options, { type = "label", title = "Run RD-8 MIDI Splitter for all regions of the 'RD8_Master_Track'? \nThis will delete all existing regions for selected instruments." })
local od = LuaDialog.Dialog("No valid region(s) selected!", dialog_options)
local ok = od:run()
if (ok) then
continue_on_full_track = true
else
goto script_end
end
end
-------------------------------------------
--create setup dialog and save user input--
-------------------------------------------
local dialog_options = {} --reuse variable
table.insert(dialog_options, { type = "heading", title = "Select instruments to create their MIDI track:" })
for _, rd8_inst in ipairs(rd8) do
table.insert(dialog_options, { type = "checkbox", key = "sel_"..rd8_inst["inst"], default = false, title = rd8_inst["name"] })
end
table.insert(dialog_options,{ type = "heading", title = "Further options:" })
table.insert(dialog_options,{ type = "checkbox", key = "auto_connect_to_rd8", default = false, title = "Auto-connect new track outputs to the RD-8"})
table.insert(dialog_options,{ type = "checkbox", key = "create_audio_tracks", default = false, title = "Create audio tracks (Mono) per instrument" })
local od = LuaDialog.Dialog("RD-8 MIDI Splitter Setup", dialog_options)
local rv = od:run()
if not (rv) then goto script_end end
----------------------------------------------------
--check if RD-8 is connected for MIDI auto-connect--
----------------------------------------------------
if rv["auto_connect_to_rd8"] then
local _, t = Session:engine():get_backend_ports("", ARDOUR.DataType("midi"), ARDOUR.PortFlags.IsInput | ARDOUR.PortFlags.IsPhysical, C.StringVector ())
for _, p in pairs(t[4]:table()) do
if Session:engine():get_pretty_name_by_name(p) == "RD-8" then
rd8["port"] = p
rd8["found"] = true
break
end
end
end
if (not rd8["found"]) and rv["auto_connect_to_rd8"] then
LuaDialog.Message("RD-8 disconnected?", "Cannot auto-connect new tracks!", LuaDialog.MessageType.Warning, LuaDialog.ButtonType.Close):run()
end
--------------------------------------------
--main loop (through selected instruments)--
--------------------------------------------
for rd8_inst_id, rd8_inst in ipairs(rd8) do
if rv["sel_"..rd8_inst["inst"]] then
--if auto-connect is enabled and RD-8 found, connect tracks to RD-8 MIDI input
if rd8_found then
Session:engine():connect("ardour:RD8_MIDI_"..rd8_inst["inst"].."/midi_out 1", rd8["port"])
end
--MIDI track creation if it does not exist
if not rd8_inst["MIDI_track_found"] then
Session:new_midi_track(ARDOUR.ChanCount(ARDOUR.DataType("midi"), 1), ARDOUR.ChanCount(ARDOUR.DataType("midi"), 1), false, ARDOUR.PluginInfo(), nil, nil, 1, "RD8_MIDI_"..rd8_inst["inst"], ARDOUR.PresentationInfo.max_order, ARDOUR.TrackMode.Normal)
rd8_inst["MIDI_track_id"] = Session:route_by_name("RD8_MIDI_"..rd8_inst["inst"]):to_stateful():id()
end
--audio track creation if enabled
if rv["create_audio_tracks"] then
Session:new_audio_track(1,2,nil,1,"RD8_"..rd8_inst["inst"],ARDOUR.PresentationInfo.max_order,ARDOUR.TrackMode.Normal)
end
--get the MIDI track for the current instrument
local cur_track = Session:route_by_id(rd8_inst["MIDI_track_id"]):to_track()
for _, region in pairs(rd8["mastertrack"]:playlist():region_list():table()) do --loop over valid regions
if continue_on_full_track or ((not continue_on_full_track) and filtered_region_ids[region:to_stateful():id():to_s()]) then
local new_region = ARDOUR.RegionFactory.clone_region(region, true, true):to_midiregion()
local midi_model = region:to_midiregion():midi_source(0):model()
local midi_command = midi_model:new_note_diff_command("Write MIDI Events")
local cur_model = new_region:midi_source(0):model()
local cur_command = cur_model:new_note_diff_command("Filter MIDI Events")
--filter notes per region and copy them if they belong to the current instrument
for note in ARDOUR.LuaAPI.note_list (midi_model):iter() do
if note:note() == rd8_inst["note"] then
local filtered_note = ARDOUR.LuaAPI.new_noteptr(rd8_inst_id, note:time(), note:length(), note:note(), note:velocity()) --separate midi channel per instrument per default
cur_command:add(filtered_note)
end
cur_command:remove(note)
end
cur_track:playlist():add_region(new_region, region:position(), 1, false, 0, 0, false) --add new_region to created instrument track
midi_model:apply_command(Session, midi_command)
cur_model:apply_command(Session, cur_command)
end
end
end
end
collectgarbage()
::script_end::
end end
---------------------------------
--define an icon for the script--
---------------------------------
function icon (params) return function (ctx, width, height, fg)
ctx:set_source_rgba(0.99, 0.41, 0.12,1) --"RD-8" fontcolor on the device
local txt = Cairo.PangoLayout(ctx, "ArdourMono " .. math.ceil(math.min(width, height) * 0.3+2) .. "px")
txt:set_text("RD-8\nMIDI")
local tw, th = txt:get_pixel_size()
ctx:move_to(0.5 * (width - tw)+1, 0.5 * (height - th))
txt:show_in_cairo_context(ctx)
end end