Repository navigation
Expand file tree
/
Copy pathPakettiAutomateLastTouched.lua
More file actions
965 lines (812 loc) · 36.5 KB
/
Copy pathPakettiAutomateLastTouched.lua
File metadata and controls
965 lines (812 loc) · 36.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
-- PakettiAutomateLastTouched.lua
-- FL Studio-style "Automate Last Touched Parameter" functionality
-- Configurable watching modes and device scopes for user-controlled performance impact
--------------------------------------------------------------------------------
-- Constants
--------------------------------------------------------------------------------
local WATCHING_MODE_SHORTCUT_FIRST = 1 -- Press shortcut to start watching (default)
local WATCHING_MODE_TRACK_WATCHING = 2 -- Always watching current track's devices
local WATCHING_MODE_ALWAYS_WATCHING = 3 -- Always watching all devices in scope
local WATCHING_MODE_TIMER = 4 -- Timer mode: parameter touch starts timer, shortcut must be pressed within timer window
local DEVICE_SCOPE_SELECTED = 1 -- Only watch selected device (default)
local DEVICE_SCOPE_ALL_TRACK = 2 -- Watch all devices on current track
local DEVICE_SCOPE_ALL_SONG = 3 -- Watch every device in entire song
--------------------------------------------------------------------------------
-- Global State Variables - Shortcut First Mode (temporary watching)
--------------------------------------------------------------------------------
local watching_device = nil
local watching_track_index = nil
local watching_active = false
local parameter_observers = {}
local is_watching_instrument_plugin = false
local continuous_mode_active = false
--------------------------------------------------------------------------------
-- Global State Variables - Persistent Watching Modes
--------------------------------------------------------------------------------
local persistent_watching_active = false
local global_last_touched_parameter = nil
local global_last_touched_device = nil
local global_last_touched_track_index = nil
local global_last_touched_is_instrument = false
local persistent_parameter_observers = {}
local track_change_observer = nil
local device_list_observers = {} -- Track -> observer for devices_observable
local persistent_setup_timer = nil -- Timer function reference for persistent watching setup
-- Song-lifecycle safety: fires BEFORE the song is released (New Song / Load
-- Song) so observers detach while renoise.song() is still the OLD song. Leaving
-- selected_track_index/track.devices observers (and the live timers) attached to
-- the dying song crashed Renoise in TWeakRefOwner::SOnWeakReferencableDying
-- during pattern-pool teardown. See features/song-lifecycle-safety.feature.
local automate_last_touched_release_doc_observer = nil
--------------------------------------------------------------------------------
-- Global State Variables - Timer Mode
--------------------------------------------------------------------------------
local timer_mode_active = false
local timer_mode_timer = nil -- Timer function reference
local timer_mode_stored_param = nil
local timer_mode_stored_device = nil
local timer_mode_stored_track_index = nil
local timer_mode_stored_is_instrument = false
--------------------------------------------------------------------------------
-- Helper: Get preference value with fallback
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedGetPref(key, default_value)
local success, result = pcall(function()
if preferences and preferences.pakettiAutomateLastTouched and preferences.pakettiAutomateLastTouched[key] then
return preferences.pakettiAutomateLastTouched[key].value
end
return default_value
end)
if success then
return result
end
return default_value
end
--------------------------------------------------------------------------------
-- Helper: Get device name safely (handles both AudioDevice and InstrumentPluginDevice)
--------------------------------------------------------------------------------
local function get_device_name(device)
if not device then return "unknown" end
-- AudioDevice has display_name, InstrumentPluginDevice only has name
-- Use pcall since accessing non-existent property on Renoise objects throws an error
local success, display_name = pcall(function() return device.display_name end)
if success and display_name then
return display_name
end
-- Fall back to name (exists on both AudioDevice and InstrumentPluginDevice)
local success2, name = pcall(function() return device.name end)
if success2 and name then
return name
end
return "unknown"
end
--------------------------------------------------------------------------------
-- Helper: Get fractional line position for continuous recording
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedGetFractionalLinePos()
local song = renoise.song()
if not song then return nil end
local beats, line
if song.transport.playing then
beats = song.transport.playback_pos_beats - math.floor(song.transport.playback_pos_beats)
line = song.transport.playback_pos.line
else
beats = song.transport.edit_pos_beats - math.floor(song.transport.edit_pos_beats)
line = song.transport.edit_pos.line
end
local lpb = song.transport.lpb
local beats_scaled = beats * lpb
local line_in_beat = math.floor(beats_scaled)
local fraction = 0
if (line_in_beat + 1) - line_in_beat ~= 0 then
fraction = (beats_scaled - line_in_beat) / ((line_in_beat + 1) - line_in_beat)
end
local line_fract = line + fraction
return line_fract
end
--------------------------------------------------------------------------------
-- Cleanup Functions
--------------------------------------------------------------------------------
-- Cleanup temporary watching (Shortcut First mode)
function PakettiAutomateLastTouchedCleanupTemporary()
print("AUTOMATE_LAST_TOUCHED: Temporary cleanup started")
for parameter, observer in pairs(parameter_observers) do
pcall(function()
if parameter and parameter.value_observable and parameter.value_observable:has_notifier(observer) then
parameter.value_observable:remove_notifier(observer)
end
end)
end
parameter_observers = {}
watching_device = nil
watching_track_index = nil
watching_active = false
is_watching_instrument_plugin = false
continuous_mode_active = false
print("AUTOMATE_LAST_TOUCHED: Temporary cleanup complete")
end
-- Cleanup persistent watching (Track/Always Watching modes)
function PakettiAutomateLastTouchedCleanupPersistent()
print("AUTOMATE_LAST_TOUCHED: Persistent cleanup started")
-- Remove all persistent parameter observers
for parameter, observer in pairs(persistent_parameter_observers) do
pcall(function()
if parameter and parameter.value_observable and parameter.value_observable:has_notifier(observer) then
parameter.value_observable:remove_notifier(observer)
end
end)
end
persistent_parameter_observers = {}
-- Remove track change observer
if track_change_observer then
pcall(function()
local song = renoise.song()
if song and song.selected_track_index_observable:has_notifier(track_change_observer) then
song.selected_track_index_observable:remove_notifier(track_change_observer)
end
end)
track_change_observer = nil
end
-- Remove device list observers
for track, observer in pairs(device_list_observers) do
pcall(function()
if track and track.devices_observable and track.devices_observable:has_notifier(observer) then
track.devices_observable:remove_notifier(observer)
end
end)
end
device_list_observers = {}
-- Reset state
persistent_watching_active = false
global_last_touched_parameter = nil
global_last_touched_device = nil
global_last_touched_track_index = nil
global_last_touched_is_instrument = false
-- Also cleanup timer mode
PakettiAutomateLastTouchedStopTimerMode()
print("AUTOMATE_LAST_TOUCHED: Persistent cleanup complete")
end
-- Full cleanup
function PakettiAutomateLastTouchedCleanup()
PakettiAutomateLastTouchedCleanupTemporary()
PakettiAutomateLastTouchedCleanupPersistent()
PakettiAutomateLastTouchedStopTimerMode()
-- Detach the song-release safety observer (idempotent; safe to run twice).
-- See features/song-lifecycle-safety.feature.
if automate_last_touched_release_doc_observer
and renoise.tool().app_release_document_observable:has_notifier(automate_last_touched_release_doc_observer) then
renoise.tool().app_release_document_observable:remove_notifier(automate_last_touched_release_doc_observer)
end
automate_last_touched_release_doc_observer = nil
end
--------------------------------------------------------------------------------
-- Timer Mode Functions
--------------------------------------------------------------------------------
-- Stop timer mode and cleanup
function PakettiAutomateLastTouchedStopTimerMode()
if timer_mode_timer and renoise.tool():has_timer(timer_mode_timer) then
renoise.tool():remove_timer(timer_mode_timer)
end
timer_mode_timer = nil
timer_mode_active = false
timer_mode_stored_param = nil
timer_mode_stored_device = nil
timer_mode_stored_track_index = nil
timer_mode_stored_is_instrument = false
end
-- Called when timer expires
function PakettiAutomateLastTouchedOnTimerExpired()
print("AUTOMATE_LAST_TOUCHED: Timer expired - no automation created")
timer_mode_active = false
timer_mode_stored_param = nil
timer_mode_stored_device = nil
timer_mode_stored_track_index = nil
timer_mode_stored_is_instrument = false
end
-- Start timer when parameter is touched
function PakettiAutomateLastTouchedStartTimerMode(param, device, track_index, is_plugin_instrument)
-- Stop any existing timer
PakettiAutomateLastTouchedStopTimerMode()
-- Store parameter info
timer_mode_stored_param = param
timer_mode_stored_device = device
timer_mode_stored_track_index = track_index
timer_mode_stored_is_instrument = is_plugin_instrument
timer_mode_active = true
-- Get timer duration from preferences
local timer_duration = PakettiAutomateLastTouchedGetPref("TimerDuration", 3)
-- Create timer function
timer_mode_timer = function()
if timer_mode_timer and renoise.tool():has_timer(timer_mode_timer) then
renoise.tool():remove_timer(timer_mode_timer)
end
timer_mode_timer = nil
PakettiAutomateLastTouchedOnTimerExpired()
end
-- Start timer
renoise.tool():add_timer(timer_mode_timer, timer_duration * 1000)
print("AUTOMATE_LAST_TOUCHED: Timer started for parameter: " .. param.name .. " (" .. timer_duration .. " seconds)")
renoise.app():show_status("Parameter touched: " .. param.name .. " - press shortcut within " .. timer_duration .. " seconds")
end
--------------------------------------------------------------------------------
-- Create Automation for a Single Parameter
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedCreateForParameter(param, device, track_index, is_plugin_instrument)
local song = renoise.song()
song:describe_undo("Paketti: Create Automation for Last Touched Parameter")
if not song then
renoise.app():show_status("No song available")
return false
end
local param_name = param.name
-- Handle instrument plugin vs track DSP
if is_plugin_instrument then
local track = song.tracks[track_index]
local instr_auto_device = nil
local instr_auto_device_index = nil
if track then
for j, dev in ipairs(track.devices) do
if dev.device_path == "Audio/Effects/Native/*Instr. Automation" then
instr_auto_device = dev
instr_auto_device_index = j
break
end
end
end
if instr_auto_device then
song.selected_device_index = instr_auto_device_index
pcall(function()
renoise.app().window.lower_frame_is_visible = true
renoise.app().window.active_lower_frame = renoise.ApplicationWindow.LOWER_FRAME_TRACK_AUTOMATION
end)
renoise.app():show_status("Instrument plugin: Use Instr. Automation device to automate '" .. param_name .. "'")
else
renoise.app():show_status("Add '*Instr. Automation' device to automate '" .. param_name .. "'")
end
return false
end
-- For track DSP devices
local current_pattern = song.selected_pattern_index
local pattern_track = song:pattern(current_pattern):track(track_index)
local automation = pattern_track:find_automation(param)
if not automation then
automation = pattern_track:create_automation(param)
print("AUTOMATE_LAST_TOUCHED: Created new automation for '" .. param_name .. "'")
end
if automation then
local current_line = song.selected_line_index
local value = param.value
local normalized_value = (value - param.value_min) / (param.value_max - param.value_min)
normalized_value = math.max(0.0, math.min(1.0, normalized_value))
if automation:has_point_at(current_line) then
automation:remove_point_at(current_line)
end
automation:add_point_at(current_line, normalized_value)
-- Copy selection range from pattern editor to automation envelope
local pattern_selection = song.selection_in_pattern
if pattern_selection then
local start_line = pattern_selection.start_line
local end_line = pattern_selection.end_line
if start_line and end_line and start_line > 0 and end_line > 0 then
-- Set selection range in automation envelope (using line numbers directly)
automation.selection_range = {start_line, end_line}
print("AUTOMATE_LAST_TOUCHED: Set automation selection range: " .. start_line .. " to " .. end_line)
end
end
pcall(function()
renoise.app().window.lower_frame_is_visible = true
renoise.app().window.active_lower_frame = renoise.ApplicationWindow.LOWER_FRAME_TRACK_AUTOMATION
song.selected_automation_parameter = param
end)
renoise.app():show_status("Created automation for: " .. param_name)
return true
end
return false
end
--------------------------------------------------------------------------------
-- Write continuous automation point
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedWriteContinuousPoint(param, track_index)
local song = renoise.song()
song:describe_undo("Paketti: Write Continuous Automation Point")
if not song or not song.transport.edit_mode then return end
local current_pattern = song.selected_pattern_index
local pattern_track = song:pattern(current_pattern):track(track_index)
local automation = pattern_track:find_automation(param)
if not automation then
automation = pattern_track:create_automation(param)
end
if automation then
local line_pos = PakettiAutomateLastTouchedGetFractionalLinePos() or song.selected_line_index
local normalized_value = (param.value - param.value_min) / (param.value_max - param.value_min)
normalized_value = math.max(0.0, math.min(1.0, normalized_value))
automation:add_point_at(line_pos, normalized_value)
end
end
--------------------------------------------------------------------------------
-- Parameter Observer Callbacks
--------------------------------------------------------------------------------
-- For Shortcut First mode - instant automation creation
function PakettiAutomateLastTouchedOnParameterTouched(param, device, track_index, is_plugin_instrument)
if not watching_active then return end
print("AUTOMATE_LAST_TOUCHED: Parameter touched: " .. param.name)
local continuous_recording = PakettiAutomateLastTouchedGetPref("ContinuousRecording", false)
if continuous_recording then
if not continuous_mode_active then
continuous_mode_active = true
PakettiAutomateLastTouchedCreateForParameter(param, device, track_index, is_plugin_instrument)
renoise.app():show_status("Continuous recording started for: " .. param.name .. " - press shortcut to stop")
else
PakettiAutomateLastTouchedWriteContinuousPoint(param, track_index)
end
else
PakettiAutomateLastTouchedCreateForParameter(param, device, track_index, is_plugin_instrument)
PakettiAutomateLastTouchedCleanupTemporary()
end
end
-- For Persistent modes - just store the last touched parameter
function PakettiAutomateLastTouchedOnPersistentParameterTouched(param, device, track_index, is_plugin_instrument)
if not persistent_watching_active then
print("AUTOMATE_LAST_TOUCHED: Parameter touched but persistent watching not active")
return
end
local watching_mode = PakettiAutomateLastTouchedGetPref("WatchingMode", WATCHING_MODE_SHORTCUT_FIRST)
-- Timer mode: start timer when parameter is touched
if watching_mode == WATCHING_MODE_TIMER then
PakettiAutomateLastTouchedStartTimerMode(param, device, track_index, is_plugin_instrument)
return
end
-- Other persistent modes: just store the parameter
global_last_touched_parameter = param
global_last_touched_device = device
global_last_touched_track_index = track_index
global_last_touched_is_instrument = is_plugin_instrument
local device_name = get_device_name(device)
print("AUTOMATE_LAST_TOUCHED: Stored last touched: " .. param.name .. " on device: " .. device_name)
renoise.app():show_status("Last touched: " .. param.name .. " - press shortcut to automate")
end
--------------------------------------------------------------------------------
-- Setup Watching for Devices
--------------------------------------------------------------------------------
-- Add observers to a single device
function PakettiAutomateLastTouchedAddDeviceObservers(device, track_index, is_plugin_instrument, observer_table, callback)
if not device then return 0 end
local param_count = 0
local device_name = get_device_name(device)
for i = 1, #device.parameters do
local param = device.parameters[i]
if param.is_automatable then
local observer = function()
callback(param, device, track_index, is_plugin_instrument)
end
if not param.value_observable:has_notifier(observer) then
param.value_observable:add_notifier(observer)
observer_table[param] = observer
param_count = param_count + 1
end
end
end
if param_count > 0 then
print("AUTOMATE_LAST_TOUCHED: Added " .. param_count .. " observers to device: " .. device_name)
end
return param_count
end
-- Setup temporary watching (Shortcut First mode)
function PakettiAutomateLastTouchedSetupTemporaryWatching(device, track_index, is_plugin_instrument)
return PakettiAutomateLastTouchedAddDeviceObservers(
device, track_index, is_plugin_instrument,
parameter_observers, PakettiAutomateLastTouchedOnParameterTouched
)
end
-- Get devices to watch based on scope
function PakettiAutomateLastTouchedGetDevicesToWatch()
local song = renoise.song()
if not song then return {} end
local scope = PakettiAutomateLastTouchedGetPref("DeviceScope", DEVICE_SCOPE_SELECTED)
local devices = {}
if scope == DEVICE_SCOPE_SELECTED then
-- Selected device only
if song.selected_device then
table.insert(devices, {
device = song.selected_device,
track_index = song.selected_track_index,
is_instrument = false
})
end
-- Also check for plugin instrument
local instrument = song.selected_instrument
if instrument and instrument.plugin_properties and instrument.plugin_properties.plugin_loaded then
table.insert(devices, {
device = instrument.plugin_properties.plugin_device,
track_index = song.selected_track_index,
is_instrument = true
})
end
elseif scope == DEVICE_SCOPE_ALL_TRACK then
-- All devices on current track
local track_index = song.selected_track_index
local track = song.tracks[track_index]
if track then
for i, device in ipairs(track.devices) do
if i > 1 then -- Skip track mixer (device 1)
table.insert(devices, {
device = device,
track_index = track_index,
is_instrument = false
})
end
end
end
-- Also check for plugin instrument
local instrument = song.selected_instrument
if instrument and instrument.plugin_properties and instrument.plugin_properties.plugin_loaded then
table.insert(devices, {
device = instrument.plugin_properties.plugin_device,
track_index = track_index,
is_instrument = true
})
end
elseif scope == DEVICE_SCOPE_ALL_SONG then
-- All devices in entire song
for track_index, track in ipairs(song.tracks) do
for i, device in ipairs(track.devices) do
if i > 1 then -- Skip track mixer
table.insert(devices, {
device = device,
track_index = track_index,
is_instrument = false
})
end
end
end
-- All plugin instruments
for i, instrument in ipairs(song.instruments) do
if instrument.plugin_properties and instrument.plugin_properties.plugin_loaded then
table.insert(devices, {
device = instrument.plugin_properties.plugin_device,
track_index = song.selected_track_index,
is_instrument = true
})
end
end
end
return devices
end
--------------------------------------------------------------------------------
-- Persistent Watching Setup
--------------------------------------------------------------------------------
-- Refresh persistent watching (called on track change, device add/remove)
function PakettiAutomateLastTouchedRefreshPersistentWatching()
if not persistent_watching_active then return end
print("AUTOMATE_LAST_TOUCHED: Refreshing persistent watching")
-- Remove old parameter observers
for parameter, observer in pairs(persistent_parameter_observers) do
pcall(function()
if parameter and parameter.value_observable and parameter.value_observable:has_notifier(observer) then
parameter.value_observable:remove_notifier(observer)
end
end)
end
persistent_parameter_observers = {}
-- Get devices to watch based on scope
local devices = PakettiAutomateLastTouchedGetDevicesToWatch()
local total_params = 0
for _, entry in ipairs(devices) do
local count = PakettiAutomateLastTouchedAddDeviceObservers(
entry.device, entry.track_index, entry.is_instrument,
persistent_parameter_observers, PakettiAutomateLastTouchedOnPersistentParameterTouched
)
total_params = total_params + count
end
print("AUTOMATE_LAST_TOUCHED: Persistent watching " .. #devices .. " devices, " .. total_params .. " parameters")
end
-- Setup device list observer for a track
function PakettiAutomateLastTouchedSetupDeviceListObserver(track, track_index)
if device_list_observers[track] then return end
local refresh_timer = nil
local observer = function()
print("AUTOMATE_LAST_TOUCHED: Device list changed on track " .. track_index .. " - scheduling refresh")
-- Remove existing refresh timer if any
if refresh_timer and renoise.tool():has_timer(refresh_timer) then
renoise.tool():remove_timer(refresh_timer)
end
-- Small delay to ensure device is fully added before refreshing
refresh_timer = function()
if refresh_timer and renoise.tool():has_timer(refresh_timer) then
renoise.tool():remove_timer(refresh_timer)
end
refresh_timer = nil
PakettiAutomateLastTouchedRefreshPersistentWatching()
end
renoise.tool():add_timer(refresh_timer, 50)
end
if not track.devices_observable:has_notifier(observer) then
track.devices_observable:add_notifier(observer)
device_list_observers[track] = observer
print("AUTOMATE_LAST_TOUCHED: Device list observer added for track " .. track_index)
end
end
-- Setup persistent watching infrastructure
function PakettiAutomateLastTouchedSetupPersistentWatching()
local song = renoise.song()
if not song then return end
local watching_mode = PakettiAutomateLastTouchedGetPref("WatchingMode", WATCHING_MODE_SHORTCUT_FIRST)
if watching_mode == WATCHING_MODE_SHORTCUT_FIRST then
return -- No persistent watching needed
end
print("AUTOMATE_LAST_TOUCHED: Setting up persistent watching (mode=" .. watching_mode .. ")")
-- Cleanup any existing persistent watching and timer mode
PakettiAutomateLastTouchedCleanupPersistent()
PakettiAutomateLastTouchedStopTimerMode()
persistent_watching_active = true
-- Setup track change observer (for Track Watching mode only)
if watching_mode == WATCHING_MODE_TRACK_WATCHING then
track_change_observer = function()
print("AUTOMATE_LAST_TOUCHED: Track changed")
PakettiAutomateLastTouchedRefreshPersistentWatching()
end
if not song.selected_track_index_observable:has_notifier(track_change_observer) then
song.selected_track_index_observable:add_notifier(track_change_observer)
end
end
-- Setup device list observers (for Track Watching and Always Watching modes)
local scope = PakettiAutomateLastTouchedGetPref("DeviceScope", DEVICE_SCOPE_SELECTED)
if watching_mode == WATCHING_MODE_TRACK_WATCHING or watching_mode == WATCHING_MODE_ALWAYS_WATCHING then
if scope == DEVICE_SCOPE_ALL_TRACK then
-- Only current track
local track = song.tracks[song.selected_track_index]
if track then
PakettiAutomateLastTouchedSetupDeviceListObserver(track, song.selected_track_index)
end
elseif scope == DEVICE_SCOPE_ALL_SONG then
-- All tracks
for track_index, track in ipairs(song.tracks) do
PakettiAutomateLastTouchedSetupDeviceListObserver(track, track_index)
end
end
end
-- Initial setup of parameter observers
PakettiAutomateLastTouchedRefreshPersistentWatching()
local scope_names = {"Selected Device", "All Track Devices", "All Song Devices"}
local mode_names = {"Shortcut First", "Track Watching", "Always Watching", "Timer Mode"}
renoise.app():show_status("Persistent watching active: " .. mode_names[watching_mode] .. " + " .. scope_names[scope])
end
--------------------------------------------------------------------------------
-- Main Entry Point
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedStart()
local song = renoise.song()
if not song then
renoise.app():show_status("No song available")
return
end
local watching_mode = PakettiAutomateLastTouchedGetPref("WatchingMode", WATCHING_MODE_SHORTCUT_FIRST)
-- Timer mode: check if timer is active and parameter is stored
if watching_mode == WATCHING_MODE_TIMER then
-- If persistent watching isn't active, set it up
if not persistent_watching_active then
PakettiAutomateLastTouchedSetupPersistentWatching()
return
end
-- Check if timer is active and we have a stored parameter
if timer_mode_active and timer_mode_stored_param then
-- Create automation immediately
PakettiAutomateLastTouchedCreateForParameter(
timer_mode_stored_param,
timer_mode_stored_device,
timer_mode_stored_track_index,
timer_mode_stored_is_instrument
)
-- Stop timer and cleanup
PakettiAutomateLastTouchedStopTimerMode()
else
-- Timer not active or no parameter stored
local timer_duration = PakettiAutomateLastTouchedGetPref("TimerDuration", 3)
renoise.app():show_status("Touch a parameter first, then press shortcut within " .. timer_duration .. " seconds to automate")
end
return
end
-- For persistent modes (2 and 3): check if we have a stored parameter
if watching_mode == WATCHING_MODE_TRACK_WATCHING or watching_mode == WATCHING_MODE_ALWAYS_WATCHING then
-- If persistent watching isn't active, set it up
if not persistent_watching_active then
PakettiAutomateLastTouchedSetupPersistentWatching()
return
end
-- Check if we have a last touched parameter
if global_last_touched_parameter then
-- Create automation immediately
PakettiAutomateLastTouchedCreateForParameter(
global_last_touched_parameter,
global_last_touched_device,
global_last_touched_track_index,
global_last_touched_is_instrument
)
-- Clear the stored parameter
global_last_touched_parameter = nil
global_last_touched_device = nil
global_last_touched_track_index = nil
global_last_touched_is_instrument = false
else
-- No parameter detected - refresh observers in case a new device was added
print("AUTOMATE_LAST_TOUCHED: No parameter detected, refreshing observers...")
PakettiAutomateLastTouchedRefreshPersistentWatching()
-- Check again after refresh
if global_last_touched_parameter then
PakettiAutomateLastTouchedCreateForParameter(
global_last_touched_parameter,
global_last_touched_device,
global_last_touched_track_index,
global_last_touched_is_instrument
)
global_last_touched_parameter = nil
global_last_touched_device = nil
global_last_touched_track_index = nil
global_last_touched_is_instrument = false
else
local devices = PakettiAutomateLastTouchedGetDevicesToWatch()
local total_params = 0
for _, entry in ipairs(devices) do
if entry.device then
for i = 1, #entry.device.parameters do
if entry.device.parameters[i].is_automatable then
total_params = total_params + 1
end
end
end
end
renoise.app():show_status("Touch a parameter first, then press shortcut to automate (watching " .. #devices .. " devices, " .. total_params .. " params)")
end
end
return
end
-- Mode 1 (Shortcut First): Original behavior
-- If already watching, cleanup and stop
if watching_active then
PakettiAutomateLastTouchedCleanupTemporary()
renoise.app():show_status("Stopped watching for parameter changes")
return
end
PakettiAutomateLastTouchedCleanupTemporary()
-- Get devices to watch based on scope
local scope = PakettiAutomateLastTouchedGetPref("DeviceScope", DEVICE_SCOPE_SELECTED)
local devices = PakettiAutomateLastTouchedGetDevicesToWatch()
if #devices == 0 then
renoise.app():show_status("No device selected - select a track device or plugin instrument first")
return
end
-- Auto-open external editor if preference is enabled
local auto_open_editor = PakettiAutomateLastTouchedGetPref("AutoOpenExternalEditor", true)
if auto_open_editor then
for _, entry in ipairs(devices) do
if entry.device.external_editor_available then
entry.device.external_editor_visible = true
end
end
end
watching_active = true
watching_track_index = song.selected_track_index
-- Setup watching for all devices
local total_params = 0
for _, entry in ipairs(devices) do
local count = PakettiAutomateLastTouchedSetupTemporaryWatching(entry.device, entry.track_index, entry.is_instrument)
total_params = total_params + count
end
if total_params > 0 then
local scope_names = {"Selected Device", "All Track Devices", "All Song Devices"}
local continuous = PakettiAutomateLastTouchedGetPref("ContinuousRecording", false)
local mode_str = continuous and " [CONTINUOUS]" or ""
renoise.app():show_status("Watching " .. #devices .. " device(s), " .. total_params .. " params (" .. scope_names[scope] .. ")" .. mode_str .. " - touch a parameter...")
else
watching_active = false
renoise.app():show_status("No automatable parameters found")
end
end
--------------------------------------------------------------------------------
-- Song Change Handler
--------------------------------------------------------------------------------
-- Register the song-release safety observer (idempotent). Fires BEFORE the old
-- song is freed; runs the existing teardown so observers/timers detach while
-- renoise.song() is still the OLD song. See features/song-lifecycle-safety.feature.
function PakettiAutomateLastTouchedRegisterReleaseDocObserver()
if not automate_last_touched_release_doc_observer then
automate_last_touched_release_doc_observer = function()
PakettiAutomateLastTouchedCleanup() -- detaches observers from the still-alive old song; removes this observer
end
end
if not renoise.tool().app_release_document_observable:has_notifier(automate_last_touched_release_doc_observer) then
renoise.tool().app_release_document_observable:add_notifier(automate_last_touched_release_doc_observer)
end
end
function PakettiAutomateLastTouchedOnSongChange()
print("AUTOMATE_LAST_TOUCHED: Song changed - cleaning up")
PakettiAutomateLastTouchedCleanup()
-- Re-arm the release-doc seatbelt for the NEW song (Cleanup detached it).
PakettiAutomateLastTouchedRegisterReleaseDocObserver()
-- Re-setup persistent watching if mode requires it
local watching_mode = PakettiAutomateLastTouchedGetPref("WatchingMode", WATCHING_MODE_SHORTCUT_FIRST)
if watching_mode == WATCHING_MODE_TRACK_WATCHING or watching_mode == WATCHING_MODE_ALWAYS_WATCHING or watching_mode == WATCHING_MODE_TIMER then
-- Remove existing timer if it exists
if persistent_setup_timer and renoise.tool():has_timer(persistent_setup_timer) then
renoise.tool():remove_timer(persistent_setup_timer)
end
-- Delay setup to ensure song is fully loaded
persistent_setup_timer = function()
if persistent_setup_timer and renoise.tool():has_timer(persistent_setup_timer) then
renoise.tool():remove_timer(persistent_setup_timer)
end
persistent_setup_timer = nil
PakettiAutomateLastTouchedSetupPersistentWatching()
end
renoise.tool():add_timer(persistent_setup_timer, 100)
end
end
if not renoise.tool().app_new_document_observable:has_notifier(PakettiAutomateLastTouchedOnSongChange) then
renoise.tool().app_new_document_observable:add_notifier(PakettiAutomateLastTouchedOnSongChange)
end
-- Song-lifecycle safety seatbelt: registered at load (persistent observers can
-- be active without any dialog). Tears down BEFORE the song is released so
-- observers detach from the OLD song. See features/song-lifecycle-safety.feature.
PakettiAutomateLastTouchedRegisterReleaseDocObserver()
--------------------------------------------------------------------------------
-- Initialize Persistent Watching on Tool Load
--------------------------------------------------------------------------------
function PakettiAutomateLastTouchedInitialize()
local watching_mode = PakettiAutomateLastTouchedGetPref("WatchingMode", WATCHING_MODE_SHORTCUT_FIRST)
if watching_mode == WATCHING_MODE_TRACK_WATCHING or watching_mode == WATCHING_MODE_ALWAYS_WATCHING or watching_mode == WATCHING_MODE_TIMER then
print("AUTOMATE_LAST_TOUCHED: Initializing persistent watching on tool load")
-- Remove existing timer if it exists
if persistent_setup_timer and renoise.tool():has_timer(persistent_setup_timer) then
renoise.tool():remove_timer(persistent_setup_timer)
end
-- Delay to ensure song is loaded
persistent_setup_timer = function()
if persistent_setup_timer and renoise.tool():has_timer(persistent_setup_timer) then
renoise.tool():remove_timer(persistent_setup_timer)
end
persistent_setup_timer = nil
PakettiAutomateLastTouchedSetupPersistentWatching()
end
renoise.tool():add_timer(persistent_setup_timer, 500)
end
end
-- Call initialize (will only setup persistent watching if mode requires it)
PakettiAutomateLastTouchedInitialize()
--------------------------------------------------------------------------------
-- Keybindings and Menu Entries
--------------------------------------------------------------------------------
renoise.tool():add_keybinding{
name = "Global:Paketti:Automate Last Touched Parameter",
invoke = PakettiAutomateLastTouchedStart
}
renoise.tool():add_keybinding{
name = "Pattern Editor:Paketti:Automate Last Touched Parameter",
invoke = PakettiAutomateLastTouchedStart
}
renoise.tool():add_keybinding{
name = "Mixer:Paketti:Automate Last Touched Parameter",
invoke = PakettiAutomateLastTouchedStart
}
PakettiAddMenuEntry{
name = "Main Menu:Tools:Paketti:Automate Last Touched Parameter...",
invoke = PakettiAutomateLastTouchedStart
}
PakettiAddMenuEntry{
name = "DSP Device:Paketti:Automate Last Touched Parameter",
invoke = PakettiAutomateLastTouchedStart
}
PakettiAddMenuEntry{
name = "Mixer:Paketti Gadgets:Automate Last Touched Parameter",
invoke = PakettiAutomateLastTouchedStart
}
--------------------------------------------------------------------------------
-- MIDI Mappings
--------------------------------------------------------------------------------
renoise.tool():add_midi_mapping{
name = "Paketti:Automate Last Touched Parameter [Trigger]",
invoke = function(message)
if message:is_trigger() then
PakettiAutomateLastTouchedStart()
end
end
}