This lab is intended to develop your understanding of encodings and how to exploit them. Specifically, you will work with a sound encoding, but the ideas are more widely applicable. Warning: If you don't use the linux server for this lab you will have to be able listen to .wav files on your personal machine. (The Chrome browser will play them for you, for instance.)
When you are done, you should be able to do the following:
Place these files in a lab8 folder. Before leaving lab, zip up the lab8 folder and hand the zip file in.
This lab uses module snd110 which is in snd110.py. Download this file into your working folder.
from snd110 import write_wave, read_wave, sine_tone # --makes a Python list that represents the sound. # --each element of the list is 1 sample (at a # --sampling rate of 44100 Hz # sine_tone(frequency, duration, amplitude) # frequency is in Hertz (cycles per second) # duration is in seconds # amplitude from 0 to 1 controls loudness # --takes the python list and converts it to a # --.wav sound format. It will be stored in the # --working directory under the given file name) write_wave("test.wav", sine_tone(1000, 0.5, 0.3))
Note that the image above is not to scale. There should actually be many more samples given the time period, but this is simply a graphical understanding of how sound data is stored.
In the file speed.py, write a function named speed(sound, factor) that changes the speed of a sound.
Here's how it works: each number in the list is a sample, and the sound is played at the rate of 44100 samples per second. If we take a given sound and repeat some of the samples at regular intervals, we get a slowed-down version of the sound. And if we skip some of the samples at regular intervals, we get a speeded-up version. So you can implement speed(sound, factor) as follows:
Test the function using the following. You can use this voice.wav file. (If your browser plays rather than downloading this link, try right clicking on the link and choose ``Save Link Target As...''.)
write_wave("speed75.wav", speed(read_wave("voice.wav"), 0.75)) write_wave("speed200.wav", speed(read_wave("voice.wav"), 2.0))
What factor would you pass to speed if you wanted the sound to be slowed down by one half? Sped up 120%?