Multimedia dataflow in Haskell using Arrows

composition of shapes

Arrows are fun for images:

image = proc p -> do
  d1 <- disc <<< arr (translate (-0.75) 0.5) <<< arr (uscale 0.5) -< p
  d2 <- disc <<< arr (translate 1.15 (-0.5)) <<< arr (uscale 0.25) -< p
  s <- square <<< arr (translate 0.25 0) <<< arr (uscale 0.5) <<< arr (rotate (2 * pi * (-72) / 360)) -< p
  c <- arr greyToRGB <<< checkers (0.25, 0.25) <<< arr (rotate (2 * pi * 45 / 360)) -< p
  r <- solid red -< p
  u <- union -< (d1, s)
  d <- diff -< (u, d2)
  o <- choose -< (d, c, r)
  returnA -< o

Arrows are fun for animation too:

rotating Mandelbrot

Arrows are also fun for audio:

wrap x = x - fromIntegral (floor x)

phasor rate phase0 = proc hz -> do
  increment <- arr (/rate) -< hz
  rec accum <- delay (wrap phase0) -< wrap (accum + increment)
  returnA -< accum

osc rate phase0 = proc hz -> do
  phase <- phasor rate phase0 -< hz
  returnA -< cos (2 * pi * phase)

fm rate = proc _ -> do
  modA <- osc rate 0 -< 110/8
  modB <- osc rate 0 -< (modA * 110) + 220
  carrier <- osc rate 0 -< (modB * 220) + 440
  returnA -< carrier

Listen to the above three-oscillator frequency modulation.