-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphrases-editor-and-sequencer.pd_lua
2969 lines (2055 loc) · 84.1 KB
/
phrases-editor-and-sequencer.pd_lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
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
local Phrases = pd.Class:new():register("phrases-editor-and-sequencer")
-- Load the code's data tables in a tidy manner
local tabs = require("phrases-main-tables")
local kbnames = tabs.kbnames
local kbhash = tabs.kbhash(kbnames)
local notenames = tabs.notenames
local cmdtable = tabs.cmdtable
local cmdnames = tabs.cmdnames
local trnames = tabs.trnames
local catchtypes = tabs.catchtypes
local modenames = tabs.modenames
-- Check whether a value falls within a particular range; return true or false
local function rangeCheck(val, low, high)
if high < low then
low, high = high, low
end
if (val >= low)
and (val <= high)
then
return true
end
return false
end
-- Recursively copy all sub-tables, when copying from one table to another. Form of: newtable = deepCopy(oldtable, {})
local function deepCopy(t, t2)
for k, v in pairs(t) do
if type(v) ~= "table" then
t2[k] = v
else
local temp = {}
deepCopy(v, temp)
t2[k] = temp
end
end
return t2
end
-- Convert a numerical MIDI note value to a more human-readable note (e.g. C-3, D-4 etc). Note: The input is 0-indexed
local function readableNote(f)
local nkey = (f % 12) + 1
local nval = math.floor(f / 12)
local mout = notenames[nkey]
if string.len(mout) == 1 then
mout = mout .. "-"
end
mout = mout .. nval
return mout
end
-- Calculate a random transference direction, from a hash of weighted directions (1-9; 10 is ignored)
local function calcTransference(trhash)
local total, count, num = 0, 0, 0
for k, v in pairs(trhash) do
if k <= 9 then
total = total + v
end
end
local sel = math.random(total)
while (count < sel)
and (num < 9)
do
num = num + 1
if trhash[num] ~= nil then
count = count + trhash[num]
end
end
return num
end
-- Convert x,y coordinates into a key, within a matrix of width,height
local function coordsToKey(x, y, width, height, inoffset, outoffset)
x = x - inoffset
y = y - inoffset
local key = (((width * y) + x) % (height * width)) + outoffset
return key
end
-- Convert a key into coords x,y, within a martix of width,height
local function keyToCoords(key, width, height, inoffset, outoffset)
local x = ((key - inoffset) % width) + outoffset
local y = ((((key - inoffset) - x) / width) % height) + outoffset
return x, y
end
-- Move from key by offset of xmod,ymod, as plotted within a matrix of width,height, and return the proper key
local function trMod(key, xmod, ymod, width, height)
local x, y = keyToCoords(key, width, height, 1, 0)
x = (x + xmod) % width
y = (y + ymod) % height
return coordsToKey(x, y, width, height, 0, 1)
end
-- Make a matrix, linking each key to its surrounding keys, and connecting all edges
local function makeTrMatrix(width, height)
local matrix = {}
for x = 1, width do
for y = 1, height do
local key = coordsToKey(x, y, width, height, 1, 1)
matrix[key] = {}
matrix[key][1] = trMod(key, -1, -1, width, height)
matrix[key][2] = trMod(key, 0, -1, width, height)
matrix[key][3] = trMod(key, 1, -1, width, height)
matrix[key][4] = trMod(key, -1, 0, width, height)
matrix[key][5] = key
matrix[key][6] = trMod(key, 1, 0, width, height)
matrix[key][7] = trMod(key, -1, 1, width, height)
matrix[key][8] = trMod(key, 0, 1, width, height)
matrix[key][9] = trMod(key, 1, 1, width, height)
end
end
return matrix
end
-- Pre-generate a hash for displaying tick numbers in the editor GUI. Implemented this way to avoid the lag that would otherwise occur from a huge number of for-loop iterations if this were done live in updateNoteButton().
local function makeDisplayValHash(t)
local hash = {}
local halts = 1
for k, v in ipairs(t) do
hash[k] = halts
if v[1] == -1 then
halts = halts + 1
end
end
return hash
end
-- Get a table of RGB values (0-255), and return a table of RGB-normal, RGB-dark, RGB-light
local function modColor(color)
local colout = {}
colout[1] = color
colout[2], colout[3] = {}, {}
for i = 1, 3 do
colout[2][i] = math.max(0, color[i] - 30)
colout[3][i] = math.min(255, color[i] + 30)
end
return colout
end
-- Arrange a send-name, a color table, and a message-color table into a flat list
local function rgbOutList(name, ctab, mtab)
return { name, ctab[1], ctab[2], ctab[3], mtab[1], mtab[2], mtab[3], }
end
-- Send a note that has been parsed by noteParse()
function Phrases:noteSend(k, note)
if note[1] == -5 then
self.bpm = note[2]
pd.send("phrases-bpm", "float", {self.bpm})
elseif note[1] == -6 then
self.tpb = note[2]
pd.send("phrases-tpb", "float", {self.tpb})
elseif note[1] == -7 then
self.gate = note[2]
pd.send("phrases-gate", "float", {self.gate})
else
self:outlet(2, "list", note)
end
if rangeCheck(note[1], 144, 159) then -- All note-ons
local x = (k - 1) % self.gridx
local y = ((k - 1) - x) / self.gridy
local active = 1
if self.phrase[k].active == false then
active = 0
end
self:outlet(4, "list", {x, y, active}) -- Send a blink command for the phrase's Monome button
end
end
-- Pass a note command through the local and global sustain-tables
function Phrases:noteParse(k, note)
local chan = note[1] % 16
local command = note[1] - chan
local pitch = note[2]
local velo = note[3]
if (command == 144)
or (command == 128)
then
-- Set pitch and velocity to their ADC-shifted values, if applicable
if self.noteshift[chan][note[2]] ~= nil then
pitch = self.noteshift[chan][note[2]]
end
if self.veloshift[chan][note[2]] ~= nil then
velo = self.veloshift[chan][note[2]]
end
-- Create the parent tables if they don't exist
if self.midi[chan][pitch] == nil then
self.midi[chan][pitch] = 0
end
if self.phrase[k].midi[chan][pitch] == nil then
self.phrase[k].midi[chan][pitch] = 0
end
-- Modify sustain values, based on incoming note-on/note-offs
if command == 128 then
self.midi[chan][pitch] = self.midi[chan][pitch] - self.phrase[k].midi[chan][pitch]
self.phrase[k].midi[chan][pitch] = 0
-- Only send the noteoff command if the note's global sustain value is 0
if self.midi[chan][pitch] == 0 then
self.noteshift[chan][pitch] = nil
self.veloshift[chan][pitch] = nil
self:noteSend(k, {note[1], pitch, velo})
end
elseif command == 144 then
local oldpitch = pitch
local oldvelo = velo
-- Only shift note values before a noteon, so that they aren't changed inbetween a noteon and its corresponding noteoff
for k, v in ipairs(self.adc) do
if v.channel == chan then
local modval = v.magnitude * v.val
if v.target == 2 then
if v.style == "relative" then
pitch = math.min(127, math.max(0, math.floor(pitch + modval - (v.magnitude / 2))))
elseif v.style == "absolute" then
pitch = math.min(127, math.max(0, modval))
end
self.noteshift[chan][note[2]] = pitch
elseif v.target == 3 then
if v.style == "relative" then
velo = math.min(127, math.max(1, math.floor(velo + modval - (v.magnitude / 2))))
elseif v.style == "absolute" then
velo = math.min(127, math.max(1, modval))
end
self.veloshift[chan][note[2]] = velo
end
end
end
if oldpitch ~= pitch then
self.noteshift[chan][oldpitch] = nil
end
if oldvelo ~= velo then
self.veloshift[chan][oldpitch] = nil
end
if self.midi[chan][pitch] >= 1 then
-- If the note is already playing, send a quick noteoff and noteon, leaving the internal values the same
-- Old note is terminated correctly, using values from before the most recent ADC value-shift
self:noteSend(k, {128 + chan, oldpitch, oldvelo})
self:noteSend(k, {note[1], pitch, velo})
if self.phrase[k].midi[chan][pitch] == 0 then -- If the note is newly active in the phrase, track that locally and globally
self.phrase[k].midi[chan][pitch] = 1
self.midi[chan][pitch] = self.midi[chan][pitch] + 1
end
else -- If the note isn't already playing, track it locally and globally, and send a noteon
self.midi[chan][pitch] = 1
self.phrase[k].midi[chan][pitch] = 1
self:noteSend(k, {note[1], pitch, velo})
end
end
else -- Send all commands that aren't NOTEON/NOTEOFFs
self:noteSend(k, note)
end
end
-- Halt all MIDI sustains in a phrase, and apply them to the global MIDI-sustain array, and MIDI note-offs.
function Phrases:haltPhraseMidi(p)
-- For every currently active note in the phrase, send a MIDI-OFF through noteParse()
for chan, v in pairs(self.phrase[p].midi) do
for pitch, num in pairs(v) do
if num == 1 then
self:noteParse(p, {128 + chan, pitch, 127})
end
end
end
end
-- Called to iterate through the notes in various phrases
function Phrases:iterate(k)
local p = self.phrase[k].pointer
local tick = self.phrase[k].tick
tick = tick + 1
repeat -- Iterate through notes until hitting a silent beat, or looping through a self-terminating phrase
local oldp = p
local note = self.phrase[k].notes[p]
local chan = note[1] % 16
-- If the current note isn't a blank-tick, parse the note value
if self.phrase[k].notes[p][1] ~= -1 then
self:noteParse(k, self.phrase[k].notes[p])
end
p = p + 1
-- If the pointer has passed the end of the phrase, add a transference command to the tick's trqueue
if p > #self.phrase[k].notes then
p = 1
tick = 1
if self.phrase[k].tdir ~= 5 then -- Insert a transference command into the tick's transference table
self.trqueue[k] = self.phrase[k].tdir
else -- Recalculate the transference direction of every stationary phrase, as it wouldn't otherwise by done by the trqueue iterations
self.phrase[k].tdir = calcTransference(self.phrase[k].transfer)
end
-- Slate all self-terminating phrases for termination
if self.phrase[k].transfer[10] == 0 then
table.insert(self.trhalts, k)
end
end
until self.phrase[k].notes[oldp][1] == -1
self.phrase[k].pointer = p
self.phrase[k].tick = tick
end
-- Shift the volume value, transference strength, or ADC magnitude by a certain amount, depending on the input mode
function Phrases:shiftVolumeVal(val)
if self.inputmode == "note" then
self.velocity = (self.velocity + val) % 128
self:updateVelocityButton()
pd.post("MIDI Velocity set to " .. self.velocity)
elseif self.inputmode == "tr" then
if rangeCheck(self.trpoint, 1, 9) then
self.phrase[self.key].transfer[self.trpoint] = math.min(255, math.max(0, self.phrase[self.key].transfer[self.trpoint] + val))
pd.post("Phrase " .. self.key .. ": Set transference direction " .. self.channel .. " to strength " .. self.phrase[self.key].transfer[self.trpoint])
elseif self.trpoint == 10 then
self.phrase[self.key].transfer[10] = (self.phrase[self.key].transfer[10] + 1) % 2
pd.post("Phrase " .. self.key .. ": Persistence set to " .. self.phrase[self.key].transfer[10])
end
-- Update the relevant transference sub-button in the grid GUI
local xtr, ytr = keyToCoords(self.key, self.gridx, self.gridy, 1, 0)
local trbut = ytr .. "-" .. xtr .. "-grid-"
if rangeCheck(self.trpoint, 1, 9) then
if self.phrase[self.key].transfer[self.trpoint] > 0 then
self:outlet(5, "list", rgbOutList(trbut .. "sub-" .. self.trpoint, self.color[8][3], self.color[8][3]))
else
self:outlet(5, "list", rgbOutList(trbut .. "sub-" .. self.trpoint, self.color[8][2], self.color[8][2]))
end
end
self:addStateToHistory()
self:updateEditorGUI()
elseif (self.inputmode == "adc")
and (self.adc[self.adcpoint] ~= nil)
then
self.adc[self.adcpoint].magnitude = (self.adc[self.adcpoint].magnitude + val) % 128
pd.post("ADC " .. self.adcpoint .. ": magnitude set to " .. self.adc[self.adcpoint].magnitude)
self:addStateToHistory()
self:updateEditorGUI()
end
end
-- Update the color and contents of a cell in the editor panel
function Phrases:updateNoteButton(cellx, celly, k, p) -- editor x pointer, editor y pointer, phrase key, note pointer
local cvals = -1 -- Color-out value
local mcvals = -1 -- Message-color-out value
local col = {} -- Note-cell color holder
local message = ""
local bname = ""
local gsize = self.gridx * self.gridy
local offsetx = math.ceil(self.editorx / 2)
local offsety = math.floor(self.editory / 2)
local notex = (cellx + k) - offsetx
if (notex < 1)
or (notex > gsize)
then
notex = ((notex - 1) % gsize) + 1
end
local notenum = #self.phrase[notex].notes
-- Wrap short, non-active phrases against the active phrase's pointer
if p > notenum then
p = ((p - 1) % notenum) + 1
end
local notey = (celly - offsety) + 1 -- All inactive notes are stationary
if cellx == offsetx then -- All notes in the active phrase follow the pointer
notey = (celly + p) - offsety
end
-- Wrap the notes, to display the phrase repeating
if (notey < 1)
or (notey > notenum)
then
notey = ((notey - 1) % notenum) + 1
end
local haltticks = self.phrase[notex].dhash
local htick = haltticks[notey]
local hlen = string.len(tostring(htick))
local hmaxlen = string.len(tostring(haltticks[#haltticks]))
local note = self.phrase[notex].notes[notey]
-- Insert a number of periods that properly aligns the note with its column's max tick value
message = htick .. string.rep(".", hmaxlen - (hlen - 1))
-- Make the relevant data bytes more human-readable, if the pitchview flag is true. Else return their internal values
if self.pitchview == true then
if rangeCheck(note[1], 128, 143) then -- All NOTE-OFFs
message = message .. " " .. (note[1] % 16) .. "off " .. readableNote(note[2]) .. " " .. note[3]
elseif rangeCheck(note[1], 144, 159) then -- All NOTE-ONs
message = message .. " " .. (note[1] % 16) .. "on " .. readableNote(note[2]) .. " " .. note[3]
elseif rangeCheck(note[1], 192, 223) then -- All two-byte notes
message = message .. " " .. note[1] .. " " .. note[2]
elseif note[1] == -1 then -- Empty notes
message = message .. " --------"
elseif note[1] == -5 then -- Local BPM
message = message .. " BPM " .. note[2]
elseif note[1] == -6 then -- Local TPB
message = message .. " TPB " .. note[2]
elseif note[1] == -7 then -- Local GATE
message = message .. " GATE " .. note[2]
else -- All other three-byte MIDI messages
message = message .. " " .. table.concat(note, " ")
end
else
message = message .. " " .. table.concat(note, " ")
end
if self.inputmode == "tr" then -- Show the transference panel when transference mode is active
local tr = self.phrase[notex].transfer
if rangeCheck(celly, 1, 10) then
if (tr[celly] >= 1)
or (self.pitchview == false)
then
message = celly .. ". " .. tr[celly]
else
message = celly .. ". --"
end
mcvals = self.color[4][1]
if cellx == offsetx then -- Set active phrase's transference values to the main user-defined color
if celly == self.trpoint then
cvals = self.color[1][1]
else
cvals = self.color[1][2]
end
else -- Set other transference values to the secondary user-defined color
cvals = self.color[2][1]
end
else
cvals = self.color[3][2]
mcvals = self.color[3][1]
end
bname = (celly - 1) .. "-" .. (cellx - 1) .. "-editor-button"
self:outlet(5, "list", {bname, cvals[1], cvals[2], cvals[3], mcvals[1], mcvals[2], mcvals[3]})
pd.send(bname, "label", {message})
elseif self.inputmode == "adc" then
cvals = self.color[3][2]
mcvals = self.color[3][1]
if cellx == offsetx then
mcvals = self.color[4][1]
local curadc = math.floor((celly - 1) / 4) + 1
if self.adc[curadc] ~= nil then
if ((celly - 1) % 4) == 0 then
message = curadc .. ". Chan: " .. self.adc[curadc].channel
elseif ((celly - 1) % 4) == 1 then
message = curadc .. ". Target: " .. self.adc[curadc].target
elseif ((celly - 1) % 4) == 2 then
message = curadc .. ". Style: " .. self.adc[curadc].style
elseif ((celly - 1) % 4) == 3 then
message = curadc .. ". Magnitude: " .. self.adc[curadc].magnitude
end
if curadc == self.adcpoint then
cvals = self.color[1][1]
else
cvals = self.color[2][2]
end
end
end
bname = (celly - 1) .. "-" .. (cellx - 1) .. "-editor-button"
self:outlet(5, "list", {bname, cvals[1], cvals[2], cvals[3], mcvals[1], mcvals[2], mcvals[3]})
pd.send(bname, "label", {message})
else -- Display colors normally when in other view modes
if note[1] == -1 then -- Blank note color
col = self.color[3]
elseif rangeCheck(note[1], 128, 159) then -- Note-on / note-off color
col = self.color[1]
else -- Other commands color
col = self.color[2]
end
if cellx == offsetx then -- For the active phrase, use regular colors
cvals = col[1]
mcvals = self.color[4][1]
else -- For all inactive notes, use dark colors
cvals = col[2]
mcvals = self.color[4][2]
end
if ((htick - 1) % self.gate) == 0 then -- For all notes that fall on the global gate value, use bright colors
cvals = col[3]
mcvals = self.color[4][3]
end
if cellx == offsetx then -- On the active phrase, replace some regular colors with copypaste colors, if the copypaste variables are active
if (
(self.copystart ~= nil)
and (notey == self.copystart)
) or (
(self.copyend ~= nil)
and (notey == self.copyend)
)
then -- Assign gaudy color values to the upper and lower bounds of the copypaste selection area
cvals = self.color[2][1]
mcvals = self.color[1][1]
elseif (self.copystart ~= nil)
and (self.copyend ~= nil)
and (notey > self.copystart)
and (notey < self.copyend)
then -- Reverse color values for all notes within the copypaste selection area
cvals, mcvals = mcvals, cvals
end
end
if celly == offsety then -- Reverse color values on the active row
cvals, mcvals = mcvals, cvals
end
bname = (celly - 1) .. "-" .. (cellx - 1) .. "-editor-button"
self:outlet(5, "list", {bname, cvals[1], cvals[2], cvals[3], mcvals[1], mcvals[2], mcvals[3]})
pd.send(bname, "label", {message})
end
end
-- Update the toggle-tracking button
function Phrases:updateToggleButton()
if self.recording == true then
self:outlet(5, "list", rgbOutList("phrases-editor-toggle-button", self.color[1][1], self.color[4][1]))
pd.send("phrases-editor-toggle-button", "label", {"REC"})
else
self:outlet(5, "list", rgbOutList("phrases-editor-toggle-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-toggle-button", "label", {"PLAY"})
end
end
-- Update the phrase-key button
function Phrases:updateKeyButton()
self:outlet(5, "list", rgbOutList("phrases-editor-key-button", self.color[2][2], self.color[4][2]))
pd.send("phrases-editor-key-button", "label", {"Phrase " .. self.key})
end
-- Update the note-item button
function Phrases:updateItemButton()
self:outlet(5, "list", rgbOutList("phrases-editor-item-button", self.color[2][2], self.color[4][2]))
pd.send("phrases-editor-item-button", "label", {"Item " .. self.pointer})
end
-- Update the tick-counter button
function Phrases:updateTickButton()
local tcount = 0
local nbyte = 0
for i = 1, #self.phrase[self.key].notes do -- Count which tick the pointer is on, as opposed to which MIDI command
nbyte = self.phrase[self.key].notes[i][1]
if nbyte == -1 then
tcount = tcount + 1
if i >= self.pointer then
do break end -- Break the for loop, after encountering the first halting tick at or past the pointer
end
end
end
self:outlet(5, "list", rgbOutList("phrases-editor-tick-button", self.color[2][2], self.color[4][2]))
pd.send("phrases-editor-tick-button", "label", {"Tick " .. tcount})
end
-- Update the data-entry-mode button
function Phrases:updateModeButton()
self:outlet(5, "list", rgbOutList("phrases-editor-mode-button", self.color[2][1], self.color[4][1]))
if self.inputmode == "note" then
pd.send("phrases-editor-mode-button", "label", {"Mode: Note"})
elseif self.inputmode == "tr" then
pd.send("phrases-editor-mode-button", "label", {"Mode: Tr"})
elseif self.inputmode == "adc" then
pd.send("phrases-editor-mode-button", "label", {"Mode: ADC"})
end
end
-- Update the MIDI-catch-style button
function Phrases:updateMIDICatchButton()
if self.midicatch == "all" then
self:outlet(5, "list", rgbOutList("phrases-editor-midicatch-button", self.color[2][2], self.color[4][2]))
pd.send("phrases-editor-midicatch-button", "label", {"Catch: All"})
elseif self.midicatch == "no-offs" then
self:outlet(5, "list", rgbOutList("phrases-editor-midicatch-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-midicatch-button", "label", {"Catch: No Offs"})
elseif self.midicatch == "auto-offs" then
self:outlet(5, "list", rgbOutList("phrases-editor-midicatch-button", self.color[1][1], self.color[4][1]))
pd.send("phrases-editor-midicatch-button", "label", {"Catch: Auto Offs"})
elseif self.midicatch == "notes" then
self:outlet(5, "list", rgbOutList("phrases-editor-midicatch-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-midicatch-button", "label", {"Catch: Notes"})
elseif self.midicatch == "ignore" then
self:outlet(5, "list", rgbOutList("phrases-editor-midicatch-button", self.color[2][3], self.color[4][3]))
pd.send("phrases-editor-midicatch-button", "label", {"Catch: None"})
end
end
-- Update the MIDI-channel button
function Phrases:updateChannelButton()
local chbcolor = -1
local chbmessage = ""
if self.inputmode == "note" then
chbcolor = self.color[2][1]
chbmessage = "Chan " .. self.channel
elseif self.inputmode == "tr" then
chbcolor = self.color[1][1]
chbmessage = "Tr: " .. trnames[self.trpoint]
elseif self.inputmode == "adc" then
chbcolor = self.color[1][1]
chbmessage = "ADC: "
if self.adc[self.adcpoint] ~= nil then
chbmessage = chbmessage .. self.adcpoint
else
chbmessage = chbmessage .. "N/A"
end
end
self:outlet(5, "list", rgbOutList("phrases-editor-channel-button", chbcolor, self.color[4][1]))
pd.send("phrases-editor-channel-button", "label", {chbmessage})
end
-- Update the MIDI-command button
function Phrases:updateCommandButton()
local cmdkey = 1
for k, v in pairs(cmdtable) do
if v == self.command then
cmdkey = k
end
end
local cmdbcol = self.color[2][1]
if rangeCheck(self.command, -2, -4)
or rangeCheck(self.command, 128, 144)
then
cmdbcol = self.color[1][1]
end
self:outlet(5, "list", rgbOutList("phrases-editor-command-button", cmdbcol, self.color[4][1]))
pd.send("phrases-editor-command-button", "label", {"Cmd: " .. cmdnames[cmdkey]})
end
-- Update the MIDI-velocity button
function Phrases:updateVelocityButton()
self:outlet(5, "list", rgbOutList("phrases-editor-velocity-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-velocity-button", "label", {"Velo " .. self.velocity})
end
-- Update the input octave button
function Phrases:updateOctaveButton()
self:outlet(5, "list", rgbOutList("phrases-editor-octave-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-octave-button", "label", {"Octave " .. self.octave})
end
-- Update the input spacing button
function Phrases:updateSpacingButton()
self:outlet(5, "list", rgbOutList("phrases-editor-spacing-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-spacing-button", "label", {"Spacing " .. self.spacing})
end
-- Update the global BPM button
function Phrases:updateGlobalBPMButton()
self:outlet(5, "list", rgbOutList("phrases-editor-global-bpm-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-global-bpm-button", "label", {"BPM " .. self.bpm})
end
-- Update the global TPB button
function Phrases:updateGlobalTPBButton()
self:outlet(5, "list", rgbOutList("phrases-editor-global-tpb-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-global-tpb-button", "label", {"TPB " .. self.tpb})
end
-- Update the global GATE button
function Phrases:updateGlobalGateButton()
self:outlet(5, "list", rgbOutList("phrases-editor-global-gate-button", self.color[2][1], self.color[4][1]))
pd.send("phrases-editor-global-gate-button", "label", {"Gate " .. self.gate})
end
-- Update the savefile hotseat buttons
function Phrases:updateHotseatButtons()
local outcolor = nil
for k, v in pairs(self.hotseats) do
if k == self.hotseatnum then
outcolor = self.color[1][1]
elseif v == self.loadname then
outcolor = self.color[2][2]
else
outcolor = self.color[2][1]
end
self:outlet(5, "list", rgbOutList("phrases-editor-hotseat-button-" .. k, outcolor, self.color[4][1]))
pd.send("phrases-editor-hotseat-button-" .. k, "label", {k .. string.rep(".", 3 - string.len(k)) .. " " .. v})
end
end
-- Update the editor's background color
function Phrases:updateBackground()
self:outlet(5, "list", rgbOutList("phrases-editor-bg", self.color[3][1], self.color[3][1]))
end
-- Update all cells and buttons in the editor GUI
function Phrases:updateEditorGUI()
self:updateToggleButton()
self:updateKeyButton()
self:updateItemButton()
self:updateTickButton()
self:updateModeButton()
self:updateMIDICatchButton()
self:updateChannelButton()
self:updateCommandButton()
self:updateVelocityButton()
self:updateOctaveButton()
self:updateSpacingButton()
self:updateGlobalBPMButton()
self:updateGlobalTPBButton()
self:updateGlobalGateButton()
self:updateHotseatButtons()
for ey = 1, self.editory do
for ex = 1, self.editorx do
self:updateNoteButton(ex, ey, self.key, self.pointer)
end
end
self:updateBackground()
end
-- Setup the color values in the grid GUI that would otherwise not be set by other code
function Phrases:setupGridGUI()
for x = 0, self.gridx - 1 do
for y = 0, self.gridy - 1 do
self:outlet(5, "list", rgbOutList(y .. "-" .. x .. "-grid-button", self.color[8][2], self.color[8][2]))
for i = 1, 9 do
self:outlet(5, "list", rgbOutList(y .. "-" .. x .. "-grid-sub-" .. i, self.color[8][1], self.color[8][1]))
end
end
end
self:outlet(5, "list", rgbOutList("phrases-grid-bg", self.color[8][1], self.color[8][1]))
for i = 1, self.adcnum do
self:outlet(5, "list", rgbOutList(i .. "-adc-button", self.color[5][1], self.color[5][1]))
end
end
-- Refresh the transference sub-buttons in the grid GUI
function Phrases:refreshSubButtons()
for x = 0, self.gridx - 1 do
for y = 0, self.gridy - 1 do
local subkey = coordsToKey(x, y, self.gridx, self.gridy, 0, 1)
for i = 1, 9 do
if self.phrase[subkey].transfer[i] > 0 then
self:outlet(5, "list", rgbOutList(y .. "-" .. x .. "-grid-sub-" .. i, self.color[8][3], self.color[8][3]))
else
self:outlet(5, "list", rgbOutList(y .. "-" .. x .. "-grid-sub-" .. i, self.color[8][2], self.color[8][2]))
end
end
end
end
end
-- Inserts an OFF-note at the location of the pointer, if there is at least one other ON-note on the same MIDI channel at any point in the phrase.
function Phrases:insertAutoOff()
local offchan = nil
local offnote = nil
local iterc = (self.pointer % #self.phrase[self.key].notes) + 1
while iterc ~= self.pointer do
if (self.phrase[self.key].notes[iterc][1] % 16) == self.channel then
offchan = self.phrase[self.key].notes[iterc][1] % 16
offnote = self.phrase[self.key].notes[iterc][2]
end
iterc = (iterc % #self.phrase[self.key].notes) + 1
end
if offchan ~= nil then
offchan = offchan + 128
local offbytes = {offchan, offnote, 127}
table.insert(self.phrase[self.key].notes, self.pointer, offbytes)
self.pointer = self.pointer + 1
pd.post("Inserted note " .. table.concat(offbytes, " ") .. " at point " .. (self.pointer - 1))