Repository navigation
Expand file tree
/
Copy pathPakettiArpeggiator.lua
More file actions
1888 lines (1618 loc) · 63.3 KB
/
Copy pathPakettiArpeggiator.lua
File metadata and controls
1888 lines (1618 loc) · 63.3 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
-- PakettiArpeggiator.lua
-- Musical Chord Progression Arpeggiator
-- Lua 5.1 only. All functions GLOBAL and defined before first use.
-- State
PakettiArp_dialog = nil
PakettiArp_vb = nil
PakettiArp_playing = false
PakettiArp_use_current_bpm = true
PakettiArp_bpm = 135
PakettiArp_key = "C"
PakettiArp_mode = "ionian"
PakettiArp_modes_per_chord = {"ionian", "ionian", "ionian", "ionian", "ionian", "ionian", "ionian", "ionian"}
PakettiArp_steps = 4
PakettiArp_pattern_type = "straight"
PakettiArp_pattern_id = 1
PakettiArp_chord_progression = {1,3,7,4,5,3,6,2} -- default progression (indexes into scale)
PakettiArp_current_chord_step = 1
PakettiArp_current_arp_step = 1
PakettiArp_arp_repeat = 2
PakettiArp_octave_base = 4
PakettiArp_current_patterns = {}
PakettiArp_current_arpeggio = {}
PakettiArp_scale_notes = {}
PakettiArp_line_notifier = nil
PakettiArp_pattern_changed_notifier = nil
PakettiArp_canvas_views = {}
PakettiArp_chord_buttons = {} -- Array of arrays: chord_buttons[chord_position][degree]
PakettiArp_highlight_timer = nil
-- Musical scale dictionary
PakettiArp_keys = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}
PakettiArp_flat_to_sharp = {
Cb = "B", Db = "C#", Eb = "D#", Fb = "E",
Gb = "F#", Ab = "G#", Bb = "A#"
}
PakettiArp_modes = {
"ionian", "dorian", "phrygian", "lydian",
"mixolydian", "aeolian", "locrian",
"major", "minor", "melodic", "harmonic",
"double harmonic", "hungarian minor", "phrygian dominant", "harmonic major",
"golden", "golden-10", "golden chromatic", "silver"
}
PakettiArp_scale_data = {
ion = { name = "Ionian", steps = {0,2,4,5,7,9,11}, triads = {"maj","min","min","maj","maj","min","dim"} },
dor = { name = "Dorian", steps = {0,2,3,5,7,9,10}, triads = {"min","min","maj","maj","min","dim","maj"} },
phr = { name = "Phrygian", steps = {0,1,3,5,7,8,10}, triads = {"min","maj","maj","min","dim","maj","min"} },
lyd = { name = "Lydian", steps = {0,2,4,6,7,9,11}, triads = {"maj","min","min","dim","maj","min","min"} },
mix = { name = "Mixolydian", steps = {0,2,4,5,7,9,10}, triads = {"maj","min","dim","maj","min","min","maj"} },
aeo = { name = "Aeolian", steps = {0,2,3,5,7,8,10}, triads = {"min","dim","maj","min","min","maj","maj"} },
loc = { name = "Locrian", steps = {0,1,3,5,6,8,10}, triads = {"dim","maj","min","min","maj","maj","min"} },
mel = { name = "Melodic Minor", steps = {0,2,3,5,7,9,11}, triads = {"min","min","aug","maj","maj","dim","dim"} },
har = { name = "Harmonic Minor", steps = {0,2,3,5,7,8,11}, triads = {"min","dim","aug","min","maj","maj","dim"} },
-- Double Harmonic family. Some tertian triads on these scales are not plain maj/min/dim/aug
-- (augmented-second gaps); those degrees use the exotic qualities defined in PakettiArp_triad_intervals below.
dblharm = { name = "Double Harmonic (Byzantine)", steps = {0,1,4,5,7,8,11}, triads = {"maj","maj","min","min","majb5","aug","sus2b5"} },
hungmin = { name = "Hungarian Minor", steps = {0,2,3,6,7,8,11}, triads = {"min","majb5","aug","sus2b5","maj","maj","min"} },
phrydom = { name = "Phrygian Dominant", steps = {0,1,4,5,7,8,10}, triads = {"maj","maj","dim","min","dim","aug","min"} },
harmmaj = { name = "Harmonic Major", steps = {0,2,4,5,7,8,11}, triads = {"maj","dim","min","min","maj","aug","dim"} }
}
PakettiArp_triad_intervals = {
maj = {0,4,7},
min = {0,3,7},
dim = {0,3,6},
aug = {0,4,8},
majb5 = {0,4,6}, -- major 3rd + diminished 5th (the "7b5" flavour, e.g. F#7b5 minus the 7th)
sus2b5 = {0,2,6} -- augmented-second artifact stack (major 2nd + tritone); non-tertian but valid
}
-- Golden harmony scales (David Chester, "Music Theory of Golden Harmony"), expressed in
-- 36-EDO degrees (one octave = 36 steps of 33.33 cents). These are NOT 12-TET: selecting
-- one auto-tunes the target instrument to 36-EDO (see PakettiArp_EnsureGoldenTuning) so the
-- intervals sound as the golden sixth/third stack intends, not as compressed semitones.
-- `golden=true` marks the branch; `triad_steps` is the fixed chord stacked on every degree
-- (golden triad {0,11,22} = root + golden third + golden fifth; silver triad {0,10,20}).
PakettiArp_golden_scale_data = {
golden = { name = "Golden Diatonic (7)", steps = {0,3,11,14,22,25,33}, triad_steps = {0,11,22}, golden = true },
golden10 = { name = "Golden-10", steps = {0,3,8,11,14,17,22,25,28,33}, triad_steps = {0,11,22}, golden = true },
goldenchr = { name = "Golden Chromatic (13)", steps = {0,3,6,8,11,14,17,19,22,25,28,30,33}, triad_steps = {0,11,22}, golden = true },
silver = { name = "Silver (13, layer mirror)", steps = {0,3,6,10,13,16,17,19,20,23,26,30,33}, triad_steps = {0,10,20}, golden = true }
}
-- Merge golden scales into the main scale dictionary so .name / ModeToKey / GenerateScale
-- all resolve them uniformly.
for k, v in pairs(PakettiArp_golden_scale_data) do PakettiArp_scale_data[k] = v end
-- Roman numerals for degrees
PakettiArp_roman_numerals = {"I", "II", "III", "IV", "V", "VI", "VII"}
-- Convert mode name to mode key
function PakettiArp_ModeToKey(mode)
local map = {
minor = "aeo", major = "ion", ionian = "ion",
dorian = "dor", phrygian = "phr", lydian = "lyd",
mixolydian = "mix", aeolian = "aeo", locrian = "loc",
melodic = "mel", harmonic = "har",
["double harmonic"] = "dblharm", byzantine = "dblharm",
["hungarian minor"] = "hungmin", ["phrygian dominant"] = "phrydom",
["harmonic major"] = "harmmaj",
["golden"] = "golden", ["golden-10"] = "golden10",
["golden chromatic"] = "goldenchr", ["silver"] = "silver"
}
return map[mode] or "ion"
end
-- True when the given arp mode name is one of the golden-harmony (36-EDO) scales.
function PakettiArp_IsGoldenMode(mode)
local info = PakettiArp_scale_data[PakettiArp_ModeToKey(mode)]
return info ~= nil and info.golden == true
end
-- Generate musical scale with triads
function PakettiArp_GenerateScale(key, mode)
local scale = {}
local mode_key = PakettiArp_ModeToKey(mode)
local scale_info = PakettiArp_scale_data[mode_key]
if not scale_info then return scale end
-- Find key offset
local key_offset = 0
for i = 1, #PakettiArp_keys do
if PakettiArp_keys[i] == key then
key_offset = i - 1
break
end
end
-- Golden-harmony branch: steps and triads are in 36-EDO degrees (octave = 36), not 12-TET.
-- Each triad note carries an absolute degree `nv` (0..35 within an octave) plus rel_octave;
-- PakettiArp_ResolveNoteValue reads nv and anchors it against a 36-EDO instrument tuning.
-- key_offset acts as a fine transposition in 36-EDO steps (0..11).
if scale_info.golden then
local triad_label = (scale_info.triad_steps[2] == 10) and "silver" or "golden"
for s = 1, #scale_info.steps do
local step = scale_info.steps[s]
local rel_octave = math.floor((key_offset + step) / 36)
local triad_notes = {}
for t = 1, #scale_info.triad_steps do
local v = key_offset + step + scale_info.triad_steps[t]
table.insert(triad_notes, {
note = "C", -- placeholder so any name-reader stays safe; nv is authoritative
nv = v % 36,
rel_octave = math.floor(v / 36)
})
end
table.insert(scale, {
step = s,
note = "d" .. ((key_offset + step) % 36), -- display label: 36-EDO degree (not a 12-TET name)
nv = (key_offset + step) % 36,
rel_octave = rel_octave,
triad = { type = triad_label, notes = triad_notes }
})
end
return scale
end
-- Generate notes
for s = 1, #scale_info.steps do
local step = scale_info.steps[s]
local note_idx = ((key_offset + step) % 12) + 1
local note_name = PakettiArp_keys[note_idx]
local rel_octave = (key_offset + step >= 12) and 1 or 0
-- Generate triad
local triad_type = scale_info.triads[s]
local triad_steps = PakettiArp_triad_intervals[triad_type]
local triad_notes = {}
for t = 1, #triad_steps do
local triad_step = triad_steps[t]
local triad_idx = ((key_offset + step + triad_step) % 12) + 1
local triad_octave = math.floor((key_offset + step + triad_step) / 12)
table.insert(triad_notes, {
note = PakettiArp_keys[triad_idx],
rel_octave = rel_octave + triad_octave
})
end
table.insert(scale, {
step = s,
note = note_name,
rel_octave = rel_octave,
triad = {
type = triad_type,
notes = triad_notes
}
})
end
return scale
end
-- Generate arpeggio patterns using permutations
function PakettiArp_Permute(arr, used, result)
if #arr == 0 then
local copy = {}
for i = 1, #used do copy[i] = used[i] end
table.insert(result, copy)
return
end
for i = 1, #arr do
local val = table.remove(arr, i)
table.insert(used, val)
PakettiArp_Permute(arr, used, result)
table.remove(used)
table.insert(arr, i, val)
end
end
function PakettiArp_GeneratePatterns(steps)
local arr = {}
for i = 0, steps - 1 do table.insert(arr, i) end
local straight = {}
PakettiArp_Permute(arr, {}, straight)
local looped = {}
for p = 1, #straight do
local perm = straight[p]
local loop = {}
for i = 1, #perm do table.insert(loop, perm[i]) end
for x = #perm - 1, 2, -1 do
table.insert(loop, perm[x])
end
table.insert(looped, loop)
end
-- Named shape patterns (single-pattern types — chosen directly without picking a permutation index)
local n = steps
local ascending = {}
local descending = {}
local pingpong = {}
local downup = {}
local skip = {}
local randomp = {}
for i = 0, n - 1 do
table.insert(ascending, i)
table.insert(descending, n - 1 - i)
end
-- Ping-Pong: 0,1,2,...,N-1,N-2,...,1
for i = 0, n - 1 do table.insert(pingpong, i) end
for i = n - 2, 1, -1 do table.insert(pingpong, i) end
-- Down-Up: N-1,N-2,...,0,1,...,N-2
for i = n - 1, 0, -1 do table.insert(downup, i) end
for i = 1, n - 2 do table.insert(downup, i) end
-- Skip: odd positions then even positions (1st,3rd,5th,..., 2nd,4th,6th,...)
for i = 0, n - 1, 2 do table.insert(skip, i) end
for i = 1, n - 1, 2 do table.insert(skip, i) end
-- Random: shuffled copy (regenerated each call so user gets a fresh shuffle on type-press)
for i = 0, n - 1 do table.insert(randomp, i) end
for i = #randomp, 2, -1 do
local j = math.random(1, i)
randomp[i], randomp[j] = randomp[j], randomp[i]
end
return {
straight = straight,
looped = looped,
ascending = { ascending },
descending = { descending },
pingpong = { pingpong },
downup = { downup },
skip = { skip },
random = { randomp },
}
end
-- Convert note to MIDI value
function PakettiArp_NoteToMidi(note_name, octave)
local note_values = {
C = 0, ["C#"] = 1, D = 2, ["D#"] = 3,
E = 4, F = 5, ["F#"] = 6, G = 7,
["G#"] = 8, A = 9, ["A#"] = 10, B = 11
}
local base = note_values[note_name]
if not base then return 60 end
return (octave * 12) + base
end
-- Resolve a note object to a Renoise note_value (0..119). Golden note objects carry `nv`
-- (a 36-EDO degree) + rel_octave: they are anchored at octave_base*12 and ride in 36-EDO
-- steps, so a golden instrument tuning plays the intended golden intervals. Plain 12-TET
-- note objects (with a note NAME and no nv) fall through to the semitone path.
function PakettiArp_ResolveNoteValue(n, octave_base)
local v
if n.nv ~= nil then
v = octave_base * 12 + n.nv + (n.rel_octave or 0) * 36
else
v = PakettiArp_NoteToMidi(n.note, (n.rel_octave or 0) + octave_base)
end
if v < 0 then v = 0 elseif v > 119 then v = 119 end
return v
end
-- If the current mode (or any per-chord mode) is a golden scale, make sure the selected
-- instrument is on 36-EDO so the arp SOUNDS golden. Idempotent and safe to call often.
function PakettiArp_EnsureGoldenTuning()
local golden = PakettiArp_IsGoldenMode(PakettiArp_mode)
if not golden then
for c = 1, #PakettiArp_modes_per_chord do
if PakettiArp_IsGoldenMode(PakettiArp_modes_per_chord[c]) then golden = true break end
end
end
if not golden then return end
if type(rawget(_G, "PakettiApply36EDOToInstrument")) == "function" then
PakettiApply36EDOToInstrument(renoise.song().selected_instrument_index)
end
end
-- Update scale
function PakettiArp_UpdateScale()
PakettiArp_scale_notes = PakettiArp_GenerateScale(PakettiArp_key, PakettiArp_mode)
PakettiArp_UpdateUI()
end
-- Update patterns
function PakettiArp_UpdatePatterns()
PakettiArp_current_patterns = PakettiArp_GeneratePatterns(PakettiArp_steps)
local patterns = PakettiArp_current_patterns[PakettiArp_pattern_type]
if PakettiArp_pattern_id > #patterns then
PakettiArp_pattern_id = 1
end
PakettiArp_current_arpeggio = patterns[PakettiArp_pattern_id]
PakettiArp_UpdateUI()
end
-- Render arpeggio pattern to canvas
function PakettiArp_RenderPattern(context, pattern, is_selected)
if not pattern or #pattern == 0 then return end
local width = context.size.width
local height = context.size.height
local padding = 6
-- Draw background based on selection
if is_selected then
context.fill_color = {255, 187, 0}
else
context.fill_color = {255, 255, 255}
end
context:fill_rect(0, 0, width, height)
-- Find max value for scaling
local max_val = 0
for i = 1, #pattern do
if pattern[i] > max_val then max_val = pattern[i] end
end
if max_val == 0 then return end
local step_width = (width - padding * 2) / (#pattern - 1)
local step_height = (height - padding * 2) / max_val
-- Draw path
context:begin_path()
for i = 1, #pattern do
local x = padding + (i - 1) * step_width
local y = height - padding - (pattern[i] * step_height)
if i == 1 then
context:move_to(x, y)
else
context:line_to(x, y)
end
end
context.stroke_color = {0, 0, 0}
context.line_width = 2
context:stroke()
-- Draw points
for i = 1, #pattern do
local x = padding + (i - 1) * step_width
local y = height - padding - (pattern[i] * step_height)
context:begin_path()
context:arc(x, y, 3, 0, math.pi * 2, false)
if is_selected then
context.fill_color = {34, 34, 34}
else
context.fill_color = {0, 0, 0}
end
context:fill()
end
end
-- Audition/Playback state
PakettiArp_audition_playing = false
PakettiArp_audition_notes = {}
PakettiArp_audition_chord_idx = 1
PakettiArp_highlight_color = {40, 0, 72} -- Deep purple from PakettiEightOneTwenty
PakettiArp_playback_timer = nil
PakettiArp_continuous_playing = false
PakettiArp_arp_step = 0
PakettiArp_bass_note = nil
PakettiArp_note_timers = {}
PakettiArp_read_from_track = nil -- Track to read from (nil = current track)
-- Stop all playback
function PakettiArp_StopAudition()
-- Stop all timers
if PakettiArp_playback_timer and renoise.tool():has_timer(PakettiArp_playback_timer) then
renoise.tool():remove_timer(PakettiArp_playback_timer)
end
PakettiArp_playback_timer = nil
for _, timer in ipairs(PakettiArp_note_timers) do
if timer and renoise.tool():has_timer(timer) then
renoise.tool():remove_timer(timer)
end
end
PakettiArp_note_timers = {}
-- Stop all notes safely
local song = renoise.song()
local track_idx = song.selected_track_index
local inst_idx = song.selected_instrument_index
if #PakettiArp_audition_notes > 0 then
local unique_notes = PakettiArp_UniquifyNotes(PakettiArp_audition_notes)
if #unique_notes > 0 then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, unique_notes) end)
end
end
-- Stop bass note
if PakettiArp_bass_note then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, {PakettiArp_bass_note}) end)
PakettiArp_bass_note = nil
end
PakettiArp_audition_playing = false
PakettiArp_continuous_playing = false
PakettiArp_audition_notes = {}
PakettiArp_audition_chord_idx = 1
PakettiArp_arp_step = 0
end
-- Build note list for chord
function PakettiArp_GetChordNotes(chord_position)
-- Generate scale for this specific chord position using its per-chord mode
local mode_for_chord = PakettiArp_modes_per_chord[chord_position]
local scale_for_chord = PakettiArp_GenerateScale(PakettiArp_key, mode_for_chord)
if #scale_for_chord == 0 then
return nil, nil
end
local chord_idx = PakettiArp_chord_progression[chord_position]
if chord_idx <= 0 or chord_idx > #scale_for_chord then
return nil, nil
end
local chord = scale_for_chord[chord_idx]
-- Build note list for arpeggio
local notes = {}
for i = 1, #chord.triad.notes do
table.insert(notes, chord.triad.notes[i])
end
-- Extend notes for higher steps
local base_count = #notes
for ext = 1, math.ceil(PakettiArp_steps / 3) do
for i = 1, base_count do
table.insert(notes, {
note = notes[i].note,
rel_octave = notes[i].rel_octave + ext
})
end
end
-- Bass note (root, one octave lower)
local root = chord.triad.notes[1]
local bass_note
if root.nv ~= nil then
-- Golden: drop a full 36-EDO octave below the root.
bass_note = PakettiArp_ResolveNoteValue(root, PakettiArp_octave_base) - 36
if bass_note < 0 then bass_note = bass_note + 36 end
else
bass_note = PakettiArp_NoteToMidi(root.note, root.rel_octave + PakettiArp_octave_base - 1)
end
return notes, bass_note
end
-- Play one arpeggio note
function PakettiArp_PlayArpNote()
if not PakettiArp_continuous_playing then return end
local song = renoise.song()
local track_idx = song.selected_track_index
local inst_idx = song.selected_instrument_index
if #PakettiArp_current_arpeggio == 0 then
PakettiArp_UpdatePatterns()
end
-- Calculate timing based on BPM and LPB
local bpm = PakettiArp_use_current_bpm and song.transport.bpm or PakettiArp_bpm
local lpb = song.transport.lpb
local beat_ms = 60000 / bpm
local line_ms = beat_ms / lpb
-- Each arpeggio step takes a fraction of one line
local arp_length = math.min(16, #PakettiArp_current_arpeggio)
local step_ms = line_ms / arp_length
local note_duration = step_ms * 0.8 -- Note lasts 80% of the interval
-- Get notes for current chord
local notes, bass_note = PakettiArp_GetChordNotes(PakettiArp_audition_chord_idx)
if not notes then return end
-- Cap arpeggio length to first 16 steps
local arp_step_capped = (PakettiArp_arp_step % math.min(16, #PakettiArp_current_arpeggio))
local arp_idx = PakettiArp_current_arpeggio[arp_step_capped + 1] + 1
if arp_idx <= #notes then
local note = notes[arp_idx]
local midi_note = PakettiArp_ResolveNoteValue(note, PakettiArp_octave_base)
-- Stop previous arp note safely
if #PakettiArp_audition_notes > 0 then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, PakettiArp_audition_notes) end)
PakettiArp_audition_notes = {}
end
-- Play new note
song:trigger_instrument_note_on(inst_idx, track_idx, {midi_note}, 1.0)
PakettiArp_audition_notes = {midi_note}
-- Schedule note-off for this note (so it doesn't ring forever)
-- Schedule one-shot note-off (timer will auto-remove after firing once)
renoise.tool():add_timer(function()
pcall(function()
if PakettiArp_continuous_playing then
song:trigger_instrument_note_off(inst_idx, track_idx, {midi_note})
end
end)
end, note_duration)
-- Handle bass note (sustain for entire chord)
if PakettiArp_arp_step == 0 then
-- Stop previous bass safely
if PakettiArp_bass_note then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, {PakettiArp_bass_note}) end)
end
-- Start new bass (avoid duplicate if same as arp note)
if bass_note ~= midi_note then
PakettiArp_bass_note = bass_note
song:trigger_instrument_note_on(inst_idx, track_idx, {bass_note}, 0.8)
else
PakettiArp_bass_note = nil
end
-- Don't rebuild dialog during playback - it's too expensive
end
end
-- Move to next step
PakettiArp_arp_step = PakettiArp_arp_step + 1
-- If we've finished the pattern, move to next chord (using capped length)
local arp_length = math.min(16, #PakettiArp_current_arpeggio)
if PakettiArp_arp_step >= arp_length then
PakettiArp_arp_step = 0
PakettiArp_audition_chord_idx = PakettiArp_audition_chord_idx + 1
if PakettiArp_audition_chord_idx > #PakettiArp_chord_progression then
PakettiArp_audition_chord_idx = 1
end
-- Don't rebuild during continuous playback - too expensive/distracting
end
end
-- Start continuous playback
function PakettiArp_Play()
if PakettiArp_continuous_playing then
PakettiArp_StopAudition()
-- Update colors without rebuilding
PakettiArp_UpdateChordButtonColors()
return
end
PakettiArp_StopAudition()
PakettiArp_EnsureGoldenTuning()
PakettiArp_continuous_playing = true
PakettiArp_audition_playing = true
PakettiArp_audition_chord_idx = 1
PakettiArp_arp_step = 0
-- Create a single repeating timer
local song = renoise.song()
local bpm = PakettiArp_use_current_bpm and song.transport.bpm or PakettiArp_bpm
local lpb = song.transport.lpb
local beat_ms = 60000 / bpm
local line_ms = beat_ms / lpb
-- Each arpeggio step takes a fraction of one line
local arp_length = math.min(16, #PakettiArp_current_arpeggio)
if arp_length == 0 then
PakettiArp_UpdatePatterns()
arp_length = math.min(16, #PakettiArp_current_arpeggio)
end
local interval_ms = line_ms / arp_length
PakettiArp_playback_timer = function()
if not PakettiArp_continuous_playing then return end
PakettiArp_PlayArpNote()
end
-- Play first note immediately
PakettiArp_PlayArpNote()
-- Add repeating timer
renoise.tool():add_timer(PakettiArp_playback_timer, interval_ms)
-- Show timing info
print(string.format("PakettiArp: Playing at %d BPM, %d LPB (%.1fms per step)",
math.floor(bpm), lpb, interval_ms))
end
-- Remove duplicates from note array
function PakettiArp_UniquifyNotes(note_array)
local seen = {}
local unique = {}
for _, note in ipairs(note_array) do
if not seen[note] then
seen[note] = true
table.insert(unique, note)
end
end
return unique
end
-- Preview one chord with arpeggio (for Space key and clicking chord buttons)
function PakettiArp_NextChord()
local was_playing = PakettiArp_audition_playing and not PakettiArp_continuous_playing
-- Stop continuous playback if running
if PakettiArp_continuous_playing then
PakettiArp_StopAudition()
end
local song = renoise.song()
local track_idx = song.selected_track_index
local inst_idx = song.selected_instrument_index
if #PakettiArp_current_arpeggio == 0 then
PakettiArp_UpdatePatterns()
end
-- Advance chord position
if was_playing then
PakettiArp_audition_chord_idx = PakettiArp_audition_chord_idx + 1
if PakettiArp_audition_chord_idx > #PakettiArp_chord_progression then
PakettiArp_audition_chord_idx = 1
end
else
PakettiArp_audition_chord_idx = 1
end
PakettiArp_audition_playing = true
-- Stop previous notes (safely)
if #PakettiArp_audition_notes > 0 then
local unique_notes = PakettiArp_UniquifyNotes(PakettiArp_audition_notes)
if #unique_notes > 0 then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, unique_notes) end)
end
PakettiArp_audition_notes = {}
end
if PakettiArp_bass_note then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, {PakettiArp_bass_note}) end)
PakettiArp_bass_note = nil
end
-- Get notes for chord
local notes, bass_note = PakettiArp_GetChordNotes(PakettiArp_audition_chord_idx)
if notes then
-- Play bass
PakettiArp_bass_note = bass_note
song:trigger_instrument_note_on(inst_idx, track_idx, {bass_note}, 0.8)
-- Build unique arpeggio notes
local temp_notes = {}
for a = 1, math.min(#PakettiArp_current_arpeggio, #notes) do
local arp_idx = PakettiArp_current_arpeggio[a] + 1
if arp_idx <= #notes then
local note = notes[arp_idx]
local midi_note = PakettiArp_ResolveNoteValue(note, PakettiArp_octave_base)
table.insert(temp_notes, midi_note)
end
end
-- Remove duplicates
PakettiArp_audition_notes = PakettiArp_UniquifyNotes(temp_notes)
-- Start arpeggio playback (one note at a time)
if #PakettiArp_audition_notes > 0 then
local bpm = PakettiArp_use_current_bpm and song.transport.bpm or PakettiArp_bpm
local lpb = song.transport.lpb
local beat_ms = 60000 / bpm
local line_ms = beat_ms / lpb
-- Each arpeggio step takes a fraction of one line
local arp_length = math.min(16, #PakettiArp_current_arpeggio)
local note_duration = line_ms / arp_length
-- Play each note sequentially
for i, midi_note in ipairs(PakettiArp_audition_notes) do
local delay_ms = (i - 1) * note_duration
local timer_id = nil
-- Note ON timer
timer_id = function()
song:trigger_instrument_note_on(inst_idx, track_idx, {midi_note}, 1.0)
-- Schedule note OFF
local off_timer = function()
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, {midi_note}) end)
end
renoise.tool():add_timer(off_timer, note_duration * 0.8)
table.insert(PakettiArp_note_timers, off_timer)
end
if delay_ms > 0 then
renoise.tool():add_timer(timer_id, delay_ms)
table.insert(PakettiArp_note_timers, timer_id)
else
timer_id()
end
end
-- Schedule cleanup after all notes finish
local total_duration = (#PakettiArp_audition_notes) * note_duration
local cleanup_timer = function()
-- Stop bass note
if PakettiArp_bass_note then
pcall(function() song:trigger_instrument_note_off(inst_idx, track_idx, {PakettiArp_bass_note}) end)
PakettiArp_bass_note = nil
end
-- Clear audition state
PakettiArp_audition_playing = false
PakettiArp_audition_notes = {}
-- Update button colors without rebuilding
PakettiArp_UpdateChordButtonColors()
end
renoise.tool():add_timer(cleanup_timer, total_duration + 100)
table.insert(PakettiArp_note_timers, cleanup_timer)
end
end
end
-- Calculate chord notes for pattern matching
function PakettiArp_CalculateChordNotes(root_offset, degree, octave)
-- Get scale using existing function
local root_note = PakettiArp_keys[(root_offset % 12) + 1]
local scale_data = PakettiArp_GenerateScale(root_note, PakettiArp_mode)
if degree < 1 or degree > #scale_data then
return {}
end
local chord = scale_data[degree]
local notes = {}
for i = 1, #chord.triad.notes do
local note_data = chord.triad.notes[i]
local midi_note = PakettiArp_NoteToMidi(note_data.note, note_data.rel_octave + octave)
table.insert(notes, midi_note)
end
return notes
end
-- Read chord progression from pattern
function PakettiArp_ReadFromPattern()
local song = renoise.song()
-- Determine which track to read from
local track_idx = PakettiArp_read_from_track or song.selected_track_index
-- Validate track
if track_idx < 1 or track_idx > #song.tracks then
renoise.app():show_status("PakettiArp: Invalid track number")
return
end
local track = song:track(track_idx)
if not track or track.type ~= renoise.Track.TRACK_TYPE_SEQUENCER then
renoise.app():show_status("PakettiArp: Not a sequencer track")
return
end
local patt = song:pattern(song.selected_pattern_index)
local ptrack = patt:track(track_idx)
-- Clear current progression
for i = 1, #PakettiArp_chord_progression do
PakettiArp_chord_progression[i] = 1
end
-- Collect all unique notes and chord positions
local all_notes = {}
local chord_data = {}
print("=== Scanning Pattern Lines ===")
print("Track: " .. track_idx .. ", Pattern: " .. song.selected_pattern_index)
print("Lines to scan: " .. patt.number_of_lines)
print("Visible note columns: " .. track.visible_note_columns)
print("")
for line_idx = 1, patt.number_of_lines do
local line = ptrack:line(line_idx)
local notes_in_line = {}
-- Collect notes from this line
for col_idx = 1, math.min(12, track.visible_note_columns) do
local ncol = line:note_column(col_idx)
if not ncol.is_empty and ncol.note_string ~= "OFF" and ncol.note_string ~= "" then
print("Line " .. line_idx .. " col " .. col_idx .. ": " .. ncol.note_string)
local note_val = PakettiArp_NoteStringToValue(ncol.note_string)
if note_val then
local note_class = note_val % 12
table.insert(notes_in_line, note_class)
all_notes[note_class] = (all_notes[note_class] or 0) + 1
end
end
end
if #notes_in_line >= 2 then
table.insert(chord_data, notes_in_line)
print(" → Found chord at line " .. line_idx .. " with " .. #notes_in_line .. " notes")
end
end
print("")
print("Total chords found: " .. #chord_data)
print("==============================")
print("")
-- Detect key AND mode together by maximizing scale-note matches
local best_key_idx = 1
local best_mode_name = "ionian"
local best_score = -1000000
for key_idx = 1, #PakettiArp_keys do
local key_name = PakettiArp_keys[key_idx]
for mode_idx = 1, #PakettiArp_modes do
local mode_name = PakettiArp_modes[mode_idx]
-- Golden modes are 36-EDO, not 12-TET pitch classes: never key-detect against them.
if not PakettiArp_IsGoldenMode(mode_name) then
local scale_data = PakettiArp_GenerateScale(key_name, mode_name)
-- Build a set of pitch classes that belong to this scale
local scale_classes = {}
for _, scale_note in ipairs(scale_data) do
local first_note = scale_note.triad.notes[1]
local midi_val = PakettiArp_NoteToMidi(first_note.note, first_note.rel_octave)
scale_classes[midi_val % 12] = true
end
local mode_score = 0
for note_class, count in pairs(all_notes) do
if scale_classes[note_class] then
mode_score = mode_score + count
end
end
if mode_score > best_score then
best_score = mode_score
best_key_idx = key_idx
best_mode_name = mode_name -- e.g. "aeolian", "ionian", "dorian", ...
end
end -- not golden mode
end
end
-- Store detected key & mode (mode is now one of PakettiArp_modes, lowercase)
PakettiArp_key = PakettiArp_keys[best_key_idx]
PakettiArp_mode = best_mode_name
-- Set all per-chord modes to the detected mode
for i = 1, 8 do
PakettiArp_modes_per_chord[i] = best_mode_name
end
-- Update scale with new key/mode
PakettiArp_UpdateScale()
-- Now match chords to degrees with the detected key/mode
local chord_found = 0
local debug_info = {}
print("=== PakettiArp Read from Pattern DEBUG ===")
local mode_pretty = PakettiArp_scale_data[PakettiArp_ModeToKey(PakettiArp_mode)].name
print("Detected Key: " .. PakettiArp_key .. " " .. mode_pretty)
print("Found " .. #chord_data .. " chord positions")
print("")
-- Get the scale for reference
local scale_data = PakettiArp_GenerateScale(PakettiArp_key, PakettiArp_mode)
-- Precompute scale pitch classes for this key/mode
local scale_pitch_classes = {}
for _, scale_note in ipairs(scale_data) do
local first_note = scale_note.triad.notes[1]
local midi_val = PakettiArp_NoteToMidi(first_note.note, first_note.rel_octave)
scale_pitch_classes[midi_val % 12] = true
end
for line_idx, notes_in_line in ipairs(chord_data) do
if chord_found >= 8 then break end
chord_found = chord_found + 1
-- Convert note classes to note names for display
local note_names_found = {}
for _, note_class in ipairs(notes_in_line) do
table.insert(note_names_found, PakettiArp_keys[(note_class % 12) + 1])
end
print("Position " .. chord_found .. " notes: " .. table.concat(note_names_found, ", "))
-- Use the lowest note (first in the collected line) as a *hint*,
-- but don't let it completely dominate (slash chords etc.)
local bass_note_class = notes_in_line[1]
local best_match = 1
local best_score = -1000000
for degree = 1, #scale_data do
local scale_note = scale_data[degree]
local root_note = scale_note.triad.notes[1]
local root_midi = PakettiArp_NoteToMidi(root_note.note, root_note.rel_octave)
local root_class = root_midi % 12
-- Triad pitch classes for this degree (root, 3rd, 5th)
local triad_classes = {}
for _, n in ipairs(scale_note.triad.notes) do
local midi_val = PakettiArp_NoteToMidi(n.note, n.rel_octave)
triad_classes[midi_val % 12] = true
end
local score = 0
-- Tiny nudge if bass matches this root (slash chords shouldn't flip function)
if bass_note_class == root_class then
score = score + 1
end
-- Root appears anywhere in the chord?
for _, note_class in ipairs(notes_in_line) do
if note_class == root_class then
score = score + 1
break
end
end
-- Score each chord tone:
-- - triad tone: strong
-- - diatonic but not triad: weak positive (extension)
-- - out of scale: penalty
for _, note_class in ipairs(notes_in_line) do
if triad_classes[note_class] then
score = score + 3
elseif scale_pitch_classes[note_class] then
score = score + 1
else
score = score - 2
end
end
-- Slight global bias towards the tonic when scores are close
if degree == 1 then
score = score + 1
end
if score > 0 then
print(string.format(" - Degree %d (%s %s) score: %d",
degree, scale_note.note, scale_note.triad.type, score))
end
if score > best_score then
best_score = score