1 / 28

8 Sights & Sounds

8 Sights & Sounds. Computing is more than Computation. So many devices – iPod, digital cameras, etc – use computers to do so much more than “compute”. Such devices manage and manipulate images and sounds in amazing ways. In this chapter we try to get some of that for ourselves. GraphWin():.

jasper
Download Presentation

8 Sights & Sounds

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 8 Sights & Sounds Intro to Robots

  2. Computing is more than Computation • So many devices – iPod, digital cameras, etc – use computers to do so much more than “compute”. • Such devices manage and manipulate images and sounds in amazing ways. • In this chapter we try to get some of that for ourselves. Intro to Robots

  3. GraphWin(): • Myro comes with a graphics windows in which we can play. • We create it by invoking it by name: • This produces a gray 200 x 200 pixel window. • The myCanvas window becomesour canvas where we exploringdrawing images. From myro import * myCanvas = GraphWin() Exercise: Are pixels round, square orrectangular? Intro to Robots

  4. GraphWin(): • Close it • And create it again with a new name and size. • Change its color myCanvas.close() myCanvas = GraphWin(‘Silly Scene’,200,300) myCanvas.setBackground(“white”) Intro to Robots

  5. Moment of Reflection: • Our graphics window is an “object” with a name – myCanvas. • You can create an object by calling a function whose name identifies the “type” of object (also called its class). • We communicate with our window by “sending it messages” myCanvas = GraphWin(‘Silly Scene’,200,300) myCanvas.setBackground(“white”) message argument message name name of message “receiver” Intro to Robots

  6. Locating the Pixels • Pixels, like points on the xy-plane, have coordinates. • The coordinate numbering system begins in the top, left-hand corner. (0,0) (100,100) Intro to Robots

  7. Creating More Objects • Besides graphics windows objects we can create many others – points, lines, circles, etc. • Up to this point, p only exists in the programming interface (IDLE). To see it, we need to place it on the graphics window at its specified location. • Remember, we are sending a message to p to draw itself on the myCanvas graphics window. p = Point(100,125) p.draw(myCanvas) Intro to Robots

  8. Point object messages: • There are some more messages we can send to a Point. getX() # returns the x-coordinategetY() # returns the y-coordinateundraw() # makes the object disappear Intro to Robots

  9. What’s the Point? • Points by themselves are not so interesting. • What is really the point is that with them we can draw lines. • There is a lab exercise where we end up drawing a line by ourselves. • Alternatively we can create a Line object. • A Line object is a straight line between two end points. myLine = Line(Point(5,100), Point(50,200)) the arguments of the Linefunction are its starting andending Points. These pointsare created by calling the Point()function twice. executing the Line function creates a new Line object myLine.draw() Intro to Robots

  10. A real Fan of Lines. for n in range(0, 200, 5): L=Line(Point(n,25),Point(100,100)) L.draw(myCanvas) all the lines drawn have a commonend-point – Point(100,100) range(x,y,z) is a function thatreturns a list of numbers startingat x, x+z, x+2z, … until y all the lines drawn have starting pointswith a common y-coordinate (25) Intro to Robots

  11. Other Line functions: • L = Line(Point(25,25), Point(100,100)) • L.draw() • start = L.getP1() # returns the starting point of line • end = L.getP2() # returns the end point of line • print start.getX(), start.getY(), end.getX(), end.getY() • 25 100 100 • L.undraw() Exercise: If L is a line, print out the starting and ending coordinates of L as (x,y) print ‘(‘,L.getP1().getX(),’,’,L.getP1().getY(),’)’print ‘(‘,L.getP2().getX(),’,’,L.getP2().getY(),’)’ We send L a message to return its starting point and the startingpoint a message to return its x-coordinate. Obviously, the dot (.) operatorfunctions like +, left-to-right. Intro to Robots

  12. Circles: • The definition of a circle is “the set of points equidistant from a given point”. • The Circle object has a similar definition. myCircle = Circle(Point(100,100), 50)myCircle.draw() radius center point Intro to Robots

  13. Circle Functions: • You can change and retrieve various Circle characteristics. c = Circle(Point(100, 150), 30) c.draw(myCanvas)centerPoint = c.getCenter() c.setOutline(“red”) # line color c.setFill(“yellow”) # interior fill colorc.setWidth(5) # line width in pixels color = color_rgb(100,125,150)c.setFill(color) Exercise: Try out these commands Intro to Robots

  14. Circle Example # Program to draw a bunch of # random colored circles from myro import * from random import * width = 500 height = 500 def makeCircle(x, y, r): # creates a Circle centered at point (x, y) of radius r # we plan to use randomly generated numbers x, y and r return Circle(Point(x, y), r) def makeColor(): # creates a new color using random RGB values red = randrange(0, 256) # returns a random number between 0 and 255 green = randrange(0, 256) # returns a random number between 0 and 255 blue = randrange(0, 256) # returns a random number between 0 and 255 return color_rgb(red, green,blue) main() Intro to Robots

  15. Circle Example def main(): # Create and display a graphics window myCanvas = GraphWin("Cicrles", width, height) myCanvas.setBackground("white") # draw a bunch of random circles with random colors. N = 500 for i in range(N): # pick random center # point and radius # in the window x = randrange(0,width) y = randrange(0,height) r = randrange(5, 25) c = makeCircle(x, y, r) # select a random color c.setFill(makeColor()) c.draw(myCanvas) Intro to Robots

  16. Function Definition Alternatives: • Consider the following two examples: • makeCircle() hides the details of makeCircle(x,y,r) exposes by requiring previously assigned values for x, y and r def makeCircle(): # creates a Circle centered at point (x, y) of radius r x = randrange(0,width) # only if width is “global” y = randrange(0,height) # only if height is “global” r = randrange(5, 25) return Circle(Point(x, y), r) x = randrange(0,width) y = randrange(0,height) r = randrange(5, 25)c = makeCircle(x, y, r) Intro to Robots

  17. Variable Scope: • Variables defined outside any function definition have “global” scope. They can be used anywhere in the program. • Variables defined inside a function have scope restricted to that function. If you want to use their values in a different functions you need to pass them as arguments. def foo(): x = 4 # scope of x is inside foo() y = 5 # scope of y is inside foo() return bar()def bar(): z = x + y # fails because x and y are # have scope within foo() only return z def foo(): x = 4 y = 5 # now pass x and y as arguments return bar(x,y)def bar(a,b): z = a + b return z Intro to Robots

  18. Pros and Cons • Having global variables is nice because you can use them whenever you need them. • However it gives you added responsibility not to misuse the variable (change its value by mistake, for example) • A variable with a function scope can only be modified inside that function. Intro to Robots

  19. Sound: • Both the computer and the robot can beep(). • What is Hertz? beep(1,440) # plays a 440 Hztone for 1 second computer.beep(1,440) # computer plays the same tone Hertz is the unit for measuring frequencies 1 Hz = 1 cycle/second 1 cycle Intro to Robots

  20. Sound (cont): • Computer CPUs measure their “speed” in Gigahertz. • Computer CPUs have an internal clock that “ticks” at the Gigahertz rate of the CPU. • All the various components of a computer synchronize their activities against this clock. 1 Gigahertz = 109 cycles / second Intro to Robots

  21. Sound (cont): • Other periodic motions: • What is sound? • What can the human ear hear? earth rotational rate: 1/day = 1/86400 seconds = 0.00001157 cycles/second cd rotational rate: 400 turns/second52x cd rotational rate: 52*400/second = 20800/second hummingbird wing flap rate: 20-78 flaps/second A periodic compression and decompression (refraction) of air. 1 cycle of sound = 1 compression + 1 decompression 440 cycles/second = 440 compressions and decompressions / second in the range 20Hz to 20000Hz (20KHz) Intro to Robots

  22. What sounds can Scribbler make? • A musical scale consists of 12 sounds • You can move up or down an octave by multiplying or dividing by 2. • The C-note in various octaves is identified as: C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B C0 C1 C2 C3 C4 C5 C6 C7 C8 Intro to Robots

  23. Music: • C4 has frequency 261.63 Hz. Try it out with Scribbler. • The notes in a scale are exponentially equidistant. By this we mean: C4 frequency = 261.63 * 2(0/12) Hz = 261.63 * 20 Hz = 261.63 Hz C#4 frequency = 261.63 * 2(1/12) Hz = 261.63 * 20.08333 Hz = 277.19 Hz D4 frequency = 261.63 * 2(2/12) Hz = 261.63 * 20.16666 Hz = 293.67 Hz D#4 frequency = 261.63 * 2(3/12) Hz = 261.63 * 20.25000 Hz = 311.13 Hz E4 frequency = 261.63 * 2(4/12) Hz = 261.63 * 20.33333 Hz = 329.63 Hz F4 frequency = 261.63 * 2(5/12) Hz = 261.63 * 20.41666 Hz = 349.23 Hz F#4 frequency = 261.63 * 2(6/12) Hz = 261.63 * 20.50000 Hz = 370.00 Hz G4 frequency = 261.63 * 2(7/12) Hz = 261.63 * 20.58333 Hz = 392.00 Hz G#4 frequency = 261.63 * 2(8/12) Hz = 261.63 * 20.66666 Hz = 415.31 Hz A4 frequency = 261.63 * 2(9/12) Hz = 261.63 * 20.75000 Hz = 440.01 Hz A#4 frequency = 261.63 * 2(10/12) Hz = 261.63 * 20.83333 Hz = 466.17 Hz B4 frequency = 261.63 * 2(11/12) Hz = 261.63 * 20.91666 Hz = 493.89 Hz C5 frequency = 261.63 * 2(12/12) Hz = 261.63 * 21 Hz = 523.26 Hz Intro to Robots

  24. Music Tempo: 4 4 • Music has tempo. In tempo • a quarter note is 0.68 seconds • A half note is 1.36 seconds • A whole note is 2.72 seconds. Intro to Robots

  25. Myro Music: • Playing notes by frequency and time is a pain. • Myro lets you “write out a tune” as a string and then converts the string into a song. • Each note in the string is eitherorwhere note = ‘C4’, etc and time = ¼, ½, 1, etc tune = “c 1; d 1; e 1; f 1; g 1; a 1; b 1; c7 1;“ song = makeSong(tune) computer.playSong(song) note time; note1 note2 time; Intro to Robots

  26. Myro Music 2: • You can also put your string in a file and myro will read the file and play the song. • Of course, if you want, the robot can play the song itself, just drop the “computer” receiver tag. song = readSong(filename) computer.playSong(song) Intro to Robots

  27. Myro Reference: • The end of chapter 8 holds a review of the myro graphics function calls. GraphWin() GraphWin(<title>, <width>, <height>) <window>.close()<window>.setBackground(<color>)<new_color> = color_rgb(<red>, <green>, <blue>)Point(<x>, <y>)<point>.getX() <point>.getY() Line(<start point>, <end point>)Circle(<center point>, <radius>)Rectangle(<point1>, <point2>)Oval(<point1>, <point2>)Polygon(<point1>, <point2>, <point3>,…) Polygon([<point1>, <point2>, …]) Text(<anchor point>, <string>) Image(<centerPoint>, <file name>) <object>.draw(<window>)<object>.undraw()<object>.getCenter()<object>.setOutline(<color>) <object>.setFill(<color>)<object>.setWidth(<pixels>)<object>.move(<dx>, <dy>) Intro to Robots

  28. Myro Reference: • The end of chapter 8 holds a review of the myro graphics function calls. beep(<seconds>, <frequency>) beep(<seconds>, <f1>, <f2>) <robot/computer object>.beep(<seconds>, <frequency>) <robot/computer object>.beep(<seconds>, <f1>, <f2>) robot.playSong(<song>)readSong(<filename>)song2text(song)makeSong(<text>) text2song(<text>) Intro to Robots

More Related