-
Notifications
You must be signed in to change notification settings - Fork 0
/
guitest4.py
250 lines (211 loc) · 8.08 KB
/
guitest4.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
import sys
# Make Kivy window Windows Scaling Aware
from ctypes import windll
windll.user32.SetProcessDpiAwarenessContext(-4)
# PythonOSC Dependencies
from pythonosc.dispatcher import Dispatcher
from pythonosc.osc_server import BlockingOSCUDPServer
from pythonosc.udp_client import SimpleUDPClient
# Color Value Manipulation
from colors import rgb
from colors import hex as _hex
# MIDI I/O
import mido
# Function Timeout
from func_timeout import func_timeout, FunctionTimedOut
# List Manipulation
from typing import List, Any
# Kivy Window Config
from kivy.config import Config
Config.set('graphics','borderless',1)
Config.set('graphics', 'width', 1920)
Config.set('graphics', 'height', 1080)
Config.set('graphics', 'resizable', 0)
Config.write()
# Kivy Dependencies
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.properties import ColorProperty
# Ableton Live 11 Clip Colors
all_clip_colors = {
"0xff94a6": 1,"0xffa529": 2,"0xcc9927": 3,"0xf7f47c": 4,"0xbffb00": 5,"0x1aff2f": 6,"0x25ffa8": 7,
"0x5cffe8": 8,"0x8bc5ff": 9,"0x5480e4":10,"0x92a7ff":11,"0xd86ce4":12,"0xe553a0":13,"0xffffff":14,
"0xff3636":15,"0xf66c03":16,"0x99724b":17,"0xfff034":18,"0x87ff67":19,"0x3dc300":20,"0xbfaf": 21,
"0x19e9ff":22,"0x10a4ee":23,"0x7dc0": 24,"0x886ce4":25,"0xb677c6":26,"0xff39d4":27,"0xd0d0d0":28,
"0xe2675a":29,"0xffa374":30,"0xd3ad71":31,"0xedffae":32,"0xd2e498":33,"0xbad074":34,"0x9bc48d":35,
"0xd4fde1":36,"0xcdf1f8":37,"0xb9c1e3":38,"0xcdbbe4":39,"0xae98e5":40,"0xe5dce1":41,"0xa9a9a9":42,
"0xc6928b":43,"0xb78256":44,"0x99836a":45,"0xbfba69":46,"0xa6be00":47,"0x7db04d":48,"0x88c2ba":49,
"0x9bb3c4":50,"0x85a5c2":51,"0x8393cc":52,"0xa595b5":53,"0xbf9fbe":54,"0xbc7196":55,"0x7b7b7b":56,
"0xaf3333":57,"0xa95131":58,"0x724f41":59,"0xdbc300":60,"0x85961f":61,"0x539f31":62,"0xa9c8e": 63,
"0x236384":64,"0x1a2f96":65,"0x2f52a2":66,"0x624bad":67,"0xa34bad":68,"0xcc2e6e":69,"0x3c3c3c":70}
# Kivy Window Builder
Builder.load_file("guitest4.kv")
class pyMachination(App):
def build(self):
# Setup/Start OSC
osc.setup()
osc.get_version()
osc.get_tempo()
#Get Clip Names and colors
print("Getting clip names and colors...")
for track in range(8):
print("Scanning Track " + str(track+1))
for clip in range (127):
try:
func_timeout(1,osc.getclipname,args=(track,clip))
except FunctionTimedOut:
break
continue
try:
func_timeout(1,osc.getclipcolor,args=(track,clip))
except FunctionTimedOut:
break
continue
self.main_screen = mainWindow()
return self.main_screen
class know_future:
def setup(self):
self.clips = []
self.clip_colors = []
self.current_track = 0
self.current_clip = 0
for j in range(8):
column_clips=[]
column_color=[]
for k in range(128):
column_clips.append("empty")
column_color.append(0x191919)
self.clips.append(column_clips)
self.clip_colors.append(column_color)
self.dispatcher = Dispatcher()
self.dispatcher.map("/live/application/get/version", self.printOutput, "version")
self.dispatcher.map("/live/song/get/tempo", self.printOutput, "tempo")
self.dispatcher.map("/live/clip/get/name", self.printOutput, "clip_name")
self.dispatcher.map("/live/clip/get/color", self.printOutput, "clip_color")
self.dispatcher.map("/live/clip/fire", self.printOutput, "clip_fire")
self.dispatcher.set_default_handler(self.printOutput)
self.start_server()
def printOutput(self, address: str, *args: List[Any]) -> None:
if args[0] == ['version']:
print("Detected Ableton Live Version " + str(args[1]) + "." + str(args[2]))
elif args[0] == ['tempo']:
print("Current tempo: " + str(args[1]))
elif args[0] == ['clip_name']:
print("Track: " + str(self.current_track+1) + " Clip " + str(self.current_clip+1) + " name: " + str(args[1]))
self.clips[self.current_track][self.current_clip]=str(args[1])
elif args[0] == ['clip_color']:
print("Clip color: " + str(hex(args[1])))
self.clip_colors[self.current_track][self.current_clip]=args[1]
elif args[0] == ['clip_fire']:
print(*args)
else:
print(*args)
def getclipname(self,track,clip):
self.current_clip = clip
self.current_track = track
self.client.send_message("/live/clip/get/name",(track,clip))
self.server.handle_request()
def getclipcolor(self,track,clip):
self.current_clip = clip
self.current_track = track
self.client.send_message("/live/clip/get/color",(track,clip))
self.server.handle_request()
def start_server(self):
print("Starting OSC server...")
self.server = BlockingOSCUDPServer(("127.0.0.1", 11001),self.dispatcher)
self.client = SimpleUDPClient("127.0.0.1", 11000)
def get_version(self):
self.client.send_message("/live/application/get/version",0)
self.server.handle_request()
def get_tempo(self):
self.client.send_message("/live/song/get/tempo",0)
self.server.handle_request()
def get_clip_and_color(self, track, clip):
self.getclipname(self,track,clip)
self.getclipcolor(self,track,clip)
def hex_to_rgb(hexcolor):
hexcolor = hexcolor.zfill(6)
rgbcolor = tuple(_hex(hexcolor).rgb)
rgbcolor = tuple(ti/255.0 for ti in rgbcolor)
return rgbcolor
def relay_message(message):
knob = message.channel
value = message.note
print("Knob, type, value: " + str(knob) + " " + str(message.type) + " " + str(value))
if knob < 8:
print("Fire!")
osc.client.send_message("/live/clip/fire",(knob,value))
else:
mainWindow.update_button(mainWindow,knob-7,value)
OutToTeensy = mido.open_output('Machination 1')
InFromTeensy = mido.open_input('Machination 0', callback=relay_message)
switch = {
1: "button1",
2: "button2",
3: "button3",
4: "button4",
5: "button5",
6: "button6",
7: "button7",
8: "button8"
}
class mainWindow(GridLayout):
button1Text = StringProperty('0')
button2Text = StringProperty('0')
button3Text = StringProperty('0')
button4Text = StringProperty('0')
button5Text = StringProperty('0')
button6Text = StringProperty('0')
button7Text = StringProperty('0')
button8Text = StringProperty('0')
button1Text2 = StringProperty('empty')
button2Text2 = StringProperty('empty')
button3Text2 = StringProperty('empty')
button4Text2 = StringProperty('empty')
button5Text2 = StringProperty('empty')
button6Text2 = StringProperty('empty')
button7Text2 = StringProperty('empty')
button8Text2 = StringProperty('empty')
button1Color = ColorProperty([0.1,0.1,0.1,1])
button2Color = ColorProperty([0.1,0.1,0.1,1])
button3Color = ColorProperty([0.1,0.1,0.1,1])
button4Color = ColorProperty([0.1,0.1,0.1,1])
button5Color = ColorProperty([0.1,0.1,0.1,1])
button6Color = ColorProperty([0.1,0.1,0.1,1])
button7Color = ColorProperty([0.1,0.1,0.1,1])
button8Color = ColorProperty([0.1,0.1,0.1,1])
statusLabelText = StringProperty('....')
fontName = StringProperty('data-latin.ttf')
def __init__(self, **kwargs):
super(mainWindow, self).__init__(**kwargs)
def update_button(self,knob,value):
rootObj = App.get_running_app().root
buttonToChange = switch.get(knob)
hexcolor = str(hex(osc.clip_colors[knob-1][value]))
if hexcolor != "0x191919":
rgbcolor = hex_to_rgb(hexcolor.lstrip('0x'))
rgbcolor = rgbcolor+(1,)
text_color = "000000"
else:
rgbcolor = (0.1,0.1,0.1,1)
text_color="ffffff"
setattr(rootObj,buttonToChange+"Color",rgbcolor)
setattr(rootObj,buttonToChange+"Text", "[color=#FFFFFF]" + str(value) + "[/color]")
setattr(rootObj,buttonToChange+"Text2", "[color=#" + text_color + "]" + str(osc.clips[knob-1][value])+"[/color]")
setattr(rootObj,"statusLabelText", "Encoder: " + str(knob) + " Value: " + str(value) + " " + str(osc.clips[knob-1][value]))
if hexcolor in all_clip_colors:
clip_number = all_clip_colors.get(hexcolor)
outMessage = mido.Message('control_change',channel=knob-1,control=1,value=clip_number)
OutToTeensy.send(outMessage)
else:
outMessage = mido.Message('control_change',channel=knob-1,control=1,value=71)
OutToTeensy.send(outMessage)
if __name__ == '__main__':
osc = know_future()
pyMachination().run()