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.

Note: It is unclear what “that” refers to in the last sentence of the question. Due to its proximity, the best assumption is that it refers to the previous sentence, especially given that that the sine and square generations can sustain for “as long as you want”.

```
def sineWave(freq,amplitude,secs):
# Get a blank sound
buildSin = makeEmptySoundBySeconds(secs)
# Set sound constant
sr = getSamplingRate(buildSin)
interval = 1.0/freq
samplesPerCycle = interval * sr
maxCycle = 2 * pi

for pos in range (0,getLength(buildSin)):
amp = amplitude
if pos<500:
amp = (pos/500.0)*amplitude
rawSample = sin((pos / samplesPerCycle) * maxCycle)
sampleVal = int(amp*rawSample)
setSampleValueAt(buildSin ,pos,sampleVal)
return buildSin
def squareWave(freq, amplitude, seconds):
# Get a blank sound
square = makeEmptySoundBySeconds(seconds)

# Set sound constant
sr = getSamplingRate(square) # sampling rate

#Set music constants
samplingRate = getSamplingRate(square)
# Build tools for this wave
# seconds per cycle: make sure floating point
interval = 1.0 * seconds / freq
# creates floating point since interval is fl point
samplesPerCycle = interval * samplingRate
# we need to switch every half-cycle
samplesPerHalfCycle = int(samplesPerCycle / 2)
sampleVal = amplitude
s=1
i=1

for s in range (0, getLength(square)):
amp = sampleVal
if s<500:
amp = (s/500.0)*sampleVal

# if end of a half-cycle
if (i > samplesPerHalfCycle):
# reverse the amplitude every half-cycle
sampleVal = sampleVal * -1
# and reinitialize the half-cycle counter
i=0
setSampleValueAt(square,s,amp)
i=i+1
return(square)
```

Computer Science & Information Technology

You might also like to view...

The CP/M, which stands for ________, was the first operating system designed for the Intel 8080 chip, the first processor for PCs

Fill in the blank(s) with correct word

Computer Science & Information Technology

What technology converts light into digital information?

A) Webcam B) Microphone C) Fitness monitor D) Sound card

Computer Science & Information Technology