AudioKit / Tonic

Swift library for music theory
https://www.audiokit.io/Tonic/
MIT License
154 stars 21 forks source link

Query the correct notes or pitches of a chord #22

Closed SebastianBoldt closed 10 months ago

SebastianBoldt commented 10 months ago

Description

It may be incorrect, but is it impossible to extract the correct notes with their respective octaves from a chord object for a specific inversion?

I tried to obtain the correct notes and pitches for a chord by mapping all the note classes, since it was the only property available in the chord object. I used the following code:

chord.noteClasses.map {
    Note($0.letter, accidental: $0.accidental, octave: 0).pitch
}

Unfortunately, the noteClasses do not contain the octave information, making it impossible for me to obtain the correct pitches for a specific chord inversion.

Proposed Solution

I would like to be able to do something like this:

chord.pitches(octave: 1) // Return the correct pitches taking into account the inversion

or maybe something like this:

chord.notes(octave: 1) // Return the correct notes taking into account the inversion

So having access to notes with the correct octaves would be awesome

Describe Alternatives You've Considered

Implementing it on my side.

extension Chord {
    func pitches(octave: Int) -> [Pitch] {
        var notes = noteClasses.map {
            Note($0.letter, accidental: $0.accidental, octave: octave)
        }

        for step in 0..<inversion {
            let index = step % notes.count
            notes[index].octave += 1
        }

        return notes.map {
            $0.pitch
        }
    }
}

Additional Context

No response

aure commented 10 months ago

Your solution looks pretty clean to me. Would you like to add it to Tonic in a Pull Request or just have me add it?

SebastianBoldt commented 10 months ago

@aure I will create a PR ✌️

SebastianBoldt commented 10 months ago

@aure I created PR: https://github.com/AudioKit/Tonic/pull/24