forked from ideoforms/LiveOSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LiveOSCCallbacks.py
1527 lines (1195 loc) · 60.4 KB
/
LiveOSCCallbacks.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
"""
# Copyright (C) 2007 Rob King ([email protected])
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# For questions regarding this module contact
# Rob King <[email protected]> or visit http://www.e-mu.org
This file contains all the current Live OSC callbacks.
"""
import Live
import RemixNet
import OSC
import LiveUtils
import ClipMonitor
import Scan
import sys
import re
import time
import string
import random
from Logger import log
debug = LiveUtils.debug
"""
This class sets the address to call for much of the LiveAPI interactions.
The callbackManager.add() puts them in a dictionary in the CallbackManager class in the OSC.py document
The CallbackManager class adds a prefix for each address so one should not be added here
"""
class LiveOSCCallbacks:
def __init__(self, c_instance, oscEndpoint):
self.oscEndpoint = oscEndpoint
self.callbackManager = oscEndpoint.callbackManager
self.scan = Scan.Scan(self.oscEndpoint)
self.c_instance = c_instance
###################################################################################################################
####################################### SESSION ##############################################
###################################################################################################################
self.callbackManager.add("/session/tempo", self.tempoCB)
self.callbackManager.add("/session/time", self.timeCB)
self.callbackManager.add("/session/quantization", self.quantizationCB)
self.callbackManager.add("/session/selection", self.selectionCB)
self.callbackManager.add("/session/undo", self.undoCB)
self.callbackManager.add("/session/redo", self.redoCB)
self.callbackManager.add("/session/play", self.playCB)
self.callbackManager.add("/session/continue", self.playContinueCB)
self.callbackManager.add("/session/playselection", self.playSelectionCB)
self.callbackManager.add("/session/stop", self.stopCB)
self.callbackManager.add("/session/stopclips", self.stopAllClipsCB)
self.callbackManager.add("/session/metronome", self.metronomeCB)
self.callbackManager.add("/session/status", self.statusPing)
self.callbackManager.add("/session/songs", self.scan.scanSongs)
self.callbackManager.add("/server/ping", self.serverPing)
self.callbackManager.add("/scan/song", self.scan.scanScenes)
self.callbackManager.add("/scan", self.scan.scanSession)
self.callbackManager.add("/multi", self.scan.scanMulti)
self.callbackManager.add("/trimSong", self.scan.trimSong)
###################################################################################################################
####################################### SCENES ##############################################
###################################################################################################################
self.callbackManager.add("/scene/play", self.playSceneCB)
self.callbackManager.add("/scene/play/id", self.playSceneIDCB)
self.callbackManager.add("/scene/count", self.scenecountCB)
self.callbackManager.add("/scene/name", self.nameSceneCB) #only for individual scenes now
self.callbackManager.add("/scene/scan", self.scan.scanScenes)
self.callbackManager.add("/scene", self.sceneCB)
self.callbackManager.add("/sceneblock/name", self.nameSceneBlockCB)
###################################################################################################################
####################################### TRACKS ##############################################
###################################################################################################################
self.callbackManager.add("/track/stop", self.stopTrackCB)
self.callbackManager.add("/track/name", self.nameTrackCB)
self.callbackManager.add("/track/count", self.trackcountCB)
self.callbackManager.add("/trackblock/name", self.nameTrackBlockCB)
self.callbackManager.add("/track/arm", self.armTrackCB)
self.callbackManager.add("/track/mute", self.muteTrackCB)
self.callbackManager.add("/track/solo", self.soloTrackCB)
self.callbackManager.add("/track/volume", self.volumeCB)
self.callbackManager.add("/track/pan", self.panCB)
self.callbackManager.add("/track/send", self.sendCB)
self.callbackManager.add("/track/jump", self.trackJump)
self.callbackManager.add("/track/info", self.trackInfoCB)
###################################################################################################################
####################################### CLIPS ##############################################
###################################################################################################################
self.callbackManager.add("/clip/play", self.playClipCB)
self.callbackManager.add("/clip/stop", self.stopClipCB)
self.callbackManager.add("/clip/name", self.nameClipCB)
self.callbackManager.add("/clip/info", self.clipInfoCB)
self.callbackManager.add("/clipblock/name", self.nameClipBlockCB)
self.callbackManager.add("/clip/pitch", self.pitchCB)
self.callbackManager.add("/clip/loopstate", self.loopStateCB)
self.callbackManager.add("/clip/loopstart", self.loopStartCB)
self.callbackManager.add("/clip/loopend", self.loopEndCB)
self.callbackManager.add("/clip/loopstate_id", self.loopStateCB)
self.callbackManager.add("/clip/loopstart_id", self.loopStartCB)
self.callbackManager.add("/clip/loopend_id", self.loopEndCB)
self.callbackManager.add("/clip/warping", self.warpingCB)
self.callbackManager.add("/clip/signature", self.sigCB)
self.callbackManager.add("/clip/add_note", self.addNoteCB)
self.callbackManager.add("/clip/notes", self.getNotesCB)
self.callbackManager.add("/clip/create", self.createClipCB)
self.callbackManager.add("/clip/delete", self.deleteClipCB)
self.callbackManager.add("/clip/mute", self.muteClipCB)
self.callbackManager.add("/clipslot/play", self.playClipSlotCB)
# self.callbackManager.add("/scene/view", self.viewSceneCB)
# self.callbackManager.add("/track/view", self.viewTrackCB)
# self.callbackManager.add("/return/view", self.viewTrackCB)
# self.callbackManager.add("/master/view", self.mviewTrackCB)
# self.callbackManager.add("/track/device/view", self.viewDeviceCB)
# self.callbackManager.add("/return/device/view", self.viewDeviceCB)
# self.callbackManager.add("/master/device/view", self.mviewDeviceCB)
# self.callbackManager.add("/clip/view", self.viewClipCB)
# self.callbackManager.add("/detail/view", self.detailViewCB)
# self.callbackManager.add("/overdub", self.overdubCB)
# self.callbackManager.add("/state", self.stateCB)
self.callbackManager.add("/return/mute", self.muteTrackCB)
self.callbackManager.add("/return/solo", self.soloTrackCB)
self.callbackManager.add("/return/volume", self.volumeCB)
self.callbackManager.add("/return/pan", self.panCB)
self.callbackManager.add("/return/send", self.sendCB)
self.callbackManager.add("/master/volume", self.volumeCB)
self.callbackManager.add("/master/pan", self.panCB)
# self.callbackManager.add("/devicelist", self.devicelistCB)
# self.callbackManager.add("/return/devicelist", self.devicelistCB)
# self.callbackManager.add("/master/devicelist", self.mdevicelistCB)
# self.callbackManager.add("/device/range", self.devicerangeCB)
# self.callbackManager.add("/return/device/range", self.devicerangeCB)
# self.callbackManager.add("/master/device/range", self.mdevicerangeCB)
# self.callbackManager.add("/device", self.deviceCB)
# self.callbackManager.add("/return/device", self.deviceCB)
# self.callbackManager.add("/master/device", self.mdeviceCB)
# self.callbackManager.add("/master/crossfader", self.crossfaderCB)
# self.callbackManager.add("/track/crossfader", self.trackxfaderCB)
# self.callbackManager.add("/return/crossfader", self.trackxfaderCB)
# self.callbackManager.add("/deactivate", self.deactivateCB)
# self.callbackManager.add("/clear_inactive", self.clearInactiveCB)
# self.callbackManager.add("/filter_clips", self.filterClipsCB)
# self.callbackManager.add("/distribute_groups", self.distributeGroupsCB)
# self.callbackManager.add("/play/group_scene", self.playGroupSceneCB)
self.callbackManager.add("/playMode", self.setPlayModeCB)
self.clip_notes_cache = {}
def serverPing(self, msg, source):
self.oscEndpoint.send("/server/running", 1)
def statusPing(self, msg, source):
tempo = LiveUtils.getTempo()
#time = LiveUtils.getSong().get_current_beats_song_time()
isPlaying = LiveUtils.isPlaying() and 1 or 0
numerator = LiveUtils.getNumerator()
denominator = LiveUtils.getDenominator()
self.oscEndpoint.send("/session/status", (tempo, isPlaying, numerator, denominator))
self.scan.scanSongs()
self.serverPing()
def tempoCB(self, msg, source):
"""Called when a /tempo message is received.
Messages:
/tempo Request current tempo, replies with /tempo (float tempo)
/tempo (float tempo) Set the tempo, replies with /tempo (float tempo)
"""
if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
self.oscEndpoint.send("/session/tempo", LiveUtils.getTempo())
elif len(msg) == 3:
tempo = msg[2]
LiveUtils.setTempo(tempo)
def timeCB(self, msg, source):
"""Called when a /time message is received.
Messages:
/time Request current song time, replies with /time (float time)
/time (float time) Set the time , replies with /time (float time)
"""
if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
self.oscEndpoint.send("/session/time", float(LiveUtils.currentTime()))
elif len(msg) == 3:
time = msg[2]
LiveUtils.currentTime(time)
# self.callbackManager.add("/next/cue", self.nextCueCB)
# self.callbackManager.add("/prev/cue", self.prevCueCB)
###################################################################################################################
####################################### PLAY / STOP ##############################################
###################################################################################################################
def playCB(self, msg, source):
LiveUtils.play()
def playContinueCB(self, msg, source):
LiveUtils.continuePlaying()
def playSelectionCB(self, msg, source):
LiveUtils.playSelection()
def playClipCB(self, msg, source):
if len(msg) == 4:
track = int(msg[2])
clip = int(msg[3])
LiveUtils.launchClip(track, clip)
def playSceneCB(self, msg, source):
if len(msg) == 3:
scene = msg[2]
LiveUtils.launchScene(scene)
def playSceneIDCB(self, msg, source):
if len(msg) == 3:
sceneID = msg[2]
for scene in LiveUtils.getScenes():
if scene.name.find(sceneID) != -1:
#if self.hasString(sceneID, scene.name):
scene.fire()
else:
log("playSceneIDCB couldn't find a matching ID for:")
log(sceneID)
def stopCB(self, msg, source):
LiveUtils.stop()
def stopAllClipsCB(self, msg, source):
"""Called when a /stop/clips message is received.
Messages:
/stop Stops playing the song
"""
LiveUtils.stopClips()
def stopClipCB(self, msg, source):
"""Called when a /stop/clip message is received.
Messages:
/stop/clip (int track, int clip) Stops clip number clip in track number track
"""
if len(msg) == 4:
track = msg[2]
clip = msg[3]
LiveUtils.stopClip(track, clip)
def stopTrackCB(self, msg, source):
"""Called when a /track/stop message is received.
Messages:
/track/stop (int track, int clip) Stops track number track
"""
if len(msg) == 3:
track = msg[2]
LiveUtils.stopTrack(track)
def metronomeCB(self, msg, source):
if len(msg) == 3:
LiveUtils.getSong().metronome = int(msg[2])
###################################################################################################################
####################################### SCENE MANAGEMENT ##############################################
###################################################################################################################
def sceneCB(self, msg, source):
"""Called when a /scene message is received.
Messages:
/scene no argument or 'query' Returns the currently selected scene index
"""
if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
selected_scene = LiveUtils.getSong().view.selected_scene
scenes = LiveUtils.getScenes()
index = 0
selected_index = 0
for scene in scenes:
index = index + 1
if scene == selected_scene:
selected_index = index
self.oscEndpoint.send("/scene", (selected_index))
elif len(msg) == 3:
scene = msg[2]
LiveUtils.getSong().view.selected_scene = LiveUtils.getSong().scenes[scene]
def trackcountCB(self, msg, source):
"""Called when a /trackcount message is received.
Messages:
/trackcount no argument or 'query' Returns the total number of scenes
"""
if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
trackTotal = len(LiveUtils.getTracks())
self.oscEndpoint.send("/tracks", (trackTotal))
return
def scenecountCB(self, msg, source):
"""Called when a /scenecount message is received.
Messages:
/scenecount no argument or 'query' Returns the total number of scenes
"""
#if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
sceneTotal = len(LiveUtils.getScenes())
self.oscEndpoint.send("/scenes", (sceneTotal))
return
def nameSceneCB(self, msg, source):
"""Called when a /scene/name message is received.
/scene/name (int scene) Returns a single scene's name in the form /scene/name (int scene, string name)
/scene/name (int scene, string name)Sets scene number scene's name to name
"""
#Requesting a single scene name
if len(msg) == 3:
sceneNumber = int(msg[2])
self.oscEndpoint.send("/scene/name", (sceneNumber, str(LiveUtils.getScene(sceneNumber).name)))
return
#renaming a scene
if len(msg) == 4:
sceneNumber = int(msg[2])
name = msg[3]
LiveUtils.getScene(sceneNumber).name = name
self.oscEndpoint.send("/scene/name", (sceneNumber, str(LiveUtils.getScene(sceneNumber).name)))
def nameSceneBlockCB(self, msg, source):
"""Called when a /name/sceneblock message is received.
/name/clipblock (int offset, int blocksize) Returns a list of blocksize scene names starting at offset
"""
if len(msg) == 4:
block = []
sceneOffset = int(msg[2])
blocksize = int(msg[3])
for scene in range(0, blocksize):
block.extend([str(LiveUtils.getScene(sceneOffset+scene).name)])
self.oscEndpoint.send("/sceneblock/name", block)
def setPlayModeCB(self, msg, source):
ClipMonitor.playMode = str(msg[2])
self.oscEndpoint.send("/playMode", msg[2])
if debug:
log("setPlayModeCB called by OSC with message: " + str(msg[2]))
def sigCB(self, msg, source):
""" Called when a /clip/signature message is recieved
"""
track = msg[2]
clip = msg[3]
c = LiveUtils.getSong().tracks[track].clip_slots[clip].clip
if len(msg) == 4:
self.oscEndpoint.send("/clip/signature", (track, clip, c.signature_numerator, c.signature_denominator))
if len(msg) == 6:
self.oscEndpoint.send("/clip/signature", 1)
c.signature_denominator = msg[5]
c.signature_numerator = msg[4]
def warpingCB(self, msg, source):
""" Called when a /clip/warping message is recieved
"""
track = msg[2]
clip = msg[3]
if len(msg) == 4:
state = LiveUtils.getSong().tracks[track].clip_slots[clip].clip.warping
self.oscEndpoint.send("/clip/warping", (track, clip, int(state)))
elif len(msg) == 5:
LiveUtils.getSong().tracks[track].clip_slots[clip].clip.warping = msg[4]
def selectionCB(self, msg, source):
""" Called when a /selection message is received
"""
if len(msg) == 6:
self.c_instance.set_session_highlight(msg[2], msg[3], msg[4], msg[5], 0)
def trackxfaderCB(self, msg, source):
""" Called when a /track/crossfader or /return/crossfader message is received
"""
ty = msg[0] == '/return/crossfader' and 1 or 0
if len(msg) == 3:
track = msg[2]
if ty == 1:
assign = LiveUtils.getSong().return_tracks[track].mixer_device.crossfade_assign
name = LiveUtils.getSong().return_tracks[track].mixer_device.crossfade_assignments.values[assign]
self.oscEndpoint.send("/return/crossfader", (track, str(assign), str(name)))
else:
assign = LiveUtils.getSong().tracks[track].mixer_device.crossfade_assign
name = LiveUtils.getSong().tracks[track].mixer_device.crossfade_assignments.values[assign]
self.oscEndpoint.send("/track/crossfader", (track, str(assign), str(name)))
elif len(msg) == 4:
track = msg[2]
assign = msg[3]
if ty == 1:
LiveUtils.getSong().return_tracks[track].mixer_device.crossfade_assign = assign
else:
LiveUtils.getSong().tracks[track].mixer_device.crossfade_assign = assign
# def nextCueCB(self, msg, source):
# """Called when a /next/cue message is received.
# Messages:
# /next/cue Jumps to the next cue point
# """
# LiveUtils.jumpToNextCue()
# def prevCueCB(self, msg, source):
# """Called when a /prev/cue message is received.
# Messages:
# /prev/cue Jumps to the previous cue point
# """
# LiveUtils.jumpToPrevCue()
def nameTrackCB(self, msg, source):
"""Called when a /name/track message is received.
Messages:
/track/name (int track) Returns a single track's name in the form /track/name (int track, string name)
/track/name (int track, string name)Sets track number track's name to name
"""
#Requesting a single track name
if len(msg) == 3:
trackNumber = msg[2]
self.oscEndpoint.send("/track/name", (trackNumber, str(LiveUtils.getTrack(trackNumber).name)))
return
#renaming a track
if len(msg) == 4:
trackNumber = msg[2]
name = msg[3]
LiveUtils.getTrack(trackNumber).name = name
def nameTrackBlockCB(self, msg, source):
"""Called when a /trackblock/name message is received.
/trackblock/name (int offset, int blocksize) Returns a list of blocksize track names starting at offset
"""
if len(msg) == 4:
block = []
trackOffset = int(msg[2])
blocksize = int(msg[3])
for track in range(0, blocksize):
block.extend([str(LiveUtils.getTrack(trackOffset+track).name)])
self.oscEndpoint.send("/trackblock/name", block)
def nameClipBlockCB(self, msg, source):
"""Called when a /name/clipblock message is received.
/clipblock/name (int track, int clip, blocksize x/tracks, blocksize y/clipslots) Returns a list of clip names for a block of clips (int blockX, int blockY, clipname)
"""
#Requesting a block of clip names X1 Y1 X2 Y2 where X1,Y1 is the first clip (track, clip) of the block, X2 the number of tracks to cover and Y2 the number of scenes
if len(msg) == 6:
block = []
trackOffset = int(msg[2])
clipOffset = int(msg[3])
blocksizeX = int(msg[4])
blocksizeY = int(msg[5])
for clip in range(0, blocksizeY):
for track in range(0, blocksizeX):
trackNumber = trackOffset+track
clipNumber = clipOffset+clip
if LiveUtils.getClip(trackNumber, clipNumber) != None:
block.extend([str(LiveUtils.getClip(trackNumber, clipNumber).name)])
else:
block.extend([""])
self.oscEndpoint.send("/clipbock/name", block)
def nameClipCB(self, msg, source):
"""Called when a /clip/name message is received.
Messages:
/clip/name Returns a a series of all the clip names in the form /clip/name (int track, int clip, string name)
/clip/name (int track, int clip) Returns a single clip's name in the form /clip/name (int clip, string name)
/clip/name (int track, int clip, string name)Sets clip number clip in track number track's name to name
"""
#Requesting all clip names
if len(msg) == 2 or (len(msg) == 3 and msg[2] == "query"):
trackNumber = 0
clipNumber = 0
for track in LiveUtils.getTracks():
bundle = OSC.OSCBundle()
for clipSlot in track.clip_slots:
if clipSlot.clip != None:
bundle.append("/clip/name", (trackNumber, clipNumber, str(clipSlot.clip.name), clipSlot.clip.color))
clipNumber = clipNumber + 1
self.oscEndpoint.sendMessage(bundle)
clipNumber = 0
trackNumber = trackNumber + 1
return
#Requesting a single clip name
if len(msg) == 4:
trackNumber = msg[2]
clipNumber = msg[3]
self.oscEndpoint.send("/clip/name", (trackNumber, clipNumber, str(LiveUtils.getClip(trackNumber, clipNumber).name), LiveUtils.getClip(trackNumber, clipNumber).color))
return
#renaming a clip
if len(msg) >= 5:
trackNumber = msg[2]
clipNumber = msg[3]
name = msg[4]
LiveUtils.getClip(trackNumber, clipNumber).name = name
if len(msg) >= 6:
trackNumber = msg[2]
clipNumber = msg[3]
color = msg[5]
LiveUtils.getClip(trackNumber, clipNumber).color = color
def addNoteCB(self, msg, source):
"""Called when a /clip/add_note message is received
Messages:
/clip/add_note (int pitch) (double time) (double duration) (int velocity) (bool muted) Add the given note to the clip
"""
trackNumber = msg[2]
clipNumber = msg[3]
pitch = int(msg[4])
time = msg[5]
duration = msg[6]
velocity = int(msg[7])
muted = msg[8]
LiveUtils.getClip(trackNumber, clipNumber).deselect_all_notes()
notes = ((pitch, time, duration, velocity, muted),)
LiveUtils.getClip(trackNumber, clipNumber).replace_selected_notes(notes)
self.oscEndpoint.send('/clip/note', (trackNumber, clipNumber, pitch, time, duration, velocity, muted))
def getNotesCB(self, msg, source):
"""Called when a /clip/notes message is received
Messages:
/clip/notes Return all notes in the clip in /clip/note messages. Each note is sent in the format
(int trackNumber) (int clipNumber) (int pitch) (double time) (double duration) (int velocity) (int muted)
"""
trackNumber = msg[2]
clipNumber = msg[3]
LiveUtils.getClip(trackNumber, clipNumber).select_all_notes()
bundle = OSC.OSCBundle()
for note in LiveUtils.getClip(trackNumber, clipNumber).get_selected_notes():
pitch = note[0]
time = note[1]
duration = note[2]
velocity = note[3]
muted = 0
if note[4]:
muted = 1
bundle.append('/clip/note', (trackNumber, clipNumber, pitch, time, duration, velocity, muted))
self.oscEndpoint.sendMessage(bundle)
def createClipCB(self, msg, source):
"""
Messages:
/clip/create (int track, int slot, float length)
Create a clip in [track, slot], length [length] beats
"""
clipSlots = LiveUtils.getClipSlots()
track_index = msg[2]
clipslot_index = msg[3]
length = msg[4]
track = LiveUtils.getTrack(track_index)
clipslot = track.clip_slots[clipslot_index]
clipslot.create_clip(length)
def deleteClipCB(self, msg, source):
"""
Messages:
/clip/delete (int track, int slot)
Delete clip at [track, slot]
"""
clipSlots = LiveUtils.getClipSlots()
track_index = msg[2]
clipslot_index = msg[3]
track = LiveUtils.getTrack(track_index)
clipslot = track.clip_slots[clipslot_index]
clipslot.delete_clip()
def armTrackCB(self, msg, source):
"""Called when a track/arm message is received.
Messages:
/arm (int track) (int armed/disarmed) Arms track number track
"""
track = msg[2]
if len(msg) == 4:
if msg[3] == 1:
LiveUtils.armTrack(track)
else:
LiveUtils.disarmTrack(track)
# Return arm status
elif len(msg) == 3:
status = LiveUtils.getTrack(track).arm
self.oscEndpoint.send("track/arm", (track, int(status)))
def muteTrackCB(self, msg, source):
"""Called when a track/mute or return/mute message is received.
Messages:
/mute (int track) Mutes track number track
"""
ty = msg[0] == '/return/mute' and 1 or 0
track = msg[2]
if len(msg) == 4:
if msg[3] == 1:
LiveUtils.muteTrack(track, ty)
else:
LiveUtils.unmuteTrack(track, ty)
elif len(msg) == 3:
if ty == 1:
status = LiveUtils.getSong().return_tracks[track].mute
self.oscEndpoint.send("/return/mute", (track, int(status)))
else:
status = LiveUtils.getTrack(track).mute
self.oscEndpoint.send("track/mute", (track, int(status)))
def deactivateCB(self, msg, source):
for track in LiveUtils.getTracks():
for slot in track.clip_slots:
if slot.clip != None:
slot.clip.muted = True
def clearInactiveCB(self, msg, source):
log("clearing inactive")
for track in LiveUtils.getTracks():
for slot in track.clip_slots:
if slot.clip != None and slot.clip.muted:
slot.delete_clip()
def nameToMIDI(self, name):
""" Maps a MIDI note name (D3, C#6) to a value.
Assumes that middle C is C4. """
note_names = [
[ "C" ],
[ "C#", "Db" ],
[ "D" ],
[ "D#", "Eb" ],
[ "E" ],
[ "F" ],
[ "F#", "Gb" ],
[ "G" ],
[ "G#", "Ab" ],
[ "A" ],
[ "A#", "Bb" ],
[ "B" ]
]
if name[-1].isdigit():
octave = int(name[-1])
name = name[:-1]
else:
octave = 0
try:
index = note_names.index([nameset for nameset in note_names if name in nameset][0])
return octave * 12 + index
except:
return None
def filterClipsCB(self, msg, source):
def bipolar_diverge(maximum):
""" returns [0, 1, -1, ...., maximum, -maximum ] """
sequence = list(sum(list(zip(list(range(maximum + 1)), list(range(0, -maximum - 1, -1)))), ()))
sequence.pop(0)
return sequence
def filter_tone_row(source, target, bend_limit = 7):
""" filters the notes in <source> by the permitted notes in <target>.
returns a tuple (<bool> acceptable, <int> pitch_bend) """
bends = bipolar_diverge(bend_limit)
for bend in bends:
if all(((note + bend) % 12) in target for note in source):
return (True, bend)
return (False, 0)
if not self.clip_notes_cache:
log("no cache found, creating")
self._filterClipsMakeCache()
try:
t0 = time.time()
note_names = msg[2:]
log("filtering clips according to %s" % note_names)
target = tuple(self.nameToMIDI(note) for note in note_names)
clip_states = {}
if target in self.clip_tone_row_cache:
log(" - found cached")
clip_states = self.clip_tone_row_cache[target]
else:
log(" - not found in cache, calculating")
for track_index, track in enumerate(LiveUtils.getTracks()):
for clip_index, slot in enumerate(track.clip_slots):
if slot.clip != None:
notes = self.clip_notes_cache[track_index][clip_index]
# log(" - found notes: %s" % notes)
if notes:
# don't transpose for now
acceptable, bend = filter_tone_row(notes, target, 0)
clip_states[slot.clip] = acceptable
self.clip_tone_row_cache[target] = clip_states
for clip, acceptable in clip_states.items():
clip.muted = not acceptable
except Exception, e:
log("filtering failed (%s); rebuilding cache" % e)
self._filterClipsMakeCache()
log("filtered clips (took %.3fs)" % (time.time() - t0))
def _filterClipsMakeCache(self, msg = None, source = None):
self.clip_notes_cache = {}
self.clip_tone_row_cache = {}
log("making clip filter cache")
t0 = time.time()
try:
for track_index, track in enumerate(LiveUtils.getTracks()):
self.clip_notes_cache[track_index] = {}
for clip_index, slot in enumerate(track.clip_slots):
if slot.clip is not None:
# match = re.search("(^|[_-])([A-G][A-G#b1-9-]*)$", slot.clip.name)
match = re.search("([_-])([A-G][A-G#b1-9-]*)$", slot.clip.name)
if match:
notes_found_str = match.group(2)
notes_found_str = re.sub("[1-9]", "", notes_found_str)
notes_found = notes_found_str.split("-")
notes = [ self.nameToMIDI(note) for note in notes_found ]
# in case we have spurious/nonsense notes, filter them out
notes = filter(lambda n: n is not None, notes)
# log(" - %s: found notes: %s" % (slot.clip.name, notes))
self.clip_notes_cache[track_index][clip_index] = notes
else:
self.clip_notes_cache[track_index][clip_index] = None
slot.clip.muted = False
log("filtered clips (took %.3fs)" % (time.time() - t0))
except Exception, e:
log("Exception: %s" % e)
def soloTrackCB(self, msg, source):
"""Called when a /solo message is received.
Messages:
/solo (int track) Solos track number track
"""
ty = msg[0] == '/return/solo' and 1 or 0
track = msg[2]
if len(msg) == 4:
if msg[3] == 1:
LiveUtils.soloTrack(track, ty)
else:
LiveUtils.unsoloTrack(track, ty)
elif len(msg) == 3:
if ty == 1:
status = LiveUtils.getSong().return_tracks[track].solo
self.oscEndpoint.send("/return/solo", (track, int(status)))
else:
status = LiveUtils.getTrack(track).solo
self.oscEndpoint.send("/solo", (track, int(status)))
def volumeCB(self, msg, source):
"""Called when a /volume message is received.
Messages:
/volume (int track) Returns the current volume of track number track as: /volume (int track, float volume(0.0 to 1.0))
/volume (int track, float volume(0.0 to 1.0)) Sets track number track's volume to volume
"""
if msg[0] == '/return/volume':
ty = 1
elif msg[0] == '/master/volume':
ty = 2
else:
ty = 0
if len(msg) == 2 and ty == 2:
self.oscEndpoint.send("/master/volume", LiveUtils.getSong().master_track.mixer_device.volume.value)
elif len(msg) == 3 and ty == 2:
volume = float(msg[2])
LiveUtils.getSong().master_track.mixer_device.volume.value = volume
elif len(msg) == 4:
track = int(msg[2])
volume = float(msg[3])
if ty == 0:
LiveUtils.trackVolume(track, volume)
elif ty == 1:
LiveUtils.getSong().return_tracks[track].mixer_device.volume.value = volume
elif len(msg) == 3:
track = int(msg[2])
if ty == 1:
self.oscEndpoint.send("/return/volume", (track, LiveUtils.getSong().return_tracks[track].mixer_device.volume.value))
else:
self.oscEndpoint.send("/volume", (track, LiveUtils.trackVolume(track)))
def panCB(self, msg, source):
"""Called when a /pan message is received.
Messages:
/pan (int track) Returns the pan of track number track as: /pan (int track, float pan(-1.0 to 1.0))
/pan (int track, float pan(-1.0 to 1.0)) Sets track number track's pan to pan
"""
if msg[0] == '/return/pan':
ty = 1
elif msg[0] == '/master/pan':
ty = 2
else:
ty = 0
if len(msg) == 2 and ty == 2:
self.oscEndpoint.send("/master/pan", LiveUtils.getSong().master_track.mixer_device.panning.value)
elif len(msg) == 3 and ty == 2:
pan = msg[2]
LiveUtils.getSong().master_track.mixer_device.panning.value = pan
elif len(msg) == 4:
track = msg[2]
pan = msg[3]
if ty == 0:
LiveUtils.trackPan(track, pan)
elif ty == 1:
LiveUtils.getSong().return_tracks[track].mixer_device.panning.value = pan
elif len(msg) == 3:
track = msg[2]
if ty == 1:
self.oscEndpoint.send("/pan", (track, LiveUtils.getSong().return_tracks[track].mixer_device.panning.value))
else:
self.oscEndpoint.send("/pan", (track, LiveUtils.trackPan(track)))
def sendCB(self, msg, source):
"""Called when a /send message is received.
Messages:
/send (int track, int send) Returns the send level of send (send) on track number track as: /send (int track, int send, float level(0.0 to 1.0))
/send (int track, int send, float level(0.0 to 1.0)) Sets the send (send) of track number (track)'s level to (level)
"""
ty = msg[0] == '/return/send' and 1 or 0
track = msg[2]
if len(msg) == 5:
send = msg[3]
level = msg[4]
if ty == 1:
LiveUtils.getSong().return_tracks[track].mixer_device.sends[send].value = level
else:
LiveUtils.trackSend(track, send, level)
elif len(msg) == 4:
send = msg[3]
if ty == 1:
self.oscEndpoint.send("/return/send", (track, send, float(LiveUtils.getSong().return_tracks[track].mixer_device.sends[send].value)))
else:
self.oscEndpoint.send("/send", (track, send, float(LiveUtils.trackSend(track, send))))
elif len(msg) == 3:
if ty == 1:
sends = LiveUtils.getSong().return_tracks[track].mixer_device.sends
else:
sends = LiveUtils.getSong().tracks[track].mixer_device.sends
so = [track]
for i in range(len(sends)):
so.append(i)
so.append(float(sends[i].value))
if ty == 1:
self.oscEndpoint.send("/return/send", tuple(so))
else:
self.oscEndpoint.send("/send", tuple(so))
def pitchCB(self, msg, source):
"""Called when a /clip/pitch message is received.
Messages:
/pitch (int track, int clip) Returns the pitch of clip as: /clip/pitch (int track, int clip, int coarse(-48 to 48), int fine (-50 to 50))
/pitch (int track, int clip, int coarse(-48 to 48), int fine (-50 to 50)) Sets clip number clip in track number track's pitch to coarse / fine
TODO: Unlike other callbacks, this does not include the track/pitch indices.
"""
if len(msg) == 6:
track = int(msg[2])
clip = int(msg[3])
coarse = msg[4]
fine = msg[5]
LiveUtils.clipPitch(track, clip, coarse, fine)
if len(msg) ==4:
track = int(msg[2])
clip = int(msg[3])
self.oscEndpoint.send("/pitch", LiveUtils.clipPitch(track, clip))