Skip to content

Commit

Permalink
keyboard stuff
Browse files Browse the repository at this point in the history
-hooked up triad highlighting to keyboard
-hooked up color coded octaves to keyboard
  • Loading branch information
0XDE57 committed Jan 31, 2018
1 parent caac862 commit 17ad835
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
20 changes: 2 additions & 18 deletions fretboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import math

import music

import util

class Fretboard:
def __init__(self, window, w, h):
Expand Down Expand Up @@ -37,7 +37,7 @@ def draw_string(self, open_note, octave, triad, x, y):
fret_note = music.notes[(note_string + fret) % len(music.notes)]
fret_frequency = str(round(self.note_map[fret_note + str(fret_oct)]))

color = self.get_color_for_octave(fret_oct)
color = util.get_color_for_octave(fret_oct)
# canvas.create_rectangle(pos_x - radius, pos_y - radius, pos_x + radius, pos_y + radius, fill=color)

# highlight triads
Expand All @@ -56,22 +56,6 @@ def draw_string(self, open_note, octave, triad, x, y):
if self.show_freq:
self.canvas.create_text(pos_x, pos_y+7, text=fret_frequency, font=("Purisa", 9), fill='white')

def get_color_for_octave(self, octave):
# colorsys.rgb_to_hls(1, 0, 0)
if octave == 1:
return '#9400D3'
elif octave == 2:
return '#4B0082'
elif octave == 3:
return '#0000FF'
elif octave == 4:
return '#00FF00'
elif octave == 5:
return '#FFFF00'
elif octave == 6:
return '#FF0000'
else:
return 'black'

# draws background and frets
def draw_fret_backing(self, x):
Expand Down
36 changes: 28 additions & 8 deletions keyboard.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import tkinter

import music
import util


class Keyboard:
def __init__(self, window, w, h):
self.canvas = tkinter.Canvas(window, width=w, height=h, bd=2, relief=tkinter.SOLID)
self.triad = None

def draw(self):
print('rendering piano')
starting_octave = 1
# per octave
white_keys = 7
white_key_notes = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
Expand All @@ -17,35 +20,52 @@ def draw(self):
keyspan_octave = ['w', 'b', 'w', 'b', 'w', 'w', 'b', 'w', 'b', 'w', 'b', 'w']
black_keys = [1, 2, 4, 5, 6]

num_octaves = 4
num_octaves = 6
num_keys = white_keys * num_octaves
key_width = (int(self.canvas["width"]) + 4) / num_keys
for key in range(white_keys * num_octaves):
pos_x = (key * key_width)
note = white_key_notes[key % len(white_key_notes)]
current_octave = starting_octave + int(key / 7)
color_note = util.get_color_for_octave(current_octave)

color_key = 'white'
if self.triad is not None:
if note in self.triad:
color_key = color_note
color_note = 'black'

self.canvas.create_rectangle(pos_x,
0,
pos_x + key_width,
int(self.canvas["height"]) + 4,
fill='white', activefill='#999999')
fill=color_key, activefill=color_note)

note = white_key_notes[key % len(white_key_notes)]
self.canvas.create_text(pos_x+key_width/2, int(self.canvas["height"])-10, text=note, fill='#333333')
self.canvas.create_text(pos_x+key_width/2, int(self.canvas["height"])-10, text=note, fill=color_note)

black_width = key_width * 0.7
height = int(self.canvas["height"])*0.7
count = 0
for key in range(len(music.notes) * num_octaves):
if key % white_keys in black_keys:
pos_x = (key * key_width)
note = black_key_notes[count % len(black_key_notes)]
count += 1
current_octave = starting_octave + int(key / 7)
color_note = util.get_color_for_octave(current_octave)
color_key = 'black'
if self.triad is not None:
if note in self.triad:
color_key = color_note
color_note = 'black'

self.canvas.create_rectangle(pos_x - black_width/2,
0,
pos_x + black_width/2,
height,
fill='black', activefill='#999999')
fill=color_key, activefill=color_note)

note = black_key_notes[count % len(black_key_notes)]
count += 1
self.canvas.create_text(pos_x, 15, text=note, fill='white')
self.canvas.create_text(pos_x, 15, text=note, font=("Purisa", 9), fill=color_note)
'''
width = int(self.canvas["width"]) + 4
height = int(self.canvas["height"]) + 4
Expand Down
2 changes: 2 additions & 0 deletions scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def change_scale(scale_tonic_note):
guitar.show_freq = sf.get()
guitar.draw_fretboard(triad)

piano.triad = triad
piano.draw()


Expand Down Expand Up @@ -112,6 +113,7 @@ def change_scale(scale_tonic_note):
# TODO:
# [ ] number of octaves
# [ ] toggle color octave
# [ ] starting octave
piano.canvas.pack()
piano_group.pack()

Expand Down
18 changes: 18 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


def get_color_for_octave(octave):
# colorsys.rgb_to_hls(1, 0, 0)
if octave == 1:
return '#9400D3'
elif octave == 2:
return '#4B0082'
elif octave == 3:
return '#0000FF'
elif octave == 4:
return '#00FF00'
elif octave == 5:
return '#FFFF00'
elif octave == 6:
return '#FF0000'
else:
return 'black'

0 comments on commit 17ad835

Please sign in to comment.