-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPakettiOctaMEDSuite.lua
601 lines (508 loc) · 22.2 KB
/
PakettiOctaMEDSuite.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
local vb = renoise.ViewBuilder()
local dialog = nil
local set_to_selected_instrument = preferences.OctaMEDPickPutSlots.SetSelectedInstrument.value or false
local use_edit_step_for_put = preferences.OctaMEDPickPutSlots.UseEditStep.value or false
local randomize_enabled = preferences.OctaMEDPickPutSlots.RandomizeEnabled.value or false
local randomize_percentage = preferences.OctaMEDPickPutSlots.RandomizePercentage.value or 100
-- Function to update the textfield display for empty slots
local function check_and_update_slot_display(slot_index)
local textfield_value = vb.views["slot_display_"..string.format("%02d", slot_index)].text
-- If the textfield only contains ' || ' or is empty, set it to "Slot is Empty"
if textfield_value == " || " or textfield_value == "" then
vb.views["slot_display_"..string.format("%02d", slot_index)].text = "Slot " .. string.format("%02d", slot_index) .. ": Empty"
end
end
-- Function to save the picked slot data to preferences
local function save_slot_to_preferences(slot_index)
local slot_key = "Slot" .. string.format("%02d", slot_index)
local slot_text = vb.views["slot_display_"..string.format("%02d", slot_index)].text
-- Ensure slot data is properly saved if the slot is not empty
if slot_text ~= "Slot " .. string.format("%02d", slot_index) .. ": Empty" and slot_text ~= "" then
print("Saving Slot", slot_index, "to preferences:", slot_text)
preferences.OctaMEDPickPutSlots[slot_key].value = slot_text
end
-- Save the preferences document
renoise.tool().preferences:save_as("preferences.xml")
end
-- Helper function to split a string by a given delimiter
local function string_split(input_str, delimiter)
local result = {}
for match in (input_str .. delimiter):gmatch("(.-)" .. delimiter) do
table.insert(result, match)
end
return result
end
-- Function to save checkbox preferences
local function save_checkbox_preference()
preferences.OctaMEDPickPutSlots.SetSelectedInstrument.value = set_to_selected_instrument
preferences.OctaMEDPickPutSlots.UseEditStep.value = use_edit_step_for_put
preferences.OctaMEDPickPutSlots.RandomizeEnabled.value = randomize_enabled
preferences.OctaMEDPickPutSlots.RandomizePercentage.value = randomize_percentage
renoise.tool().preferences:save_as("preferences.xml")
end
-- Call this function after loading from preferences
local function load_slots_from_preferences()
for i = 1, 10 do
local slot_key = "Slot" .. string.format("%02d", i)
local saved_slot_data = preferences.OctaMEDPickPutSlots[slot_key].value
if saved_slot_data ~= "" then
print("Loading Slot", i, "from preferences:", saved_slot_data)
vb.views["slot_display_"..string.format("%02d", i)].text = saved_slot_data
-- Check and update the slot display after loading data
check_and_update_slot_display(i)
end
end
end
local function clear_pick(slot_index)
-- Reset the slot text in preferences
local slot_key = "Slot" .. string.format("%02d", slot_index)
preferences.OctaMEDPickPutSlots[slot_key].value = "Slot " .. string.format("%02d", slot_index) .. ": Empty"
renoise.tool().preferences:save_as("preferences.xml")
-- Reset the textfield to empty state if the dialog is open
if vb and vb.views["slot_display_"..string.format("%02d", slot_index)] then
vb.views["slot_display_"..string.format("%02d", slot_index)].text = "Slot " .. string.format("%02d", slot_index) .. ": Empty"
end
-- Update status message
renoise.app():show_status("Cleared Pick Slot " .. slot_index)
end
-- Function to handle the Put operation for Effect Columns
local function put_effect_columns(effect_data, line_indices)
local track = renoise.song().selected_track
-- Ensure the track has effect columns
if track.visible_effect_columns == 0 then
renoise.app():show_status("This track does not have visible effect columns.")
return
end
-- Update the number of visible effect columns only if the pick-slot has more columns
local effect_count_in_pick_slot = #effect_data
if effect_count_in_pick_slot > track.visible_effect_columns then
track.visible_effect_columns = effect_count_in_pick_slot
end
-- Iterate over the specified lines
for _, line_index in ipairs(line_indices) do
local pattern_line = renoise.song().selected_pattern.tracks[renoise.song().selected_track_index].lines[line_index]
-- Write data into the visible effect columns without reducing the number of columns
for i = 1, math.min(effect_count_in_pick_slot, track.visible_effect_columns) do
local effect_column = pattern_line.effect_columns[i]
local effect_str = effect_data[i]
-- Handle the effect command and value properly
effect_column.number_string = effect_str:sub(1, 2) -- First two characters (effect number)
effect_column.amount_string = effect_str:sub(3, 4) -- Last two characters (effect amount)
end
end
renoise.app():show_status("Effect columns updated successfully")
end
-- Function to handle the Put operation for Note and Effect Columns
local function put_note_instrument(slot_index)
local track = renoise.song().selected_track
local pattern = renoise.song().selected_pattern
local current_line_index = renoise.song().selected_line_index
local textfield_value = vb.views["slot_display_"..string.format("%02d", slot_index)].text
if textfield_value == "Slot " .. string.format("%02d", slot_index) .. ": Empty" then
renoise.app():show_status("Slot " .. string.format("%02d", slot_index) .. " is empty.")
return
end
-- Split the text into note data and effect data
local parts = string_split(textfield_value, "||")
local note_data = string_split(parts[1] or "", "|")
local effect_data = string_split(parts[2] or "", "|")
local process_note_columns = true
if track.type ~= renoise.Track.TRACK_TYPE_SEQUENCER then
process_note_columns = false
end
-- If there are no note columns in the slot data, do not overwrite current note columns
if not note_data or #note_data == 0 or note_data[1]:match("^%s*$") then
process_note_columns = false
end
-- Determine the lines to process based on randomization and edit step
local line_indices = {}
local pattern_length = pattern.number_of_lines
local edit_step = renoise.song().transport.edit_step
local start_line = current_line_index
if randomize_enabled then
local total_steps = pattern_length
local step = 1
if use_edit_step_for_put and edit_step > 0 then
step = edit_step
total_steps = math.floor((pattern_length - start_line + 1) / step)
end
for i = start_line, pattern_length, step do
if math.random(100) <= randomize_percentage then
table.insert(line_indices, i)
end
end
else
if use_edit_step_for_put and edit_step > 0 then
table.insert(line_indices, current_line_index)
renoise.song().selected_line_index = math.min(current_line_index + edit_step, pattern_length)
else
table.insert(line_indices, current_line_index)
end
end
for _, line_index in ipairs(line_indices) do
local pattern_line = pattern.tracks[renoise.song().selected_track_index].lines[line_index]
-- Process Note Columns if applicable
if process_note_columns then
-- Update the number of visible note columns to fit the picked data
if #note_data > track.visible_note_columns then
track.visible_note_columns = #note_data
end
-- Process and write to all note columns
for i = 1, math.min(#note_data, track.visible_note_columns) do
local note_column = pattern_line.note_columns[i]
local note_parts = string_split(note_data[i]:gsub("^%s+", ""), " ") -- Remove leading spaces
-- Assign the note column values only if they are not empty
if note_parts[1] ~= "---" and note_parts[1] ~= "..." then
note_column.note_string = note_parts[1]
end
if note_parts[2] ~= ".." then
if set_to_selected_instrument then
note_column.instrument_value = renoise.song().selected_instrument_index - 1
else
note_column.instrument_string = note_parts[2]
end
end
if note_parts[3] ~= ".." then
renoise.song().selected_track.volume_column_visible = true
note_column.volume_string = note_parts[3]
end
if note_parts[4] ~= ".." then
renoise.song().selected_track.panning_column_visible = true
note_column.panning_string = note_parts[4]
end
if note_parts[5] ~= ".." then
renoise.song().selected_track.delay_column_visible = true
note_column.delay_string = note_parts[5]
end
-- Handle samplefx data
if #note_parts > 5 and note_parts[6] ~= "...." then
renoise.song().selected_track.sample_effects_column_visible = true
note_column.effect_number_string = note_parts[6]:sub(1, 2) -- Effect command
note_column.effect_amount_string = note_parts[6]:sub(3, 4) -- Effect value
end
end
end
-- After processing note columns, call put_effect_columns for effect data
if effect_data and #effect_data > 0 and effect_data[1] ~= "" then
put_effect_columns(effect_data, {line_index})
end
end
-- Update status and return focus to pattern editor
renoise.app():show_status("Put: Slot " .. string.format("%02d", slot_index) .. " - " .. textfield_value)
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
end
local function put_from_preferences(slot_index)
local track = renoise.song().selected_track
local pattern = renoise.song().selected_pattern
local current_line_index = renoise.song().selected_line_index
-- Retrieve the slot text from preferences
local slot_key = "Slot" .. string.format("%02d", slot_index)
local slot_text = preferences.OctaMEDPickPutSlots[slot_key].value
if slot_text == "" or slot_text == "Slot " .. string.format("%02d", slot_index) .. ": Empty" then
renoise.app():show_status("Slot " .. string.format("%02d", slot_index) .. " is empty in preferences.")
return
end
-- Split the text into note data and effect data
local parts = string_split(slot_text, "||")
local note_data = string_split(parts[1] or "", "|")
local effect_data = string_split(parts[2] or "", "|")
local process_note_columns = true
if track.type ~= renoise.Track.TRACK_TYPE_SEQUENCER then
process_note_columns = false
end
-- If there are no note columns in the slot data, do not overwrite current note columns
if not note_data or #note_data == 0 or note_data[1]:match("^%s*$") then
process_note_columns = false
end
-- Determine the lines to process based on randomization and edit step
local line_indices = {}
local pattern_length = pattern.number_of_lines
local edit_step = renoise.song().transport.edit_step
local start_line = current_line_index
if randomize_enabled then
local total_steps = pattern_length
local step = 1
if use_edit_step_for_put and edit_step > 0 then
step = edit_step
total_steps = math.floor((pattern_length - start_line + 1) / step)
end
for i = start_line, pattern_length, step do
if math.random(100) <= randomize_percentage then
table.insert(line_indices, i)
end
end
else
if use_edit_step_for_put and edit_step > 0 then
table.insert(line_indices, current_line_index)
renoise.song().selected_line_index = math.min(current_line_index + edit_step, pattern_length)
else
table.insert(line_indices, current_line_index)
end
end
for _, line_index in ipairs(line_indices) do
local pattern_line = pattern.tracks[renoise.song().selected_track_index].lines[line_index]
-- Process Note Columns if applicable
if process_note_columns then
-- Update the number of visible note columns to fit the picked data
if #note_data > track.visible_note_columns then
track.visible_note_columns = #note_data
end
-- Process and write to all note columns
for i = 1, math.min(#note_data, track.visible_note_columns) do
local note_column = pattern_line.note_columns[i]
local note_parts = string_split(note_data[i]:gsub("^%s+", ""), " ") -- Remove leading spaces
-- Assign the note column values only if they are not empty
if note_parts[1] ~= "---" and note_parts[1] ~= "..." then
note_column.note_string = note_parts[1]
end
if note_parts[2] ~= ".." then
if set_to_selected_instrument then
note_column.instrument_value = renoise.song().selected_instrument_index - 1
else
note_column.instrument_string = note_parts[2]
end
end
if note_parts[3] ~= ".." then
renoise.song().selected_track.volume_column_visible = true
note_column.volume_string = note_parts[3]
end
if note_parts[4] ~= ".." then
renoise.song().selected_track.panning_column_visible = true
note_column.panning_string = note_parts[4]
end
if note_parts[5] ~= ".." then
renoise.song().selected_track.delay_column_visible = true
note_column.delay_string = note_parts[5]
end
-- Handle samplefx data
if #note_parts > 5 and note_parts[6] ~= "...." then
renoise.song().selected_track.sample_effects_column_visible = true
note_column.effect_number_string = note_parts[6]:sub(1, 2) -- Effect command
note_column.effect_amount_string = note_parts[6]:sub(3, 4) -- Effect value
end
end
end
-- After processing note columns, call put_effect_columns for effect data
if effect_data and #effect_data > 0 and effect_data[1] ~= "" then
put_effect_columns(effect_data, {line_index})
end
end
-- Update status and return focus to pattern editor
renoise.app():show_status("Put: Slot " .. string.format("%02d", slot_index) .. " from preferences.")
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
end
local function pick_to_preferences(slot_index)
local track = renoise.song().selected_track
local pattern_line = renoise.song().selected_pattern.tracks[renoise.song().selected_track_index].lines[renoise.song().selected_line_index]
local note_columns_str = {}
local effect_columns_str = {}
-- Process Note Columns if applicable
if track.type == renoise.Track.TRACK_TYPE_SEQUENCER then
for i = 1, track.visible_note_columns do
local note_column = pattern_line.note_columns[i]
local note_str = note_column.note_string .. " " ..
note_column.instrument_string .. " " ..
note_column.volume_string .. " " ..
note_column.panning_string .. " " ..
note_column.delay_string .. " "
-- Handle Sample FX column, if empty, put "...."
if note_column.effect_number_string == "00" and note_column.effect_amount_string == "00" then
note_str = note_str .. "...."
else
note_str = note_str .. note_column.effect_number_string .. note_column.effect_amount_string
end
table.insert(note_columns_str, note_str)
end
end
-- Process Effect Columns
local last_non_empty_effect_index = 0
for i = 1, track.visible_effect_columns do
local effect_column = pattern_line.effect_columns[i]
local effect_str = effect_column.number_string .. effect_column.amount_string
-- If the effect column is not empty, update the last non-empty index
if effect_str ~= "0000" then
last_non_empty_effect_index = i
end
-- Collect all effect columns
table.insert(effect_columns_str, effect_str)
end
-- Truncate effect columns after the last non-empty column
effect_columns_str = {unpack(effect_columns_str, 1, last_non_empty_effect_index)}
-- Combine Note and Effect Columns
local slot_text = table.concat(note_columns_str, " | ") .. "||" .. table.concat(effect_columns_str, "|")
-- Update the corresponding textfield
vb.views["slot_display_"..string.format("%02d", slot_index)].text = slot_text
-- Save the picked slot data
save_slot_to_preferences(slot_index)
end
-- Function to handle picking note and instrument data
local function pick_note_instrument(slot_index)
pick_to_preferences(slot_index)
renoise.app():show_status("Picked to Slot " .. string.format("%02d", slot_index))
end
-- Function to save dialog content to a text file
local function save_dialog_content_to_file()
local filename = renoise.app():prompt_for_filename_to_write(".txt", "Save Pick/Put Dialog Content")
if filename and filename ~= "" then
local file, err = io.open(filename, "w")
if file then
for i = 1, 10 do
local slot_text = vb.views["slot_display_"..string.format("%02d", i)].text
file:write(slot_text, "\n")
end
file:close()
renoise.app():show_status("Dialog content saved to " .. filename)
else
renoise.app():show_warning("Error saving file: " .. err)
end
end
end
-- Function to load dialog content from a text file
local function load_dialog_content_from_file()
local filename = renoise.app():prompt_for_filename_to_read({"*.txt", "*.TXT", "*.Txt", "*"}, "Load Pick/Put Dialog Content")
if filename and filename ~= "" then
local file, err = io.open(filename, "r")
if file then
local i = 1
for line in file:lines() do
if i > 10 then break end
vb.views["slot_display_"..string.format("%02d", i)].text = line
-- Update preferences
local slot_key = "Slot" .. string.format("%02d", i)
preferences.OctaMEDPickPutSlots[slot_key].value = line
i = i + 1
end
file:close()
renoise.app():show_status("Dialog content loaded from " .. filename)
-- Save preferences after loading
renoise.tool().preferences:save_as("preferences.xml")
else
renoise.app():show_warning("Error loading file: " .. err)
end
end
end
-- Custom key handler function to allow key events to pass through to pattern editor
local function my_keyhandler_func(dialog, key)
local closer = preferences.pakettiDialogClose.value
if key.modifiers == "" and key.name == closer then
dialog:close()
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
return
end
-- Allow other keys to pass through to the pattern editor
return key
end
-- Function to toggle the visibility of the dialog
function toggle_paketti_pick_dialog()
if dialog and dialog.visible then
dialog:close()
dialog = nil
else
dialog = renoise.app():show_custom_dialog(
"Paketti OctaMED Pick/Put",
create_paketti_pick_dialog(),
my_keyhandler_func
)
end
load_slots_from_preferences()
-- Ensure focus returns to pattern editor
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
end
-- Function to create the GUI dialog
function create_paketti_pick_dialog()
vb = renoise.ViewBuilder()
local rows = {}
for i = 1, 10 do
rows[#rows+1] = vb:row{
vb:button{text="Pick " .. string.format("%02d", i), notifier=function()
pick_note_instrument(i)
end},
vb:button{text="Put " .. string.format("%02d", i), notifier=function()
put_note_instrument(i)
end},
vb:textfield{id="slot_display_"..string.format("%02d", i), text="Slot " .. string.format("%02d", i) .. ": Empty", width=800},
vb:button{text="Clear", notifier=function()
clear_pick(i)
end}
}
end
return vb:column{
vb:row{
vb:checkbox{
value=set_to_selected_instrument,
notifier=function(value)
set_to_selected_instrument = value
save_checkbox_preference()
end
},
vb:text{text="Set to Selected Instrument"}
},
vb:row{
vb:checkbox{
value=use_edit_step_for_put,
notifier=function(value)
use_edit_step_for_put = value
save_checkbox_preference()
end
},
vb:text{text="Use EditStep for Put"}
},
vb:row{
vb:checkbox{
id = "randomize_checkbox",
value = randomize_enabled,
notifier = function(value)
randomize_enabled = value
save_checkbox_preference()
end
},
vb:text{text="Randomize"},
vb:slider{
id = "randomize_slider",
min = 0,
max = 100,
value = randomize_percentage,
notifier = function(value)
randomize_percentage = value
vb.views["randomize_percentage_label"].text = tostring(math.floor(value)) .. "%"
save_checkbox_preference()
end
},
vb:text{id = "randomize_percentage_label", text = tostring(randomize_percentage) .. "%"}
},
vb:row{
vb:button{
text = "Save Slots",
notifier = function()
save_dialog_content_to_file()
end
},
vb:button{
text = "Load Slots",
notifier = function()
load_dialog_content_from_file()
end
}
},
vb:column(rows)
}
end
-- Function to clear the current row
function clear_columns()
local pattern_line = renoise.song().selected_pattern.tracks[renoise.song().selected_track_index].lines[renoise.song().selected_line_index]
local note_column = renoise.song().selected_note_column
note_column:clear()
for i = 1, #pattern_line.effect_columns do
pattern_line.effect_columns[i]:clear()
end
renoise.app():show_status("Columns cleared!")
renoise.app().window.active_middle_frame = renoise.ApplicationWindow.MIDDLE_FRAME_PATTERN_EDITOR
end
for i = 1, 10 do
renoise.tool():add_keybinding{name="Pattern Editor:Paketti:OctaMED Pick Slot "..formatDigits(2,i), invoke=function() pick_note_instrument(i) end}
renoise.tool():add_keybinding{name="Pattern Editor:Paketti:OctaMED Put Slot "..formatDigits(2,i), invoke=function() put_from_preferences(i) end}
renoise.tool():add_midi_mapping{name="Paketti:OctaMED Pick Slot "..formatDigits(2,i), invoke=function() pick_note_instrument(i) end}
renoise.tool():add_midi_mapping{name="Paketti:OctaMED Put Slot "..formatDigits(2,i), invoke=function() put_from_preferences(i) end}
end
renoise.tool():add_keybinding{name="Pattern Editor:Paketti:OctaMED Pick/Put Dialog",invoke=function() toggle_paketti_pick_dialog() end}
renoise.tool():add_menu_entry{name="Pattern Editor:Paketti..:Other Trackers..:OctaMED Pick/Put Dialog",invoke=function() toggle_paketti_pick_dialog() end}