Question: First things first: download a zipped module pythonSounds.zip and try out the next function, which should already be in your Assignment5_Template.py file in this zip

First things first: download a zipped module pythonSounds.zip

and try out the next function, which should already be in your Assignment5_Template.py file in this zip file.

You can run it with test():

# a function to make sure everything is working 
def test(): 
 """ a test function that plays swfaith.wav 
 You'll need swfailt.wav in this folder. 
 """
 play( 'swfaith.wav' )

For this to work, your Python will need to support sound (every version I've tested does). Let me know if yours does not. Also, you'll need the swfaith.wav file in the folder in which Assignment5_Template.py is located. As long as you're in the original folder, all of this should be the case. If not, go grab all of those files that came with Assignment5_Template.py and copy them over to whichever folder you're working in. If you work with Eclipse PyDev, you should copy and paste into the src folder by clicking it and choosing a paste option, not directly pasting out into the folder (see below).

First things first: download a zipped module

Before we go on, you'll need a bit of background information on audio data. After getting a basic understanding of an audio file, you'll have a chance to write a number of audio-processing function Depending on the format, the actual audio data might be encoded in many different ways. One of the most basic is known as pulse code modulation (PCM), in which the sound waves are sampled every so often and given values in the range -128 to 127 (if 1 byte per sound sample is used) or -32768 to 32767 (if there are 2 bytes for each sample). Wikipedia explains it here.

The .wav file format encodes audio in basically this way, and the cross-platform program Audacity is an excellent tool for visualizing the individual PCM samples of an audio file. You don't need Audacity for this problem, but it runs on Windows and Macs and is fun to play around with if you'd like to. Audacity can also convert to .wav from .mp3 and many other formats.

Here are two examples to start acquiring and manipulating sound data. They are implemented in Assignment_Try these.

Example #1: changeSpeed

This function is implemented in your file. Read over this example and try it out on the three sound files provided:

 changeSpeed( "swfaith.wav", 44100 ) # fast Vader
... some printing ...
 changeSpeed( "spam.wav", 11025 ) # slow Monty Python 
... some printing ...
 changeSpeed( "swnotry.wav", 22050 ) # regular-speed Yoda
... some printing ... 

Let's review some points about the code and how it works:

The sound data is returned by the call to readwav in two parts using the call samps, sr = readwav(filename). Python is nice in that you can return any number of values from a function. Here, readwav is returning two values.

After that call, the variable samps should have a large list of raw pressure samples (floats). Don't print this listit can be too big and can slow down or choke the Python shell!

Also, after that call, the variable sr is an integer that represents the sampling rate, i.e., the number of samples that should be played per second for normal-speed playback.

Some printing happens, so that you can see a little bit of the data. Again, it's a bad idea to print all of the sound samples - some versions of Python can choke or freeze when printing so much data!

We already have the new sampling ratethat was the newsr argument. For consistency, we use the variable newsamps to label the old sound data samples. In this case they're not changing at all, but in later programs they will.

The code then writes newsamps and newsr out to a file, named out.wav, which will appear in the folder you're working in (replacing the old one if there was already an out.wav).

To finish, the function plays that new file, which may be a different speed than the original depending on the value of newsr.

Example #2: flipflop

Take a look at the middle part of this code, where the new sound samples are created from the old ones. In this case, the newsamps are a "flipflopped" version of the old samps.As a result, the sound's second half is placed before its first half. In building your audio-processing functions, use flipflop as a starting point.

Your task (#1) to produce a sound function: reverse

Implement a sound-handling function reverse with the following signature:

def reverse(filename):

such that reverse takes in a filename as did flipflop. Like flipflop, the sampling rate should not change, but the function should create a reversed set of sound samples and then handle them in the same way as the two examples above. That is, you'll want to write them to the file out.wav and then play that file. Remember to reverse the list samps, you can write samps[::-1] in Python! One example is:

 reverse('swfaith.wav') # redaV htraD sounds eerier but less intimidating
... lots of printing ... 

Note that this reverse function won't need to use one any the helper functions you wrote at your lab recitation session, but the next few will!

Your task (#2) to produce a sound function: volume

Implement a sound-handling function volume with the following signature:

def volume(filename, scale_factor):

such that volume takes in a filename as usual and a floating-point value scale_factor. Then, volume should handle the sound in the usual way, with the output file and played sound being scaled in amplitude (volume) by the scaling factor scale_factor. That is, each sample should be multiplied by scale_factor. Here, you could use the helper function scale you wrote at the beginning of the lab... Two examples are:

 volume( 'swfaith.wav', .1 ) # A calmer Darth... 
... lots of printing ... 
 volume( 'swfaith.wav', 10.0 ) # A caffeinated Darth! 
... lots of printing ... 

You'll notice that your hearing adjusts remarkably well to this function's changes in absolute volume, making the perceived effect considerably less than you might expect. You will also find that if you increase the volume too much, the sound becomes distorted, just as when an amplifier is turned up to 11.

Your task (#3) to produce a sound function: static

Implement a sound-handling function static with the following signature:

def static(filename, probability_of_static):

such that static takes in a filename as usual and a floating-point value probability_of_static, which you can assume will be between 0 and 1. Then, static should handle the sound in the usual way, with the output samples being replaced with the probability of probability_of_static. When they're replaced, the samples should simply be random values, uniformly chosen in the valid range from -32768 to 32767.

Here, you should use the helper function replace_some that you wrote earlier in the lab. You won't need randomize, because replace_some already uses it. Two examples:

 static('swfaith.wav', .05) # Vader, driving into a tunnel 
... lots of printing ... 
 static('swfaith.wav', .25) # Vader on dial-up from a galaxy far, far away
... lots of printing ... 

You might see how high you can increase the percentage of static until the original is no longer discernable. People adapt less well to this than to volume changes.

Your task (#4) to produce a sound function: overlay

Implement a sound-handling function overlay with the following signature:

def overlay(filename1, filename2):

such that overlay takes in two filenames as usual, and it creates a new sound that overlays the two. The result should be as long as the shorter of the two. (Drop any extra samples, just as in add_scale_2.)

So. use your add_scale_2 helper function to assist with this! That way, you can adjust the relative loudness of the two input files. You are welcome, but certainly not required, to add more input arguments to your overlay function so that you can change the relative volumes on the fly (or crop the sounds on the fly, which is a bit more ambitious).

Remember that add_scale_2(samps1, samps2, 0.5, 0.5) must take lists (samps) as input -- not filenames, which are simply strings! The samps are lists of the raw sound data. One example is:

 overlay( 'swfaith.wav', 'swnotry.wav' ) # Vader vs. Yoda
... lots of printing ...

Your task (#5) to produce a sound function: echo

This one is more of a challenge -- implement a function that overlays a file with a shifted version of itself. Try writing a sound-handling function echo with the following signature:

def echo(filename, time_delay):

such that echo takes in a filename as usual and a floating-point value time_delay, which represents a number of seconds. Then, echo should handle the sound in the usual way, with the original sound being overlaid by a copy of itself shifted forward in time by time_delay.

To do the overlaying, you'll want to use add_scale_2, as before. To handle the time-shifting, notice that you can use the sampling rate to convert between the number of samples and time in seconds:

For example, if time_delay is 0.1 and the sampling rate is 22050, then the number of samples to wait is 2205

Similarly, if time_delay is 0.25 and the sampling rate is 44100, then the number of samples to wait is 11025

Here is a hint on how to "wait samples": the easiest way to add "blank space" or "blank sound" in front of samps is to concatenate (add a list of) zeros to the front of the list samps. For example,

samps2 = [0]*42 + samps

would "wait" 42 samples (include 42 blank-sound samples) at the start of the sound data samps. You'll probably want a value other than 42! So, your task here is to figure out what integer you need instead of 42...

Also, do make sure it's an integer - you can use int( ) for that purpose.

By the way, there are other approaches that work for echo, as well!

Here is an example:

 echo( 'swfaith.wav', .1 ) # How many zeros are there to add!? 
... lots of printing ...

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!