-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPakettiCommandWheel.lua
More file actions
1735 lines (1473 loc) · 56.5 KB
/
PakettiCommandWheel.lua
File metadata and controls
1735 lines (1473 loc) · 56.5 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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- PakettiCommandWheel.lua
-- A unified scroll-wheel control interface for Renoise
-- Modes: MACRO, MIDI CC (*MIDI Control), PATTERN COMMAND, DEVICE
--------------------------------------------------------------------------------
-- DEBUG FLAG
--------------------------------------------------------------------------------
PakettiCommandWheelDebug = false
--------------------------------------------------------------------------------
-- MODE CONSTANTS
--------------------------------------------------------------------------------
PAKETTI_COMMAND_WHEEL_MODE_MACRO = 1
PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC = 2
PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND = 3
PAKETTI_COMMAND_WHEEL_MODE_DEVICE = 4
-- Mode names for display
PAKETTI_COMMAND_WHEEL_MODE_NAMES = {
[PAKETTI_COMMAND_WHEEL_MODE_MACRO] = "MACRO",
[PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC] = "*MIDI Control",
[PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND] = "PATTERN CMD",
[PAKETTI_COMMAND_WHEEL_MODE_DEVICE] = "DEVICE"
}
--------------------------------------------------------------------------------
-- PLACEMENT POLICIES (5 from original spec)
--------------------------------------------------------------------------------
PAKETTI_COMMAND_WHEEL_PLACEMENT_CURSOR = 1
PAKETTI_COMMAND_WHEEL_PLACEMENT_FIND_APPROPRIATE = 2
PAKETTI_COMMAND_WHEEL_PLACEMENT_FIND_COMPATIBLE = 3
PAKETTI_COMMAND_WHEEL_PLACEMENT_CREATE_NEW = 4
PAKETTI_COMMAND_WHEEL_PLACEMENT_NEVER_EXPAND = 5
PAKETTI_COMMAND_WHEEL_PLACEMENT_NAMES = {
[PAKETTI_COMMAND_WHEEL_PLACEMENT_CURSOR] = "Overwrite Current",
[PAKETTI_COMMAND_WHEEL_PLACEMENT_FIND_APPROPRIATE] = "Find Appropriate",
[PAKETTI_COMMAND_WHEEL_PLACEMENT_FIND_COMPATIBLE] = "Find Compatible",
[PAKETTI_COMMAND_WHEEL_PLACEMENT_CREATE_NEW] = "Create New Column",
[PAKETTI_COMMAND_WHEEL_PLACEMENT_NEVER_EXPAND] = "Never Expand"
}
--------------------------------------------------------------------------------
-- STATE MANAGEMENT
--------------------------------------------------------------------------------
PakettiCommandWheelState = {
mode = PAKETTI_COMMAND_WHEEL_MODE_MACRO,
index = 1,
value = 0,
placement_policy = PAKETTI_COMMAND_WHEEL_PLACEMENT_CURSOR,
write_on_scroll = false
}
-- Dialog and UI references
PakettiCommandWheelDialog = nil
PakettiCommandWheelViewBuilder = nil
-- Observable tracking
PakettiCommandWheelObservables = {
device_observer = nil,
instrument_observer = nil,
track_observer = nil
}
--------------------------------------------------------------------------------
-- PHRASE-VALID EFFECTS (from PakettiPatternEditorCheatSheet)
--------------------------------------------------------------------------------
PAKETTI_COMMAND_WHEEL_PHRASE_VALID_EFFECTS = {
["0A"] = true, ["0U"] = true, ["0D"] = true, ["0G"] = true,
["0I"] = true, ["0O"] = true, ["0C"] = true, ["0Q"] = true,
["0M"] = true, ["0S"] = true, ["0B"] = true, ["0R"] = true,
["0Y"] = true, ["0V"] = true, ["0T"] = true, ["0N"] = true,
["0E"] = true
}
--------------------------------------------------------------------------------
-- PATTERN COMMANDS LIST
--------------------------------------------------------------------------------
PAKETTI_COMMAND_WHEEL_PATTERN_COMMANDS = {
-- Note column sub-columns (special handling)
{id = "VOLUME", name = "Volume", is_note_column = true, max_value = 128, phrase_valid = true},
{id = "PANNING", name = "Panning", is_note_column = true, max_value = 128, phrase_valid = true},
{id = "DELAY", name = "Delay", is_note_column = true, max_value = 255, phrase_valid = true},
-- Sample/Note effects (work in phrases)
{id = "0A", name = "Arpeggio", max_value = 255, phrase_valid = true},
{id = "0U", name = "Slide Pitch Up", max_value = 255, phrase_valid = true},
{id = "0D", name = "Slide Pitch Down", max_value = 255, phrase_valid = true},
{id = "0G", name = "Glide to Note", max_value = 255, phrase_valid = true},
{id = "0I", name = "Fade Volume In", max_value = 255, phrase_valid = true},
{id = "0O", name = "Fade Volume Out", max_value = 255, phrase_valid = true},
{id = "0C", name = "Cut Volume", max_value = 255, phrase_valid = true},
{id = "0Q", name = "Delay Note", max_value = 255, phrase_valid = true},
{id = "0M", name = "Set Note Volume", max_value = 255, phrase_valid = true},
{id = "0S", name = "Trigger Slice/Offset", max_value = 255, phrase_valid = true},
{id = "0B", name = "Play Backwards/Forwards", max_value = 255, phrase_valid = true},
{id = "0R", name = "Retrigger", max_value = 255, phrase_valid = true},
{id = "0Y", name = "Probability", max_value = 255, phrase_valid = true},
{id = "0Z", name = "Trigger Phrase", max_value = 255, phrase_valid = false},
{id = "0V", name = "Vibrato", max_value = 255, phrase_valid = true},
{id = "0T", name = "Tremolo", max_value = 255, phrase_valid = true},
{id = "0N", name = "Auto Pan", max_value = 255, phrase_valid = true},
{id = "0E", name = "Set Envelope Position", max_value = 255, phrase_valid = true},
-- Track effects (pattern-only)
{id = "0L", name = "Track Volume", max_value = 255, phrase_valid = false},
{id = "0P", name = "Track Pan", max_value = 255, phrase_valid = false},
{id = "0W", name = "Track Surround Width", max_value = 255, phrase_valid = false},
{id = "0J", name = "Track Routing", max_value = 255, phrase_valid = false},
{id = "0X", name = "Stop Notes/FX", max_value = 255, phrase_valid = false},
-- Global effects
{id = "ZT", name = "Set Tempo", max_value = 255, phrase_valid = false},
{id = "ZL", name = "Set LPB", max_value = 255, phrase_valid = false},
{id = "ZK", name = "Set TPL", max_value = 16, phrase_valid = false},
{id = "ZG", name = "Toggle Groove", max_value = 1, phrase_valid = false},
{id = "ZB", name = "Pattern Break", max_value = 255, phrase_valid = false},
{id = "ZD", name = "Pattern Delay", max_value = 255, phrase_valid = false}
}
--------------------------------------------------------------------------------
-- DEBUG HELPER
--------------------------------------------------------------------------------
function PakettiCommandWheelPrint(msg)
if PakettiCommandWheelDebug then
print("CommandWheel: " .. tostring(msg))
end
end
--------------------------------------------------------------------------------
-- PHRASE MODE DETECTION
--------------------------------------------------------------------------------
function PakettiCommandWheelIsPhraseMode()
local success, result = pcall(function()
return renoise.app().window.active_middle_frame == renoise.ApplicationWindow.MIDDLE_FRAME_INSTRUMENT_PHRASE_EDITOR
end)
if success then
return result
end
return false
end
--------------------------------------------------------------------------------
-- SAFE ACCESSORS WITH PCALL
--------------------------------------------------------------------------------
function PakettiCommandWheelGetSong()
local success, song = pcall(function() return renoise.song() end)
if success and song then
return song
end
return nil
end
function PakettiCommandWheelGetSelectedDevice()
local song = PakettiCommandWheelGetSong()
if not song then return nil end
local success, device = pcall(function() return song.selected_device end)
if success and device then
return device
end
return nil
end
function PakettiCommandWheelGetSelectedInstrument()
local song = PakettiCommandWheelGetSong()
if not song then return nil end
local success, instrument = pcall(function() return song.selected_instrument end)
if success and instrument then
return instrument
end
return nil
end
function PakettiCommandWheelGetSelectedPhrase()
local song = PakettiCommandWheelGetSong()
if not song then return nil end
local success, phrase = pcall(function()
local instr = song.selected_instrument
if instr and #instr.phrases > 0 then
local phrase_idx = song.selected_phrase_index
if phrase_idx and phrase_idx > 0 and phrase_idx <= #instr.phrases then
return instr.phrases[phrase_idx]
end
end
return nil
end)
if success then
return phrase
end
return nil
end
function PakettiCommandWheelGetDeviceParameter(device, index)
if not device then return nil end
local success, param = pcall(function()
if index > 0 and index <= #device.parameters then
return device.parameters[index]
end
return nil
end)
if success then
return param
end
return nil
end
function PakettiCommandWheelGetMacro(instrument, index)
if not instrument then return nil end
local success, macro = pcall(function()
if index > 0 and index <= 8 then
return instrument.macros[index]
end
return nil
end)
if success then
return macro
end
return nil
end
--------------------------------------------------------------------------------
-- *MIDI CONTROL DEVICE DETECTION
--------------------------------------------------------------------------------
function PakettiCommandWheelFindMidiControlDevice()
local song = PakettiCommandWheelGetSong()
if not song then return nil, nil end
local success, result = pcall(function()
local track = song.selected_track
if not track then return nil, nil end
for i, device in ipairs(track.devices) do
if device.device_path == "Audio/Effects/Native/*MIDI Control" or
device.name == "*MIDI Control" then
return device, i
end
end
return nil, nil
end)
if success then
return result
end
return nil, nil
end
--------------------------------------------------------------------------------
-- WRITE POSITION HELPER
--------------------------------------------------------------------------------
function PakettiCommandWheelGetWritePosition()
local song = PakettiCommandWheelGetSong()
if not song then return nil, nil, nil end
local success, result = pcall(function()
local write_line
local write_pattern_index = song.selected_pattern_index
local write_track_index = song.selected_track_index
if song.transport.playing and song.transport.follow_player then
write_line = song.transport.playback_pos.line
local playback_seq = song.transport.playback_pos.sequence
write_pattern_index = song.sequencer.pattern_sequence[playback_seq]
else
write_line = song.selected_line_index
end
return {write_pattern_index, write_track_index, write_line}
end)
if success and result then
return result[1], result[2], result[3]
end
return nil, nil, nil
end
--------------------------------------------------------------------------------
-- SELECTION RANGE HELPER
--------------------------------------------------------------------------------
function PakettiCommandWheelGetSelectionRange()
local song = PakettiCommandWheelGetSong()
if not song then return nil end
local success, selection = pcall(function()
return song.selection_in_pattern
end)
if success and selection then
return selection
end
return nil
end
--------------------------------------------------------------------------------
-- MAX VALUE / MAX INDEX
--------------------------------------------------------------------------------
function PakettiCommandWheelGetMaxValue()
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
return 127
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
return 127
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND then
local cmd = PAKETTI_COMMAND_WHEEL_PATTERN_COMMANDS[state.index]
if cmd then
return cmd.max_value or 255
end
return 255
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
return 255
end
return 255
end
function PakettiCommandWheelGetMaxIndex()
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
return 8
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
-- *MIDI Control device has parameters for MIDI CCs
local device = PakettiCommandWheelFindMidiControlDevice()
if device then
local success, count = pcall(function() return #device.parameters end)
if success and count > 0 then
return count
end
end
return 16 -- Default to 16 if no device found
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND then
return #PAKETTI_COMMAND_WHEEL_PATTERN_COMMANDS
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
local device = PakettiCommandWheelGetSelectedDevice()
if device then
local success, count = pcall(function() return #device.parameters end)
if success and count > 0 then
return count
end
end
return 1
end
return 1
end
--------------------------------------------------------------------------------
-- INDEX NAME
--------------------------------------------------------------------------------
function PakettiCommandWheelGetIndexName()
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
return "Macro " .. state.index
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
local device = PakettiCommandWheelFindMidiControlDevice()
if device then
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, name = pcall(function() return param.name end)
if success then
return name
end
end
end
return "CC Param " .. state.index .. " (No *MIDI Control)"
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND then
local cmd = PAKETTI_COMMAND_WHEEL_PATTERN_COMMANDS[state.index]
if cmd then
return cmd.id .. " - " .. cmd.name
end
return "Unknown"
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
local device = PakettiCommandWheelGetSelectedDevice()
if device then
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, name = pcall(function() return param.name end)
if success then
return name
end
end
end
return "No Device"
end
return "Unknown"
end
function PakettiCommandWheelFormatValue(value)
return string.format("%02X (%d)", value, value)
end
--------------------------------------------------------------------------------
-- OBSERVABLE MANAGEMENT
--------------------------------------------------------------------------------
function PakettiCommandWheelSetupObservables()
PakettiCommandWheelCleanupObservables()
local song = PakettiCommandWheelGetSong()
if not song then return end
PakettiCommandWheelPrint("Setting up observables")
-- Device selection observer
local success1 = pcall(function()
if song.selected_device_observable and
not song.selected_device_observable:has_notifier(PakettiCommandWheelOnDeviceChanged) then
song.selected_device_observable:add_notifier(PakettiCommandWheelOnDeviceChanged)
PakettiCommandWheelObservables.device_observer = PakettiCommandWheelOnDeviceChanged
PakettiCommandWheelPrint("Device observer added")
end
end)
-- Instrument selection observer
local success2 = pcall(function()
if song.selected_instrument_observable and
not song.selected_instrument_observable:has_notifier(PakettiCommandWheelOnInstrumentChanged) then
song.selected_instrument_observable:add_notifier(PakettiCommandWheelOnInstrumentChanged)
PakettiCommandWheelObservables.instrument_observer = PakettiCommandWheelOnInstrumentChanged
PakettiCommandWheelPrint("Instrument observer added")
end
end)
-- Track selection observer
local success3 = pcall(function()
if song.selected_track_observable and
not song.selected_track_observable:has_notifier(PakettiCommandWheelOnTrackChanged) then
song.selected_track_observable:add_notifier(PakettiCommandWheelOnTrackChanged)
PakettiCommandWheelObservables.track_observer = PakettiCommandWheelOnTrackChanged
PakettiCommandWheelPrint("Track observer added")
end
end)
end
function PakettiCommandWheelCleanupObservables()
local song = PakettiCommandWheelGetSong()
if not song then return end
PakettiCommandWheelPrint("Cleaning up observables")
pcall(function()
if PakettiCommandWheelObservables.device_observer and song.selected_device_observable then
if song.selected_device_observable:has_notifier(PakettiCommandWheelObservables.device_observer) then
song.selected_device_observable:remove_notifier(PakettiCommandWheelObservables.device_observer)
end
end
end)
PakettiCommandWheelObservables.device_observer = nil
pcall(function()
if PakettiCommandWheelObservables.instrument_observer and song.selected_instrument_observable then
if song.selected_instrument_observable:has_notifier(PakettiCommandWheelObservables.instrument_observer) then
song.selected_instrument_observable:remove_notifier(PakettiCommandWheelObservables.instrument_observer)
end
end
end)
PakettiCommandWheelObservables.instrument_observer = nil
pcall(function()
if PakettiCommandWheelObservables.track_observer and song.selected_track_observable then
if song.selected_track_observable:has_notifier(PakettiCommandWheelObservables.track_observer) then
song.selected_track_observable:remove_notifier(PakettiCommandWheelObservables.track_observer)
end
end
end)
PakettiCommandWheelObservables.track_observer = nil
end
function PakettiCommandWheelOnDeviceChanged()
PakettiCommandWheelPrint("Device changed")
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
-- Reset index to 1 and sync value
state.index = 1
PakettiCommandWheelSyncDeviceValue()
PakettiCommandWheelUpdateDisplay()
end
end
function PakettiCommandWheelOnInstrumentChanged()
PakettiCommandWheelPrint("Instrument changed")
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelSyncMacroValue()
PakettiCommandWheelUpdateDisplay()
end
end
function PakettiCommandWheelOnTrackChanged()
PakettiCommandWheelPrint("Track changed")
local state = PakettiCommandWheelState
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
-- Re-check for *MIDI Control device
state.index = 1
PakettiCommandWheelSyncMidiCCValue()
PakettiCommandWheelUpdateDisplay()
end
end
--------------------------------------------------------------------------------
-- MODE SWITCHING
--------------------------------------------------------------------------------
function PakettiCommandWheelSetMode(new_mode)
local state = PakettiCommandWheelState
state.mode = new_mode
state.index = 1
state.value = 0
if new_mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
PakettiCommandWheelSyncDeviceValue()
elseif new_mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelSyncMacroValue()
elseif new_mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
PakettiCommandWheelSyncMidiCCValue()
end
PakettiCommandWheelUpdateDisplay()
renoise.app():show_status("Command Wheel: " .. PAKETTI_COMMAND_WHEEL_MODE_NAMES[new_mode] .. " mode")
end
function PakettiCommandWheelSetModeMacro()
PakettiCommandWheelSetMode(PAKETTI_COMMAND_WHEEL_MODE_MACRO)
end
function PakettiCommandWheelSetModeMidiCC()
PakettiCommandWheelSetMode(PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC)
end
function PakettiCommandWheelSetModePatternCommand()
PakettiCommandWheelSetMode(PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND)
end
function PakettiCommandWheelSetModeDevice()
PakettiCommandWheelSetMode(PAKETTI_COMMAND_WHEEL_MODE_DEVICE)
end
--------------------------------------------------------------------------------
-- INDEX NAVIGATION
--------------------------------------------------------------------------------
function PakettiCommandWheelIndexNext()
local state = PakettiCommandWheelState
local max_index = PakettiCommandWheelGetMaxIndex()
state.index = state.index + 1
if state.index > max_index then
state.index = 1
end
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
PakettiCommandWheelSyncDeviceValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelSyncMacroValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
PakettiCommandWheelSyncMidiCCValue()
else
state.value = 0
end
PakettiCommandWheelUpdateDisplay()
renoise.app():show_status("Command Wheel: " .. PakettiCommandWheelGetIndexName())
end
function PakettiCommandWheelIndexPrev()
local state = PakettiCommandWheelState
local max_index = PakettiCommandWheelGetMaxIndex()
state.index = state.index - 1
if state.index < 1 then
state.index = max_index
end
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
PakettiCommandWheelSyncDeviceValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelSyncMacroValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
PakettiCommandWheelSyncMidiCCValue()
else
state.value = 0
end
PakettiCommandWheelUpdateDisplay()
renoise.app():show_status("Command Wheel: " .. PakettiCommandWheelGetIndexName())
end
--------------------------------------------------------------------------------
-- VALUE SYNC
--------------------------------------------------------------------------------
function PakettiCommandWheelSyncDeviceValue()
local state = PakettiCommandWheelState
local device = PakettiCommandWheelGetSelectedDevice()
if not device then
state.value = 0
return
end
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, result = pcall(function()
local normalized = (param.value - param.value_min) / (param.value_max - param.value_min)
return math.floor(normalized * 255 + 0.5)
end)
if success then
state.value = result
else
state.value = 0
end
else
state.value = 0
end
end
function PakettiCommandWheelSyncMacroValue()
local state = PakettiCommandWheelState
local instrument = PakettiCommandWheelGetSelectedInstrument()
if not instrument then
state.value = 0
return
end
local macro = PakettiCommandWheelGetMacro(instrument, state.index)
if macro then
local success, result = pcall(function()
return math.floor(macro.value * 127 + 0.5)
end)
if success then
state.value = result
else
state.value = 0
end
else
state.value = 0
end
end
function PakettiCommandWheelSyncMidiCCValue()
local state = PakettiCommandWheelState
local device = PakettiCommandWheelFindMidiControlDevice()
if not device then
state.value = 0
return
end
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, result = pcall(function()
local normalized = (param.value - param.value_min) / (param.value_max - param.value_min)
return math.floor(normalized * 127 + 0.5)
end)
if success then
state.value = result
else
state.value = 0
end
else
state.value = 0
end
end
--------------------------------------------------------------------------------
-- VALUE ADJUSTMENT
--------------------------------------------------------------------------------
function PakettiCommandWheelAdjustValue(delta)
local state = PakettiCommandWheelState
local max_value = PakettiCommandWheelGetMaxValue()
state.value = state.value + delta
if state.value > max_value then
state.value = max_value
elseif state.value < 0 then
state.value = 0
end
-- Apply value immediately for modes that support audition
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelApplyMacroValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
PakettiCommandWheelApplyDeviceValue()
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
PakettiCommandWheelApplyMidiCCValue()
end
if state.write_on_scroll then
PakettiCommandWheelWriteToPattern()
end
PakettiCommandWheelUpdateDisplay()
local hex_value = string.format("%02X", state.value)
renoise.app():show_status("Command Wheel: " .. PakettiCommandWheelGetIndexName() .. " = " .. hex_value .. " (" .. state.value .. ")")
end
function PakettiCommandWheelValueUp1()
PakettiCommandWheelAdjustValue(1)
end
function PakettiCommandWheelValueDown1()
PakettiCommandWheelAdjustValue(-1)
end
function PakettiCommandWheelValueUp10()
PakettiCommandWheelAdjustValue(10)
end
function PakettiCommandWheelValueDown10()
PakettiCommandWheelAdjustValue(-10)
end
--------------------------------------------------------------------------------
-- VALUE APPLICATION (Audition)
--------------------------------------------------------------------------------
function PakettiCommandWheelApplyMacroValue()
local state = PakettiCommandWheelState
local instrument = PakettiCommandWheelGetSelectedInstrument()
if not instrument then
PakettiCommandWheelPrint("No instrument for macro apply")
return
end
local macro = PakettiCommandWheelGetMacro(instrument, state.index)
if macro then
local success, err = pcall(function()
macro.value = state.value / 127
end)
if not success then
PakettiCommandWheelPrint("Error applying macro: " .. tostring(err))
end
end
end
function PakettiCommandWheelApplyDeviceValue()
local state = PakettiCommandWheelState
local device = PakettiCommandWheelGetSelectedDevice()
if not device then
PakettiCommandWheelPrint("No device for value apply")
return
end
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, err = pcall(function()
local normalized = state.value / 255
param.value = param.value_min + normalized * (param.value_max - param.value_min)
end)
if not success then
PakettiCommandWheelPrint("Error applying device value: " .. tostring(err))
end
end
end
function PakettiCommandWheelApplyMidiCCValue()
local state = PakettiCommandWheelState
local device = PakettiCommandWheelFindMidiControlDevice()
if not device then
PakettiCommandWheelPrint("No *MIDI Control device for CC apply")
return
end
local param = PakettiCommandWheelGetDeviceParameter(device, state.index)
if param then
local success, err = pcall(function()
local normalized = state.value / 127
param.value = param.value_min + normalized * (param.value_max - param.value_min)
end)
if not success then
PakettiCommandWheelPrint("Error applying MIDI CC: " .. tostring(err))
end
end
end
--------------------------------------------------------------------------------
-- PATTERN/PHRASE WRITING - CORE DISPATCHER
--------------------------------------------------------------------------------
function PakettiCommandWheelWriteToPattern()
local state = PakettiCommandWheelState
local is_phrase_mode = PakettiCommandWheelIsPhraseMode()
PakettiCommandWheelPrint("WriteToPattern: mode=" .. state.mode .. ", phrase_mode=" .. tostring(is_phrase_mode))
if state.mode == PAKETTI_COMMAND_WHEEL_MODE_MACRO then
PakettiCommandWheelWriteMacro(is_phrase_mode)
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_MIDI_CC then
if is_phrase_mode then
renoise.app():show_status("Command Wheel: *MIDI Control cannot write to phrases")
else
PakettiCommandWheelWriteMidiCC()
end
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_PATTERN_COMMAND then
PakettiCommandWheelWritePatternCommand(is_phrase_mode)
elseif state.mode == PAKETTI_COMMAND_WHEEL_MODE_DEVICE then
renoise.app():show_status("Command Wheel: DEVICE mode does not write to pattern")
end
end
--------------------------------------------------------------------------------
-- MACRO WRITING
--------------------------------------------------------------------------------
function PakettiCommandWheelWriteMacro(is_phrase_mode)
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
-- Macro commands use 1X format where X is the macro number (1-8)
local effect_number = string.format("1%d", state.index)
local amount_string = string.format("%02X", state.value)
if is_phrase_mode then
PakettiCommandWheelWriteEffectToPhrase(effect_number, amount_string)
else
PakettiCommandWheelWriteEffectToPattern(effect_number, amount_string)
end
end
--------------------------------------------------------------------------------
-- MIDI CC WRITING (*MIDI Control device)
--------------------------------------------------------------------------------
function PakettiCommandWheelWriteMidiCC()
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
local device, device_index = PakettiCommandWheelFindMidiControlDevice()
if not device then
renoise.app():show_status("Command Wheel: No *MIDI Control device on track")
return
end
-- Write device parameter automation command
-- Format: XYxx where X is device index (hex), Y is parameter index (hex), xx is value
local device_hex = string.format("%X", device_index)
local param_hex = string.format("%X", state.index)
local effect_number = device_hex .. param_hex
local amount_string = string.format("%02X", state.value)
PakettiCommandWheelWriteEffectToPattern(effect_number, amount_string)
end
--------------------------------------------------------------------------------
-- PATTERN COMMAND WRITING
--------------------------------------------------------------------------------
function PakettiCommandWheelWritePatternCommand(is_phrase_mode)
local state = PakettiCommandWheelState
local cmd = PAKETTI_COMMAND_WHEEL_PATTERN_COMMANDS[state.index]
if not cmd then
PakettiCommandWheelPrint("Invalid command index: " .. state.index)
return
end
-- Check if command is valid for phrase mode
if is_phrase_mode and not cmd.phrase_valid then
renoise.app():show_status("Command Wheel: " .. cmd.id .. " not valid in phrases")
return
end
-- Handle note column sub-columns
if cmd.is_note_column then
if is_phrase_mode then
PakettiCommandWheelWriteNoteColumnToPhrase(cmd)
else
PakettiCommandWheelWriteNoteColumnToPattern(cmd)
end
return
end
-- Handle effect column commands
local effect_number = cmd.id
local amount_string = string.format("%02X", state.value)
if is_phrase_mode then
PakettiCommandWheelWriteEffectToPhrase(effect_number, amount_string)
else
PakettiCommandWheelWriteEffectToPattern(effect_number, amount_string)
end
end
--------------------------------------------------------------------------------
-- EFFECT COLUMN WRITERS (WITH SELECTION SUPPORT)
--------------------------------------------------------------------------------
function PakettiCommandWheelWriteEffectToPattern(effect_number, amount_string)
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
local selection = PakettiCommandWheelGetSelectionRange()
if selection then
-- Write to selection range
PakettiCommandWheelWriteEffectToSelection(effect_number, amount_string, selection)
else
-- Write to single line
PakettiCommandWheelWriteEffectToSingleLine(effect_number, amount_string)
end
end
function PakettiCommandWheelWriteEffectToSingleLine(effect_number, amount_string)
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
local pattern_idx, track_idx, line_idx = PakettiCommandWheelGetWritePosition()
if not pattern_idx then return end
local success, err = pcall(function()
local track = song:track(track_idx)
if track.type ~= renoise.Track.TRACK_TYPE_SEQUENCER then
renoise.app():show_status("Command Wheel: Cannot write to non-sequencer track")
return
end
local pattern = song:pattern(pattern_idx)
local pattern_track = pattern:track(track_idx)
local line = pattern_track:line(line_idx)
-- Apply placement policy
local effect_column = PakettiCommandWheelGetTargetEffectColumn(track, line)
if not effect_column then
renoise.app():show_status("Command Wheel: No valid effect column (policy: " ..
PAKETTI_COMMAND_WHEEL_PLACEMENT_NAMES[state.placement_policy] .. ")")
return
end
effect_column.number_string = effect_number
effect_column.amount_string = amount_string
renoise.app():show_status("Command Wheel: Wrote " .. effect_number .. amount_string .. " at line " .. line_idx)
end)
if not success then
PakettiCommandWheelPrint("Error writing effect: " .. tostring(err))
end
end
function PakettiCommandWheelWriteEffectToSelection(effect_number, amount_string, selection)
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
local effects_written = 0
local success, err = pcall(function()
local pattern = song:pattern(song.selected_pattern_index)
for track_idx = selection.start_track, selection.end_track do
local track = song:track(track_idx)
if track.type == renoise.Track.TRACK_TYPE_SEQUENCER then
local pattern_track = pattern:track(track_idx)
for line_idx = selection.start_line, selection.end_line do
local line = pattern_track:line(line_idx)
local effect_column = PakettiCommandWheelGetTargetEffectColumn(track, line)
if effect_column then
effect_column.number_string = effect_number
effect_column.amount_string = amount_string
effects_written = effects_written + 1
end
end
end
end
end)
if success and effects_written > 0 then
renoise.app():show_status("Command Wheel: Wrote " .. effect_number .. amount_string ..
" to " .. effects_written .. " positions in selection")
elseif not success then
PakettiCommandWheelPrint("Error writing to selection: " .. tostring(err))
end
end
function PakettiCommandWheelWriteEffectToPhrase(effect_number, amount_string)
local state = PakettiCommandWheelState
local song = PakettiCommandWheelGetSong()
if not song then return end
local phrase = PakettiCommandWheelGetSelectedPhrase()
if not phrase then