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):
28 import matplotlib.pyplot as plt
29 from matplotlib.path import Path
30 import matplotlib.patches as mpatches
32 blacks, whites, b_width, w_width = get_keyboard_edges(firstnote, lastnote)
36 ax = fig.add_subplot(111)
40 verts += [ (white - w_width, 0), (white - w_width, 1), (white + w_width, 1), (white + w_width, 0) ]
41 verts += [ (white - w_width, 0) ]
42 codes += [Path.MOVETO] + [Path.LINETO] * 4
43 path = Path(verts, codes)
44 patch = mpatches.PathPatch(path, facecolor= 'white', edgecolor='black', lw=1)
49 verts += [ (black - b_width, 0.33), (black - b_width, 1), (black + b_width, 1), (black + b_width, 0.33) ]
50 verts += [ (black - b_width, 0.33) ]
51 codes += [Path.MOVETO] + [Path.LINETO] * 4
52 path = Path(verts, codes)
53 patch = mpatches.PathPatch(path, facecolor= 'black', edgecolor='black', lw=1)
56 ax.axis(xmin = firstnote, xmax = lastnote)
58 if __name__ == '__main__':
60 import matplotlib.pyplot as plt
61 create_keyboard_patches(firstnote = 58, lastnote = 84)