# 0.10 Jbs2060-a player piano simulator  (Page 13/17)

 Page 13 / 17

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.

#### Questions & Answers

what is math number
Tric Reply
x-2y+3z=-3 2x-y+z=7 -x+3y-z=6
Sidiki Reply
Need help solving this problem (2/7)^-2
Simone Reply
x+2y-z=7
Sidiki
what is the coefficient of -4×
Mehri Reply
-1
Shedrak
the operation * is x * y =x + y/ 1+(x × y) show if the operation is commutative if x × y is not equal to -1
Alfred Reply
An investment account was opened with an initial deposit of \$9,600 and earns 7.4% interest, compounded continuously. How much will the account be worth after 15 years?
Kala Reply
lim x to infinity e^1-e^-1/log(1+x)
given eccentricity and a point find the equiation
Moses Reply
12, 17, 22.... 25th term
Alexandra Reply
12, 17, 22.... 25th term
Akash
College algebra is really hard?
Shirleen Reply
Absolutely, for me. My problems with math started in First grade...involving a nun Sister Anastasia, bad vision, talking & getting expelled from Catholic school. When it comes to math I just can't focus and all I can hear is our family silverware banging and clanging on the pink Formica table.
Carole
I'm 13 and I understand it great
AJ
I am 1 year old but I can do it! 1+1=2 proof very hard for me though.
Atone
hi
Adu
Not really they are just easy concepts which can be understood if you have great basics. I am 14 I understood them easily.
Vedant
find the 15th term of the geometric sequince whose first is 18 and last term of 387
Jerwin Reply
I know this work
salma
The given of f(x=x-2. then what is the value of this f(3) 5f(x+1)
virgelyn Reply
hmm well what is the answer
Abhi
If f(x) = x-2 then, f(3) when 5f(x+1) 5((3-2)+1) 5(1+1) 5(2) 10
Augustine
how do they get the third part x = (32)5/4
kinnecy Reply
make 5/4 into a mixed number, make that a decimal, and then multiply 32 by the decimal 5/4 turns out to be
AJ
how
Sheref
can someone help me with some logarithmic and exponential equations.
Jeffrey Reply
sure. what is your question?
ninjadapaul
20/(×-6^2)
Salomon
okay, so you have 6 raised to the power of 2. what is that part of your answer
ninjadapaul
I don't understand what the A with approx sign and the boxed x mean
ninjadapaul
it think it's written 20/(X-6)^2 so it's 20 divided by X-6 squared
Salomon
I'm not sure why it wrote it the other way
Salomon
I got X =-6
Salomon
ok. so take the square root of both sides, now you have plus or minus the square root of 20= x-6
ninjadapaul
oops. ignore that.
ninjadapaul
so you not have an equal sign anywhere in the original equation?
ninjadapaul
hmm
Abhi
is it a question of log
Abhi
🤔.
Abhi
I rally confuse this number And equations too I need exactly help
salma
But this is not salma it's Faiza live in lousvile Ky I garbage this so I am going collage with JCTC that the of the collage thank you my friends
salma
Commplementary angles
Idrissa Reply
hello
Sherica
im all ears I need to learn
Sherica
right! what he said ⤴⤴⤴
Tamia
hii
Uday
hi
salma
hi
Ayuba
Hello
opoku
hi
Ali
greetings from Iran
Ali
salut. from Algeria
Bach
hi
Nharnhar
A soccer field is a rectangle 130 meters wide and 110 meters long. The coach asks players to run from one corner to the other corner diagonally across. What is that distance, to the nearest tenths place.
Kimberly Reply
Jeannette has \$5 and \$10 bills in her wallet. The number of fives is three more than six times the number of tens. Let t represent the number of tens. Write an expression for the number of fives.
August Reply
What is the expressiin for seven less than four times the number of nickels
Leonardo Reply
How do i figure this problem out.
how do you translate this in Algebraic Expressions
linda Reply
why surface tension is zero at critical temperature
Shanjida
I think if critical temperature denote high temperature then a liquid stats boils that time the water stats to evaporate so some moles of h2o to up and due to high temp the bonding break they have low density so it can be a reason
s.
Need to simplify the expresin. 3/7 (x+y)-1/7 (x-1)=
Crystal Reply
. After 3 months on a diet, Lisa had lost 12% of her original weight. She lost 21 pounds. What was Lisa's original weight?
Chris Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

### Read also:

#### Get the best Algebra and trigonometry course in your pocket!

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?

 By By By By By Rhodes