-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PakettiLoadDevices.lua
538 lines (465 loc) · 16.7 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
local vb
local checkboxes = {}
local deviceReadableNames = {}
local addedKeyBindings = {}
local preferencesFile = renoise.tool().bundle_path .. "preferences_deviceLoaders.xml"
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
-- Initialize Preferences File
function initializePreferencesFile()
local file, err = io.open(preferencesFile, "r")
if not file then
file, err = io.open(preferencesFile, "w")
if not file then
print("Error creating preferences file: " .. err)
return
end
file:write("<preferences_deviceLoaders>\n</preferences_deviceLoaders>\n")
file:close()
else
file:close()
end
end
initializePreferencesFile()
-- 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
-- Save to Preferences File
function saveToPreferencesFile(keyBindingName, midiMappingName, path)
local file, err = io.open(preferencesFile, "a")
if not file then
print("Error opening preferences file: " .. err)
return
end
local keybindingEntry = string.format(
'<KeyBinding name="%s">\n <Path>%s</Path>\n</KeyBinding>\n',
keyBindingName, path
)
local midiMappingEntry = string.format(
'<MIDIMapping name="%s">\n <Path>%s</Path>\n</MIDIMapping>\n',
midiMappingName, path
)
file:write(keybindingEntry)
file:write(midiMappingEntry)
file:close()
end
-- Load from Preferences File
function loadFromPreferencesFile()
local file, err = io.open(preferencesFile, "r")
if not file then
print("Error opening preferences file: " .. err)
return
end
local entries = {}
local current_entry = nil
for line in file:lines() do
local keybinding_start = line:match('<KeyBinding name="(.-)">')
if keybinding_start then
current_entry = {type = "KeyBinding", name = keybinding_start}
end
local midimapping_start = line:match('<MIDIMapping name="(.-)">')
if midimapping_start then
current_entry = {type = "MIDIMapping", name = midimapping_start}
end
local path_line = line:match('<Path>(.-)</Path>')
if path_line and current_entry then
current_entry.path = path_line
table.insert(entries, current_entry)
current_entry = nil
end
end
file:close()
for _, entry in ipairs(entries) do
local device_type = entry.name:match("Load Device %((.-)%)")
local path = entry.path
if entry.type == "KeyBinding" then
-- Re-add keybinding
local success, err = pcall(function()
local device_type_copy = device_type
local path_copy = path
renoise.tool():add_keybinding{
name = entry.name,
invoke = function()
if device_type_copy == "Native" then
loadnative(path_copy)
else
loadvst(path_copy)
end
end
}
end)
if not success then
print("Could not add keybinding for " .. entry.name .. ": " .. err)
end
elseif entry.type == "MIDIMapping" then
-- Re-add midi mapping
local success, err = pcall(function()
local device_type_copy = device_type
local path_copy = path
renoise.tool():add_midi_mapping{
name = entry.name,
invoke = function(message)
if message:is_trigger() then
if device_type_copy == "Native" then
loadnative(path_copy)
else
loadvst(path_copy)
end
end
end
}
end)
if not success then
print("Could not add midi mapping for " .. entry.name .. ": " .. err)
end
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
print("Loading Device:", pluginPath)
if current_device_type == "Native" then
loadnative(pluginPath)
else
loadvst(pluginPath)
end
end
end
return true
end
function addAsShortcut()
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 keyBindingName = "Global:Paketti:Load Device (" .. current_device_type .. ") " .. cb_info.name
local midiMappingName = "Track Devices:Paketti:Load Device (" .. current_device_type .. ") " .. cb_info.name
local device_type = current_device_type
local path = cb_info.path
if not addedKeyBindings[keyBindingName] then
print("Adding shortcut for: " .. cb_info.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
saveToPreferencesFile(keyBindingName, midiMappingName, cb_info.path)
else
print("Could not add keybinding for " .. cb_info.name .. ". It might already exist.")
end
else
print("Keybinding for " .. cb_info.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 = addAsShortcut},
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
loadFromPreferencesFile()