From 1729dabb98a7972b2d4a99a3c5bd980c37594e67 Mon Sep 17 00:00:00 2001 From: Sebastian Boldt Date: Fri, 26 Jan 2024 15:34:39 +0100 Subject: [PATCH 1/3] added new pitch and notes helper function + unit-tests --- Sources/Tonic/Chord.swift | 26 +++++++++++++++- Tests/TonicTests/ChordTests.swift | 52 +++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/Sources/Tonic/Chord.swift b/Sources/Tonic/Chord.swift index 055b3a6..86ef76e 100644 --- a/Sources/Tonic/Chord.swift +++ b/Sources/Tonic/Chord.swift @@ -128,7 +128,6 @@ extension Chord: CustomStringConvertible { } extension Chord { - public static func getRankedChords(from notes: [Note]) -> [Chord] { let potentialChords = ChordTable.shared.getAllChordsForNoteSet(NoteSet(notes: notes)) let orderedNotes = notes.sorted(by: { f, s in f.noteNumber < s.noteNumber }) @@ -142,3 +141,28 @@ extension Chord { return sortedRanks.map({ $0.1 }) } } + +extension Chord { + /// Returns all Pitches of a certain chord, taking into account the inversion, starting at the given octave + /// - Parameter octave: octave of the chord for inversion 0 + /// - Returns: All pitches in that Chord + public func pitches(octave: Int) -> [Pitch] { + return notes(octave: octave).map { $0.pitch } + } + + /// Returns all Notes of a certain chord, taking into account the inversion, starting at the given octave + /// - Parameter octave: initial octave of the chord for inversion 0 + /// - Returns: All notes in that chord + public func notes(octave: Int) -> [Note] { + var notes = noteClasses.map { + Note($0.letter, accidental: $0.accidental, octave: octave) + }.sorted() + + for step in 0.. Date: Fri, 26 Jan 2024 15:41:00 +0100 Subject: [PATCH 2/3] fixed PR remarks --- Tests/TonicTests/ChordTests.swift | 49 +++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/Tests/TonicTests/ChordTests.swift b/Tests/TonicTests/ChordTests.swift index eef1b63..16d5c95 100644 --- a/Tests/TonicTests/ChordTests.swift +++ b/Tests/TonicTests/ChordTests.swift @@ -145,49 +145,80 @@ class ChordTests: XCTestCase { func testPitchesWithNoInversion() { // Arrange let chord = Chord(.C, type: .majorTriad, inversion: 0) - let expectedPitches = [Note(.C, octave: 0), Note(.E, octave: 0), Note(.G, octave: 0)].map { $0.pitch } + let expectedPitches = [ + Note(.C, octave: 0), + Note(.E, octave: 0), + Note(.G, octave: 0) + ].map { $0.pitch } // Act let pitches = chord.pitches(octave: 0) // Assert - XCTAssertEqual(pitches, expectedPitches, "Pitches should match expected pitches for no inversion") + XCTAssertEqual( + pitches, + expectedPitches, + "Pitches should match expected pitches for no inversion" + ) } func testPitchesWithInversion() { // Arrange let chord = Chord(.C, type: .majorTriad, inversion: 1) - let expectedPitches = [Note(.E, octave: 4), Note(.G, octave: 4), Note(.C, octave: 5)].map { $0.pitch } - + let expectedPitches = [ + Note(.E, octave: 4), + Note(.G, octave: 4), + Note(.C, octave: 5) + ].map { $0.pitch } // Act let pitches = chord.pitches(octave: 4) // Assert - XCTAssertEqual(pitches.sorted(), expectedPitches.sorted(), "Pitches should match expected pitches for 1st inversion") + XCTAssertEqual( + pitches.sorted(), + expectedPitches.sorted(), + "Pitches should match expected pitches for 1st inversion" + ) } func testNotesWithNoInversion() { // Arrange let chord = Chord(.C, type: .majorTriad, inversion: 0) - let expectedNotes = [Note(.C, octave: 4), Note(.E, octave: 4), Note(.G, octave: 4)] + let expectedNotes = [ + Note(.C, octave: 4), + Note(.E, octave: 4), + Note(.G, octave: 4) + ] // Act let notes = chord.notes(octave: 4) // Assert - XCTAssertEqual(notes, expectedNotes, "Notes should match expected notes for no inversion") + XCTAssertEqual( + notes, + expectedNotes, + "Notes should match expected notes for no inversion" + ) } func testNotesWithInversion() { // Arrange let chord = Chord(.C, type: .majorTriad, inversion: 1) - let expectedNotes = [Note(.E, octave: 4), Note(.G, octave: 4), Note(.C, octave: 5)].sorted() + let expectedNotes = [ + Note(.E, octave: 4), + Note(.G, octave: 4), + Note(.C, octave: 5) + ] // Act let notes = chord.notes(octave: 4) // Assert - XCTAssertEqual(notes.sorted(), expectedNotes.sorted(), "Notes should match expected notes for 1st inversion") + XCTAssertEqual( + notes.sorted(), + expectedNotes.sorted(), + "Notes should match expected notes for 1st inversion" + ) } } From 9db082c24084aab9da7b274e69f4b4fa531a709c Mon Sep 17 00:00:00 2001 From: Sebastian Boldt Date: Fri, 26 Jan 2024 15:55:13 +0100 Subject: [PATCH 3/3] improved ordering --- Sources/Tonic/Chord.swift | 4 ++-- Tests/TonicTests/ChordTests.swift | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Tonic/Chord.swift b/Sources/Tonic/Chord.swift index 86ef76e..edc9f81 100644 --- a/Sources/Tonic/Chord.swift +++ b/Sources/Tonic/Chord.swift @@ -156,13 +156,13 @@ extension Chord { public func notes(octave: Int) -> [Note] { var notes = noteClasses.map { Note($0.letter, accidental: $0.accidental, octave: octave) - }.sorted() + } for step in 0..