CS 177 Week 9 Recitation Slides

Slides:



Advertisements
Similar presentations
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
Advertisements

Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 8: Making Sounds by Combining Pieces.
Chapter 9: Building Bigger Programs. Chapter Objectives.
Providing a Context to Motivate Non- Majors Into Computing Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Sound, Part 2 Using range to manipulate samples by index number.
1 CS 177 Week 6 Recitation Slides Scaling Drawing on images Vector-based Vs. Bitmap graphical representation.
Copying and Transforming Pictures. First, finding the min or max… Next homework asks you to write a function to find the darkest and lightest shade of.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 7: Modifying Samples in a Range.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
CS2984: Introduction to Media Computation Using Loops for Pictures Conditionals Copying images.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 8: Making Sounds by Combining Pieces.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
CS 101: Introduction to Computing Rotating and Blurring Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan,
Chapter 4: Modifying Pixels in a Range (partial slide deck)
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
CS1315: Introduction to Media Computation How to design and debug a program: Top-down, bottom-up, and debugging. Using background subtraction and chromakey.
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
ACM SIGCSE 2003: Multimedia Construction Projects Mark Guzdial College of Computing Georgia Institute of Technology
Chapter 8: Making Sounds by Combining Pieces. Chapter Objectives.
“But it looks right”: Bugs in non-majors media programs Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
Chapter 8: Modifying Samples in a Range. Chapter Objectives.
Chapter 10: Building Bigger Programs. Chapter Objectives.
CSC 112Introduction to Media Computation 1 Rotating Images.
1 CS 177 Week 8 Recitation Slides JES Sound functions and Modifying Sounds Increasing/Decreasing Volume Maximizing (Normalizing) Splicing Reversing Mirroring.
CS1315: Introduction to Media Computation Transforming pictures by index number.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 4: Modifying Pixels in a Range.
Media computation as a context for learning computing Mark Guzdial College of Computing/GVU Georgia Institute of Technology.
Working with Sounds Barb Ericson College of Computing Georgia Institute of Technology
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Chapter 10: Building Bigger Programs
Chapter 8: Making Sounds by Combining Pieces
The Selection Structure
Processing Sound Ranges part 3
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Working with ranges in pictures
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 7: Modifying Samples in a Range
Problem Solving Techniques
Learning to Program in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Exception Handling.
Chapter 8: Making Sounds by Combining Pieces
Loops CIS 40 – Introduction to Programming in Python
We’re moving on to more recap from other programming languages
How sound works: Acoustics, the physics of sound
Chapter 7: Modifying Samples in a Range
Design and Implementation
Module 4 Loops.
Testing and Repetition
FUNCTION PRACTICE !!!.
Processing Sound Ranges part 2
Processing Sound Ranges
For loops Taken from notes by Dr. Neil Moore
CSC1401 Viewing a picture as a 2D image - 2
CS1315: Introduction to Media Computation
Chapter 9: Building Bigger Programs
Repetition Statements (Loops) - 2
Processing Sound Ranges part 3
Chapter 4: Modifying Pixels in a Range
CS 144 Advanced C++ Programming January 31 Class Meeting
Chapter 3 Debugging Section 3.4
Review of Previous Lesson
Presentation transcript:

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

ANY QUESTIONS?

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

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

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

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

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

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

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

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 = 0 #we must check the boundary condition play(target) return target 10

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

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

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

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

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

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

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

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

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

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

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

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

Final QUESTIONS???