The objective of this mini-lab is to practice working with sound in the jes4py library.
Note: Sounds tend to be recorded/saved in many more formats than
images. The functions in jes4py that work with sounds are meant to be used
with .wav
files. Even some of the .wav
files
you save from the internet may not work properly with our functions in
jes4py. It is recommended that you download some sounds from the SoundFiles
folder in the MediaSources
Directory to use in testing your functions first. Then feel free
to test with other sounds, but keep in mind the format of the sounds may
not be compatible with our programs.
Create a new function called changeVolume
that allows us to
increase or decrease the volume of a sound by any factor. This function
should take a sound and a factor as parameters. It should duplicate the
sound, and then change the sample values of the new sound appropriately
so that the volume changes. At the end of the function, the new sound
should be returned.
Test your new function to see that it can increase or decrease the volume of the sound. Remember to use a .wav file when you select a sound to use. If you need some sounds to pratice with, there are some available in the Media Sources directory.
increaseAndDecrease
that doubles the volume of the
first half of a sound and decreases the volume of the second half of
a sound by 40%. Type in this function (or copy and paste it) and
test it with several different sounds. Do you hear how the volume
changes?
decreaseAndIncrease
that decreases the volume by 50% in the first half of a sound, and
increases the volume by 50% in the second half of the sound. This
function should take a sound as a parameter, should duplicate the
sound, make changes to the new sound, and then return the new sound. Think
about how this function may be similar/different from the
increaseAndDecrease
function.soundGraph
function below takes in a sound and plots the wave form graph. Once the graph is shown, it is
saved as a .jpg file. Use this function to obtain a wave form graph of a original sound and a modified sound.
Do you see what you would expect?
#Function takes as input a sound and generates a wave form graph for viewing.
#This graph is saved in the main project folder as a jpg.
def soundGraph(aSound):
signal = np.frombuffer(aSound.getBuffer(), dtype ="int16")
# gets the frame rate
f_rate = aSound.getSamplingRate()
# to Plot the x-axis in seconds
# you need get the frame rate
# and divide by size of your signal
# to create a Time Vector
# spaced linearly with the size
# of the audio file
time = np.linspace(
0, # start
len(signal) / f_rate,
num = len(signal)
)
# using matplotlib to plot
# creates a new figure
plt.figure(1)
# title of the plot
plt.title("Sound Wave")
# label of x-axis
plt.xlabel("Time")
# actual plotting
plt.plot(time, signal)
# you can also save
# the plot using
pictureName = aSound.getFileName()
pictureName = pictureName[pictureName.rfind('\\') + 1:pictureName.rfind('.')] + "_Wave_Graph.jpg"
plt.savefig(pictureName)
# shows the plot
# in new window
plt.show()
gradualDecrease
that
decreases the volume of the first third of a sound by 30%, then
decreases the volume of the second third of a sound by 60%, and
finally decreases the last third of a sound by 90%. Think about
what the starting and ending values of your ranges will be.
soundGraph
function
to see that it is doing what it should be
doing.normalize
. This function increases the volume of a
sound as much as possible without introducing any clipping. Type in
this function and test it with several sounds.
.jpg/.png
. Include the wave form graph associated with each sound file.