-
Notifications
You must be signed in to change notification settings - Fork 16
/
StepSequencerComponent.py
executable file
·1637 lines (1426 loc) · 57.6 KB
/
StepSequencerComponent.py
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
import Live
from .consts import *
from _Framework.ControlSurfaceComponent import ControlSurfaceComponent
from _Framework.CompoundComponent import CompoundComponent
from _Framework.ButtonElement import ButtonElement
from _Framework.Util import find_if
try:
from itertools import imap
except ImportError:
# Python 3...
imap=map
from .NoteEditorComponent import NoteEditorComponent
from .ScaleComponent import *
from .TrackControllerComponent import TrackControllerComponent
import time
from .Settings import *
# quantization button colours. this must remain of length 4.
QUANTIZATION_MAP = [1, 0.5, 0.25, 0.125] # 1/4 1/8 1/16 1/32
QUANTIZATION_NAMES = ["1/4", "1/8", "1/16", "1/32"]
STEPSEQ_MODE_NORMAL = 1
STEPSEQ_MODE_MULTINOTE = 2
STEPSEQ_MODE_SCALE_EDIT = 10
LONG_BUTTON_PRESS = 1.0
class NoteSelectorComponent(ControlSurfaceComponent):
def __init__(self, control_surface = None, stepsequencer = None, buttons = None):
ControlSurfaceComponent.__init__(self)
self._control_surface = control_surface
self._stepsequencer = stepsequencer
self.set_enabled(False)
self._clip = None
self._track = None
self._notes = None
self._playhead = None
self._root_note = 36
self._offset = 0
self._key = 0
self._scale = [0, 2, 4, 5, 7, 9, 11, 12]
self._force = True
self._up_button = None
self._down_button = None
self._buttons = None
self._enable_buttons = True
# cache to optimise display for offset buttons minimizing midi traffic
self._cache = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
self._was_velocity_shifted = False
if buttons!=None:
self.set_buttons(buttons)
def set_buttons(self, buttons):
if self._buttons:
for button in self._buttons:
assert isinstance(button, ButtonElement)
button.remove_value_listener(self.note_offset_button_value)
self._buttons = buttons
if self._buttons:
for button in self._buttons:
assert isinstance(button, ButtonElement)
button.add_value_listener(self.note_offset_button_value, identify_sender=True)
def set_clip(self, clip):
self._clip = clip
def set_note_cache(self, note_cache):
self._note_cache = note_cache
def set_playhead(self, playhead):
self._playhead = playhead
if self._buttons != None:
self._update_buttons()
@property
def _is_mute_shifted(self):
return self._stepsequencer._is_mute_shifted
@property
def _is_velocity_shifted(self):
return self._stepsequencer._is_velocity_shifted
@property
def _drum_group_device(self):
return None
#self._stepsequencer._drum_group_device
@property
def is_drumrack(self):
return self._stepsequencer._scale_component.is_drumrack# and self._drum_group_device != None
@property
def is_chromatic(self):
return self._stepsequencer._scale_component.is_chromatic
@property
def is_diatonic(self):
return self._stepsequencer._scale_component.is_diatonic
# DOWN Button
def _update_down_button(self):
if self.is_enabled():
if self._down_button != None:
if self._clip == None:
self._down_button.set_on_off_values("DefaultButton.Disabled", "DefaultButton.Disabled")
self._down_button.turn_off()
else:
self._down_button.set_on_off_values("StepSequencer.Octave.On", "StepSequencer.Octave.Off")
if not self._is_mute_shifted and not self._enable_buttons or self._is_mute_shifted and self._enable_buttons:
if self.can_scroll_down():
self._down_button.turn_on()
else:
self._down_button.turn_off()
else:
if self.can_page_down():
self._down_button.turn_on()
else:
self._down_button.turn_off()
def set_down_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if (self._down_button != button):
if (self._down_button != None):
self._down_button.remove_value_listener(self._down_button_value)
self._down_button = button
if (self._down_button != None):
assert isinstance(button, ButtonElement)
self._down_button.add_value_listener(self._down_button_value, identify_sender=True)
self._update_down_button()
def _down_button_value(self, value, sender):
assert (self._down_button != None)
assert (value in range(128))
if self.is_enabled() and self._clip != None:
if value is not 0 or not sender.is_momentary():
if not self._is_mute_shifted and not self._enable_buttons or self._is_mute_shifted and self._enable_buttons:
self.scroll_down()
else:
self.page_down()
self._stepsequencer.update()
# UP button
def _update_up_button(self):
if self.is_enabled():
if self._up_button != None:
if self._clip == None:
self._up_button.set_on_off_values("DefaultButton.Disabled", "DefaultButton.Disabled")
self._up_button.turn_off()
else:
self._up_button.set_on_off_values("StepSequencer.Octave.On", "StepSequencer.Octave.Off")
if not self._is_mute_shifted and not self._enable_buttons or self._is_mute_shifted and self._enable_buttons:
if self.can_scroll_up():
self._up_button.turn_on()
else:
self._up_button.turn_off()
else:
if self.can_page_up():
self._up_button.turn_on()
else:
self._up_button.turn_off()
def set_up_button(self, button):
assert (isinstance(button, (ButtonElement, type(None))))
if self._up_button != button:
if self._up_button != None:
self._up_button.remove_value_listener(self._up_button_value)
self._up_button = button
if self._up_button != None:
assert isinstance(button, ButtonElement)
self._up_button.add_value_listener(self._up_button_value, identify_sender=True)
self._update_up_button()
def _up_button_value(self, value, sender):
assert (self._up_button != None)
assert (value in range(128))
if self.is_enabled() and self._clip != None:
if value is not 0 or not sender.is_momentary():
if not self._is_mute_shifted and not self._enable_buttons or self._is_mute_shifted and self._enable_buttons:
self.scroll_up()
else:
self.page_up()
self._stepsequencer.update()
# Note Offeset Buttons
def note_offset_button_value(self, value, sender):
if self.is_enabled() and value > 0 and self._enable_buttons:
if self._is_mute_shifted:
try:
index = self._buttons.index(sender)
except ValueError:
index = -1
self._stepsequencer._note_editor.mute_lane(self._root_note + index)
else:
try:
self._offset = self._buttons.index(sender)
except ValueError:
self._offset = -1
if self.is_drumrack and self._drum_group_device!=None:
self._drum_group_device.view.selected_drum_pad = self._drum_group_device.drum_pads[self.selected_note]
self.update()
self._stepsequencer._scale_updated()
def update(self):
if self.is_enabled():
self._stepsequencer._track_controller._implicit_arm = self._is_velocity_shifted and not self._stepsequencer._is_locked
if self._stepsequencer._track_controller._implicit_arm:
self._stepsequencer._track_controller._do_implicit_arm()
if self._is_velocity_shifted and not self._stepsequencer._is_locked:
self._control_surface.set_feedback_channels([11])
# set goblaly in my script. #self._control_surface._c_instance.set_feedback_velocity(5)#self._skin.scale.note_recording)
self._was_velocity_shifted = True
elif self.is_drumrack and self._was_velocity_shifted:
self._was_velocity_shifted = False
self._offset = self._drum_group_device.view.selected_drum_pad.note - self._root_note
self._stepsequencer._scale_updated()
self._update_up_button()
self._update_down_button()
self._update_buttons()
def _update_buttons(self):
if self._enable_buttons and self.is_enabled() and self._buttons!=None:
for i in range(len(self._buttons)):
if self._clip == None:
self._buttons[i].set_on_off_values("DefaultButton.Disabled", "DefaultButton.Disabled")
self._buttons[i].turn_off()
else:
note = self._root_note + i
if self.is_drumrack:
if self._drum_group_device==None:
self._buttons[i].set_on_off_values("DrumGroup.PadSelected","DrumGroup.PadFilled")
else:
if self._drum_group_device.drum_pads[note].chains:
self._buttons[i].set_on_off_values("DrumGroup.PadSelected","DrumGroup.PadFilled")
else:
self._buttons[i].set_on_off_values("DrumGroup.PadSelected", "DrumGroup.PadEmpty")
else:
if i % 12 == self._scale[0]:
self._buttons[i].set_on_off_values("StepSequencer.NoteSelector.Selected", "Note.Pads.Root")
elif i % 12 == self._scale[2] or i % 12 == self._scale[4]:
self._buttons[i].set_on_off_values("StepSequencer.NoteSelector.Selected", "Note.Pads.Highlight")
elif self._scale != None and i % 12 in self._scale:
self._buttons[i].set_on_off_values("StepSequencer.NoteSelector.Selected", "Note.Pads.InScale")
else:
self._buttons[i].set_on_off_values("StepSequencer.NoteSelector.Selected", "Note.Pads.OutOfScale")
if self._is_velocity_shifted and not self._stepsequencer._is_locked:
self._buttons[i].force_next_send()
# self._buttons[i].turn_off()
self._buttons[i].set_enabled(False)
self._buttons[i].set_channel(11)
self._buttons[i].set_identifier(note)
else:
self._buttons[i].set_enabled(True)
self._buttons[i].use_default_message()
if self._playhead != None and self.note_is_playing(self._clip, self._note_cache, note, self._playhead):
self._buttons[i].set_on_off_values("StepSequencer.NoteSelector.Playing","StepSequencer.NoteSelector.Playing")
if self.selected_note == note:
if self._cache[i] != self._buttons[i]._on_value or self._force:
self._buttons[i].turn_on()
self._cache[i] = self._buttons[i]._on_value
else:
if self._cache[i] != self._buttons[i]._off_value or self._force:
self._buttons[i].turn_off()
self._cache[i] = self._buttons[i]._off_value
self._force = False
def set_enabled(self, enabled):
if enabled:
self._cache = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
ControlSurfaceComponent.set_enabled(self, enabled)
def set_scale(self, scale, key=-1):
if key != -1:
self._key = key
self._scale = scale
# relativise scale
for i in range(len(self._scale)):
self._scale[i] = self._scale[i] - self._key
def note_is_playing(self, clip, note_cache, midi_note, playhead):
if clip != None and clip.is_playing and note_cache != None:
for note in note_cache:
note_key = note[0]
note_position = note[1]
note_length = note[2]
# note_velocity = note[3]
note_muted = note[4]
if midi_note == note_key and note_position <= playhead and note_position + note_length >= playhead and not note_muted:
return True
return False
def scroll_down(self):
self.move(-1)
def scroll_up(self):
self.move(1)
def page_down(self):
if self.is_drumrack:
self.move(-16)
else:
self.move(-12)
def page_up(self):
if self.is_drumrack:
self.move(16)
else:
self.move(12)
def can_scroll_down(self):
return self.can_move(-1)
def can_scroll_up(self):
return self.can_move(1)
def can_page_down(self):
if self.is_drumrack:
return self.can_move(-16)
else:
return self.can_move(-12)
def can_page_up(self):
if self.is_drumrack:
return self.can_move(16)
else:
return self.can_move(12)
def can_move(self, steps):
return self.selected_note + steps >= 0 and self.selected_note + steps < 128
def move(self, steps):
if self.can_move(steps):
if self.is_diatonic:
# self._control_surface.log_message("can move dia "+ str(steps))
# find the next note in scale in that direction
oct = 0
if steps > 0:
while self._offset + steps >= 12:
steps = steps - 12
oct = oct + 1
while steps != 0 and self._offset + steps not in self._scale:
steps = steps + 1
while self._offset + steps > 12:
steps = steps - 12
oct = oct + 1
elif steps < 0:
while self._offset + steps < 0:
steps = steps + 12
oct = oct - 1
while steps != 0 and self._offset + steps not in self._scale:
steps = steps - 1
if self._offset + steps < 0:
steps = steps + 12
oct = oct - 1
try:
idx = self._scale.index(self._offset + steps)
except ValueError:
idx = -1
self.set_selected_note(self._root_note + oct * 12 + self._scale[idx])
else:
# self._control_surface.log_message("can move drum/chrom "+ str(steps) + " root:"+str(self._root_note)+" offset:"+str(self._offset))
self.set_selected_note(self._root_note + self._offset + steps)
def set_selected_note(self, selected_note):
if self.is_drumrack:
self._root_note = int((selected_note + 12) / 16 - 1) * 16 + 4
self._offset = (selected_note - self._root_note + 16) % 16
# self._control_surface.log_message("DR selected_note:"+str(selected_note)+" self._root_note: "+ str(self._root_note)+" offset: "+ str(self._offset))
else:
self._root_note = int((selected_note - self._key) / 12) * 12 + self._key
self._offset = (selected_note + 12 - self._root_note) % 12
# self._control_surface.log_message("CHR selected_note:"+str(selected_note)+" self._root_note: "+ str(self._root_note)+" offset: "+ str(self._offset))
self.update()
self._stepsequencer._scale_updated()
def set_key(self, key):
self._key = key
def note_is_available(self, key):
if self.is_drumrack:
if self._drum_group_device.drum_pads[key].chains:
return True
else:
return False
else:
for note in scale:
if key % 12 == (self.root_note + note) % 12:
return True
else:
return False
def note_is_used(self, key):
if clip != None:
for note in self._clip_notes:
if note[0] == key: # key: 0-127 MIDI note #
return True
return False
@property
def selected_note(self):
return self._root_note + self._offset
class LoopSelectorComponent(ControlSurfaceComponent):
def __init__(self, control_surface = None, stepsequencer = None, buttons = None):
ControlSurfaceComponent.__init__(self)
self.set_enabled(False)
self._stepsequencer = stepsequencer
self._control_surface = control_surface
self._clip = None
self._notes = None
self._playhead = None
self._buttons = None
self._loop_end = 0
self._loop_start = 0
self._blocksize = 8 # number of note per block
self._block = 0 # current block
# used for loop selection
self._last_button = -1
self._last_button_time = time.time()
self._loop_point1 = -1
self._loop_point2 = -1
self._cache = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
if buttons!= None:
self.set_buttons(buttons)
def set_buttons(self, buttons):
if self._buttons:
for button in self._buttons:
assert isinstance(button, ButtonElement)
button.remove_value_listener(self._loop_button_value)
self._buttons = buttons
if self._buttons:
for button in self._buttons:
assert isinstance(button, ButtonElement)
button.add_value_listener(self._loop_button_value, identify_sender=True)
def disconnect(self):
self._top_buttons = None
@property
def _number_of_lines_per_note(self):
if self._mode == STEPSEQ_MODE_MULTINOTE:
return self._stepsequencer._number_of_lines_per_note
else:
return 1
def set_clip(self, clip):
self._clip = clip
@property
def _mode(self):
return self._stepsequencer._mode
def set_note_cache(self, note_cache):
self._note_cache = note_cache
def set_playhead(self, playhead):
self._playhead = playhead
if self._buttons != None:
self.update()
@property
def _is_mute_shifted(self):
return self._stepsequencer._is_mute_shifted
@property
def _is_velocity_shifted(self):
return self._stepsequencer._note_editor._is_velocity_shifted
@property
def _quantization(self):
return self._stepsequencer._quantization
@property
def block(self):
return self._block
def set_blocksize(self, blocksize):
self._blocksize = blocksize
def set_enabled(self, enabled):
self._force = True
ControlSurfaceComponent.set_enabled(self, enabled)
def _get_clip_loop(self):
if self._clip != None:
self._loop_start = self._clip.loop_start
self._loop_end = self._clip.loop_end
else:
self._loop_start = 0
self._loop_end = 0
def set_clip_loop(self, start, end):
if self._clip != None:
self._loop_end = end
self._loop_start = start
if self._loop_start >= self._clip.loop_end:
self._clip.loop_end = self._loop_end
self._clip.loop_start = self._loop_start
self._clip.end_marker = self._loop_end
self._clip.start_marker = self._loop_start
else:
self._clip.loop_start = self._loop_start
self._clip.loop_end = self._loop_end
self._clip.start_marker = self._loop_start
self._clip.end_marker = self._loop_end
self.update()
def _loop_button_value(self, value, sender):
if self.is_enabled():
x = self._buttons.index(sender)
if value > 0:
if self._loop_point1 == -1:
self._loop_point1 = x
elif self._loop_point2 == -1:
self._loop_point2 = x
elif self._loop_point1 != -1:
setloop = self._loop_point2 != -1
if self._loop_point2 == -1:
self._loop_point2 = x
if self._last_button == x and time.time() - self.last_button_time < 0.25:
setloop = True
self.last_button_time = time.time()
self._last_button = -1
if self._loop_point1 != -1 and self._loop_point2 != -1:
if self._loop_point1 > self._loop_point2:
start = self._loop_point2
end = self._loop_point1
else:
start = self._loop_point1
end = self._loop_point2
end = end + 1
self._block = start
if setloop:
if self._is_mute_shifted:
if self._is_velocity_shifted:
self._mute_notes_in_range(start * self._blocksize * self._quantization, end * self._blocksize * self._quantization)
else:
self._delete_notes_in_range(start * self._blocksize * self._quantization, end * self._blocksize * self._quantization)
else:
if self._is_velocity_shifted:
self._extend_clip_content(start * self._blocksize * self._quantization, self._loop_end, end * self._blocksize * self._quantization)
self.set_clip_loop(start * self._blocksize * self._quantization, end * self._blocksize * self._quantization)
self._stepsequencer.set_page(self._block)
self._loop_point1 = -1
self._loop_point2 = -1
self.update()
self.last_button_time = time.time()
self._last_button = x
def can_scroll(self, blocks):
if self._clip != None:
if (blocks + self._block) < 0:
return False
if (blocks + self._block) * 8 * self._quantization * self._number_of_lines_per_note < self._clip.loop_start:
return False
if (blocks + self._block + 1) * 8 * self._quantization * self._number_of_lines_per_note > self._clip.loop_end:
return False
return True
return False
def scroll(self, blocks):
if self._clip != None and self.can_scroll(blocks):
self._block = blocks + self._block
self._stepsequencer.set_page(self._block)
def update(self):
if self.is_enabled() and self._buttons:
self._get_clip_loop()
i = 0
for button in self._buttons:
if self._clip == None:
button.set_on_off_values("DefaultButton.Disabled","DefaultButton.Disabled")
if self._cache[i] != button._off_value:
button.turn_off()
self._cache[i] = button._off_value
else:
in_loop = (i * self._blocksize * self._quantization < self._loop_end) and (i * self._blocksize * self._quantization >= self._loop_start)
playing = self._playhead != None and self._playhead >= i * self._blocksize * self._quantization and self._playhead < (i + 1) * self._blocksize * self._quantization
selected = i == self.block
if in_loop:
if playing:
if selected:
self._cache[i] = "StepSequencer.LoopSelector.SelectedPlaying"
else:
self._cache[i] = "StepSequencer.LoopSelector.Playing"
else:
if selected:
self._cache[i] = "StepSequencer.LoopSelector.Selected"
else:
self._cache[i] = "StepSequencer.LoopSelector.InLoop"
else:
if playing:
if selected:
self._cache[i] = "StepSequencer.LoopSelector.SelectedPlaying"
else:
self._cache[i] = "StepSequencer.LoopSelector.Playing"
else:
if selected:
self._cache[i] = "StepSequencer.LoopSelector.Selected"
else:
self._cache[i] = "DefaultButton.Disabled"
if self._cache[i] != button._on_value or self._force:
button.set_on_off_values(self._cache[i], self._cache[i])
button.turn_on()
i = i + 1
self._force = False
def _extend_clip_content(self, loop_start, old_loop_end, new_loop_end):
if(self._no_notes_in_range(old_loop_end, new_loop_end, True)):
clip_looping_length = 0
if(old_loop_end > 1):
power = 1
while(power * 2 < old_loop_end):
power *= 2
clip_looping_length = (power)
clone_length = new_loop_end - old_loop_end
if(clip_looping_length > 0):
clone_start_point = (old_loop_end % clip_looping_length)
else:
clone_start_point = 0
self._copy_notes_in_range(clone_start_point, clone_start_point + clone_length, old_loop_end)
def _no_notes_in_range(self, start, end, or_after):
for note in list(self._note_cache):
if note[1] >= start and (note[1] < end or or_after):
return(False)
return(True)
def _delete_notes_in_range(self, start, end):
new_notes = list()
for note in list(self._note_cache):
if note[1] < start or note[1] >= end:
new_notes.append(note)
self._clip.select_all_notes()
self._clip.replace_selected_notes(tuple(new_notes))
def _mute_notes_in_range(self, start, end):
new_notes = list()
for note in list(self._note_cache):
if note[1] < start or note[1] >= end:
new_notes.append(note)
else:
new_notes.append([note[0], note[1], note[2], note[3], not note[4]])
self._clip.select_all_notes()
self._clip.replace_selected_notes(tuple(new_notes))
def _copy_notes_in_range(self, start, end, new_start):
new_notes = list(self._note_cache)
# for i in range()
for note in new_notes:
if note[1] >= start and note[1] < end:
new_notes.append([note[0], note[1] + new_start - start, note[2], note[3], note[4]])
self._clip.select_all_notes()
self._clip.replace_selected_notes(tuple(new_notes))
class StepSequencerComponent(CompoundComponent):
def __init__(self, control_surface = None, matrix = None, side_buttons = None, top_buttons = None):
self._osd = None
self._control_surface = control_surface
super(StepSequencerComponent, self).__init__()
self.QUANTIZATION_COLOR_MAP = ["StepSequencer.Quantization.One","StepSequencer.Quantization.Two","StepSequencer.Quantization.Three","StepSequencer.Quantization.Four"]
self._name = "drum step sequencer"
# clip
self._clip = None
self._clip_slot = None
self._note_cache = []
self._playhead = 0
self._new_clip_pages = 4
# mode
self._mode = -1
self._mode_backup = self._mode
# buttons
self._is_mute_shifted = None
self._scale_button = None
self._quantization_button = None
self._mode_button = None
self._mute_shift_button = None
self._matrix = None
self._left_button = None
self._right_button = None
# scale
self._root_note = 36
self._chromatic_scale = []
self._diatonic_scale = []
# setup
self._number_of_lines_per_note = 1
self._quantization_index = 1
self._quantization = QUANTIZATION_MAP[self._quantization_index]
self._set_loop_selector()
self._set_note_editor()
self._set_note_selector()
self._set_track_controller()
self._set_scale_component()
self._set_lock_function()
self.set_enabled(False)
# TODO: maybe clean this... this should be done on enable.
# self.on_clip_slot_changed()
def set_left_button(self, button):
self._loop_selector.set_left_button(button)
def set_right_button(self, button):
self._loop_selector.set_right_button(button)
def set_octave_down_button(self, button):
self._note_selector.set_down_button(button)
def set_octave_up_button(self, button):
self._note_selector.set_up_button(button)
def set_velocity_button(self, button):
self._note_editor.set_velocity_button(button)
def set_prev_scene_button(self, button):
self._track_controller.set_prev_scene_button(button)
def set_next_scene_button(self, button):
self._track_controller.set_next_scene_button(button)
def set_prev_track_button(self, button):
self._track_controller.set_prev_track_button(button)
def set_next_track_button(self, button):
self._track_controller.set_next_track_button(button)
def set_matrix(self,matrix):
self._matrix = matrix
if self._matrix!=None:
for x in range(self._matrix.width()):
for y in range(self._matrix.height()):
self._matrix.get_button(x, y).set_enabled(True)
self._delegate_matrix()
if self._mode==-1:
self.set_mode(STEPSEQ_MODE_NORMAL)
self.set_enabled(self._matrix!=None)
def _delegate_matrix(self):
if self._matrix !=None:
if self._mode == STEPSEQ_MODE_SCALE_EDIT:
self._scale_component.set_matrix(self._matrix)
self._loop_selector.set_buttons(None)
self._note_selector.set_buttons(None)
self._note_editor.set_matrix(None)
elif self._mode == STEPSEQ_MODE_MULTINOTE:
self._height = self._matrix.height()-1
self._width = self._matrix.width()
self._scale_component.set_matrix(None)
self._note_selector.set_buttons(None)
self._loop_selector.set_buttons(
[self._matrix.get_button(0, 7), self._matrix.get_button(1, 7), self._matrix.get_button(2, 7), self._matrix.get_button(3, 7),
self._matrix.get_button(4, 7), self._matrix.get_button(5, 7), self._matrix.get_button(6, 7), self._matrix.get_button(7, 7)]
)
self._note_editor.set_matrix(self._matrix)
elif self._mode == STEPSEQ_MODE_NORMAL:
self._height = self._matrix.height()
self._width = self._matrix.width()
self._scale_component.set_matrix(None)
self._loop_selector.set_buttons(
[self._matrix.get_button(4, 4), self._matrix.get_button(5, 4), self._matrix.get_button(6, 4), self._matrix.get_button(7, 4),
self._matrix.get_button(4, 5), self._matrix.get_button(5, 5), self._matrix.get_button(6, 5), self._matrix.get_button(7, 5),
self._matrix.get_button(4, 6), self._matrix.get_button(5, 6), self._matrix.get_button(6, 6), self._matrix.get_button(7, 6),
self._matrix.get_button(4, 7), self._matrix.get_button(5, 7), self._matrix.get_button(6, 7), self._matrix.get_button(7, 7)]
)
self._note_selector.set_buttons([
self._matrix.get_button(0, 7), self._matrix.get_button(1, 7), self._matrix.get_button(2, 7), self._matrix.get_button(3, 7),
self._matrix.get_button(0, 6), self._matrix.get_button(1, 6), self._matrix.get_button(2, 6), self._matrix.get_button(3, 6),
self._matrix.get_button(0, 5), self._matrix.get_button(1, 5), self._matrix.get_button(2, 5), self._matrix.get_button(3, 5),
self._matrix.get_button(0, 4), self._matrix.get_button(1, 4), self._matrix.get_button(2, 4), self._matrix.get_button(3, 4)]
)
self._note_editor.set_matrix(self._matrix)
else:
self._loop_selector.set_buttons(None)
self._note_selector.set_buttons(None)
self._scale_component.set_matrix(None)
self._note_editor.set_matrix(None)
def disconnect(self):
self._clip = None
self._lock_button = None
self._shift_button = None
self._quantization_button = None
self._top_buttons = None
self._side_buttons = None
self._matrix = None
#self._loop_selector = None
#self._note_editor = None
#self._note_selector = None
#self._scale_component = None
#self._track_controller = None
# SET FUNCTIONS
def _set_lock_function(self):
self._is_locked = False
self._lock_to_track = False
self._last_lock_button_press = time.time()
self._long_press = 0.5
self._lock_button = None
self._selected_track = None
def _set_loop_selector(self):
self._loop_selector = self.register_component(LoopSelectorComponent(self._control_surface, self))
def _set_note_editor(self):
self._note_editor = self.register_component(NoteEditorComponent(control_surface = self._control_surface, stepsequencer = self))
#self._note_editor.set_velocity_button(self._side_buttons[6])
def _set_note_selector(self):
self._note_selector = self.register_component(NoteSelectorComponent(control_surface = self._control_surface, stepsequencer = self))
#self._note_selector.set_up_button(self._side_buttons[4])
#self._note_selector.set_down_button(self._side_buttons[5])
def _set_track_controller(self):
self._track_controller = self.register_component(
TrackControllerComponent(
control_surface = self._control_surface,
implicit_arm = False,
enabled = True,
skin_name = "StepSequencer"
)
)
def _set_scale_component(self):
self._scale_component = self.register_component(
ScaleComponent(
control_surface = self._control_surface,
enabled = False,
mode = "chromatic"
)
)
#self._scale_component.set_enabled(False)
#self._scale_component.set_matrix(self._matrix)
#self._scale_component._mode = "chromatic"
#self._scale_component_button = None
#self.set_scale_button(self._side_buttons[0])
@property
def _is_velocity_shifted(self):
return self._note_editor._is_velocity_shifted
def set_osd(self, osd):
self._osd = osd
self._scale_selector.set_osd(osd)
def _update_OSD(self):
if self._osd != None:
if self._mode == STEPSEQ_MODE_MULTINOTE:
self._osd.set_mode('Drum Step Sequencer (multinote)')
else:
self._osd.set_mode('Drum Step Sequencer')
if self._clip != None:
self._osd.attributes[0] = MUSICAL_MODES[self._scale_selector._modus * 2]
self._osd.attribute_names[0] = "Scale"
self._osd.attributes[1] = KEY_NAMES[self._scale_selector._key % 12]
self._osd.attribute_names[1] = "Root Note"
self._osd.attributes[2] = self._scale_selector._octave
self._osd.attribute_names[2] = "Octave"
self._osd.attributes[3] = QUANTIZATION_NAMES[self._quantization_index]
self._osd.attribute_names[3] = "Quantisation"
self._osd.attributes[4] = " "
self._osd.attribute_names[4] = " "
self._osd.attributes[5] = " "
self._osd.attribute_names[5] = " "
self._osd.attributes[6] = " "
self._osd.attribute_names[6] = " "
self._osd.attributes[7] = " "
self._osd.attribute_names[7] = " "
else:
self._osd.attributes[0] = " "
self._osd.attribute_names[0] = " "
self._osd.attributes[1] = " "
self._osd.attribute_names[1] = " "
self._osd.attributes[2] = " "
self._osd.attribute_names[2] = " "
self._osd.attributes[3] = " "
self._osd.attribute_names[3] = " "
self._osd.attributes[4] = " "
self._osd.attribute_names[4] = " "
self._osd.attributes[5] = " "
self._osd.attribute_names[5] = " "
self._osd.attributes[6] = " "
self._osd.attribute_names[6] = " "
self._osd.attributes[7] = " "
self._osd.attribute_names[7] = " "
if self._selected_track != None:
if self._lock_to_track and self._is_locked:
self._osd.info[0] = "track : " + self._selected_track.name + " (locked)"
else:
self._osd.info[0] = "track : " + self._selected_track.name
else:
self._osd.info[0] = " "
if self._clip != None:
name = self._clip.name
if name == "":
name = "(unamed clip)"
if not self._lock_to_track and self._is_locked:
self._osd.info[1] = "clip : " + name + " (locked)"
else:
self._osd.info[1] = "clip : " + name
else:
self._osd.info[1] = "no clip selected"
self._osd.update()
# enabled
def set_enabled(self, enabled):
#self._control_surface.log_message("stepseq.set_enabled:"+str(enabled))
if enabled:
#restore mode backup when coming back
if self._mode == STEPSEQ_MODE_SCALE_EDIT:
self.set_mode(self._mode_backup, self._number_of_lines_per_note)
# clear note editor cache
self._note_editor._force_update = True
# todo: find a better way to init?
#if self._mode == -1:
# self._mode = STEPSEQ_MODE_NORMAL
#self._detect_scale_mode()
#sync to selected pad
#self._update_drum_group_device()
#if(self._drum_group_device):
# self._note_selector.set_selected_note(self.index_of(self._drum_group_device.drum_pads,self._drum_group_device.view.selected_drum_pad))
#load scale settings from clip
if STEPSEQ__SAVE_SCALE != None and STEPSEQ__SAVE_SCALE == "clip":
self._scale_component.from_object(self._clip)
self._note_selector.set_scale(self._scale_component.notes, self._scale_component._selected_key)
self._note_selector.set_selected_note(self._scale_component._octave_index * 12 + self._scale_component._selected_key)
self._track_controller.set_enabled(enabled)
# update clip notes as they might have changed while we were sleeping
self.on_clip_slot_changed()
# call super.set_enabled()
CompoundComponent.set_enabled(self, enabled)
if self._clip != None and self._is_locked:
self._control_surface.show_message("stepseq : clip '"+str(self._clip.name)+"'")
self._on_notes_changed()
self._update_OSD()
else:
CompoundComponent.set_enabled(self, enabled)
def set_mode(self, mode, number_of_lines_per_note= -1):
if number_of_lines_per_note == -1:
number_of_lines_per_note = self._number_of_lines_per_note
#self._control_surface.log_message("stepseq.set_mode:"+str(mode))
if self._mode != mode or number_of_lines_per_note != self._number_of_lines_per_note:
self._number_of_lines_per_note = number_of_lines_per_note
self._note_editor.set_multinote(mode == STEPSEQ_MODE_MULTINOTE, number_of_lines_per_note)
if mode == STEPSEQ_MODE_NORMAL:
if self._mode != mode:
# self._loop_selector._block = self._loop_selector._block * self._number_of_lines_per_note
self._note_editor.set_page(self._loop_selector._block)
#self.set_left_button(None)
#self.set_right_button(None)
#self._track_controller.set_prev_track_button(self._top_buttons[2])
#self._track_controller.set_next_track_button(self._top_buttons[3])
else:
if self._mode != mode:
# self._loop_selector._block = self._loop_selector._block / self._number_of_lines_per_note