The objective of this mini-lab is to practice working with sound in JES.
def changeVolume(sound, factor):
newSound = duplicateSound(sound)
for s in getSamples(newSound):
val = getSampleValue(s)
setSampleValue(s, val * factor)
return newSound
changeVolume function, every sample value is
multiplied by the same factor. There is nothing to stop us from
changing the volume of different parts of the sound by different
amounts. In particular, we will write a function that leaves the
volume at the beginning of the sound unchanged, and then slowly
reduces the volume over time. Your function should take two
parameters: the sound, and the largest factor that the volume should
be reduced by. Your function will return a new sound that fades out
by the desired factor. For example, after the command:
>>> faded = fade(sound, 8.0)The variable
faded should contain a sound that is
unchanged at the beginning, and quieter by a factor of 8.0 at the end.
In order to accomplish this, you can create a variable,
curFactor, that represents the amount that the current sample
will be reduced by, and increase the value of that variable after each
sample is processed. In other words, instead of the line:
setSampleValue(s, val * factor)from the function above, your new function will have a line like the following:
setSampleValue(s, val / curFactor)Note: When calling this function, the value used for the largest factor should be a floating point number. Otherwise, Python will use integer division, and your sound will not change.
Design Questions: What should the initial value ofcurFactorbe? (What amount can you divide a sample value by without changing the volume at all?) By how much shouldcurFactorbe increased after every sample? Keep in mind that this will depend on the total number of samples in the sound, which can be obtained using thegetNumSamplesfunction.
fadeUp that slowly increases the volume of a sound by the
desired amount. In other words, after this expression is executed:
>>> faded = fadeUp(sound, 8.0)The variable
faded should contain a sound that is
unchanged at the beginning, and louder by a factor of 8.0 at the end.