source: python/demos/demo_keyboard.py @ 2da7526

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 2da7526 was 5caf474, checked in by Paul Brossier <piem@piem.org>, 12 years ago

python/demos/demo_keyboard.py: remove gnuplot stuff

  • Property mode set to 100755
File size: 2.0 KB
RevLine 
[18a0552]1#! /usr/bin/env python
2
[5caf474]3def get_keyboard_edges(firstnote = 21, lastnote = 108):
[03f27b0]4    octaves = 10
[18a0552]5
[03f27b0]6    # build template of white notes
7    scalew  = 12/7.
8    xw_temp = [i*scalew for i in range(0,7)]
9    # build template of black notes
10    scaleb  = 6/7.
11    xb_temp = [i*scaleb for i in [1,3,7,9,11]]
[18a0552]12
[03f27b0]13    xb,xw = [],[]
14    for octave in range(octaves-1):
15        for i in xb_temp:
16            curnote = i+12*octave
17            if  curnote > firstnote-1 and curnote < lastnote+1:
18                xb = xb + [curnote]
19    for octave in range(octaves-1):
20        for i in xw_temp:
21            curnote = i+12*octave
22            if  curnote > firstnote-1 and curnote < lastnote+1:
23                xw = xw + [curnote]
[18a0552]24
[5caf474]25    return xb, xw, 2/3. *scaleb, 1/2. * scalew
[18a0552]26
27def create_keyboard_patches(firstnote, lastnote, ax = None):
28    import numpy as np
29    import matplotlib.pyplot as plt
30    from matplotlib.path import Path
31    import matplotlib.patches as mpatches
32
33    blacks, whites, b_width, w_width = get_keyboard_edges(firstnote, lastnote)
34
35    if not ax:
36        fig = plt.figure()
37        ax = fig.add_subplot(111)
38
39    verts, codes = [], []
40    for white in whites:
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)
46    ax.add_patch(patch)
47
48    verts, codes = [], []
49    for black in blacks:
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)
55    ax.add_patch(patch)
56
57    ax.axis(xmin = firstnote, xmax = lastnote)
58
59if __name__ == '__main__':
60
[5caf474]61    import matplotlib.pyplot as plt
62    create_keyboard_patches(firstnote = 58, lastnote = 84)
63    plt.show()
Note: See TracBrowser for help on using the repository browser.