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

Query the notes of Key with correct octave information #25

Open
SebastianBoldt opened this issue Jan 26, 2024 · 1 comment
Open

Query the notes of Key with correct octave information #25

SebastianBoldt opened this issue Jan 26, 2024 · 1 comment

Comments

@SebastianBoldt
Copy link
Contributor

SebastianBoldt commented Jan 26, 2024

Description

It may be incorrect, but is extracting the correct notes with their respective octaves from a Key object impossible?

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

key.noteSet.array.map {
     var note = $0
     note.octave = 0
     return note.pitch 
}

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

po Key.init(root: .init(.D, accidental: .natural), scale: .major).noteSet.array
▿ 7 elements
  ▿ 0 : C♯4
    ▿ noteClass : C♯
      - letter : Tonic.Letter.C
      - accidental : ♯
    - octave : 4
  ▿ 1 : D4
    ▿ noteClass : D
      - letter : Tonic.Letter.D
      - accidental : 
    - octave : 4
  ▿ 2 : E4
    ▿ noteClass : E
      - letter : Tonic.Letter.E
      - accidental : 
    - octave : 4
  ▿ 3 : F♯4
    ▿ noteClass : F♯
      - letter : Tonic.Letter.F
      - accidental : ♯
    - octave : 4
  ▿ 4 : G4
    ▿ noteClass : G
      - letter : Tonic.Letter.G
      - accidental : 
    - octave : 4
  ▿ 5 : A4
    ▿ noteClass : A
      - letter : Tonic.Letter.A
      - accidental : 
    - octave : 4
  ▿ 6 : B4
    ▿ noteClass : B
      - letter : Tonic.Letter.B
      - accidental : 
    - octave : 4

Proposed Solution

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

key.pitches(octave: 1)

or maybe something like this:

key.notes(octave: 1) 

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

In this code snippet, I am increasing the octave for all notes that are lower than the pitch of the root note. Please note that I am not entirely sure if this approach will work for all possible Scale Types, but at first glance, it appears to be a promising solution.

extension Key {
    func notes(octave: Int) -> [Note] {
        var orderedNotes: [Note] = []

        for note in noteSet.array {
            let shift = note.noteClass.canonicalNote.pitch < root.canonicalNote.pitch
            let shiftedNote = Note(note.letter, accidental: note.accidental, octave: shift ? octave + 1 : octave)
            orderedNotes.append(shiftedNote)
        }

        return orderedNotes.sorted()
    }
}

Describe Alternatives You've Considered

No alternatives considered

Additional Context

No response

@aure
Copy link
Member

aure commented Feb 23, 2024

Happy to add this if you would like to write a test and make a pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants