When musicians work with additive synthesis, they will often wrap envelopes around the sounds, and even around each added sine wave. An envelope changes the amplitude over time: It might start out small, then grow (rapidly or slowly), then hold at a certain value during the sound, and then drop before the sound ends. That kind of pattern is sometimes called the attack-sustain-decay (ASD) envelope. Pianos tend to attack quickly then decay quickly. Flutes tend to attack slowly and sustain as long as you want. Try implementing that for the sine and square wave generators.

What will be an ideal response?

```
/**
* Method to c r e a t e a piano sound from a s ine wave
* @param f r e q the f r e quenc y o f the sound wave
* @param maxAmplitude the maximum ampl i tude ( volume ) of the wave
*/
public s tat ic Sound createPianoSoundSineWave ( int f r eq , int maxAmplitude )
{
Sound sineWave = Sound . createSineWave ( f r eq , maxAmplitude ) ;
int l en = sineWave . getLength ( ) ;
sineWave . de c r eas eVolBe for e Index ( ( int ) ( l en * 0 . 2 ) , 1 0 ) ;
sineWave . de c r eas eVolAf t e r Index ( ( int ) ( l en * 0 . 8 ) , 1 0 ) ;
return sineWave ;
}
/**
* Method to c r e a t e a f l u t e sound from a s ine wave
* @param f r e q the f r e quenc y o f the sound wave
* @param maxAmplitude the maximum ampl i tude ( volume ) of the wave
*/
public s tat ic Sound createFluteSoundSineWave ( int f r eq , int maxAmplitude )
{
Sound sineWave = Sound . createSineWave ( f r eq , maxAmplitude ) ;
int l en = sineWave . getLength ( ) ;
sineWave . de c r eas eVolBe for e Index ( ( int ) ( l en * 0 . 5 ) , 1 0 ) ;
return sineWave ;
}
/**
* Method to modi fy a sound to s t a r t at lower v o l and inc r e a s e
* the volume l i n e a r l y in num s t e p s to the volume at the pas sed inde x
* @param inde x the inde x to s top the change at
* @param numSteps the number of s t e p s to inc r e a s e
*/
public void de c r eas eVolBe for e Index ( int index , int numSteps )
{
int l e n g t h I n t e r v a l = index / numSteps ;
int cur r Index = 0 ;
double increment = 1 . 0 / numSteps ;
double f a c t o r = increment ;
int value ;
for ( int j = 0 ; j < numSteps ; j++)
{
for ( int i = 0 ; i < l e n g t h I n t e r v a l ; i++)
{
cur r Index = j * l e n g t h I n t e r v a l + i ;
value = this . getSampleValueAt ( cur r Index ) ;
this . setSampleValueAt ( cur r Index , ( int ) ( value * f a c t o r ) ) ;
}
f a c t o r = f a c t o r + increment ;
}
}
/**
```

Computer Science & Information Technology

You might also like to view...

When adjusting the alignment of a table row, the Align Center button can be found on the ________ tab

Fill in the blank(s) with correct word

Computer Science & Information Technology

Which is not a typical application of queues?

a. Routing packets in a computer network. b. File server handling file access requests from many clients. c. High-speed sorting. d. Print spooling.

Computer Science & Information Technology