Computer Science 111 - Fall Semester, 2006 - Homework #3
due: by midnight on Wednesday September 28

Part 1. Setting up the image and sound files Your task in this week's programming homework is to use definite loops (for loops) to manipulate images and sounds using jython and the jes media.py module. Make sure to boot into Linux!
We can manipulate jpeg images (files end with .jpg) and wav files (.wav) using the tools in media.py. You will need 4 images and 4 very short sound files. You are welcome to use my image and sound files. My theme is a Star Wars theme. You can also choose your own theme and find files on the web. The sound files must be in a subdirectory called sounds (in the Desktop/JES/ directory). There is already a subdirectoy called images. You must place your images there.
If you want to use mine, run the Linux commands below that change directories to Desktop/JES, make a sounds directory, change to the sounds directory, get all the sound (.wav) files, change back up one level, change to the images directory, and get all the images. Then change up one directory level (back to Desktop/JES).
cd Desktop/JES
mkdir sounds
cd sounds
getcopy sounds/*.wav
cd ../
cd images
getcopy images/*.jpg
cd ../
Type ls sounds then ls images to see all the files you have now!
Part 2. A picture show. After you open a file called hw3.py using emacs, enter the information needed at the top to document it. Then, since we are using the media.py module as well as the sleep function from the time module, place these two python statements at the top of your file:
from media import *
from time import sleep
Next, start your definition of the main function, and make a variable called path and set it equal to the string "images".
path = "images/"
Use an assignment statement to define a variable called f1, and set it equal to:
f1 = path+"starWars1.jpg"
The value stored in f1 will actually be
"images/" + "starWars1.jpg" which is simply
"images/starWars1.jpg"
Now assign to variable p1:
p1 = makePicture(f1)

Now call the function show(), passing it p1:
show(p1)
Save your file and call jes as you did in lab:
[111a@localhost JES]$ jes
Jython 2.1 on java1.5.0_07 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>>
import hw3 and then call hw3.main(). Make sure this part works before moving on. Important Note: Jython keeps around old information about a file when you import it. If hw3.py has errors and you fix them in emacs, make sure you EXIT jython by typing ctrl-d and ctrl-c and then rerun it by typing
[111a@localhost JES]$ jes
before importing hw3 again. Otherwise the changes you made in emacs are not imported.
Part 3. Add more pictures: Now create 3 more variables, f2, f3, and f4 that have strings of jpg files stored in them. Use the path variable as you did with f1. And make 3 more variables p2, p3, p4 that are assigned values using the makePicture() function.
Then show each one by calling show() and passing it the correct p-variable. In between each call to show(), call the function sleep(). Sleep needs to be passed how many seconds you want the program to stall. e.g. show(2) makes it stall for 2 seconds before showing the next picture. You decide you how long to have it sleep. It can be passed floating point numbers as well such as 1.5 for 1 and one-half seconds.
Save and exit emacs, run jes, import hw3 and call hw3.main(). Make sure it works properly before moving on to the next part. Remember the above note!
Part 4. Add sounds: You .wav files must be stored in the sounds sub-directory. Please make sure to use wav files that are really short. No more than a few seconds each. You can reuse the variable path:
path = "sounds/"

Set up variable sf1 to store the first sound file name:
sf1 = path + "yodalaff.wav"
Do this for four sound files, using variables sf1, sf2, sf3, sf4.
We're going to use a for loop to call makeSound() on each of these files and we'll store the results in a list!
First set the variable sounds equal to the empty list
sounds = []
Construct a for loop that has as a sequence a list that contains variables sf1, sf2, sf3, and sf4. Make your loop variable i.
In the body of the for loop, you'll have just one statement.
  1. In the statement you'll call a new list function we haven't seen yet. It appends whatever you pass it to the list. The second statement is
    sounds.append(makeSound(i))
    
Notice that the index variable is passed into the function makeSound().
Now construct another for loop that goes through the list stored in variable sounds, and has two statements in its body.
  1. The first one is a call to the function play(), and it must be passed each sound to play. You have to figure that out, but remember that the list/sequence used in the for loop is the one you just made, stored in the variable sounds.
  2. The second statement calls the sleep() function. You decide how many seconds to pause between sounds.

Make sure this works before moving on to the next part. Remember that you may need to adjust the volume using kMix as you did in lab.
Part 5. Add lots of red to an image: The function getPixels() makes a list of all the picture elements (pixels) in an image. Very handy! Call the function getPixels() and make sure to pass it the image stored in variable p4. Assign its return value to a variable called pixels. This will be a sequence of pixels we can use in a for loop.
So make a for loop that uses the variable pixels as its sequence. The for loop will have one statement in its body:
  1. The statement is a call to function setRed(). setRed() must be passed two values: one pixel, and one value between 0 and 255. 255 is the highest red value possible. So make the second value passed into setRed() be 255.
After the loop, call repaint(p4) to see the changed pixels.
Make sure this all works before moving to the next part.
Part 6. Sounding off! Choose your favorite sound. Play it 30 times, with a 0.1 second delay between each time. Notice that if the sound takes longer that 0.1 seconds to play, the sound will overlap itself. That's fine. Of course, use a for loop!
Part 7. Submitting
Since these files are all in your Desktop/JES/ subdirectory, make sure you are in that directory when submitting your homework files.
Please make sure you do not call main() at the end of your program.
Also make sure to include the filename, description of the program, your name, account number, and date at the top, in comments.
Name the program hw3.py. Make sure you are in directory Desktop/JES, and submit hw3.py by the due date with the command
submit homework3 hw3.py
You must also submit all of your .jpg and .wav files, even if you used some of mine.
cd images
submit homework3 *.jpg
cd ../sounds
submit homework3 *.wav
Part 8. Pair programming Make sure that your partner has all of the image and sound files and a copy of hw3.py. If you used my images and sounds, the partner can use getcopy to get them. Then just use sftp as we did in the first lab to transfer a copy of hw3.py the the partner. sftp can also transfer image and sound files.