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


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

