Skip to content

Commit d01221c

Browse files
committed
Improved transposition
1 parent 0924a3a commit d01221c

1 file changed

Lines changed: 37 additions & 24 deletions

File tree

midimech.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ def set_light(self, x, y, col): # col is [1,11], 0 resets
268268
self.launchpad.LedCtrlXY(x, y+1, lp_col[0], lp_col[1], lp_col[2])
269269

270270
def reset_light(self, x, y, reset_red=True):
271-
note = (self.get_note_index(x, y) - self.tonic) % 12
271+
note = self.get_note_index(x, y)
272+
# print(note)
272273
if self.is_split():
273274
split_chan = self.channel_from_split(x, self.board_h - y - 1)
274275
if split_chan:
@@ -297,7 +298,7 @@ def reset_light(self, x, y, reset_red=True):
297298
self.red_lights[y][x] = False
298299

299300
def reset_launchpad_light(self, x, y):
300-
note = self.get_note_index(x, 8-y-1) % 12
301+
note = self.get_note_index(x, 8-y-1)
301302
# if self.is_split():
302303
# split_chan = self.channel_from_split(x, self.board_h - y - 1)
303304
# if split_chan:
@@ -335,11 +336,11 @@ def reset_lights(self):
335336
for x in range(self.board_w):
336337
self.set_light(x, y, 0)
337338

338-
def get_octave(self, x, y):
339-
try:
340-
return self.octaves[y - self.board_h + self.flipped][x] + self.octave
341-
except IndexError:
342-
pass
339+
# def get_octave(self, x, y):
340+
# try:
341+
# return self.octaves[y - self.board_h + self.flipped][x] + self.octave
342+
# except IndexError:
343+
# pass
343344

344345
def xy_to_midi(self, x, y):
345346
row = self.board_h - y
@@ -352,23 +353,24 @@ def xy_to_midi(self, x, y):
352353
r += 7
353354
return r
354355

355-
def get_note_index(self, x, y):
356+
def get_note_index(self, x, y, transpose=True):
356357
y += self.flipped
357358
x += self.transpose
358359
ofs = (self.board_h - y) // 2 + BASE_OFFSET
359360
step = 2 if WHOLETONE else 1
361+
tr = self.tonic if transpose else 0
360362
if y % 2 == 1:
361-
return ((x - ofs) * step) % len(NOTES)
363+
return ((x - ofs) * step - tr) % len(NOTES)
362364
else:
363-
return ((x - ofs) * step + 7) % len(NOTES)
365+
return ((x - ofs) * step + 7 - tr) % len(NOTES)
364366

365-
def get_note(self, x, y):
366-
return NOTES[self.get_note_index(x, y)]
367+
def get_note(self, x, y, transpose=True):
368+
return NOTES[self.get_note_index(x, y, transpose=transpose)]
367369

368370
def get_color(self, x, y):
369371
# return NOTE_COLORS[get_note_index(x, y)]
370372
note = self.get_note_index(x, y)
371-
note = (note - self.tonic) % 12
373+
# note = (note - self.tonic) % 12
372374
if self.is_split():
373375
split_chan = self.channel_from_split(x, self.board_h - y - 1)
374376
if split_chan:
@@ -482,8 +484,14 @@ def mouse_hover(self, x, y):
482484

483485
# Given an x,y position, find the octave
484486
# (used to initialize octaves 2D array)
485-
def init_octave(self, x, y):
486-
return int((x + 4 + self.transpose + y * 2.5) // 6)
487+
def get_octave(self, x, y):
488+
y = self.board_h - y - 1
489+
if self.tonic % 2 == 0:
490+
octave = int(x + 4 + self.transpose + y * 2.5) // 6
491+
else:
492+
y -= 1
493+
octave = int(x + 4 + self.transpose + y * 2.5) // 6
494+
return octave
487495

488496
def init_board(self):
489497
# self.octaves = [
@@ -502,9 +510,12 @@ def init_board(self):
502510
self.octaves = []
503511
for y in range(self.board_h + 1): # 1 = flipping
504512
self.octaves.append([])
513+
line = []
505514
for x in range(self.max_width):
506-
self.octaves[y].append(self.init_octave(x, y))
507-
self.octaves = list(reversed(self.octaves))
515+
octave = self.get_octave(x, y)
516+
self.octaves[y].append(octave)
517+
line.append(octave)
518+
# print(line)
508519

509520
self.notes = [None] * 16 # polyphony
510521
for i, note in enumerate(self.notes):
@@ -1644,8 +1655,8 @@ def clear_marks(self, use_lights=False):
16441655
def mark_xy(self, x, y, state, use_lights=False):
16451656
if self.flipped:
16461657
y -= 1
1658+
# print(x, y)
16471659
idx = self.get_note_index(x, y)
1648-
octave = self.get_octave(x, y)
16491660
try:
16501661
self.board[y + self.flipped][x] = state
16511662
except IndexError:
@@ -1663,20 +1674,22 @@ def mark(self, midinote, state, use_lights=False, only_row=None):
16631674
only_row = self.board_h - only_row - 1 - self.flipped # flip
16641675
try:
16651676
rows = [self.board[only_row]]
1677+
y = only_row
16661678
except IndexError:
16671679
rows = self.board
1668-
y = only_row
1680+
y = 0
16691681
else:
16701682
rows = self.board
16711683
y = 0
16721684
for row in rows:
16731685
x = 0
16741686
for x in range(len(row)):
1675-
idx = self.get_note_index(x, y)
1687+
idx = self.get_note_index(x, y, transpose=False)
16761688
# print(x, y, midinote%12, idx)
16771689
if midinote % 12 == idx:
16781690
octave = self.get_octave(x, y)
16791691
if octave == midinote // 12:
1692+
# print(octave)
16801693
self.board[y + self.flipped][x] = state
16811694
if use_lights:
16821695
if state:
@@ -1710,7 +1723,7 @@ def is_split(self):
17101723

17111724
def set_tonic(self, val):
17121725
odd = (self.tonic % 2 == 1)
1713-
self.tonic = val % 12
1726+
self.tonic = val
17141727
new_odd = (self.tonic % 2 == 1)
17151728
if odd != new_odd:
17161729
self.flipped = not self.flipped
@@ -1837,10 +1850,10 @@ def logic(self, dt):
18371850
else:
18381851
print("You need to add another MIDI loopback device called 'split'")
18391852
elif ev.ui_element == self.btn_prev_root:
1840-
self.set_tonic((self.tonic - 1) % 12)
1853+
self.set_tonic(self.tonic - 1)
18411854
self.dirty = self.dirty_lights = True
18421855
elif ev.ui_element == self.btn_next_root:
1843-
self.set_tonic((self.tonic + 1) % 12)
1856+
self.set_tonic(self.tonic + 1)
18441857
self.dirty = self.dirty_lights = True
18451858
elif ev.ui_element == self.btn_next_scale:
18461859
self.next_scale()
@@ -1963,7 +1976,7 @@ def render(self):
19631976
x = 0
19641977
for cell in row:
19651978
# write text
1966-
note = self.get_note(x, y)
1979+
note = self.get_note(x, y, False)
19671980

19681981
split_chan = self.channel_from_split(x, y)
19691982

0 commit comments

Comments
 (0)