-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPakettiAutomationCurves.lua
More file actions
2037 lines (1758 loc) · 72.2 KB
/
Copy pathPakettiAutomationCurves.lua
File metadata and controls
2037 lines (1758 loc) · 72.2 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
-- PakettiAutomationCurves.lua
-- Paketti Enhanced Automation - Rapid automation envelope drawing with shape presets
-- Inspired by various automation tools, enhanced for Paketti workflow
-- Dialog and ViewBuilder references
PakettiAutomationCurvesDialog = nil
PakettiAutomationCurvesVb = nil
-- Control state
PakettiAutomationCurvesOffset = 0.0
PakettiAutomationCurvesAttenuation = 1.0
PakettiAutomationCurvesInputDivisor = 1
PakettiAutomationCurvesStepLength = 4
PakettiAutomationCurvesMoveBySelection = false
-- LFO Custom Waveform state
PakettiAutomationCurvesWriteToLFO = false
PakettiAutomationCurvesLFOEnvelopeLength = 64
PakettiAutomationCurvesSelectedShape = nil -- Currently selected shape name
-- Automation Clipboard state
PakettiAutomationClipboard = {
points = {}, -- Array of {relative_time, value, scaling}
length = 0, -- Original selection length in lines
playmode = 1, -- Original playmode for reference
source_param = "" -- Source parameter name (for display)
}
PakettiAutomationClipboardPasteMode = "normal" -- "normal", "stretch", "floodfill"
-- Shape data (initialized at load time)
PakettiAutomationCurvesShapes = nil
PakettiAutomationCurvesKeyMap = nil
-- Image path for bitmaps
PakettiAutomationCurvesImagePath = "images/automation_shapes/"
------------------------------------------------------------------------
-- Validation Helper Functions
------------------------------------------------------------------------
-- Clamp step length to valid range (1 to num_lines)
function PakettiAutomationCurvesClampStepLength(step_length, num_lines)
if not num_lines or num_lines < 1 then
num_lines = 1
end
if step_length < 1 then
return 1
elseif step_length > num_lines then
return num_lines
else
return step_length
end
end
------------------------------------------------------------------------
-- Shape Definitions
-- Each shape has: values (array of {x, y} points), key (keyboard shortcut)
------------------------------------------------------------------------
-- Resolution for calculated curves
PakettiAutomationCurveResolution = 16
-- Pre-calculate curved shapes
function PakettiAutomationCurvesCalculateShapes()
local res = PakettiAutomationCurveResolution
local sinUp_values = {}
local sinDown_values = {}
local circBr_values = {}
local circTr_values = {}
local circTl_values = {}
local circBl_values = {}
local cosUp_values = {}
local cosDown_values = {}
local bellUp_values = {}
local bellDown_values = {}
local sCurveUp_values = {}
local sCurveDown_values = {}
local bounceUp_values = {}
local bounceDown_values = {}
local logUp_values = {}
local logDown_values = {}
local expUp_values = {}
local expDown_values = {}
local pumpDown_values = {}
local pumpUp_values = {}
for i = 0, (res - 1) do
local x = i / res
-- Sine curves
table.insert(sinUp_values, {x, math.sin(x * 3.141)})
table.insert(sinDown_values, {x, 1 - math.sin(x * 3.141)})
-- Circle quadrants
table.insert(circBr_values, {x, 1 - math.sqrt(1 - x * x)})
table.insert(circTr_values, {x, math.sqrt(1 - x * x)})
table.insert(circTl_values, {x, math.sqrt(2 * x - x * x)})
table.insert(circBl_values, {x, 1 - math.sqrt(2 * x - x * x)})
-- Cosine curves
table.insert(cosUp_values, {x, 1 - (0.5 * math.cos(x * 3.14) + 0.5)})
table.insert(cosDown_values, {x, 0.5 * math.cos(x * 3.14) + 0.5})
-- Bell curve (Gaussian)
local gaussX = (x - 0.5) * 4
local gaussY = math.exp(-gaussX * gaussX / 2)
table.insert(bellUp_values, {x, gaussY})
table.insert(bellDown_values, {x, 1 - gaussY})
-- S-Curve (Sigmoid)
local sigX = (x - 0.5) * 12
local sigY = 1 / (1 + math.exp(-sigX))
table.insert(sCurveUp_values, {x, sigY})
table.insert(sCurveDown_values, {x, 1 - sigY})
-- Bounce (decaying sine)
local bounceDecay = math.exp(-x * 3)
local bounceOsc = math.abs(math.sin(x * 3.141 * 4))
table.insert(bounceUp_values, {x, (1 - bounceDecay * bounceOsc)})
table.insert(bounceDown_values, {x, bounceDecay * bounceOsc})
-- Log / exponential style ramps
local logY = x * x * x
local expY = 1 - ((1 - x) * (1 - x) * (1 - x))
table.insert(logUp_values, {x, logY})
table.insert(logDown_values, {x, 1 - logY})
table.insert(expUp_values, {x, expY})
table.insert(expDown_values, {x, (1 - x) * (1 - x) * (1 - x)})
-- Half-sine pump / lift
local pumpY = 0.5 + 0.5 * math.cos(x * 2 * 3.141)
table.insert(pumpDown_values, {x, pumpY})
table.insert(pumpUp_values, {x, 1 - pumpY})
end
-- Add final points
table.insert(sinUp_values, {0.99, 0})
table.insert(sinDown_values, {0.99, 1})
table.insert(circBr_values, {0.99, 1})
table.insert(circTr_values, {0.99, 0})
table.insert(circTl_values, {0.99, 1})
table.insert(circBl_values, {0.99, 0})
table.insert(cosUp_values, {0.99, 1})
table.insert(cosDown_values, {0.99, 0})
table.insert(bellUp_values, {0.99, 0})
table.insert(bellDown_values, {0.99, 1})
table.insert(sCurveUp_values, {0.99, 1})
table.insert(sCurveDown_values, {0.99, 0})
table.insert(bounceUp_values, {0.99, 1})
table.insert(bounceDown_values, {0.99, 0})
table.insert(logUp_values, {0.99, 1})
table.insert(logDown_values, {0.99, 0})
table.insert(expUp_values, {0.99, 1})
table.insert(expDown_values, {0.99, 0})
table.insert(pumpDown_values, {0.99, 1})
table.insert(pumpUp_values, {0.99, 0})
return {
sinUp = sinUp_values,
sinDown = sinDown_values,
circBr = circBr_values,
circTr = circTr_values,
circTl = circTl_values,
circBl = circBl_values,
cosUp = cosUp_values,
cosDown = cosDown_values,
bellUp = bellUp_values,
bellDown = bellDown_values,
sCurveUp = sCurveUp_values,
sCurveDown = sCurveDown_values,
bounceUp = bounceUp_values,
bounceDown = bounceDown_values,
logUp = logUp_values,
logDown = logDown_values,
expUp = expUp_values,
expDown = expDown_values,
pumpDown = pumpDown_values,
pumpUp = pumpUp_values
}
end
-- Calculate trapezoid shapes
function PakettiAutomationCurvesCalculateTrapezoid()
local res = PakettiAutomationCurveResolution
local trapUp_values = {{0, 0}, {1/3, 1/2}}
local trapDown_values = {{0, 1}, {1/3, 1/2}}
for i = 0, (math.floor(res / (3/2)) - 1) do
local p = (2/3) * i / (math.floor(res / (3/2)))
local x = 1/3 + p
local h2 = (3/2) - (9/4) * p
local y = (1/2) + (h2) * p + (((3/2) - h2) * p / 2)
table.insert(trapUp_values, {x, y})
table.insert(trapDown_values, {x, 1 - y})
end
table.insert(trapUp_values, {0.99, 1})
table.insert(trapDown_values, {0.99, 0})
return {
trapUp = trapUp_values,
trapDown = trapDown_values
}
end
-- Initialize all shapes
function PakettiAutomationCurvesInitShapes()
local calculated = PakettiAutomationCurvesCalculateShapes()
local trapezoid = PakettiAutomationCurvesCalculateTrapezoid()
local CURVES = PAKETTI_PLAYMODE_CURVES
local LINES = PAKETTI_PLAYMODE_LINES
local POINTS = PAKETTI_PLAYMODE_POINTS
PakettiAutomationCurvesShapes = {
-- Ramps (linear)
rampUp = {values = {{0, 0}, {0.99, 1}}, key = "q", image = "ramp-up.png", label = "Ramp Up", playmode = LINES},
rampDown = {values = {{0, 1}, {0.99, 0}}, key = "w", image = "ramp-down.png", label = "Ramp Down", playmode = LINES},
-- Circle quadrants (cubic curves for smooth arcs)
circTl = {values = calculated.circTl, key = "e", image = "circ-tl.png", label = "Curve TL", playmode = CURVES},
circTr = {values = calculated.circTr, key = "r", image = "circ-tr.png", label = "Curve TR", playmode = CURVES},
-- Squares (step/hold for sharp transitions)
sqUp = {values = {{0, 0}, {0.5, 0}, {0.51, 1}, {0.99, 1}}, key = "t", image = "sq-up.png", label = "Square Up", playmode = POINTS},
sqDown = {values = {{0, 1}, {0.5, 1}, {0.51, 0}, {0.99, 0}}, key = "y", image = "sq-down.png", label = "Square Down", playmode = POINTS},
-- Trapezoids (linear interpolation)
trapUp = {values = trapezoid.trapUp, key = "u", image = "trap-up.png", label = "Trapezoid Up", playmode = LINES},
trapDown = {values = trapezoid.trapDown, key = "i", image = "trap-down.png", label = "Trapezoid Down", playmode = LINES},
-- Triangle/Vee (linear interpolation)
tri = {values = {{0, 0}, {0.5, 1}, {0.99, 0}}, key = "a", image = "tri.png", label = "Triangle", playmode = LINES},
vee = {values = {{0, 1}, {0.5, 0}, {0.99, 1}}, key = "s", image = "vee.png", label = "Vee", playmode = LINES},
-- Circle quadrants (bottom, cubic curves)
circBl = {values = calculated.circBl, key = "d", image = "circ-bl.png", label = "Curve BL", playmode = CURVES},
circBr = {values = calculated.circBr, key = "f", image = "circ-br.png", label = "Curve BR", playmode = CURVES},
-- Sine (cubic curves for smooth interpolation)
sinUp = {values = calculated.sinUp, key = "g", image = "sin-up.png", label = "Sine Up", playmode = CURVES},
sinDown = {values = calculated.sinDown, key = "h", image = "sin-down.png", label = "Sine Down", playmode = CURVES},
-- Stairs (step/hold for sharp transitions)
stairUp = {values = {{0, 0}, {0.25, 0}, {0.26, 0.25}, {0.5, 0.25}, {0.51, 0.5}, {0.75, 0.5}, {0.76, 0.75}, {0.98, 0.75}, {0.99, 1}}, key = "z", image = "stair-up.png", label = "Stairs Up", playmode = POINTS},
stairDown = {values = {{0, 1}, {0.25, 1}, {0.26, 0.75}, {0.5, 0.75}, {0.51, 0.5}, {0.75, 0.5}, {0.76, 0.25}, {0.98, 0.25}, {0.99, 0}}, key = "x", image = "stair-down.png", label = "Stairs Down", playmode = POINTS},
-- Cosine (cubic curves for smooth interpolation)
cosUp = {values = calculated.cosUp, key = "c", image = "cos-up.png", label = "Cosine Up", playmode = CURVES},
cosDown = {values = calculated.cosDown, key = "v", image = "cos-down.png", label = "Cosine Down", playmode = CURVES},
-- On/Off constants (step/hold)
off = {values = {{0, 0}, {0.99, 0}}, key = "1", image = "off.png", label = "Constant Off", playmode = POINTS},
on = {values = {{0, 1}, {0.99, 1}}, key = "0", image = "on.png", label = "Constant On", playmode = POINTS},
-- Bell curve (cubic curves for smooth Gaussian shape)
bellUp = {values = calculated.bellUp, key = "o", image = "bell-up.png", label = "Bell Up", playmode = CURVES},
bellDown = {values = calculated.bellDown, key = "p", image = "bell-down.png", label = "Bell Down", playmode = CURVES},
-- S-Curve (cubic curves for smooth sigmoid)
sCurveUp = {values = calculated.sCurveUp, key = "j", image = "scurve-up.png", label = "S-Curve Up", playmode = CURVES},
sCurveDown = {values = calculated.sCurveDown, key = "k", image = "scurve-down.png", label = "S-Curve Down", playmode = CURVES},
-- Bounce (cubic curves for smooth decay)
bounceUp = {values = calculated.bounceUp, key = "l", image = "bounce-up.png", label = "Bounce Up", playmode = CURVES},
bounceDown = {values = calculated.bounceDown, key = "b", image = "bounce-down.png", label = "Bounce Down", playmode = CURVES},
-- Additional musical ramps / pumps
logUp = {values = calculated.logUp, image = "log-up.png", label = "Log Up", playmode = CURVES},
logDown = {values = calculated.logDown, image = "log-down.png", label = "Log Down", playmode = CURVES},
expUp = {values = calculated.expUp, image = "exp-up.png", label = "Exponential Up", playmode = CURVES},
expDown = {values = calculated.expDown, image = "exp-down.png", label = "Exponential Down", playmode = CURVES},
pumpDown = {values = calculated.pumpDown, image = "pump-down.png", label = "Half-Sine Pump", playmode = CURVES},
pumpUp = {values = calculated.pumpUp, image = "pump-up.png", label = "Half-Sine Lift", playmode = CURVES},
doublePulse = {values = {{0, 0}, {0.18, 0}, {0.19, 1}, {0.39, 1}, {0.4, 0}, {0.58, 0}, {0.59, 1}, {0.79, 1}, {0.8, 0}, {0.99, 0}}, image = "double-pulse.png", label = "Double Pulse", playmode = POINTS},
-- Pulse variations (step/hold for sharp transitions) - keys 2-9
pulse10 = {values = {{0, 0}, {0.10, 0}, {0.11, 1}, {0.99, 1}}, key = "2", image = "pulse10.png", label = "Pulse 10%", playmode = POINTS},
pulse25 = {values = {{0, 0}, {0.25, 0}, {0.26, 1}, {0.99, 1}}, key = "3", image = "pulse25.png", label = "Pulse 25%", playmode = POINTS},
pulse33 = {values = {{0, 0}, {0.33, 0}, {0.34, 1}, {0.99, 1}}, key = "4", image = "pulse33.png", label = "Pulse 33%", playmode = POINTS},
pulse50 = {values = {{0, 0}, {0.5, 0}, {0.51, 1}, {0.99, 1}}, key = "5", image = "pulse50.png", label = "Pulse 50%", playmode = POINTS},
pulse66 = {values = {{0, 0}, {0.66, 0}, {0.67, 1}, {0.99, 1}}, key = "6", image = "pulse66.png", label = "Pulse 66%", playmode = POINTS},
pulse75 = {values = {{0, 0}, {0.75, 0}, {0.76, 1}, {0.99, 1}}, key = "7", image = "pulse75.png", label = "Pulse 75%", playmode = POINTS},
pulse80 = {values = {{0, 0}, {0.80, 0}, {0.81, 1}, {0.99, 1}}, key = "8", image = "pulse80.png", label = "Pulse 80%", playmode = POINTS},
pulse90 = {values = {{0, 0}, {0.90, 0}, {0.91, 1}, {0.99, 1}}, key = "9", image = "pulse90.png", label = "Pulse 90%", playmode = POINTS},
-- Random (generated at insert time)
randomSmooth = {values = nil, key = "n", image = "random-smooth.png", generator = "smooth", label = "Random Smooth", playmode = CURVES},
randomStep = {values = nil, key = "m", image = "random-step.png", generator = "step", label = "Random Step", playmode = POINTS},
-- Sawtooth with overshoot (cubic curves for smooth oscillation)
sawtoothUp = {values = {{0, 0}, {0.8, 1.1}, {0.85, 0.95}, {0.9, 1.02}, {0.95, 0.99}, {0.99, 1}}, key = "comma", image = "sawtooth-up.png", label = "Sawtooth Up", playmode = CURVES},
sawtoothDown = {values = {{0, 1}, {0.8, -0.1}, {0.85, 0.05}, {0.9, -0.02}, {0.95, 0.01}, {0.99, 0}}, key = "period", image = "sawtooth-down.png", label = "Sawtooth Down", playmode = CURVES}
}
-- Build reverse key map
PakettiAutomationCurvesKeyMap = {}
for name, shape in pairs(PakettiAutomationCurvesShapes) do
if shape.key then
PakettiAutomationCurvesKeyMap[shape.key] = name
end
end
end
------------------------------------------------------------------------
-- Get or Create Automation Envelope
------------------------------------------------------------------------
function PakettiAutomationCurvesGetAutomation()
local ra = renoise.app()
ra.window.active_lower_frame = renoise.ApplicationWindow.LOWER_FRAME_TRACK_AUTOMATION
local rs = renoise.song()
local track = rs.selected_pattern_track
local param = rs.selected_automation_parameter
if not param then
renoise.app():show_status("No automation parameter selected")
return nil
end
local automation = track:find_automation(param)
if automation == nil then
automation = track:create_automation(param)
print("PakettiAutomationCurves: Created new automation envelope for " .. param.name)
end
return automation
end
------------------------------------------------------------------------
-- Generate Random Values for Random Shapes
------------------------------------------------------------------------
function PakettiAutomationCurvesGenerateRandom(generator_type, num_points)
local values = {}
num_points = num_points or 16
if generator_type == "smooth" then
-- Smooth random using interpolation
local prev = math.random()
for i = 0, num_points - 1 do
local x = i / num_points
local target = math.random()
local value = prev + (target - prev) * 0.3
prev = value
table.insert(values, {x, math.max(0, math.min(1, value))})
end
else
-- Step random
for i = 0, num_points - 1 do
local x = i / num_points
table.insert(values, {x, math.random()})
end
end
table.insert(values, {0.99, values[#values][2]})
return values
end
------------------------------------------------------------------------
-- Write Shape to LFO Custom Waveform
------------------------------------------------------------------------
function PakettiAutomationCurvesWriteToLFOCustom(shape_name)
local rs = renoise.song()
if not rs then
renoise.app():show_status("No song loaded")
return false
end
local device = rs.selected_device
if not device then
renoise.app():show_status("No device selected")
return false
end
-- Check if device is an LFO
if device.name ~= "*LFO" then
renoise.app():show_status("Selected device is not an LFO (got: " .. device.name .. ")")
return false
end
-- Get shape data
local shape = PakettiAutomationCurvesShapes[shape_name]
if not shape then
renoise.app():show_status("Unknown shape: " .. tostring(shape_name))
return false
end
-- Get or generate values
local shape_values
if shape.generator then
shape_values = PakettiAutomationCurvesGenerateRandom(shape.generator, PakettiAutomationCurveResolution)
else
shape_values = shape.values
end
if not shape_values then
renoise.app():show_status("No values for shape: " .. shape_name)
return false
end
-- Store current LFO routing parameters (so we can restore them after XML injection)
local stored_dest_track = device.parameters[1].value
local stored_dest_effect = device.parameters[2].value
local stored_dest_param = device.parameters[3].value
local stored_amplitude = device.parameters[4].value
local stored_offset = device.parameters[5].value
local stored_frequency = device.parameters[6].value
-- Get envelope length
local envelope_length = PakettiAutomationCurvesLFOEnvelopeLength
-- Convert shape values to LFO envelope points
-- Shape values are {x, y} where x is 0.0-0.99 and y is 0.0-1.0
-- LFO points are index,value,curve where index is 0 to (length-1)
local points_xml = {}
-- Apply offset and attenuation to values (same as automation curve logic)
local offset = PakettiAutomationCurvesOffset
local attenuation = PakettiAutomationCurvesAttenuation
local divisor = PakettiAutomationCurvesInputDivisor
-- Track which indices we've already added (to avoid duplicates)
local added_indices = {}
-- Loop through repetitions (1x to 8x repeat count)
for slice = 0, (divisor - 1) do
local slice_start = slice / divisor -- Start position of this slice (0.0 to 1.0)
local slice_width = 1 / divisor -- Width of each slice
for _, point in ipairs(shape_values) do
local x_pos = point[1] -- 0.0 to 0.99
local y_val = point[2] -- 0.0 to 1.0
-- Apply offset and attenuation
local val = offset + ((1 - offset) * y_val) * attenuation
-- Clamp value to 0-1
val = math.max(0, math.min(1, val))
-- Scale x position to fit within this slice
local scaled_x = slice_start + (x_pos * slice_width)
-- Convert x position to envelope index
local index = math.floor(scaled_x * envelope_length)
if index >= envelope_length then
index = envelope_length - 1
end
-- Only add if we haven't already added a point at this index
if not added_indices[index] then
added_indices[index] = true
table.insert(points_xml, string.format('<Point>%d,%.6f,0.0</Point>', index, val))
end
end
end
local points_str = table.concat(points_xml, "\n ")
-- Build the LFO XML with Type=4 (Custom) and the custom envelope
local lfo_xml = string.format([=[<?xml version="1.0" encoding="UTF-8"?>
<FilterDevicePreset doc_version="14">
<DeviceSlot type="LfoDevice">
<IsMaximized>true</IsMaximized>
<Amplitude>
<Value>%.6f</Value>
</Amplitude>
<Offset>
<Value>%.6f</Value>
</Offset>
<Frequency>
<Value>%.6f</Value>
</Frequency>
<Type>
<Value>4</Value>
</Type>
<CustomEnvelope>
<PlayMode>Lines</PlayMode>
<Length>%d</Length>
<ValueQuantum>0.0</ValueQuantum>
<Polarity>Unipolar</Polarity>
<Points>
%s
</Points>
</CustomEnvelope>
<CustomEnvelopeOneShot>false</CustomEnvelopeOneShot>
<UseAdjustedEnvelopeLength>true</UseAdjustedEnvelopeLength>
</DeviceSlot>
</FilterDevicePreset>]=], stored_amplitude, stored_offset, stored_frequency, envelope_length, points_str)
-- Inject the XML
device.active_preset_data = lfo_xml
-- Restore routing parameters (they get reset by XML injection)
device.parameters[1].value = stored_dest_track
device.parameters[2].value = stored_dest_effect
device.parameters[3].value = stored_dest_param
local label = shape.label or shape_name
local repeat_str = divisor > 1 and (" x" .. divisor) or ""
renoise.app():show_status("Wrote " .. label .. repeat_str .. " to LFO Custom Waveform (" .. envelope_length .. " points)")
print("PakettiAutomationCurves: Wrote " .. shape_name .. " to LFO custom waveform with " .. envelope_length .. " points, repeat: " .. divisor .. "x")
return true
end
------------------------------------------------------------------------
-- Insert Shape into Automation
------------------------------------------------------------------------
function PakettiAutomationCurvesInsert(shape_name)
local rs = renoise.song()
rs:describe_undo("Paketti: Insert Automation Curve")
if not rs then
renoise.app():show_status("No song loaded")
return
end
-- If "Write to LFO" checkbox is enabled, write to LFO custom waveform instead
if PakettiAutomationCurvesWriteToLFO then
PakettiAutomationCurvesWriteToLFOCustom(shape_name)
return
end
local pattern = rs.selected_pattern
if not pattern then
renoise.app():show_status("No pattern selected")
return
end
local num_lines = pattern.number_of_lines
if num_lines < 1 then
num_lines = 1
end
-- Clamp step length to pattern length
PakettiAutomationCurvesStepLength = PakettiAutomationCurvesClampStepLength(PakettiAutomationCurvesStepLength, num_lines)
local current_line = rs.selected_line_index
local step = PakettiAutomationCurvesStepLength
local automation = PakettiAutomationCurvesGetAutomation()
if not automation then
return
end
-- Check for automation selection range
local start_line, end_line
local selection = automation.selection_range
if selection and selection[1] and selection[2] then
start_line = selection[1]
end_line = selection[2]
step = end_line - start_line
print("PakettiAutomationCurves: Using selection range from " .. start_line .. " to " .. end_line)
else
-- No selection: write to full pattern
start_line = 1
end_line = num_lines + 1
step = num_lines
print("PakettiAutomationCurves: No selection, writing to full pattern (1 to " .. num_lines .. ")")
end
-- Get shape data
local shape = PakettiAutomationCurvesShapes[shape_name]
if not shape then
renoise.app():show_status("Unknown shape: " .. tostring(shape_name))
return
end
-- Get or generate values
local shape_values
if shape.generator then
shape_values = PakettiAutomationCurvesGenerateRandom(shape.generator, PakettiAutomationCurveResolution)
else
shape_values = shape.values
end
if not shape_values then
renoise.app():show_status("No values for shape: " .. shape_name)
return
end
-- Clear existing points in range
local old_points = automation.points
local new_points = {}
for _, v in pairs(old_points) do
if v.time >= start_line and v.time < end_line then
-- Skip points in the range we're about to fill
else
table.insert(new_points, v)
end
end
automation.points = new_points
-- Set playmode from shape definition (CURVES for smooth shapes, LINES for linear, POINTS for step)
if shape.playmode then
automation.playmode = shape.playmode
end
-- Insert shape points with offset and attenuation
local offset = PakettiAutomationCurvesOffset
local attenuation = PakettiAutomationCurvesAttenuation
local divisor = PakettiAutomationCurvesInputDivisor
for slice = 0, (divisor - 1) do
local start = slice / divisor * step
for _, point in ipairs(shape_values) do
local time = start_line + start + step * point[1] * (1 / divisor)
local val = offset + ((1 - offset) * point[2]) * attenuation
-- Clamp value to 0-1
val = math.max(0, math.min(1, val))
automation:add_point_at(time, val, point[3] or 0.0)
end
end
-- Advance cursor by step length (only if not using selection range)
if not selection or not selection[1] then
local new_line = current_line + step
while new_line > num_lines do
new_line = new_line - num_lines
end
rs.selected_line_index = new_line
end
-- Move selection forward if checkbox is enabled and there was a selection
if PakettiAutomationCurvesMoveBySelection and selection and selection[1] and selection[2] then
local selection_length = end_line - start_line
local new_start = end_line
local new_end = end_line + selection_length
-- Handle pattern boundary: clamp to pattern end if selection would exceed it
if new_end > num_lines then
new_end = num_lines
-- Adjust start to maintain selection length if possible
new_start = math.max(1, new_end - selection_length)
end
-- Set new selection range
automation.selection_range = {new_start, new_end}
-- Move cursor to new selection start
rs.selected_line_index = new_start
print("PakettiAutomationCurves: Moved selection from " .. start_line .. "-" .. end_line .. " to " .. new_start .. "-" .. new_end)
end
local label = shape.label or shape_name
renoise.app():show_status("Inserted " .. label .. " (" .. step .. " lines)")
print("PakettiAutomationCurves: Inserted " .. shape_name .. " at line " .. current_line .. " with step " .. step)
end
------------------------------------------------------------------------
-- Pattern Effect Functions
------------------------------------------------------------------------
function PakettiAutomationCurvesProcessPoints(process_func)
local automation = PakettiAutomationCurvesGetAutomation()
if not automation then
return
end
local old_points = automation.points
local new_points = {}
for i, v in pairs(old_points) do
local point = process_func(i, v)
if point.value < 0 then point.value = 0 end
if point.value > 1 then point.value = 1 end
table.insert(new_points, point)
end
automation.points = new_points
end
function PakettiAutomationCurvesFadeIn()
local rs = renoise.song()
rs:describe_undo("Paketti: Automation Fade In")
if not rs or not rs.selected_pattern then
renoise.app():show_status("No pattern selected")
return
end
local num_lines = rs.selected_pattern.number_of_lines
if num_lines < 1 then
num_lines = 1
end
PakettiAutomationCurvesProcessPoints(function(index, point)
point.value = (point.time - 1) / num_lines * point.value
return point
end)
renoise.app():show_status("Applied Fade In to automation")
end
function PakettiAutomationCurvesFadeOut()
local rs = renoise.song()
rs:describe_undo("Paketti: Automation Fade Out")
if not rs or not rs.selected_pattern then
renoise.app():show_status("No pattern selected")
return
end
local num_lines = rs.selected_pattern.number_of_lines
if num_lines < 1 then
num_lines = 1
end
PakettiAutomationCurvesProcessPoints(function(index, point)
point.value = (1 - (point.time - 1) / num_lines) * point.value
return point
end)
renoise.app():show_status("Applied Fade Out to automation")
end
function PakettiAutomationCurvesZeroOdd()
renoise.song():describe_undo("Paketti: Zero Odd Automation Points")
PakettiAutomationCurvesProcessPoints(function(index, point)
if index % 2 == 1 then point.value = 0 end
return point
end)
renoise.app():show_status("Zeroed odd points")
end
function PakettiAutomationCurvesZeroEven()
renoise.song():describe_undo("Paketti: Zero Even Automation Points")
PakettiAutomationCurvesProcessPoints(function(index, point)
if index % 2 == 0 then point.value = 0 end
return point
end)
renoise.app():show_status("Zeroed even points")
end
function PakettiAutomationCurvesMaxOdd()
renoise.song():describe_undo("Paketti: Max Odd Automation Points")
PakettiAutomationCurvesProcessPoints(function(index, point)
if index % 2 == 1 then point.value = 1 end
return point
end)
renoise.app():show_status("Maxed odd points")
end
function PakettiAutomationCurvesMaxEven()
renoise.song():describe_undo("Paketti: Max Even Automation Points")
PakettiAutomationCurvesProcessPoints(function(index, point)
if index % 2 == 0 then point.value = 1 end
return point
end)
renoise.app():show_status("Maxed even points")
end
-- Randomize existing points (selection-aware)
function PakettiAutomationCurvesRandomize(amount)
renoise.song():describe_undo("Paketti: Randomize Automation Points")
amount = amount or 0.2
local automation = PakettiAutomationCurvesGetAutomation()
if not automation then
return
end
local old_points = automation.points
if #old_points == 0 then
renoise.app():show_status("No automation points to randomize")
return
end
local new_points = {}
-- Check for selection range
local selection = automation.selection_range
local has_selection = selection and selection[1] and selection[2]
local start_line, end_line
if has_selection then
start_line = selection[1]
end_line = selection[2]
end
local randomized_count = 0
for i, point in ipairs(old_points) do
local new_point = {time = point.time, value = point.value}
-- Randomize if no selection, or if point is within selection range
if not has_selection or (point.time >= start_line and point.time < end_line) then
local noise = (math.random() - 0.5) * 2 * amount
new_point.value = new_point.value + noise
-- Clamp value to 0-1
if new_point.value < 0 then new_point.value = 0 end
if new_point.value > 1 then new_point.value = 1 end
randomized_count = randomized_count + 1
end
table.insert(new_points, new_point)
end
automation.points = new_points
if has_selection then
renoise.app():show_status("Randomized " .. randomized_count .. " points in selection")
else
renoise.app():show_status("Randomized " .. randomized_count .. " automation points")
end
end
-- Smooth existing points
function PakettiAutomationCurvesSmooth()
renoise.song():describe_undo("Paketti: Smooth Automation Points")
local automation = PakettiAutomationCurvesGetAutomation()
if not automation or #automation.points < 3 then
return
end
local old_points = automation.points
local new_points = {}
-- Keep first point unchanged
table.insert(new_points, old_points[1])
-- Smooth middle points
for i = 2, #old_points - 1 do
local prev = old_points[i - 1].value
local curr = old_points[i].value
local next_val = old_points[i + 1].value
local smoothed = (prev + curr + next_val) / 3
table.insert(new_points, {time = old_points[i].time, value = smoothed})
end
-- Keep last point unchanged
table.insert(new_points, old_points[#old_points])
automation.points = new_points
renoise.app():show_status("Smoothed automation points")
end
-- Quantize to grid
function PakettiAutomationCurvesQuantize(grid_size)
renoise.song():describe_undo("Paketti: Quantize Automation Points")
grid_size = grid_size or 0.125 -- Default to 8 steps
PakettiAutomationCurvesProcessPoints(function(index, point)
point.value = math.floor(point.value / grid_size + 0.5) * grid_size
return point
end)
renoise.app():show_status("Quantized automation to " .. tostring(1 / grid_size) .. " steps")
end
------------------------------------------------------------------------
-- Automation Clipboard Functions
------------------------------------------------------------------------
-- Copy automation points from current selection to clipboard
function PakettiAutomationClipboardCopy()
local rs = renoise.song()
if not rs then
renoise.app():show_status("No song loaded")
return false
end
local pattern = rs.selected_pattern
if not pattern then
renoise.app():show_status("No pattern selected")
return false
end
local param = rs.selected_automation_parameter
if not param then
renoise.app():show_status("No automation parameter selected")
return false
end
local track = rs.selected_pattern_track
local automation = track:find_automation(param)
if not automation then
renoise.app():show_status("No automation envelope found for " .. param.name)
return false
end
-- Get selection range (or use full pattern if no selection)
local selection = automation.selection_range
local start_line, end_line
local num_lines = pattern.number_of_lines
if selection and selection[1] and selection[2] and selection[1] ~= selection[2] then
start_line = selection[1]
end_line = selection[2]
else
-- No selection, use full pattern
start_line = 1
end_line = num_lines + 1
end
-- Filter points within selection range
local source_points = automation.points
local clipboard_points = {}
for _, point in ipairs(source_points) do
if point.time >= start_line and point.time < end_line then
-- Store with time normalized relative to selection start (0-based)
local relative_time = point.time - start_line
table.insert(clipboard_points, {
relative_time = relative_time,
value = point.value,
scaling = pakettiSafeGetScaling(point)
})
end
end
if #clipboard_points == 0 then
renoise.app():show_status("No automation points in selection")
return false
end
-- Store in clipboard
PakettiAutomationClipboard.points = clipboard_points
PakettiAutomationClipboard.length = end_line - start_line
PakettiAutomationClipboard.playmode = automation.playmode
PakettiAutomationClipboard.source_param = param.name
renoise.app():show_status("Copied " .. #clipboard_points .. " points from " .. param.name .. " (" .. PakettiAutomationClipboard.length .. " lines)")
print("PakettiAutomationClipboard: Copied " .. #clipboard_points .. " points, length: " .. PakettiAutomationClipboard.length)
return true
end
-- Cut automation points (copy then clear)
function PakettiAutomationClipboardCut()
local rs = renoise.song()
rs:describe_undo("Paketti: Cut Automation Clipboard")
if not rs then
renoise.app():show_status("No song loaded")
return false
end
local pattern = rs.selected_pattern
if not pattern then
renoise.app():show_status("No pattern selected")
return false
end
local param = rs.selected_automation_parameter
if not param then
renoise.app():show_status("No automation parameter selected")
return false
end
local track = rs.selected_pattern_track
local automation = track:find_automation(param)
if not automation then
renoise.app():show_status("No automation envelope found for " .. param.name)
return false
end
-- Get selection range before copying
local selection = automation.selection_range
local start_line, end_line
local num_lines = pattern.number_of_lines
if selection and selection[1] and selection[2] and selection[1] ~= selection[2] then
start_line = selection[1]
end_line = selection[2]
else
-- No selection, use full pattern
start_line = 1
end_line = num_lines + 1
end
-- Copy first
local success = PakettiAutomationClipboardCopy()
if not success then
return false
end
-- Clear the range
automation:clear_range(start_line, end_line)
renoise.app():show_status("Cut " .. #PakettiAutomationClipboard.points .. " points from " .. param.name)
print("PakettiAutomationClipboard: Cut " .. #PakettiAutomationClipboard.points .. " points")
return true
end
-- Paste automation points to current parameter
function PakettiAutomationClipboardPaste()
local rs = renoise.song()
rs:describe_undo("Paketti: Paste Automation Clipboard")
if not rs then
renoise.app():show_status("No song loaded")
return false
end
-- Check clipboard
if not PakettiAutomationClipboard.points or #PakettiAutomationClipboard.points == 0 then
renoise.app():show_status("Clipboard is empty - copy automation first")
return false
end
local pattern = rs.selected_pattern
if not pattern then
renoise.app():show_status("No pattern selected")
return false
end
local param = rs.selected_automation_parameter
if not param then
renoise.app():show_status("No automation parameter selected")
return false
end