Skip to content

Commit

Permalink
Add module for guitars.
Browse files Browse the repository at this point in the history
  • Loading branch information
skytreader committed Jun 16, 2017
1 parent 3df32bb commit 83fe05e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions music_theory/guitar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .scales import CHROMATIC_SCALE

def construct_fretting(tuning, fret_limit=12):
"""
`tuning` is a string describing the notes sounded by the guitar string when
played in open. No length restrictions are imposed because, hey, whatever
set-up floats your guitar. :)
"""
fretting = []

for open_note in tuning:
string = []
open_norm = open_note.upper()
initial_index = CHROMATIC_SCALE.index(open_norm)

for i in range(fret_limit + 1):
string.append(CHROMATIC_SCALE[(initial_index + i) % len(CHROMATIC_SCALE)])

fretting.append(string)

return fretting
17 changes: 17 additions & 0 deletions music_theory/tests/guitar_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from ..guitar import construct_fretting

import unittest

class GuitarTests(unittest.TestCase):

def test_construct_fretting(self):
standard_fretting = [
["E", "F", "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E"],
["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A"],
["D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D"],
["G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G"],
["B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"],
["E", "F", "F#", "G", "G#", "A", "A#", "B", "C", "C#", "D", "D#", "E"],
]

self.assertEqual(standard_fretting, construct_fretting("EADGBe"))

0 comments on commit 83fe05e

Please sign in to comment.