Warning: before listening to any sound files through ear buds turn down the sound.

Goals

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:

1. Explain in terms of sampling and sampling rate, how splicing two lists of numbers together can create a longer sound out of two shorter ones.
2. Choose a number to speed up or slow down a sound by a predictable amount and explain your reasoning in terms of samples and sampling rate.

Deliverables

1. composer.py
2. speed.py

Place these files in a lab8 folder. Before leaving lab, zip up the lab8 folder and hand the zip file in.

Part 1 - Sounds and Songs

1.1 - TA Demonstration

• Explain that we will represent sounds using lists.
• Each list element is a sample, i.e., a floating point value from -1 to +1 representing the instantaneous sound pressure.
• There are 44100 samples per second. This is the sampling rate.
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.

• Show students how to write a song using composer.py.
• Play a sound (try aplay test.wav) or use Chrome

1.2 - Class Activity

• In composer.py, each student will select a note and a duration to make a class song. At the end, you will put together a song named section_(i).wav. Each song from each section will be posted on Piazza at the end of the day!
• Remember that when you hand in, this song should be in your composer.py file!

Part 2 - Manipulating Sounds

2.1 TA Demonstration

• Explanation of how to speed up and slow down sounds
• How does this change the frequency of the sound?

2.2 Student Activity

• 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:

1. Initially, set result to an empty list and set loc to zero.
2. While loc is less than the length of sound, use the integer part of loc to index sound and append the value to result. (Use the function int.)
3. (Still within the while loop:) Increment loc by factor. If factor is greater than one, this will step through the sound more quickly. If factor is less than one, this will step through sound more slowly.
4. When the loop exits, return the result.

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))