<< Chapter < Page
  Accessible objected-oriented     Page 13 / 17
Chapter >> Page >

Listing 22 shows a for loop that is used to compute the amplitude at the current time in the current sample including the possibility oftwo or more keys being pressed. When two or more keys are pressed simultaneously, the amplitude of the output is computed as the sum of theamplitudes of the individual keys. This code shouldn't require an explanation beyond that provided by the embedded comments. Note that the name of the key isused to extract the frequency for that key from the Hashtable object that was created by the method named getPiano that was called in Listing 19 .

Listing 22 . Process each piano key that is pressed.
for(int element = 1;element<array.length;element++){ //Iterate on the piano keys defined in the array.//Get the name of the next key and make sure that it is upper-case. String noteName = array[element].toUpperCase(); try{//Use the noteName and get the corresponding frequency from the // hashtable. Note that results are retrieved from the list as// type Object and must be cast to the correct type. freq = (double)piano.get(noteName);}catch(java.lang.NullPointerException ex){ ex.printStackTrace();System.out.println("noteName: " + noteName); }//end catch//Compute the amplitude for this note at this time and add it to// the sum unless it is a musical rest. if(!noteName.equals("X")){//This is not a musical rest. sum += Math.cos(2*Math.PI*(freq)*time);}//end if //Go back to the top of the loop and get the next key from the// array, if any. }//end for loop

Use a scale factor to shape the note

If you press a key on some electronic keyboards and hold the key down, the sound intensity will remain constant with time. However, if you press a pianokey and hold it down, the sound intensity decreases over time. The code in Listing 23 attempts to achieve that same effect.

Listing 23 . Use a scale factor to shape the note.
scaleFactor = gain*((beats*audioParams.sampleRate/beatsPerSec) - cnt) /(beats*audioParams.sampleRate/beatsPerSec);//Scale the amplitude value and put it into the output array. byteBuffer.putShort((short)(scaleFactor*sum));//Go back and compute the next sample values for this set of keys // until the note duration is satisfied.}//end for loop //Go back, retrieve, and process the next set of keys for a given// duration value. }//end while}//end makeMusic method

At this point in the creation of the audio sample, the amplitude values for all the keys that were pressed simultaneously have been added together.The resulting amplitude value is scaled so that each note has a maximum amplitude at the beginning and a zero amplitude when the duration has expired. Alinear scale factor is used to accomplish this.

The remaining code in Listing 23 shouldn't require an explanation.

Listing 23 ends the for loop that began in Listing 21 , ends the while loop that began in Listing 20 , and signals the end of the makeMusic method.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Accessible objected-oriented programming concepts for blind students using java. OpenStax CNX. Sep 01, 2014 Download for free at https://legacy.cnx.org/content/col11349/1.17
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?

Ask