Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 177 Week 9 Recitation Slides

Similar presentations


Presentation on theme: "CS 177 Week 9 Recitation Slides"— Presentation transcript:

1 CS 177 Week 9 Recitation Slides
Sound Combination, Sampling and Building Bigger Programs Look at 1-fridayMix slides

2 ANY QUESTIONS?

3 Media File Path is Important
>>> setMediaPath() #must be called before getMediaPath >>> c4=makeSound(getMediaPath("bassoon-c4.wav")) >>> e4=makeSound(getMediaPath("bassoon-e4.wav")) >>> g4=makeSound(getMediaPath("bassoon-g4.wav")) >>> addSoundInto(e4,c4) >>> play(c4) >>> addSoundInto(g4,c4) 3

4 Doubling the frequency
Why +1 here? Here’s the piece that does the doubling def double(source): len = getLength(source) / 2 + 1 target = makeEmptySound(len) targetIndex = 0 for sourceIndex in range(0, getLength( source), 2): value = getSampleValueAt( source, sourceIndex) setSampleValueAt( target, targetIndex, value) targetIndex = targetIndex + 1 play(target) return target 4

5 Halving the frequency This is how a sampling synthesizer works!
def half(source): target = makeEmptySound(getLength(source) * 2) sourceIndex = 0 for targetIndex in range(0, getLength( target)): value = getSampleValueAt( source, int(sourceIndex)) setSampleValueAt( target, targetIndex, value) sourceIndex = sourceIndex + 0.5 play(target) return target Here’s the piece that does the halving 5

6 Sampling as an Algorithm
Think about the similarities between: Halving the sound’s frequency and scaling a picture larger. Doubling the sound’s frequency and scaling a picture smaller. 6

7 Recall these two functions
def half(filename): source = makeSound(filename) target = makeSound(filename) sourceIndex = 1 for targetIndex in range(1, getLength( target)+1): setSampleValueAt( target, targetIndex, getSampleValueAt( source, int(sourceIndex))) sourceIndex = sourceIndex + 0.5 play(target) return target def copyBarbsFaceLarger(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying sourceX = 45 for targetX in range(100,100+((200-45)*2)): sourceY = 25 for targetY in range(100,100+((200-25)*2)): color = getColor( getPixel(barb,int(sourceX),int(sourceY))) setColor(getPixel(canvas,targetX,targetY), color) sourceY = sourceY + 0.5 sourceX = sourceX + 0.5 show(barb) show(canvas) return canvas 7

8 Can we generalize shifting a sound into other frequencies?
This way does NOT work: def shift(source, factor): target = makeEmptySound(getLength(source)) sourceIndex = 0 for targetIndex in range(0, getLength( target)): value = getSampleValueAt( source, int(sourceIndex)) setSampleValueAt( target, targetIndex, value) sourceIndex = sourceIndex + factor play(target) return target 8

9 Watching it not work It’ll work for shifting down, but not shifting up. Why? >>> hello=pickAFile() >>> print hello /Users/guzdial/mediasources/hello.wav >>> lowerhello=shift(hello,0.75) >>> higherhello=shift(hello,1.5) I wasn't able to do what you wanted. The error java.lang.ArrayIndexOutOfBoundsException has occured Please check line 7 of /Users/guzdial/shift-broken.py 9

10 We need to prevent going past the end of the sound
def shift(source, factor): target = makeEmptySound(getLength(source)) sourceIndex = 0 for targetIndex in range(0, getLength( target)): value = getSampleValueAt( source, int(sourceIndex)) setSampleValueAt( target, targetIndex, value) sourceIndex = sourceIndex + factor if sourceIndex > getLength(source): sourceIndex = #we must check the boundary condition play(target) return target 10

11 Top-Down Design Start from a problem statement. What are you trying to do? Refine the problem statement. Use hierarchical decomposition to define subparts. Refine until you know how to write the programs. Use procedural abstraction so that higher-level functions are written in terms of lower-level functions. 11

12 Example Top-Down Design: An Adventure Game
Top-level function: Tell the user how to play the game. Describe the room. Get the player’s command. Figure out the next room. Return to Step 2, until the user Quits. 12

13 Functions implemented
Top level functions def playGame () Subfunctions def showIntroduction () def showRoom (room) def pickRoom (direction, room) def showPorch() 13

14 An important new loop How do we keep going, indefinitely, until the user says “quit”? A while loop repeats a block until a test becomes false. 14

15 Syntax of while Loop while test : block of statements
test is evaluated first, if test is true, then the block of statements are executed repeatedly until test becomes false. Don’t forget the colon after test 15

16 Another Example of while Loop
Compute the sum of first 5 integers (1 to 5) use while loop def sumWhile(): i = 1 sum = 0 while i < 6: sum = sum + i    i = i + 1 print sum use for loop def sumFor(): sum = 0 for i in range (1,6): sum = sum + i    print sum 16

17 Use while Loop in playGame ()
def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") location = pickRoom(direction , location) The game continues until you find the Exit. 17

18 Testing our program Try both expected, and unexpected input.
We should return something reasonable in response to unreasonable input. 18

19 Returning a reasonable response to unreasonable pickRoom() input
def pickRoom(direction , room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south": return "LivingRoom" printNow("You can’t (or don’t want to) go in that direction.") return room #Stay in current room 19

20 Now we handle unexpected input better
>>> pickRoom(’north’, ‘DiningRoom’) You can’t (or don’t want to) go in that direction. >>> pickRoom(’Entryway’, ‘DiningRoom’) 20

21 Tips on Debugging Learn to trace code
Print statements are your friends Don’t be afraid to change the program Use comments to “remove” parts temporarily when testing. 21

22 Stepping through sumWhile() with the Watcher
We can monitor the values of sum and i in intermediate steps (Demo) def sumWhile(): i = 1 sum = 0 while i < 6: sum = sum + i    i = i + 1 print sum 22

23 Final QUESTIONS???


Download ppt "CS 177 Week 9 Recitation Slides"

Similar presentations


Ads by Google