Skip to content

Commit

Permalink
Added new function for getRankedChords that takes a pitch set (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
maksutovic authored Feb 9, 2024
1 parent 8cee360 commit 73ce411
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/Tonic/Chord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ extension Chord: CustomStringConvertible {
}

extension Chord {

/// Get chords that match a set of pitches, listing enharmonic chords with sharp before flats
public static func getRankedChords(from pitchSet: PitchSet) -> [Chord] {
var flatNotes: [Note] = []
var sharpNotes: [Note] = []
var returnArray: [Chord] = []

for pitch in pitchSet.array {
flatNotes.append(Note(pitch: pitch, key: .F))
sharpNotes.append(Note(pitch: pitch, key: .C))
}
returnArray.append(contentsOf: Chord.getRankedChords(from: sharpNotes))
returnArray.append(contentsOf: Chord.getRankedChords(from: flatNotes))

return returnArray
}
/// Get chords from actual notes (spelling matters, C# F G# will not return a C# major)
/// Use pitch set version of this function for all enharmonic chords
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 })
Expand Down
7 changes: 7 additions & 0 deletions Tests/TonicTests/ChordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ class ChordTests: XCTestCase {
XCTAssertEqual(gChords.map { $0.description }, ["Gsus4", "Csus2"])
XCTAssertEqual(cChords.map { $0.description }, ["Csus2", "Gsus4"])
}

func testEnharmonicChords() {
let midiNotes: [Int8] = [54, 58, 61]
let fSharp = PitchSet(pitches: midiNotes.map { Pitch($0) } )
let chords = Chord.getRankedChords(from: fSharp)
XCTAssertEqual(chords.map { $0.description }, ["F♯", "G♭"])
}

func testPitchesWithNoInversion() {
// Arrange
Expand Down

0 comments on commit 73ce411

Please sign in to comment.