<< Chapter < Page | Chapter >> Page > |
In case you have forgotten, a Hashtable object is a data structure that ties keys to values . (Items in a Hashtable are called keys but this is unrelated to the keys on a piano keyboard.) In this case, the keys in the Hashtable are the note names andthe values are the frequency values that are tied to those not names. The program can ask the Hashtable object to provide the frequency for a given note name. Forexample, the piano key commonly known as middle-C is named C4 . The program can ask the Hashtable for the frequency value for the key C4 and the Hashtable will return 261.63 Hz.
I mentioned earlier that I am not a musician and I am clearly not a musical theoretician. What I about to report to you is what I have learned during thepast few weeks while working on this program.
In order for multiple instruments to play and sound well together, they must be tuned using compatible frequencies regardless of the type of instrument.There is a set of six notes or keys, moving from left to right on the piano keyboard, where there is general agreement as to the frequency that should betuned for each piano key:
Note that each frequency is double the frequency above it in this list. This doubling in frequency is known as an octave. There is one octave between A2 andA3, one octave between A3 and A4, etc. As you move to the right on the piano keyboard, the frequency doubles between a particular piano key and the next keyto the right with the same name (A, B, C, etc.)
While there appears to be general agreement as to the frequencies that should be assigned to the A keys, there appears to be something less thantotal agreement as to the frequencies for the keys in between. However, I believe that I understand the formula that is often usedto tune the keys on a piano. The frequency for any key is multiplied by a factor that is the twelfth root of two (1.05946309436) to obtain the frequency for the key immediately to its right.
The key sequence from any A-key to the next A-key to the right is as follows:
A, A#, B, C, C#, D, D#, E, F, F#, G, G#, A
If you count the number of keys, there are twelve steps from an A-key to the next A-key to the right. That is one octave, meaning that the frequency mustdouble from any A-key to the next A-key to the right. If you multiply the twelfth root of two by itself twelve times, the result will be two. If you multiply thefrequency of any key by the twelfth root of two to get the frequency for the key to the right, and you do that twelve times, the frequency after twelve stepswill have doubled.
Now that we know what we need to do, let's look at the code that will do it.
The getPiano method begins in Listing 16 . This method creates and returns a reference to a Hashtable object containing the name (key) and frequency (value) of every note from A2 (110 Hz) at the low end to A7 (3520 Hz) at the high end. This 61-note, five-octave range is close to the limits of the frequency range that I can hear on my computer. In fact, I canonly barely hear A2. (Click AllNotes to hear each of the 61 notes played from lowest to highest.)
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?