=============================================================================
E     E||---|---|-G-|---|-A-|---|---|-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|---|---|
B      ||-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|---|---|-C-|---|-D-|---|-E-|---|---|
G     G||---|-A-|---|---|-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|---|---|-C-|---|-D-|
D     D||---|-E-|---|---|-G-|---|-A-|---|---|-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|
A     A||---|---|-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|---|---|-C-|---|-D-|---|-E-|
E     E||---|---|-G-|---|-A-|---|---|-C-|---|-D-|---|-E-|---|---|-G-|---|-A-|---|---|
        =============================================================================
                  3       5       7       9          12          15      17      19

The fretboard python module

The fretboard module is a relatively simple python module for displaying notes and scales relative to a guitar's fretboard. It currently supports ASCII output.

Notes

Before getting to scales and displays, it is useful to know how the module handles representations of musical notes. The fretboard module doesn't deal with the acoustic pitch of notes; rather it represents notes in terms of their number of semitones relative to a C reference.

Creating notes by name

Notes can be created either by naming the note and optionally appending "#" or "b" to indicate a sharp or flat note.


In [1]:
from fretboard import *
c = Note('C')
print (c)


C

In [2]:
Note('C#')


Out[2]:
C#

In [3]:
Note('Db')


Out[3]:
Db

In [4]:
Note('B#')


Out[4]:
C

If you like, you can use multiple sharps or flats.


In [5]:
Note('B##'), Note('B###'), Note('B##b')


Out[5]:
(C#, D, C)

Creating relative notes

Notes can also be createde by specifying an interval (in number of semitones) relative to another note. For example, F is 5 semitones above C.


In [6]:
f = c + 5
print (f)


F

Adding 12 semitones gets us back to the same note.


In [7]:
ff = f + 12
print (ff)


F

Intervals

We can also take the difference of two notes, which yields the number of semitones between the notes.


In [8]:
print (ff - c)


17

Note that the interval printed above is the total number of semitones (which can span more than one octave) and can be positive or negative. If we want the to know the simple (within-octave) interval from a note up to the next instance of another note, the interval method (or function) can be used).


In [9]:
print (c.interval(f))


5

In [10]:
print (f.interval(c))


7

Scales

The main purpose of this module is to display scales over a fretboard diagram. Lets take a look at the Major scale in the key of C.


In [11]:
c_maj = Major('C')
print (c_maj)


[C, D, E, F, G, A, B]

We can also create different modes of the diatonic scale. For example, the minor scale is just the sixth mode of the diatonic scale.


In [12]:
c_min = Diatonic('C', mode=6)
print (c_min)


[C, D, D#, F, G, G#, A#]

There is also a Minor scale class so you can save some typing (and don't have to remember which mode of the major scale it is). In addition to the Major and Minor, there are also classes for HarmonicMinor, Pentatonic, and Blues scales.


In [13]:
a_pmin = MinorPentatonic('A')
print (a_pmin)


[A, C, D, E, G]

The Blues scale adds a flat fifth (the blue note) to the pentatonic minor.


In [14]:
a_blues = Blues('A')
print (a_blues)


[A, C, D, Eb, E, G]

To make the scales useful, we have the ability to test for membership.


In [15]:
'C' in a_blues


Out[15]:
True

In [16]:
'B' in a_blues


Out[16]:
False

We can also get the interval of a given note with respect to the scale.


In [17]:
print (a_blues.get_interval_name('G'))


m7

Displaying Notes and Scales

The fretboard module supports printing fretboard diagrams to the terminal output (stdout) via the console submodule. The quickest way to display simple scale diagrams is visa the show_scale function.


In [18]:
console.show_scale(Minor('A'))


        =============================================================================
E     E||-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|---|-B-|
B     B||-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|---|-E-|-F-|---|
G     G||---|-A-|---|-B-|-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|
D     D||---|-E-|-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|
A     A||---|-B-|-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|---|-E-|
E     E||-F-|---|-G-|---|-A-|---|-B-|-C-|---|-D-|---|-E-|-F-|---|-G-|---|-A-|---|-B-|
        =============================================================================
                  3       5       7       9          12          15      17      19 

In [19]:
console.show_scale(Major('C'), tuning='D A D G A D', fmt='interval')


        =============================================================================
D    M2||---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|
A    M6||---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|---|M3-|
G     5||---|M6-|---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|
D    M2||---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|
A    M6||---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|---|M3-|
D    M2||---|M3-|-4-|---|-5-|---|M6-|---|M7-|-R-|---|M2-|---|M3-|-4-|---|-5-|---|M6-|
        =============================================================================
                  3       5       7       9          12          15      17      19 

There are various optional show_scale arguments that can be used to customize the display. The fmt argument can be any of the following:

  • "note" (default) - display the name of each note
  • "interval" - display name of the interval of each note with respect to the scale key
  • <text char> - display the specified character (e.g., '*') for each note
  • <callable> - display the return value of the callable oject applied to each Fret object of the display

If a callable object is given for fmt, it is passed a Fret object that has the following attributes:

  • note - the Note object associated with the fret
  • string - the number of the string associated with the fret
  • number - the number of the fret (zero indicates open string)

For example, let's display A Minor Pentatonic with "R" displayed for the root note and "*" for all others.


In [20]:
scale = MinorPentatonic('A')
console.show_scale(scale, fmt=lambda f: 'R' if f.note == scale.root else '*')


        =============================================================================
E     *||---|---|-*-|---|-R-|---|---|-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|---|---|
B      ||-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|---|---|-*-|---|-*-|---|-*-|---|---|
G     *||---|-R-|---|---|-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|---|---|-*-|---|-*-|
D     *||---|-*-|---|---|-*-|---|-R-|---|---|-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|
A     R||---|---|-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|---|---|-*-|---|-*-|---|-*-|
E     *||---|---|-*-|---|-R-|---|---|-*-|---|-*-|---|-*-|---|---|-*-|---|-R-|---|---|
        =============================================================================
                  3       5       7       9          12          15      17      19 

Let's display scale intervals for Box 2 of the A Minor Pentatonic scale.


In [21]:
console.show_scale(scale, fmt=lambda f: scale.get_interval_name(f.note) if f.number in range(7, 11) else None)


        =============================================================================
E      ||---|---|---|---|---|---|---|m3-|---|-4-|---|---|---|---|---|---|---|---|---|
B      ||---|---|---|---|---|---|---|m7-|---|-R-|---|---|---|---|---|---|---|---|---|
G      ||---|---|---|---|---|---|-4-|---|-5-|---|---|---|---|---|---|---|---|---|---|
D      ||---|---|---|---|---|---|-R-|---|---|m3-|---|---|---|---|---|---|---|---|---|
A      ||---|---|---|---|---|---|-5-|---|---|m7-|---|---|---|---|---|---|---|---|---|
E      ||---|---|---|---|---|---|---|m3-|---|-4-|---|---|---|---|---|---|---|---|---|
        =============================================================================
                  3       5       7       9          12          15      17      19 

The show_scale function is a convenient wrapper around the Console class. To have greater control over the display, we can work with a Console object directly. We'll create one and change the fill characters to be just empty space.


In [22]:
c = Console()
c.fret_fill_char = ' '
c.fret_empty_fill_char = ' '
c.display_scale(scale)
c.show()


        =============================================================================
E     E||   |   | G |   | A |   |   | C |   | D |   | E |   |   | G |   | A |   |   |
B      || C |   | D |   | E |   |   | G |   | A |   |   | C |   | D |   | E |   |   |
G     G||   | A |   |   | C |   | D |   | E |   |   | G |   | A |   |   | C |   | D |
D     D||   | E |   |   | G |   | A |   |   | C |   | D |   | E |   |   | G |   | A |
A     A||   |   | C |   | D |   | E |   |   | G |   | A |   |   | C |   | D |   | E |
E     E||   |   | G |   | A |   |   | C |   | D |   | E |   |   | G |   | A |   |   |
        =============================================================================
                  3       5       7       9          12          15      17      19 

The Console class also has a display_fret method that allows us to control display of individual frets. Let's use that to display a C chord.


In [23]:
c = Console()
for (s, f) in [(1, 0), (2, 1), (3, 0), (4, 2), (5, 3)]:
    c.display_fret(s, f)
c.display_fret(6, 0, 'x')
c.show(fmax=5)


        =====================
E     E||---|---|---|---|---|
B      ||-C-|---|---|---|---|
G     G||---|---|---|---|---|
D      ||---|-E-|---|---|---|
A      ||---|---|-C-|---|---|
E     x||---|---|---|---|---|
        =====================
                  3       5 

Finally, the Console.show method prints the display to stdout. To get the display string directly, use the get_display method instead.


In [24]:
text = c.get_display(fmax=5)
print (text)


        =====================
E     E||---|---|---|---|---|
B      ||-C-|---|---|---|---|
G     G||---|---|---|---|---|
D      ||---|-E-|---|---|---|
A      ||---|---|-C-|---|---|
E     x||---|---|---|---|---|
        =====================
                  3       5