CPSC120A
Fundamentals of Computer Science I

Lab 30

Sound

Use the command line to create a new directory called lab30 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Sound Module

Computers represent sounds as lists of numbers. The rate at which the numbers change effects the sounds that we hear when we play audio files. The sound.py module has functions that make converting sound files into Python lists easier. For more information on the module and examples of how to use it, please read the sound module documentation. By modifying the numbers it is possible to generate many cool sound effects.

Sound Stretching

When DJ's mix tracks they will gradually speed up or slow down a track so that there are seamless transitions between songs. Speeding up or slowing down a sound without changing its pitch is difficult, but stretching a sound without regard for the pitch is easy and produces some really interesting results. If you've ever wanted to sound like Alvin and the Chipmunks, nows your chance!

Details

Write a function called speed_up(input_sound_name, output_sound_name) in a file called sound_stretch.py. The function should load the sound in the file input_sound_name, speed up the sound so that it is half as long, and save the sound to a file called output_sound_name. You can speed up a sound by replacing each pair of samples with the average of the two samples.

Write a second function called slow_down(input_sound_name, output_sound_name) in the same file. The function should load the sound in the file input_sound_name, slow down the sound so that it is twice as long, and save the sound to a file called output_sound_name. You can slow a sound down by adding samples between each pair of samples with the average of the two samples.

Example

If the a sound has the following samples:
[-1.0, -0.5, 0.0, 0.5, 1.0, 0.5]
then it become the following when sped up:
[-0.75, 0.25, 0.75]
and would become the following when slowed down:
[-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0, 0.75, 0.5]

Hint

There are many ways to write both of these functions. One of the easier ways is to use the accumulator pattern to accumulate a new list of samples that represents the new sound.

Speeding up: Create a list to hold the new sound that is initially empty. Add a for loop with a range over the even indices of the sample list. For each iteration of the loop, average the samples at index i and i + 1 and append them to the new sound list.

Slowing down: Create a list to hold the new sound that is initially empty. Add a for loop with a range over all samples except the last sample. For each iteration of the loop, average the samples at index i and i + 1 and append the sample at index i and the average to the new sound list.

 

Challenge

It was postulated that people like Ozzy Osbourne and Metallica published music with embedded messages in them. The claim was that these were reversed messages for satanic worship. No evidence of such messages were ever found (except for joking messages in the occasional Weird Al song).

Write a function called reverse, which takes a sound and reverses it. Can you find any hidden messages in the sounds you found? Can you create your own hidden messages?

Echo

Many electric guitarists love to change the sound of their guitars using effects pedals. The effects pedal sits between the output of the guitar and the input of the amplifier and can be used to create very different sounds. One simple and commonly used effect is echo.

Details

Write a function called echo(input_sound_name, output_sound_name, delay) in a file called echo.py. The function should load the sound in the file input_sound_name, add an echo to the sound, and save the sound to a file called output_sound_name.

Add an echo to a sound by averaging every sample in a sound with the sample that occurs delay samples earlier. To produce a good echo set the delay to somewhere between 1/300th of a second and 1 second. Use the input file's sample rate to compute a suitable delay. For example, if a sound has a sample rate of 28000 samples per second then a delay of 14000 would be a half second delay. Note that in order to get the echo to continiously repeat at lower volumes like a real echo, the echo should be computed by modifying the input sample list. Do not create a new list for the echo sound or the echo will only repeat once.

Example

If the a sound has the following samples:
[-1.0, -0.5, 0.0, 0.5, 1.0, 0.5]
then it becomes the following with a 2 sample delay:
[-1.0, -0.5, -0.5, 0.0, 0.25, 0.25]

Hint

Instead of creating a new sound that contains the echo, it is easier to modify the input sample list. Add a for loop with a range over all samples. For each iteration replace the sample at index i with the average of the sample at index i and the sample at index i - delay. If there is no sample at index i - delay, do not modify the sample at index i.

 

Challenge

Create a function that adds a chorus effect. A chous effect makes a sound seem like it is being played in a large room where sounds bounce off of walls at different distances. The effect is like a chorus singing together where each person begins and ends the sounds at slightly different times.

To produce the chorus effect, recombine a single sound with itself multiple times. Each of the different sounds should be shifted by a small amount from the orginal. For example to create a chorus of three: sample[i] = sample[i - 5] * 0.33 + sample[i] * 0.33 + sample[i + 5] * 0.33

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.