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)
```
You might also like to view...
Which of the following types of variables is capable of holding the information contained in a table that has four rows and four columns?
(A) one-dimensional arrays (B) simple variables (C) single-subscripted variables (D) double-subscripted variables
It is not possible to import an iCalendar file.
Answer the following statement true (T) or false (F)
What is the difference between continuous tone art and line art?
What will be an ideal response?
A ____ displays table information with a more formal, businesslike appearance.
A. report B. query C. form D. field