1 / 38

Art, Math, Music and Computer Programming Languages

Art, Math, Music and Computer Programming Languages Creativity with Patterns Programming with Functions Everyone understands functions: f(x) = x + 1 g(x,y) = sine(x) - y abs(x) = if x < 0 then -x else x

Mia_John
Download Presentation

Art, Math, Music and Computer Programming Languages

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. Art, Math, MusicandComputer Programming Languages Creativity with Patterns

  2. Programming with Functions Everyone understands functions: f(x) = x + 1 g(x,y) = sine(x) - y abs(x) = if x < 0 then -x else x Functional Programming uses functions (and nothing else!) to write computer programs. We use functions to create some simple but interesting programs that explore things like math or music or art …

  3. Programming languages If you use languages like Java or C++ you have to tell the computer a lot about how to do something. You need to learn a lot to do a little but you can do almost anything if you work really hard … We are interested in languages where you don’t have to say how to do things - anyone can write programs in these languages! Our languages deal with specific domains - they are not meant to do everything.

  4. Everything is a Function Why functions? Because we can represent just about everything with them: Pictures are functions from points onto colors Music is a function from time to sets of notes Math is a study of functions In all of these areas, we want to capture patterns and give them names Reflection Shapes Scales Rotations Repetition

  5. Notation Using a computer requires precise notations. f x y -- function f applied to arguments x and y (x,y) -- A 2-dimensional point (or vector) f x = x+1 -- A function definition f (x,y) = (y,x) -- f has one argument, a point

  6. Functional Images We use a language called Pan (Picture Animation) to create pictures. A picture is a function from a coordinate (place on the X-Y plane) to a color. Computer geeks write things like this: a Picture has type “Point -> Color” You should know about coordinates but ... What’s a color?

  7. Colors We describe colors using 3 numbers, each between 0 and 1 Amount of Red Amount of Green Amount of Blue Example: Yellow = Red + Green rgb 1 1 0 We can also add Transparency 0 = clear, 1 = solid rgba 1 1 0 0.5 -- semi-transparent yellow

  8. A Picture! We can define a simple picture like this: pic (x,y) = if abs x + abs y < 100 then black else white What does this define? The coordinate system is in pixels and the origin is at the center of the window. Test1

  9. Another Picture! More colors! Let’s change the black to a color that varies with location pic (x,y) = if abs x + abs y < 100 then rgb 0.5 (abs x/100) (abs y/100) else white rgb defines a color from the red, green, and blue components. Test2

  10. Adding Controls r <- slider “red” (0,1) 0 pic (x,y) = if abs x + abs y < 100 then rgb r (abs x/100) (abs y/100) else white slider uses a label, a range, and an initial value to define an interactive controller Test3

  11. Transformations A function that maps one point onto another is a transformation: A Transformation has type “Point -> Point” moveIt (x,y) = (x+1, y+1) What is moveIt (2,3)? twistIt (x,y) = (y, -x) What is twistIt (2,3)

  12. Visualizing Transformations So how can we “see” a transform on points? One way is to use it as a lens to view an image. Here’s a transformation: f (x,y) = (x*x, y*y) f (0,0) = (0,0) f (1,1) = f(-1,1) = f(1,-1) = f (-1,-1) = (1,1) f (2,2) = (4,4) So, if we look at (2,2) we’ll see what’s at (4,4)

  13. y x Original Image Warped Image Here’s what happens: y x Test4

  14. Symmetry The image symmetric about both the x and y axis: f(-x,y) = f(x,y) Can you prove this?? f(x,-y) = f(x,y)

  15. Polar Coordinates A different way to locate points on a plane Use distance and angle to the origin d angle Simple transformation in polar coordinates have interesting effects …

  16. Swirling This transformation adds extra rotation as points move out from the origin f (distance, angle) = (distance, angle + k * distance) k is a parameter – we can adjust this and watch how the picture changes. Test5

  17. Altering the Distance f (r, theta) = (g r theta, theta) What if g r theta = r + k (k is a parameter) g r theta = r + k*theta g r theta = r*r*k Test6 Test7 Test8

  18. Rippling Move further or closer to the origin depending on the angle g r theta = r * (1 + k*sine (n * theta)) The “sine” function generates a ripple. Test9 Sine tells you how high up you are on the unit circle given an angle.

  19. Ramps A periodic function repeats itself. One of the simplest periodic functions is a series of ramps from 0 to 1, repeated over and over: ramps x = x when 0 <= x and x < 1 = x = ramps (x+1) when x < 0 = ramps (x-1) when x >= 1 A more useful version of this function is parameterized by its period: pramps p x = p * ramps (x/p) y = ramps p x

  20. Using Ramps p <- slider “Size of square” (10,300) 100 f (x,y) = (pramps p x, pramps p y) This replicates the image in the square of size s over the entire plane (tiling) Test10

  21. A Sawtooth These function is symmetric: teeth(x) = x when 0 <= x and x < ½ = 1-x when ½ <= x and x < 1 ..... plus rest of “ramps” We can also parameterize by the period (sawtooth) Tiling the angle in polar coordinates = kaleidoscope f (distance, angle) = (distance, sawtooth(width, angle))

  22. Using the Sawtooth Tiling: f (x,y) = (pteeth p x, pteeth p y) Circular reflections: f (r, theta) = (pteeth p r, theta) Kaleidoscope: f (t, theta) = (r, pteeth p theta) test11 test12 test13

  23. The Grand Finale Define factor to take apart a number x as follows: (big, little) = factor p x where big is a multiple of p , -p/2 < little <= p/2, and big + little = x Now, apply a warping t such that slicer p t (x,y) = (x’ + bigx, y’ + bigy) where (bigx, littlex) = factor p x (bigy, littley) = factor p y (x’, y’) = t (littlex, littley)

  24. Apply this to the following transformations: f (r, theta) = (r+k, theta) f (r, theta) = (0, 0) test14 test15

  25. Haskore • Motivation: • Traditional music notation has many limitations: • Unable to express a composer’s intentions. • Biased toward music that is humanly performable. • Unable to express notions of algorithmic composition.Haskore (“Haskell” + “Score”) is a library of Haskellfunctions and datatypes for composing music.

  26. Defining Music… Can we create music with functions? Of course! NoteName = Cf | C | Cs | Df | D | Ds | Ef | E | Es | Ff | F | Fs | Gf | G | Gs | Af | A | As | Bf | B | Bs Music = Note (NoteName, Integer) Duration [Attributes] -- a note Rest Duration -- a rest Music :+: Music -- sequential composition Music :=: Music -- parallel composition Tempo Float Music -- scale the tempo Trans Integer Music -- transposition

  27. C Major Scale For convenience, c oct duration attributes = Note (C, oct) duration attributes cmaj = c 3 0.2 [] :+: d 3 0.2 [] :+: e 3 0.2 [] :+: ….. c 4 0.2 []

  28. Simple Transformations Add a parallel line 7 notes higher: pfifth m = m :=: Trans 7 m cmaj5 = pfifth cmaj againFaster m = m :+: (Tempo 2 m) cmaj5twice = againFaster cmaj5 cmaj5four = againFaster cmaj5twice

  29. Function Composition Function composition notation: (f . g) x = f (g x) Make a “pipeline” of transformers. pfifth . Trans 5 . Tempo 2 -- Music transfomers means: double the tempo, transpose up 5, and add a parallel part a fifth higher Such notation makes it easier to work with functions!

  30. Types Types help us understand functions x :: Music -- x is a piece of music y :: Music -> Music -- y a function that accepts music and returns music (a music transformer) z :: Integer -> Music -> Music -- z has two arguments, an integer and some music z 3 -- if passed only one argument, you get a music transformer Using only some of the arguments is called currying: named after Haskell Curry

  31. Simple Iteration We use functions to capture music structures One such structure is simple iteration: rpt :: Integer -> Music -> Music rpt 0 m = Rest 0 --- a “null” piece of music rpt n m = m :+: rpt (n-1) m Use recursion to define a function that iterates n times cmaj4 = rpt 4 cmaj What does rpt 3 . rpt 3 do?

  32. More Complex Patterns Why repeat the same thing exactly the same? We can add a function that changes the music with each iteration: rptm :: Integer -> (Music -> Music) -> Music -> Music rptm 0 f m = Rest 0 rptm n f m = m :+: rptm (n-1) f (f m) cmr = rptm 4 pfifth cmaj cmr1 = rptm 4 (pfifth . Tempo 1.5) cmaj This changes the music!

  33. More Examples A function to recursively apply transformations f (to elements in a sequence) and g (to accumulated phrases): rep :: (Music -> Music) -> (Music -> Music) -> Integer -> Music -> Music rep f g 0 m = Rest 0 rep f g n m = m :=: g (rep f g (n-1) (f m)) An example using "rep" three times, recursively, to create a "cascade" of sounds. run = rep (Trans 5) (delay tn) 8 (c 4 tn) cascade = rep (Trans 4) (delay en) 8 run cascades = rep id (delay sn) 2 cascade waterfall = cascades :+: revM cascades

  34. How Did That Work? rep f g 0 m = Rest 0 rep f g n m = m :=: g (rep f g (n-1) (f m)) At each step, apply f “going down” and g “coming back” Play m at the same time as the “rep” part If n = 2, we get rep f g 2 m = m :=: g ( rep f g 1 (f m)) = m :=: g ( f m :=: g (rep f g o (f (f m)))) = m :=: g ( f m :=: g (Rest 0)) run = rep (Trans 5) (delay tn) 8 (c 4 tn)

  35. The Grand Finale Write a function that plays a piece of music with the same music played twice as fast an octave higher, and so on. m Higher & Faster m m m m m m

  36. The Program pyr 1 m = m pyr n m = m :=: (rpt 2 . Trans 12 . Tempo 2 . pyr (n-1)) m tune = c 4 0.1 [] :+: g 4 0.1 [] :+: e 4 0.1 [] :+: g 4 0.1 [] alldone = pyr 4 (Tempo 0.3 tune)

  37. Gloveby Tom Makucevichwith help from Paul HudakComposed using Haskore, a Haskell DSL,and rendered using csound.

  38. Thank You! Join us tomorrow to get some “hands on” experience with tunes and toons. Help us build a web page. Bring your creativity! Embarrass your friends. Compose music. Play with pictures. We will have a camera available …

More Related