-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PakettiLoadDevices.lua
496 lines (425 loc) · 15.6 KB
/
PakettiLoadDevices.lua
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
local vb
local checkboxes = {}
local deviceReadableNames = {}
local addedKeyBindings = {}
local current_device_type = "Native"
local device_types = {"Native", "VST", "VST3", "AudioUnit", "LADSPA", "DSSI"}
local custom_dialog
local dialog_content_view
local device_list_view
local current_device_list_content = nil
local DEVICES_PER_COLUMN = 39
local random_select_percentage = 0
-- Function to check if a keybinding exists
function doesKeybindingExist(keyBindingName)
for _, binding in ipairs(renoise.tool().keybindings) do
if binding.name == keyBindingName then
return true
end
end
return false
end
function saveDeviceToPreferences(entryName, path, device_type)
if not entryName or not path or not device_type then
print("Error: Cannot save to preferences. Missing data.")
return
end
local loaders = preferences.PakettiDeviceLoaders
-- Check for duplicates
for i = 1, #loaders do
local device = loaders:property(i)
if device.name.value == entryName and device.device_type.value == device_type then
print("Device entry already exists. Skipping addition for:", entryName)
return
end
end
-- Add new device entry
local newDevice = create_device_entry(entryName, path, device_type)
loaders:insert(#loaders + 1, newDevice)
print(string.format("Saved Device '%s' to preferences.", entryName))
end
-- Load from Preferences
function loadDeviceFromPreferences()
if not preferences.PakettiDeviceLoaders then
print("No PakettiDeviceLoaders found in preferences.")
return
end
local loaders = preferences.PakettiDeviceLoaders
for i = 1, #loaders do
local device = loaders:property(i)
local device_name = device.name.value
local path = device.path.value
local device_type = device.device_type.value
-- Generate keybinding and midi mapping names
local entryName = device_name
local keyBindingName = "Global:Paketti:Load Device (" .. device_type .. ") " .. entryName
local midiMappingName = "Paketti:Load Device (" .. device_type .. ") " .. entryName
-- Re-add keybinding and midi mapping
local success, err = pcall(function()
renoise.tool():add_keybinding{
name = keyBindingName,
invoke = function()
if device_type == "Native" then
loadnative(path)
else
loadvst(path)
end
end
}
renoise.tool():add_midi_mapping{
name = midiMappingName,
invoke = function(message)
if message:is_trigger() then
if device_type == "Native" then
loadnative(path)
else
loadvst(path)
end
end
end
}
end)
if not success then
print("Could not add keybinding or midi mapping for " .. device_name .. ": " .. err)
else
addedKeyBindings[keyBindingName] = true
end
end
end
function isAnyDeviceSelected()
for _, cb_info in ipairs(checkboxes) do
if cb_info.checkbox.value then
return true
end
end
return false
end
function loadSelectedDevices()
if not isAnyDeviceSelected() then
renoise.app():show_status("Nothing was selected, doing nothing.")
return false
end
local track_index = renoise.song().selected_track_index
for _, cb_info in ipairs(checkboxes) do
if cb_info.checkbox.value then
local pluginPath = cb_info.path
if current_device_type == "Native" then
loadnative(pluginPath)
else
loadvst(pluginPath)
end
end
end
return true
end
function addDeviceAsShortcut()
if not isAnyDeviceSelected() then
renoise.app():show_status("Nothing was selected, doing nothing.")
return
end
for _, cb_info in ipairs(checkboxes) do
if cb_info.checkbox.value then
local device_type = current_device_type
local path = cb_info.path
local device_name = cb_info.name
local entryName = device_name
local keyBindingName = "Global:Paketti:Load Device (" .. device_type .. ") " .. entryName
local midiMappingName = "Paketti:Load Device (" .. device_type .. ") " .. entryName
if not addedKeyBindings[keyBindingName] then
print("Adding shortcut for: " .. device_name)
local success, err = pcall(function()
renoise.tool():add_keybinding{
name = keyBindingName,
invoke = function()
if device_type == "Native" then
loadnative(path)
else
loadvst(path)
end
end
}
renoise.tool():add_midi_mapping{
name = midiMappingName,
invoke = function(message)
if message:is_trigger() then
if device_type == "Native" then
loadnative(path)
else
loadvst(path)
end
end
end
}
end)
if success then
addedKeyBindings[keyBindingName] = true
saveDeviceToPreferences(entryName, path, device_type)
else
print("Could not add keybinding for " .. device_name .. ". It might already exist.")
end
else
print("Keybinding for " .. device_name .. " already added.")
end
end
end
renoise.app():show_status("Devices added. Open Settings -> Keys, search for 'Load Device' or MIDI Mappings and search for 'Load Device'")
end
function resetSelection()
for _, cb_info in ipairs(checkboxes) do
cb_info.checkbox.value = false
end
end
function updateRandomSelection()
if #checkboxes == 0 then
renoise.app():show_status("Nothing to randomize from.")
return
end
resetSelection()
local numDevices = #checkboxes
local percentage = random_select_percentage
local numSelections = math.floor((percentage / 100) * numDevices + 0.5)
local percentage_text_view = vb.views["random_percentage_text"]
if numSelections == 0 then
percentage_text_view.text = "None"
return
elseif numSelections >= numDevices then
percentage_text_view.text = "All"
for _, cb_info in ipairs(checkboxes) do
cb_info.checkbox.value = true
end
return
else
percentage_text_view.text = tostring(math.floor(percentage + 0.5)) .. "%"
end
local indices = {}
for i = 1, numDevices do
indices[i] = i
end
for i = numDevices, 2, -1 do
local j = math.random(1, i)
indices[i], indices[j] = indices[j], indices[i]
end
for i = 1, numSelections do
local idx = indices[i]
checkboxes[idx].checkbox.value = true
end
end
function createDeviceList(plugins, title)
if #plugins == 0 then
return vb:column{vb:text{text="No Devices found for this type.", font="italic", height=20}}
end
-- Determine number of columns based on DEVICES_PER_COLUMN
local num_devices = #plugins
local devices_per_column = DEVICES_PER_COLUMN
local num_columns = math.ceil(num_devices / devices_per_column)
local columns = {}
for i = 1, num_columns do
columns[i] = vb:column{spacing=2}
end
-- Split devices into columns sequentially
local device_index = 1
for col = 1, num_columns do
for row = 1, devices_per_column do
if device_index > num_devices then break end
local plugin = plugins[device_index]
local checkbox_id = "checkbox_" .. title .. "_" .. tostring(device_index) .. "_" .. tostring(math.random(1000000))
local checkbox = vb:checkbox{value=false, id=checkbox_id}
checkboxes[#checkboxes + 1] = {checkbox=checkbox, path=plugin.path, name=plugin.name}
local plugin_row = vb:row{spacing=4,checkbox,vb:text{text=plugin.name}}
columns[col]:add_child(plugin_row)
device_index = device_index + 1
end
end
local column_container = vb:row{spacing=20}
for _, column in ipairs(columns) do
column_container:add_child(column)
end
return vb:column{
vb:horizontal_aligner{mode="center",column_container}}
end
function updateDeviceList()
checkboxes = {}
deviceReadableNames = {}
local track_index = renoise.song().selected_track_index
local available_devices = renoise.song().tracks[track_index].available_devices
local available_device_infos = renoise.song().tracks[track_index].available_device_infos
local pluginReadableNames = {}
for i, plugin_info in ipairs(available_device_infos) do
pluginReadableNames[available_devices[i]] = plugin_info.short_name
end
local device_list_content
if current_device_type == "Native" then
-- Collect Native devices
local native_devices = {}
local hidden_devices = {
{name = "(Hidden) Chorus", path = "Audio/Effects/Native/Chorus"},
{name = "(Hidden) Comb Filter", path = "Audio/Effects/Native/Comb Filter"},
{name = "(Hidden) Distortion", path = "Audio/Effects/Native/Distortion"},
{name = "(Hidden) Filter", path = "Audio/Effects/Native/Filter"},
{name = "(Hidden) Filter 2", path = "Audio/Effects/Native/Filter 2"},
{name = "(Hidden) Filter 3", path = "Audio/Effects/Native/Filter 3"},
{name = "(Hidden) Flanger", path = "Audio/Effects/Native/Flanger"},
{name = "(Hidden) Gate", path = "Audio/Effects/Native/Gate"},
{name = "(Hidden) LofiMat", path = "Audio/Effects/Native/LofiMat"},
{name = "(Hidden) mpReverb", path = "Audio/Effects/Native/mpReverb"},
{name = "(Hidden) Phaser", path = "Audio/Effects/Native/Phaser"},
{name = "(Hidden) RingMod", path = "Audio/Effects/Native/RingMod"},
{name = "(Hidden) Scream Filter", path = "Audio/Effects/Native/Scream Filter"},
{name = "(Hidden) Shaper", path = "Audio/Effects/Native/Shaper"},
{name = "(Hidden) Stutter", path = "Audio/Effects/Native/Stutter"}}
for i, device_path in ipairs(available_devices) do
if device_path:find("Native/") then
local device_name = device_path:match("([^/]+)$")
table.insert(native_devices, {name = device_name, path = device_path})
end
end
table.sort(native_devices, function(a, b)
return a.name:lower() < b.name:lower()
end)
for _, hidden_device in ipairs(hidden_devices) do
table.insert(native_devices, hidden_device)
end
device_list_content = createDeviceList(native_devices, "Native Devices")
elseif current_device_type == "VST" then
local vst_devices = {}
for i, device_path in ipairs(available_devices) do
if device_path:find("VST") and not device_path:find("VST3") then
local device_name = pluginReadableNames[device_path] or device_path:match("([^/]+)$")
table.insert(vst_devices, {name = device_name, path = device_path})
end
end
device_list_content = createDeviceList(vst_devices, "VST Devices")
elseif current_device_type == "VST3" then
local vst3_devices = {}
for i, device_path in ipairs(available_devices) do
if device_path:find("VST3") then
local device_name = pluginReadableNames[device_path] or device_path:match("([^/]+)$")
table.insert(vst3_devices, {name = device_name, path = device_path})
end
end
table.sort(vst3_devices, function(a, b)
return a.name:lower() < b.name:lower()
end)
device_list_content = createDeviceList(vst3_devices, "VST3 Devices")
elseif current_device_type == "AudioUnit" then
local au_devices = {}
for i, device_path in ipairs(available_devices) do
if device_path:find("AU") then
local device_name = pluginReadableNames[device_path] or device_path:match("([^/]+)$")
table.insert(au_devices, {name = device_name, path = device_path})
end
end
table.sort(au_devices, function(a, b)
return a.name:lower() < b.name:lower()
end)
device_list_content = createDeviceList(au_devices, "AudioUnit Devices")
elseif current_device_type == "LADSPA" then
local ladspa_devices = {}
for i, device_path in ipairs(available_devices) do
if device_path:find("LADSPA") then
local device_name = pluginReadableNames[device_path] or device_path:match("([^/]+)$")
device_name = device_name:match("([^:]+)$")
device_name = device_name:match("([^/]+)$")
table.insert(ladspa_devices, {name = device_name, path = device_path})
end
end
-- Sort the LADSPA devices by name
table.sort(ladspa_devices, function(a, b)
return a.name:lower() < b.name:lower()
end)
device_list_content = createDeviceList(ladspa_devices, "LADSPA Devices")
elseif current_device_type == "DSSI" then
local dssi_devices = {}
for i, device_path in ipairs(available_devices) do
if device_path:find("DSSI") then
local device_name = pluginReadableNames[device_path] or device_path:match("([^/]+)$")
device_name = device_name:match("([^:]+)$") -- Extract after the last colon
table.insert(dssi_devices, {name = device_name, path = device_path})
end
end
table.sort(dssi_devices, function(a, b)
return a.name:lower() < b.name:lower()
end)
device_list_content = createDeviceList(dssi_devices, "DSSI Devices")
end
if current_device_list_content then
device_list_view:remove_child(current_device_list_content)
end
device_list_view:add_child(device_list_content)
current_device_list_content = device_list_content
end
function showDeviceListDialog()
current_device_list_content = nil
vb = renoise.ViewBuilder()
checkboxes = {}
local track_index = renoise.song().selected_track_index
local dropdown = vb:popup{
items = device_types,
value = 1,
notifier = function(index)
current_device_type = device_types[index]
updateDeviceList()
end}
local random_selection_controls = vb:row{
vb:text{text = "Random Select:", width = 80, style="strong",font="bold"},
vb:slider{
id = "random_select_slider",
min = 0,
max = 100,
value = 0,
width = 200,
notifier = function(value)
random_select_percentage = value
updateRandomSelection()
end},
vb:text{id="random_percentage_text",text="None",width=40,
align="center"},
vb:button{text="All",width=20,
notifier = function()
for _, cb_info in ipairs(checkboxes) do
cb_info.checkbox.value = true
end
vb.views["random_select_slider"].value = 100
vb.views["random_percentage_text"].text = "All"
end},
vb:button{text="None",width=20,
notifier = function()
resetSelection()
vb.views["random_select_slider"].value = 0
vb.views["random_percentage_text"].text = "None"
end}}
local button_height = renoise.ViewBuilder.DEFAULT_DIALOG_BUTTON_HEIGHT
local action_buttons = vb:column{
vb:horizontal_aligner{width="100%",
vb:button{text="Load Device(s)",width=60,
notifier = function()
if loadSelectedDevices() then
renoise.app():show_status("Devices loaded.")
end
end
},
vb:button{text="Add Device(s) as Shortcut(s) & MidiMappings",width=140,
notifier = addDeviceAsShortcut},
vb:button{text="Cancel",width=30,
notifier = function() custom_dialog:close() end}}}
device_list_view = vb:column{}
dialog_content_view = vb:column{margin = 10,spacing = 5,device_list_view,}
-- Wrap in a column to include the dropdown
local dialog_content = vb:column{
vb:horizontal_aligner{
vb:text{text = "Device Type: ", font="bold",style="strong"},
dropdown,action_buttons,random_selection_controls},dialog_content_view}
custom_dialog = renoise.app():show_custom_dialog("Load Device(s)", dialog_content, my_Devicekeyhandler_func)
updateDeviceList()
end
function my_Devicekeyhandler_func(custom_dialog, key)
local closer = preferences.pakettiDialogClose.value
if key.modifiers == "" and key.name == closer then
custom_dialog:close()
custom_dialog = nil
return nil
else
return key
end
end
loadDeviceFromPreferences()