diff --git a/pychord/chord.py b/pychord/chord.py index 8058e91..68cfdea 100644 --- a/pychord/chord.py +++ b/pychord/chord.py @@ -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): diff --git a/pychord/quality.py b/pychord/quality.py index 5dfc816..8488c2a 100644 --- a/pychord/quality.py +++ b/pychord/quality.py @@ -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): @@ -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 diff --git a/test/test_component.py b/test/test_component.py index eba4773..876c955 100644 --- a/test/test_component.py +++ b/test/test_component.py @@ -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)