Quick musical refresher:
The piano keyboard consists of 88 notes. On each octave, there are 12 notes,
C, C♯/D♭, D, D♯/E♭, E, F, F♯/G♭, G, G♯/A♭, A, A♯/B♭ and
B. Each time you hit a 'C', the pattern repeats an octave higher.
A note is uniquely identified by 1) the letter, including any sharps or flats, and 2) the octave, which is a number from 0 to 8. The first three notes of the keyboard, are
A0, A♯/B♭ and
B0. After this comes the full chromatic scale on octave 1.
C1, C♯1/D♭1, D1, D♯1/E♭1, E1, F1, F♯1/G♭1, G1, G♯1/A♭1, A1, A♯1/B♭1 and
B1. After this comes a full chromatic scale on octaves 2, 3, 4, 5, 6, and 7. Then, the last note is a
Each note corresponds to a frequency in the 20-4100 Hz range. With
A0 starting at exactly 27.500 hertz, each corresponding note is the previous note times the twelfth root of two, or roughly 1.059463. A more general formula is:
where n is the number of the note, with A0 being 1. (More information here)
Write a program or function that takes in a string representing a note, and prints or returns the frequency of that note. We will use a pound sign
# for the sharp symbol (or hashtag for you youngins) and a lowercase
b for the flat symbol. All inputs will look like
(uppercase letter) + (optional sharp or flat) + (number) with no whitespace. If the input is outside of the range of the keyboard (lower than A0, or higher than C8), or there are invalid, missing or extra characters, this is an invalid input, and you do not have to handle it. You can also safely assume you will not get any weird inputs such as E#, or Cb.
Since infinite precision isn't really possible, we'll say that anything within one cent of the true value is acceptable. Without going into excess detail, a cent is the 1200th root of two, or 1.0005777895. Let's use a concrete example to make it more clear. Let's say your input was A4. The exact value of this note is 440 Hz. Once cent flat is
440 / 1.0005777895 = 439.7459. Once cent sharp is
440 * 1.0005777895 = 440.2542 Therefore, any number greater than 439.7459 but smaller than 440.2542 is precise enough to count.
A0 --> 27.500 C4 --> 261.626 F#3 --> 184.997 Bb6 --> 1864.66 A#6 --> 1864.66 A4 --> 440 D9 --> Too high, invalid input. G0 --> Too low, invalid input. Fb5 --> Invalid input. E --> Missing octave, invalid input b2 --> Lowercase, invalid input H#4 --> H is not a real note, invalid input.
Keep in mind that you don't have to handle the invalid inputs. If your program pretends they are real inputs and prints out a value, that is acceptable. If your program crashes, that it also acceptable. Anything can happen when you get one. For the full list of inputs and outputs, see this page
As usual, this is code-golf, so standard loopholes apply, and shortest answer in bytes wins.