Skip to content

Commit 4693f85

Browse files
committed
Change init string as discussed in #22
1 parent ddb4d64 commit 4693f85

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

SynthesiaKontrol.py

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
# The MIT License
2-
#
2+
#
33
# Copyright (c) 2018-2021 Olivier Jacques
4-
#
4+
#
55
# Synthesia Kontrol: an app to light the keys of Native Instruments
66
# Komplete Kontrol MK2 keyboard, driven by Synthesia
77

8+
import time
89
import hid
910
import mido
10-
import time
11-
import sys
1211

1312
NATIVE_INSTRUMENTS = 0x17cc
1413
INSTR_ADDR = 0x1620
1514
NB_KEYS = 61
1615
MODE = "MK2"
1716

17+
1818
def init():
1919
"""Connect to the keyboard, switch all lights off"""
2020
global bufferC # Buffer with the full key/lights mapping
2121
global device
2222

2323
print("Opening Keyboard device...")
24-
device=hid.device()
24+
device = hid.device()
2525
try:
2626
device.open(NATIVE_INSTRUMENTS, INSTR_ADDR)
2727
except Exception as e:
2828
print("Error: " + str(e))
2929
quit()
3030

31-
device.write([0xa0])
32-
31+
# Write 3 bytes to the device: 0xa0, 0x00, 0x00
32+
# Thanks to @xample for investigating this
33+
device.write([0xa0, 0x00, 0x00])
34+
3335
bufferC = [0x00] * 249
3436
notes_off()
3537

3638
return True
3739

40+
3841
def notes_off():
3942
"""Turn off lights for all notes"""
4043

@@ -46,26 +49,28 @@ def notes_off():
4649
elif (MODE == "MK1"):
4750
device.write([0x82] + bufferC)
4851
else:
49-
print ("Error: unsupported mode - should be MK1 or MK2")
52+
print("Error: unsupported mode - should be MK1 or MK2")
5053
quit()
5154

55+
5256
def accept_notes(port):
5357
"""Only let note_on and note_off messages through."""
5458
for message in port:
5559
if message.type in ('note_on', 'note_off'):
5660
yield message
5761
if message.type == 'control_change' and message.channel == 0 and message.control == 16:
5862
if (message.value & 4):
59-
print ("User is playing")
63+
print("User is playing")
6064
if (message.value & 1):
61-
print ("Playing Right Hand")
65+
print("Playing Right Hand")
6266
if (message.value & 2):
63-
print ("Playing Left Hand")
67+
print("Playing Left Hand")
6468
notes_off()
6569

70+
6671
def CoolDemoSweep(loopcount):
6772
speed = 0.01
68-
for loop in range(0,loopcount):
73+
for loop in range(0, loopcount):
6974
# Forward
7075
for x in range(0, NB_KEYS-3):
7176
if (MODE == "MK2"):
@@ -97,30 +102,31 @@ def CoolDemoSweep(loopcount):
97102
device.write(bufferC)
98103
time.sleep(speed)
99104
notes_off()
100-
105+
106+
101107
def LightNote(note, status, channel, velocity):
102108
"""Light a note ON or OFF"""
103109

104-
#bufferC[0] = 0x81 # For Komplete Kontrol MK2
110+
# bufferC[0] = 0x81 # For Komplete Kontrol MK2
105111
offset = OFFSET
106112
key = (note + offset)
107113

108114
if key < 0 or key >= NB_KEYS:
109-
return
115+
return
110116

111117
# Determine color
112118
if (MODE == "MK2"):
113-
left = 0x2d # Blue
114-
left_thumb = 0x2f # Lighter Blue
115-
right = 0x1d # Green
119+
left = 0x2d # Blue
120+
left_thumb = 0x2f # Lighter Blue
121+
right = 0x1d # Green
116122
right_thumb = 0x1f # Lighter Green
117123
elif (MODE == "MK1"):
118-
left = [0x00] + [0x00] + [0xFF] # Blue
119-
left_thumb = [0x00] + [0x00] + [0x80] # Lighter Blue
120-
right = [0x00] + [0xFF] + [0x00] # Green
124+
left = [0x00] + [0x00] + [0xFF] # Blue
125+
left_thumb = [0x00] + [0x00] + [0x80] # Lighter Blue
126+
right = [0x00] + [0xFF] + [0x00] # Green
121127
right_thumb = [0x00] + [0x80] + [0x00] # Lighter Green
122128
else:
123-
print ("Error: unsupported mode - should be MK1 or MK2")
129+
print("Error: unsupported mode - should be MK1 or MK2")
124130
quit()
125131

126132
default = right
@@ -167,66 +173,68 @@ def LightNote(note, status, channel, velocity):
167173
else:
168174
device.write([0x82] + bufferC)
169175

176+
170177
if __name__ == '__main__':
171178
"""Main: connect to keyboard, open midi input port, listen to midi"""
172-
print ("Select your keyboard (1, 2, 3, ...) and press 'Enter':")
173-
print (" 1-Komplete Kontrol S61 MK2")
174-
print (" 2-Komplete Kontrol S88 MK2")
175-
print (" 3-Komplete Kontrol S49 MK2")
176-
print (" 4-Komplete Kontrol S61 MK1")
177-
print (" 5-Komplete Kontrol S88 MK1")
178-
print (" 6-Komplete Kontrol S49 MK1")
179-
print (" 7-Komplete Kontrol S25 MK1")
179+
print("Select your keyboard (1, 2, 3, ...) and press 'Enter':")
180+
print(" 1-Komplete Kontrol S61 MK2")
181+
print(" 2-Komplete Kontrol S88 MK2")
182+
print(" 3-Komplete Kontrol S49 MK2")
183+
print(" 4-Komplete Kontrol S61 MK1")
184+
print(" 5-Komplete Kontrol S88 MK1")
185+
print(" 6-Komplete Kontrol S49 MK1")
186+
print(" 7-Komplete Kontrol S25 MK1")
180187
keyboard = input()
181-
188+
182189
# Customize here for new keyboards
183190
# Pull requests welcome!
184191
if keyboard == "1":
185192
MODE = "MK2"
186-
INSTR_ADDR = 0x1620 # KK S61 MK2
193+
INSTR_ADDR = 0x1620 # KK S61 MK2
187194
NB_KEYS = 61
188195
OFFSET = -36
189196
elif keyboard == "2":
190197
MODE = "MK2"
191-
INSTR_ADDR = 0x1630 # KK S88 MK2
198+
INSTR_ADDR = 0x1630 # KK S88 MK2
192199
NB_KEYS = 88
193200
OFFSET = -21
194201
elif keyboard == "3":
195202
MODE = "MK2"
196-
INSTR_ADDR = 0x1610 # KK S49 MK2
203+
INSTR_ADDR = 0x1610 # KK S49 MK2
197204
NB_KEYS = 49
198205
OFFSET = -36
199206
elif keyboard == "4":
200207
MODE = "MK1"
201-
INSTR_ADDR = 0x1360 # KK S61 MK1
208+
INSTR_ADDR = 0x1360 # KK S61 MK1
202209
NB_KEYS = 61
203210
OFFSET = -36
204211
elif keyboard == "5":
205212
MODE = "MK1"
206-
INSTR_ADDR = 0x1410 # KK S88 MK1
213+
INSTR_ADDR = 0x1410 # KK S88 MK1
207214
NB_KEYS = 88
208215
OFFSET = -21
209216
elif keyboard == "6":
210217
MODE = "MK1"
211-
INSTR_ADDR = 0x1350 # KK S49 MK1
218+
INSTR_ADDR = 0x1350 # KK S49 MK1
212219
NB_KEYS = 49
213220
OFFSET = -36
214221
elif keyboard == "7":
215222
MODE = "MK1"
216-
INSTR_ADDR = 0x1340 # KK S25 MK1
223+
INSTR_ADDR = 0x1340 # KK S25 MK1
217224
NB_KEYS = 25
218225
OFFSET = -21
219226
else:
220-
print ("Got '" + keyboard + "' - please type a number which corresponds to your keyboard, then Enter")
227+
print("Got '" + keyboard +
228+
"' - please type a number which corresponds to your keyboard, then Enter")
221229
quit()
222-
223-
print ("Connecting to Komplete Kontrol Keyboard")
230+
231+
print("Connecting to Komplete Kontrol Keyboard")
224232
connected = init()
225233
portName = ""
226234
if connected:
227235
print("Connected to Komplete Kontrol!")
228236
CoolDemoSweep(2) # Happy dance
229-
print ("Opening LoopBe input port")
237+
print("Opening LoopBe input port")
230238
ports = mido.get_input_names()
231239
for port in ports:
232240
print(" Found MIDI port " + port + "...")
@@ -236,8 +244,9 @@ def LightNote(note, status, channel, velocity):
236244
print("Error: can't find 'LoopBe' midi port. Please install LoopBe1 from http://www.nerds.de/en/download.html (Windows) or name your IAC midi device 'LoopBe' (on Mac).")
237245
exit(1)
238246

239-
print ("Listening to Midi on LoopBe midi port")
247+
print("Listening to Midi on LoopBe midi port")
240248
with mido.open_input(portName) as midiPort:
241249
for message in accept_notes(midiPort):
242250
print('Received {}'.format(message))
243-
LightNote(message.note, message.type, message.channel, message.velocity)
251+
LightNote(message.note, message.type,
252+
message.channel, message.velocity)

0 commit comments

Comments
 (0)