Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 5 #84

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pychord/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,17 @@ def get_all_rotated_notes(notes: List[str]) -> List[List[str]]:

get_all_rotated_notes([A,C,E]) -> [[A,C,E],[C,E,A],[E,A,C]]
"""
notes_list = []
for x in range(len(notes)):
notes_list.append(notes[x:] + notes[:x])
return notes_list
if len(notes) == 0:
return []
if len(notes) == 1:
return [notes]
if len(notes) == 2:
return [[notes[0], notes[1]], [notes[1], notes[0]]]
list_of_chords = []
for i in range(0, len(notes)):
m = notes[i]
remaining_list = notes[:i] + notes[i + 1:]
permutations_on_remaining_list = get_all_rotated_notes(remaining_list)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are regular rotations of a chord. We should keep the backward compatibility for the get_all_rotated_notes function if it is not a bug.
Why do you need this modification? If you think this feature is useful for others, I recommend you create a new function or add a new argument to enable this.

for p in permutations_on_remaining_list:
list_of_chords.append([m] + p)
return list_of_chords
2 changes: 2 additions & 0 deletions pychord/constants/qualities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
('sus', (0, 5, 7)),
# 4 notes
('6', (0, 4, 7, 9)),
('6b5', (0, 4, 6, 9)), # https://www.scales-chords.com/chord/piano/C%236b5
('6-5', (0, 4, 6, 9)), # https://www.scales-chords.com/chord/piano/C%236b5
('7', (0, 4, 7, 10)),
('7-5', (0, 4, 6, 10)),
('7b5', (0, 4, 6, 10)),
Expand Down
76 changes: 65 additions & 11 deletions test/test_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from builtins import sorted

from pychord import Chord
from pychord.analyzer import get_all_rotated_notes, find_chords_from_notes, notes_to_positions
Expand Down Expand Up @@ -47,11 +48,56 @@ class TestGetAllRotatedNotes(unittest.TestCase):

def test_two(self):
notes_list = get_all_rotated_notes(["C", "G"])
self.assertEqual(notes_list, [["C", "G"], ["G", "C"]])
expected = [["C", "G"], ["G", "C"]]
self.assertTrue(self._are_list_of_notes_equal(notes_list, expected))

def test_three(self):
notes_list = get_all_rotated_notes(["C", "F", "G"])
self.assertEqual(notes_list, [["C", "F", "G"], ["F", "G", "C"], ["G", "C", "F"]])
expected = [['C', 'F', 'G'], ['C', 'G', 'F'], ['F', 'C', 'G'],
['F', 'G', 'C'], ['G', 'C', 'F'], ['G', 'F', 'C']]
self.assertTrue(self._are_list_of_notes_equal(notes_list, expected))

def test_four(self):
notes_list = get_all_rotated_notes(["C", "F", "G", "A"])
# expected can be checked at https://www.dcode.fr/generateur-permutations
expected = [["C", "F", "G", "A"],
["F", "C", "G", "A"],
["G", "C", "F", "A"],
["C", "G", "F", "A"],
["F", "G", "C", "A"],
["G", "F", "C", "A"],
["G", "F", "A", "C"],
["F", "G", "A", "C"],
["A", "G", "F", "C"],
["G", "A", "F", "C"],
["F", "A", "G", "C"],
["A", "F", "G", "C"],
["A", "C", "G", "F"],
["C", "A", "G", "F"],
["G", "A", "C", "F"],
["A", "G", "C", "F"],
["C", "G", "A", "F"],
["G", "C", "A", "F"],
["F", "C", "A", "G"],
["C", "F", "A", "G"],
["A", "F", "C", "G"],
["F", "A", "C", "G"],
["C", "A", "F", "G"],
["A", "C", "F", "G"]]
self.assertTrue(self._are_list_of_notes_equal(notes_list, expected))

def _are_list_of_notes_equal(self, l1: [[str]], l2: [[str]]) -> bool:
for (i, j) in zip(l1, l2):
a = sorted(i)
b = sorted(j)
if a != b:
return False
return True

def test_are_list_of_notes_equal(self):
self.assertTrue(self._are_list_of_notes_equal([["A", "B", "C"]], [["C", "A", "B"]]))
self.assertTrue(
self._are_list_of_notes_equal([["A", "B", "C"], ["C", "A", "B"]], [["C", "A", "B"], ["A", "B", "C"]]))


class TestFindChordsFromNotes(unittest.TestCase):
Expand All @@ -67,10 +113,18 @@ def _assert_chords(self, notes, expected_chords):
"""
if isinstance(notes, str):
notes = notes.split()
c0 = find_chords_from_notes(notes)
list_of_chord_found = find_chords_from_notes(notes)
if isinstance(expected_chords, str):
expected_chords = [expected_chords]
self.assertEqual(c0, [Chord(c) for c in expected_chords])
expected_chords = expected_chords.split()
for c1 in list_of_chord_found:
chord_found = False
for c2 in expected_chords:
if str(c1) == c2:
chord_found = True
break
if not chord_found:
self.assertTrue(False)
self.assertTrue(True)

def test_major(self):
chords = find_chords_from_notes(["C", "E", "G"])
Expand Down Expand Up @@ -102,26 +156,26 @@ def test_aug(self):

def test_add9(self):
chords = find_chords_from_notes(["C", "E", "G", "D"])
self.assertEqual(chords, [Chord("Cadd9")])
self.assertEqual(chords, [Chord("Cadd9"), Chord("Em7+5/C")])

def test_m7b5(self):
chords = find_chords_from_notes(["F#", "A", "C", "E"])
self.assertEqual(chords, [Chord("F#m7-5"), Chord("Am6/F#")])
self.assertEqual(chords, [Chord("F#m7-5"), Chord("Am6/F#"), Chord("C6b5/F#")])

def test_m7dim5(self):
chords = find_chords_from_notes(["F#", "A", "C", "E"])
self.assertEqual(chords, [Chord("F#m7-5"), Chord("Am6/F#")])
self.assertEqual(chords, [Chord("F#m7-5"), Chord("Am6/F#"), Chord("C6b5/F#")])

def test_add4(self):
chords = find_chords_from_notes(["C", "E", "F", "G"])
self.assertEqual(chords, [Chord("Cadd4")])
self.assertEqual(chords, [Chord("Cadd4"), Chord("Cadd11")])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be Cadd11.


def test_minor_add4(self):
chords = find_chords_from_notes(["C", "Eb", "F", "G"])
self.assertEqual(chords, [Chord("Cmadd4")])

def test_minor7_add11(self):
self._assert_chords("C Eb G Bb F", ["Cm7add11", "F11/C"])
self._assert_chords("C Eb G Bb F", ["Cm7add11", "F11/C", "Eb69/C", "F9sus4/C"])

def test_major7_add11(self):
self._assert_chords("C E G B F", "CM7add11")
Expand All @@ -130,7 +184,7 @@ def test_minormajor7_add11(self):
self._assert_chords("C Eb G B F", "CmM7add11")

def test_major7_add13(self):
self._assert_chords("C E G A B D", "CM7add13")
self._assert_chords("C E G A B D", ["CM7add13", "Cmaj13", "Am11/C"])

def test_idempotence(self):
for _ in range(2):
Expand Down