From 95ed13e45d036d344916203852514684e5ade32a Mon Sep 17 00:00:00 2001 From: Sebastian Boldt Date: Sun, 28 Jan 2024 17:57:05 +0100 Subject: [PATCH] improved notes function, added additional test (#28) * improved notes function, added additional test * cleanup --- Sources/Tonic/Chord.swift | 31 +++++++++++++++++++++++++++++-- Tests/TonicTests/ChordTests.swift | 24 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Sources/Tonic/Chord.swift b/Sources/Tonic/Chord.swift index 6cc397d..34d8e05 100644 --- a/Sources/Tonic/Chord.swift +++ b/Sources/Tonic/Chord.swift @@ -154,13 +154,40 @@ extension Chord { /// - 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) + // This array will store all the notes with the correct octaves + var notes: [Note] = [] + // Convert the root note class to a note object + let rootNote = Note(root.letter, accidental: root.accidental, octave: octave) + // append the note to the array of our notes + notes.append(rootNote) + + // Iterate over all intervals + for interval in self.type.intervals { + // Create the next note by using the shiftup function + if let shifted = rootNote.shiftUp(interval) { + notes.append(shifted) + } } + // Stores all shifted notes + var shiftedNotes: [Note] = [] + + // Iterate over all inversion steps for step in 0..