Skip to content

Commit

Permalink
Intervals are not reversible.
Browse files Browse the repository at this point in the history
  • Loading branch information
skytreader committed Jun 20, 2017
1 parent 5d5a0a6 commit 6aa8849
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions music_theory/scales.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ def interval_quality(note1, note2):
norm_note2 = note2.upper()
index1 = CHROMATIC_SCALE.index(norm_note1)
index2 = CHROMATIC_SCALE.index(norm_note2)
return abs(index1 - index2) + 1

if index2 >= index1:
return index2 - index1 + 1
else:
index2 += len(CHROMATIC_SCALE)
return index2 - index1 + 1

def interval_quantity(note1, note2):
# TODO Work this in terms of minor and major intervals.
plain_note1 = note1[0].upper()
plain_note2 = note2[0].upper()
index1 = NO_SHARPS.index(plain_note1)
index2 = NO_SHARPS.index(plain_note2)
return abs(index1 - index2) + 1

if index2 >= index1:
return index2 - index1 + 1
else:
index2 += len(NO_SHARPS)
return index2 - index1 + 1
6 changes: 6 additions & 0 deletions music_theory/tests/scales_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def test_interval_quantity(self):
self.assertEqual(3, interval_quantity("A#", "c"))
self.assertEqual(3, interval_quantity("a", "C#"))

self.assertNotEqual(interval_quantity("A", "C"), interval_quantity("C", "A"))
self.assertEqual(6, interval_quantity("C", "A"))

def test_eval_flat(self):
self.assertEqual("G#", eval_flat("Ab"))
self.assertEqual("B", eval_flat("Cb"))
Expand All @@ -49,3 +52,6 @@ def test_interval_quality(self):
self.assertEqual(5, interval_quality("a", "C#"))
self.assertEqual(3, interval_quality("A#", "c"))
self.assertEqual(5, interval_quality("a", "C#"))

self.assertNotEqual(interval_quality("A", "C"), interval_quality("C", "A"))
self.assertEqual(10, interval_quality("C", "A"))

0 comments on commit 6aa8849

Please sign in to comment.