2

I have another conversion question. Given the number of the key on a standard piano keyboard counting from 1 as A to 88 as C, I need to find out witch octave and note it designates. For example, an input of 49 would output octave 4 and note A.

The octave is easy, it is simply the floor of the key number divided by twelve. But I can't figure out an elegant way to calculate the note.

I don't know wether this is the right place to ask this question, so please migrate if anyone has can think of a better place.

3
  • 4
    It sounds a bit like you're trying to re-invent the wheel. Have you investigated MIDI Note numbers? midi.org/specifications
    – Tetsujin
    Dec 22 '19 at 9:28
  • 1
    You're trying to use base 10 numbering to depict something in base 12. Going to find all sorts of problems - mostly already solved by MIDI.
    – Tim
    Dec 22 '19 at 9:41
  • You may want to hear about this library.
    – moonwave99
    Jun 13 '20 at 15:35
6

The octave number can be found with the floor function and an offset (because the octave changes at C and not at A):

octave (n) =  floor ( (n+8)/12 )

The letter number can be found with modular arithmetic:

letterNumber (n) = n mod 12  

To convert the letter number to a pitch class, hard code the various cases:

letter (letterNumber) = { 1 = A, 
                          2 = A#/Bb, 
                          3 = B,  
                           ...,
                         10 = F#/Gb,
                         11 = G, 
                          0 = G#/Ab } 


Worked example:

n = 50

octave (50) = floor ( (50+8)/12 )
            = floor ( 58/12 )
            = floor ( 4.833 )
            = 4

letterNumber (50) = 50 mod 12
                  = (2+4*12) mod 12
                  = 2

letter (2) = { 1 = A,
               2 = A#/Bb 
                ... }
           = A#/Bb

NB:

  • This will give enharmonically equivalent results only
  • You might find it easier to work with only sharps (or only flats)
1
  • As a computer scientist, this answer is beautiful! 😊 nice use of the mod operator.
    – WillRoss1
    Dec 22 '19 at 17:12
3

I would advise against starting to count with the first physical key, since smaller keyboards with fewer keys break the system and would use Midi numbers instead. The first note of a numbered octave is always c, while your proposed approach would start with a.

The programmatical approach would be to use the remainder of the division by 12 as an index into an array of note names, but for the black keys you have the sharp/flat ambiguity, which can‘t be resolved easily.

1
  • Sharp/flat ambiguity exists for white notes, too. You solve that by making an arbitrary choice
    – phoog
    Dec 22 '19 at 14:08
0
  • If key nr.1 = > A0 then you have to count Y=(X-1):12 to find the octave of the key A.

  • The rest is the difference from the key you are looking for (counting in the chromatic scale: 1=A, 2=A#/Bb, 3=C ... etc

X=the given key number Y= the key you are searching

A1=octave 0

49=??? = > (49-1):12=4 = > octave 4 (rest=0) 49=A (4)

88=??? = > (88-1):12=7 = > octave 7 (rest=3) 88=C (7)

102=??? = > (102-1):12 = > octave 8 (rest=5) 102=D (8)

0

I'm confused by your question and some of the answers. Is this what you are looking for? enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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