I'm programming some Music stuff for piano and I need to know when exactly to call the note b or #. Can you help me out in understanding it? I know the difference and the purpose for those but I cannot make a universal formula out of it to always call the Notes correctly in my program.

Example: I say: Hey program gimme all the notes for the D7, or CM7, Em7 etc. chord. And then I want it to spit out the notes correctly called b or # where fit.

Thx in advance

  • Is your question "how to know that the third of D7 is F♯" or "is the third of D7 F♯ or G♭"?
    – Richard
    Commented Jan 4, 2019 at 23:42
  • You should flesh out any reasons why this question is not a duplicate of music.stackexchange.com/questions/54234/…, lest it be closed as a duplicate thereof. SE doesn't like duplication. SE doesn't like duplication. Commented Jan 4, 2019 at 23:45
  • A subset of the considerations in this answer should help: music.stackexchange.com/a/26781/2639
    – Dave
    Commented Jan 5, 2019 at 4:38

1 Answer 1


It's better to consider the following three parameters.

  1. User input

  2. The key signature

  3. Circle of fifth.

By default have the keys as "C G D A E B Gb Db Ab Eb Bb F"

By default have the key signatures mapping as "no sharps and no falts, one sharp, two sharp,three sharp,four sharp,five sharp,six flats, five flats, four flats, three flats, two flats, one flat"

If user input is any of the above key, then get the note names from the predefined key signature mapping above.

Ex) G - one sharp, so G A B C D E F#

Gb - six flats, so Gb Ab Bb Cb Db Eb F

If user input is not in the above predefined keys, then define additional keys and key signature mappings as shown below and use them.

Additional keys as "Cb C# G# D# Db ..."

Additional key signatures mapping as "7 flats, 2 sharps, 3 sharps, ...." ( Check last table in https://en.wikipedia.org/wiki/Key_signature )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.