-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPakettiLaunchApp.lua
637 lines (583 loc) · 23.7 KB
/
PakettiLaunchApp.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
_AUTO_RELOAD_DEBUG = true
local dialog = nil
local dialog_content = nil
local vb = renoise.ViewBuilder()
local app_paths = {}
local smart_folder_paths = {}
-- Function to browse for an app and update the corresponding field
function appSelectionBrowseForApp(index)
local file_extensions = {"*.*"}
local dialog_title = "Select an Application"
local selected_file = renoise.app():prompt_for_filename_to_read(file_extensions, dialog_title)
if selected_file ~= "" then
-- Detect the operating system
local os_name = os.platform()
if os_name == "WINDOWS" then
-- Replace backslashes with double backslashes for Windows paths
selected_file = string.gsub(selected_file, "\\", "\\\\")
end
preferences.AppSelection["AppSelection"..index].value = selected_file
if app_paths[index] then
app_paths[index].text = selected_file
end
renoise.app():show_status("Selected file: " .. selected_file)
else
renoise.app():show_status("No file selected")
end
end
-- Function to browse for a smart folder and update the corresponding field
function browseForSmartFolder(index)
local dialog_title = "Select a Smart Folder / Backup Folder"
local selected_folder = renoise.app():prompt_for_path(dialog_title)
if selected_folder ~= "" then
preferences.AppSelection["SmartFoldersApp"..index].value = selected_folder
if smart_folder_paths[index] then
smart_folder_paths[index].text = selected_folder
end
renoise.app():show_status("Selected folder: " .. selected_folder)
else
renoise.app():show_status("No folder selected")
end
end
-- Function to save selected sample to temp and open with the selected app
function saveSelectedSampleToTempAndOpen(app_path)
if renoise.song() == nil then return end
local song = renoise.song()
if song.selected_sample == nil or not song.selected_sample.sample_buffer.has_sample_data then
renoise.app():show_status("No sample data available.")
return
end
local temp_file_path = os.tmpname() .. ".wav"
song.selected_sample.sample_buffer:save_as(temp_file_path, "wav")
-- Detect the operating system
local os_name = os.platform()
local command
if os_name == "WINDOWS" then
command = 'start "" "' .. app_path .. '" "' .. temp_file_path .. '"'
elseif os_name == "MACINTOSH" then
command = 'open -a "' .. app_path .. '" "' .. temp_file_path .. '"'
else
command = 'exec "' .. app_path .. '" "' .. temp_file_path .. '" &'
end
os.execute(command)
renoise.app():show_status("Sample sent to " .. app_path)
end
-- Create the dialog UI
local function create_dialog_content(closeLA_dialog)
app_paths = {}
smart_folder_paths = {}
return vb:column{
margin=10,
--spacing=10,
width=900,
vb:row{vb:text{text="App Selection", font="bold", style="strong"}},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(1) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection1.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection1.value ~= "" and preferences.AppSelection.AppSelection1.value or "None"),
width=600,
font="bold"
}
app_paths[1] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(2) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection2.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection2.value ~= "" and preferences.AppSelection.AppSelection2.value or "None"),
width=600,
font="bold"
}
app_paths[2] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(3) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection3.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection3.value ~= "" and preferences.AppSelection.AppSelection3.value or "None"),
width=600,
font="bold"
}
app_paths[3] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(4) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection4.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection4.value ~= "" and preferences.AppSelection.AppSelection4.value or "None"),
width=600,
font="bold"
}
app_paths[4] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(5) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection5.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection5.value ~= "" and preferences.AppSelection.AppSelection5.value or "None"),
width=600,
font="bold"
}
app_paths[5] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() appSelectionBrowseForApp(6) end
},
vb:button{
text="Send Selected Sample to App",
notifier=function()
saveSelectedSampleToTempAndOpen(preferences.AppSelection.AppSelection6.value)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.AppSelection6.value ~= "" and preferences.AppSelection.AppSelection6.value or "None"),
width=600,
font="bold"
}
app_paths[6] = path
return path
end)()
},
vb:row{vb:text{text="Smart Folders / Backup Folders", font="bold", style="strong"}},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() browseForSmartFolder(1) end
},
vb:button{
text="Save Selected Sample to Folder",
notifier=function()
saveSampleToSmartFolder(1)
end,
width=200
},
vb:button{
text="Save All Samples to Folder",
notifier=function()
saveSamplesToSmartFolder(1)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.SmartFoldersApp1.value ~= "" and preferences.AppSelection.SmartFoldersApp1.value or "None"),
width=600,
font="bold"
}
smart_folder_paths[1] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() browseForSmartFolder(2) end
},
vb:button{
text="Save Selected Sample to Folder",
notifier=function()
saveSampleToSmartFolder(2)
end,
width=200
},
vb:button{
text="Save All Samples to Folder",
notifier=function()
saveSamplesToSmartFolder(2)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.SmartFoldersApp2.value ~= "" and preferences.AppSelection.SmartFoldersApp2.value or "None"),
width=600,
font="bold"
}
smart_folder_paths[2] = path
return path
end)()
},
vb:row{
spacing=10,
vb:button{
text="Browse",
notifier=function() browseForSmartFolder(3) end
},
vb:button{
text="Save Selected Sample to Folder",
notifier=function()
saveSampleToSmartFolder(3)
end,
width=200
},
vb:button{
text="Save All Samples to Folder",
notifier=function()
saveSamplesToSmartFolder(3)
end,
width=200
},
(function()
local path = vb:text{
text=(preferences.AppSelection.SmartFoldersApp3.value ~= "" and preferences.AppSelection.SmartFoldersApp3.value or "None"),
width=600,
font="bold"
}
smart_folder_paths[3] = path
return path
end)()
},
vb:button{
text="OK",
notifier=function()
appSelectionUpdateMenuEntries()
dialog:close()
dialog = nil
end
}
}
end
function my_appSelection_keyhandlerfunc(dialog,key)
local closer = preferences.pakettiDialogClose.value
if key.modifiers == "" and key.name == closer then
dialog:close()
return end
if key.name == "!" then
dialog:close()
-- renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
else
return key
end
end
-- Show the dialog
function show_app_selection_dialog()
if dialog and dialog.visible then
dialog:close()
dialog = nil
return
end
dialog = renoise.app():show_custom_dialog("App Selection & Smart Folders / Backup Folders", create_dialog_content(function()
dialog:close()
appSelectionUpdateMenuEntries()
end), my_appSelection_keyhandlerfunc)
end
--renoise.tool():add_menu_entry{name="Sample Editor:Paketti..:App Selection & Smart Folders",invoke=show_app_selection_dialog}
-- Add key bindings and MIDI mappings for AppSelection shortcuts
for i=1, 6 do
local app_path = preferences.AppSelection["AppSelection"..i].value
-- Only create entries if app_path exists and isn't empty
if app_path and app_path ~= "" then
renoise.tool():add_keybinding{name="Global:Paketti:Send Selected Sample to AppSelection" .. i,
invoke=function()
saveSelectedSampleToTempAndOpen(app_path)
end
}
renoise.tool():add_menu_entry{name="Sample Editor:Paketti..:Launch App..:Send Selected Sample to AppSelection" .. i,
invoke=function()
saveSelectedSampleToTempAndOpen(app_path)
end
}
renoise.tool():add_menu_entry{name="Main Menu:Tools:Paketti..:Launch App..:Send Selected Sample to AppSelection" .. i,
invoke=function()
saveSelectedSampleToTempAndOpen(app_path)
end
}
renoise.tool():add_midi_mapping{name="Paketti:Send Selected Sample to AppSelection" .. i,
invoke=function(message)
if message:is_trigger() then
saveSelectedSampleToTempAndOpen(app_path)
end
end
}
end
end
for i=1, 3 do
renoise.tool():add_keybinding{name="Global:Paketti:Save Sample to Smart/Backup Folder " .. i,invoke=function() saveSampleToSmartFolder(i) end }
renoise.tool():add_menu_entry{name="Sample Navigator:Paketti..:Save Sample to Smart/Backup Folder " .. i,invoke=function() saveSampleToSmartFolder(i) end }
renoise.tool():add_menu_entry{name="Sample Editor:Paketti..:Save..:Save Sample to Smart/Backup Folder " .. i,invoke=function() saveSampleToSmartFolder(i) end }
renoise.tool():add_midi_mapping{name="Paketti:Save Sample to Smart/Backup Folder " .. i,invoke=function(message) if message:is_trigger() then saveSampleToSmartFolder(i) end end}
end
for i=1, 3 do
renoise.tool():add_keybinding{name="Global:Paketti:Save All Samples to Smart/Backup Folder " .. i,invoke=function() saveSamplesToSmartFolder(i) end}
renoise.tool():add_menu_entry{name="Sample Navigator:Paketti..:Save All Samples to Smart/Backup Folder " .. i,invoke=function() saveSamplesToSmartFolder(i) end }
renoise.tool():add_menu_entry{name="Instrument Box:Paketti..:Save All Samples to Smart/Backup Folder " .. i,invoke=function() saveSamplesToSmartFolder(i) end }
renoise.tool():add_midi_mapping{name="Paketti:Save All Samples to Smart/Backup Folder " .. i,invoke=function(message)
if message:is_trigger() then saveSamplesToSmartFolder(i) end end}
end
----------------
-- Function to save selected sample to the specified Smart Folder
function saveSampleToSmartFolder(index)
local smart_folder_path = preferences.AppSelection["SmartFoldersApp"..index].value
if smart_folder_path == "" then
renoise.app():show_status("Please set the Smart Folder path for " .. index)
renoise.app():show_custom_dialog("Set Smart Folder Path", create_dialog_content())
return
end
local lsfvariable = nil
lsfvariable = os.tmpname("wav")
local path = smart_folder_path .. "/"
local s = renoise.song()
local instboxname = s.selected_instrument.name
if not s.selected_sample or not s.selected_sample.sample_buffer.has_sample_data then
renoise.app():show_status("No sample data available.")
return
end
local sample = s.selected_sample.sample_buffer
local file_name = instboxname .. ".wav"
if sample.bit_depth == 32 then
-- local temp_sample = sample:clone()
-- temp_sample.bit_depth = 24
-- temp_sample:save_as(path .. file_name, "wav")
sample:save_as(path .. file_name, "wav")
else
sample:save_as(path .. file_name, "wav")
end
renoise.app():show_status("Saved " .. file_name .. " to Smart Folder " .. path)
end
-- Function to save all samples to the specified Smart Folder
function saveSamplesToSmartFolder(index)
local smart_folder_path = preferences.AppSelection["SmartFoldersApp"..index].value
if smart_folder_path == "" then
renoise.app():show_status("Please set the Smart Folder path for " .. index)
renoise.app():show_custom_dialog("Set Smart Folder Path", create_dialog_content())
return
end
local s = renoise.song()
local path = smart_folder_path .. "/"
local saved_samples_count = 0
for i = 1, #s.instruments do
local instrument = s.instruments[i]
if instrument and #instrument.samples > 0 then
for j = 1, #instrument.samples do
local sample = instrument.samples[j].sample_buffer
if sample.has_sample_data then
local file_name = instrument.name .. "_" .. j .. ".wav"
if sample.bit_depth == 32 then
-- local temp_sample = sample:clone()
-- temp_sample.bit_depth = 24
-- temp_sample:save_as(path .. file_name, "wav")
sample:save_as(path .. file_name, "wav")
else
sample:save_as(path .. file_name, "wav")
end
saved_samples_count = saved_samples_count + 1
end
end
end
end
renoise.app():show_status("Saved " .. saved_samples_count .. " samples to Smart Folder " .. path)
-- Open the folder in system's file explorer
local os_name = os.platform()
if os_name == "WINDOWS" then
os.execute('explorer "' .. smart_folder_path .. '"')
elseif os_name == "MACINTOSH" then
os.execute('open "' .. smart_folder_path .. '"')
else -- Linux/Unix systems
os.execute('xdg-open "' .. smart_folder_path .. '"')
end
end
------
-- Table to keep track of added menu entries
local added_menu_entries = {}
-- Global function to launch the applications
function appSelectionLaunchApp(app_path)
local os_name = os.platform()
local command
if os_name == "WINDOWS" then
command = 'start "" "' .. app_path .. '"'
elseif os_name == "MACINTOSH" then
command = 'open -a "' .. app_path .. '"'
else
command = 'exec "' .. app_path .. '" &'
end
os.execute(command)
renoise.app():show_status("Launched app " .. app_path:match("([^/\\]+)%.app$"))
end
-- Function to remove existing menu entries
function appSelectionRemoveMenuEntries()
for _, entry in ipairs(added_menu_entries) do
if renoise.tool():has_menu_entry(entry) then
renoise.tool():remove_menu_entry(entry)
end
end
-- Clear the tracking table
added_menu_entries = {}
end
function appSelectionCreateMenuEntries()
local preferences = renoise.tool().preferences
local app_selections = {
preferences.AppSelection.AppSelection1 and preferences.AppSelection.AppSelection1.value or "",
preferences.AppSelection.AppSelection2 and preferences.AppSelection.AppSelection2.value or "",
preferences.AppSelection.AppSelection3 and preferences.AppSelection.AppSelection3.value or "",
preferences.AppSelection.AppSelection4 and preferences.AppSelection.AppSelection4.value or "",
preferences.AppSelection.AppSelection5 and preferences.AppSelection.AppSelection5.value or "",
preferences.AppSelection.AppSelection6 and preferences.AppSelection.AppSelection6.value or ""
}
local apps_present = false
-- Create menu entries for each app selection
for i, app_path in ipairs(app_selections) do
if app_path ~= "" then
apps_present = true
-- local app_name = app_path:match("([^/\\]+)%.app$")
local prefix = (i == 1) and "--" or "" -- Add prefix only for first item
local app_name = app_path:match("([^/\\]+)%.app$") or app_path:match("([^/\\]+)$")
local menu_entry_name = prefix .. "Instrument Box:Paketti..:Launch App..:Launch App "..i.." "..app_name
if not renoise.tool():has_menu_entry(menu_entry_name) then
renoise.tool():add_menu_entry{name=menu_entry_name,invoke=function() appSelectionLaunchApp(app_path) end}
table.insert(added_menu_entries, menu_entry_name)
end
menu_entry_name = prefix .. "Main Menu:Tools:Paketti..:Launch App..:Launch App "..i.." "..app_name
if not renoise.tool():has_menu_entry(menu_entry_name) then
renoise.tool():add_menu_entry{name=menu_entry_name,
invoke=function() appSelectionLaunchApp(app_path) end
}
table.insert(added_menu_entries, menu_entry_name)
end
menu_entry_name = prefix .."Sample Navigator:Paketti..:Launch App..:Launch App "..i.." "..app_name
if not renoise.tool():has_menu_entry(menu_entry_name) then
renoise.tool():add_menu_entry{name=menu_entry_name,
invoke=function() appSelectionLaunchApp(app_path) end
}
table.insert(added_menu_entries, menu_entry_name)
end
menu_entry_name = prefix .. "Sample Editor:Paketti..:Launch App..:Launch App "..i.." "..app_name
if not renoise.tool():has_menu_entry(menu_entry_name) then
renoise.tool():add_menu_entry{name=menu_entry_name,
invoke=function() appSelectionLaunchApp(app_path) end
}
table.insert(added_menu_entries, menu_entry_name)
end
end
end
-- If no app selections are set, show the app selection dialog
if not apps_present then
renoise.app():show_status("No apps have been configured in Paketti..:Launch App..:Configure Launch App Selection, cannot populate Menu.")
end
-- Add static menu entry for configuring app selections
local configure_entry_name="--Instrument Box:Paketti..:Launch App..:Configure Launch App Selection..."
if not renoise.tool():has_menu_entry(configure_entry_name) then
renoise.tool():add_menu_entry{name=configure_entry_name,
invoke=show_app_selection_dialog
}
table.insert(added_menu_entries, configure_entry_name)
end
configure_entry_name="--Main Menu:Tools:Paketti..:Launch App..:Configure Launch App Selection..."
if not renoise.tool():has_menu_entry(configure_entry_name) then
renoise.tool():add_menu_entry{name=configure_entry_name,
invoke=show_app_selection_dialog
}
table.insert(added_menu_entries, configure_entry_name)
end
configure_entry_name="--Sample Navigator:Paketti..:Launch App..:Configure Launch App Selection..."
if not renoise.tool():has_menu_entry(configure_entry_name) then
renoise.tool():add_menu_entry{name=configure_entry_name,
invoke=show_app_selection_dialog
}
table.insert(added_menu_entries, configure_entry_name)
end
configure_entry_name="--Sample Editor:Paketti..:Launch App..:Configure Launch App Selection..."
if not renoise.tool():has_menu_entry(configure_entry_name) then
renoise.tool():add_menu_entry{name=configure_entry_name,
invoke=show_app_selection_dialog
}
table.insert(added_menu_entries, configure_entry_name)
end
end
renoise.tool():add_keybinding{name="Global:Paketti:Configure Launch App Selection...",invoke=show_app_selection_dialog}
function appSelectionUpdateMenuEntries()
if renoise.song() == nil then return end
appSelectionRemoveMenuEntries()
appSelectionCreateMenuEntries()
end
-- Idle notifier function
local function handle_idle_notifier()
if renoise.song() then
appSelectionUpdateMenuEntries()
-- Remove this notifier to prevent repeated execution
renoise.tool().app_idle_observable:remove_notifier(handle_idle_notifier)
end
end
-- Ensure menu entries are created only after renoise.song() is initialized
local function handle_new_document()
if not renoise.tool().app_idle_observable:has_notifier(handle_idle_notifier) then
renoise.tool().app_idle_observable:add_notifier(handle_idle_notifier)
end
end
renoise.tool().app_new_document_observable:add_notifier(handle_new_document)
-- Ensure menu entries are created only after renoise.song() is initialized (initial load)
if not renoise.tool().app_idle_observable:has_notifier(handle_idle_notifier) then
renoise.tool().app_idle_observable:add_notifier(handle_idle_notifier)
end