3 def get_keyboard_edges(firstnote = 21, lastnote = 108):
6 # build template of white notes
8 xw_temp = [i*scalew for i in range(0,7)]
9 # build template of black notes
11 xb_temp = [i*scaleb for i in [1,3,7,9,11]]
14 for octave in range(octaves-1):
17 if curnote > firstnote-1 and curnote < lastnote+1:
19 for octave in range(octaves-1):
22 if curnote > firstnote-1 and curnote < lastnote+1:
25 return xb, xw, 2/3. *scaleb, 1/2. * scalew
27 def create_keyboard_patches(firstnote, lastnote, ax = None):
29 import matplotlib.pyplot as plt
30 from matplotlib.path import Path
31 import matplotlib.patches as mpatches
33 blacks, whites, b_width, w_width = get_keyboard_edges(firstnote, lastnote)
37 ax = fig.add_subplot(111)
41 verts += [ (white - w_width, 0), (white - w_width, 1), (white + w_width, 1), (white + w_width, 0) ]
42 verts += [ (white - w_width, 0) ]
43 codes += [Path.MOVETO] + [Path.LINETO] * 4
44 path = Path(verts, codes)
45 patch = mpatches.PathPatch(path, facecolor= 'white', edgecolor='black', lw=1)
50 verts += [ (black - b_width, 0.33), (black - b_width, 1), (black + b_width, 1), (black + b_width, 0.33) ]
51 verts += [ (black - b_width, 0.33) ]
52 codes += [Path.MOVETO] + [Path.LINETO] * 4
53 path = Path(verts, codes)
54 patch = mpatches.PathPatch(path, facecolor= 'black', edgecolor='black', lw=1)
57 ax.axis(xmin = firstnote, xmax = lastnote)
59 if __name__ == '__main__':
61 import matplotlib.pyplot as plt
62 create_keyboard_patches(firstnote = 58, lastnote = 84)