# Scientific names to piano key numbers

Is there a formula that converts scientific note names to piano key numbers? As an example, given A4 it would output 49.

• This doesn’t feel like the right place for this question. Anyone have a better spot? – Dan Szabo Dec 14 '19 at 10:27

The frequencies are on an exponential scale. You can use any note as your base value. On a Chromatic scale, each note is a twelfth step of an octave (doubling of frequency, same tone). So, a very common formula is to use the 440Hz A note. (Search on 432Hz to see a controversy about this.)

$$f = 440 \cdot 2^{\frac{n}{12}}$$

Where $$n$$ is the semi-tone count from your base.

Here is a good reference: Note names, MIDI numbers and frequencies

Upon rereading, I didn't really answer your question. Because of the gaps in note names (black keys on the piano), this is more of a programming exercise than a simple formula.

In Python:

#=======================================================
def NoteConverter( ArgNoteName ):

theLetter = ArgNoteName[0:1]
theNumber = int( ArgNoteName[1:] )

theOrdinal = "C D EF G A B".find( theLetter )

theMidiNumber = theNumber * 12 + theOrdinal + 12

return theMidiNumber

#=======================================================


This is for Midi numbering. Subtract 20 to get your piano key number (on most pianos).

• Note that the logarithmic computation is for an even-tempered scale, which is good enough for most work. But there are all sorts of temperings, each of which is a compromise of some sort. – TimWescott Dec 16 '19 at 16:15