Skip to content

Instantly share code, notes, and snippets.

@schneiderfelipe
Created September 5, 2018 16:35
Show Gist options
  • Save schneiderfelipe/500afeebdca756954e4a105d3734ae12 to your computer and use it in GitHub Desktop.
Save schneiderfelipe/500afeebdca756954e4a105d3734ae12 to your computer and use it in GitHub Desktop.
Fretboard objects in Python
class Note:
"""
Abstract representation of a musical note.
Parameters
----------
note : `str`, `int`, `Note`
String representing note (e.g. 'C3', 'Ab', 'F#', etc.) or another `Note` object.
Notes
-----
A note-like object is defined as any object than can be converted to a `Note` object.
"""
def __init__(note):
if isinstance(note, str):
self._note = _note_string_to_int(note)
elif isinstance(note, int):
self._note = note
else:
self._note = note._note
class Fretboard:
"""
Abstract representation of a fretboard of a stringed instrument.
Parameters
----------
tuning : sequence of note-like objects, optional
Note representation for open strings.
Examples
--------
>>> guitar = Fretboard()
>>> print(guitar)
E' |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
B |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|--/--|-----|-----|-----|
G |-----|-----|--/--|-----|--/--|-----|--/--|-----|--/--|-----|-----|-----|-----|-----|--/--|
D |-----|-----|--/--|-----|--/--|-----|--/--|-----|--/--|-----|-----|-----|-----|-----|--/--|
A |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|--/--|-----|-----|-----|
E |-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
>>> print(guitar.to_string(kind="chord"))
E A D G B E'
| | | | | |
| | | | | |
| | \ \ | |
| | | | | |
| | \ \ | |
>>> for chord_position in guitar.find_chord(('B2', 'C', 'E', 'B')):
... guitar.fret(chord_position)
... print(guitar.to_string(kind="chord"))
... guitar.clear()
5 | | X X X |
| | | | | |
| | \ \ | X
| | | | | |
| | \ \ | |
"""
def __init__(tuning=('E2', 'A2', 'D3', 'G3', 'B3', 'E3')):
self._tuning = (Note(t) for t in tuning)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment