Skip to content

Commit

Permalink
Merge pull request #16 from mstuttgart/fix/component_function
Browse files Browse the repository at this point in the history
[FIX] components() function ignores the base note
  • Loading branch information
yuma-m authored Jul 31, 2017
2 parents 3709e4d + cf03859 commit 257feeb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
3 changes: 3 additions & 0 deletions pychord/chord.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def components(self, visible=True):
:rtype: list[(str or int)]
:return component notes of chord
"""
if self._on:
self._quality.append_on_chord(self.on, self.root)

return self._quality.get_components(root=self._root, visible=visible)

def _parse(self, chord):
Expand Down
15 changes: 8 additions & 7 deletions pychord/quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def get_components(self, root='C', visible=False):
"""
root_val = note_to_val(root)
components = [v + root_val for v in self.components]

if visible:
components = [val_to_note(c, scale=root) for c in components]

return components

def append_on_chord(self, on_chord, root):
Expand All @@ -56,20 +58,19 @@ def append_on_chord(self, on_chord, root):
"""
root_val = note_to_val(root)
on_chord_val = note_to_val(on_chord) - root_val
for idx, val in enumerate(self.components):

lista = list(self.components)
for idx, val in enumerate(lista):
if val % 12 == on_chord_val:
scale = val / 12
self._rotate_components(idx, scale)
self.components.remove(val)
break

if on_chord_val > root_val:
on_chord_val -= 12

if on_chord_val not in self.components:
self.components.insert(0, on_chord_val)

def _rotate_components(self, stop_idx, scale):
for idx, val in enumerate(self.components[:stop_idx]):
self.components[idx] += (scale + 1) * 12

def append_note(self, note, root, scale=0):
""" Append a note to quality
Expand Down
7 changes: 7 additions & 0 deletions test/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_aug_chord(self):
com1 = c.components(visible=True)
self.assertEqual(com1, ["E", "G#", "C"])

def test_slash_chord(self):
c = Chord("CM9/D")
com0 = c.components(visible=False)
self.assertEqual(com0, [-10, 0, 4, 7, 11])
com1 = c.components(visible=True)
self.assertEqual(com1, ["D", "C", "E", "G", "B"])

def test_sus4_chord(self):
c = Chord("Fsus4")
com0 = c.components(visible=False)
Expand Down

0 comments on commit 257feeb

Please sign in to comment.