Skip to content

Commit

Permalink
visualisation working a bit more
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Dec 20, 2012
1 parent 8c942dc commit afcd347
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
37 changes: 36 additions & 1 deletion Dirt.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import Pattern
import Parse import Parse
import Sound.OpenSoundControl import Sound.OpenSoundControl
import qualified Data.Map as Map import qualified Data.Map as Map
import Control.Applicative
import Control.Concurrent.MVar import Control.Concurrent.MVar
import Visual import Visual
import Data.Colour.SRGB
import Data.Colour.Names
import Data.Hashable
import Data.Bits
import Data.Maybe


dirt :: OscShape dirt :: OscShape
dirt = OscShape {path = "/play", dirt = OscShape {path = "/play",
Expand Down Expand Up @@ -36,6 +42,33 @@ dirt = OscShape {path = "/play",


dirtstream name = stream "127.0.0.1" "127.0.0.1" name "127.0.0.1" 7771 dirt dirtstream name = stream "127.0.0.1" "127.0.0.1" name "127.0.0.1" 7771 dirt


dirtToColour :: OscSequence -> Sequence ColourD
dirtToColour p = s
where s = fmap (\x -> maybe black (maybe black datumToColour) (Map.lookup (param dirt "sound") x)) p

datumToColour :: Datum -> ColourD
datumToColour = stringToColour . show

stringToColour :: String -> ColourD
stringToColour s = sRGB (r/256) (g/256) (b/256)
where i = (hash s) `mod` 16777216
r = fromIntegral $ (i .&. 0xFF0000) `shiftR` 16;
g = fromIntegral $ (i .&. 0x00FF00) `shiftR` 8;
b = fromIntegral $ (i .&. 0x0000FF);


visualcallback :: IO (OscSequence -> IO ())
visualcallback = do t <- ticker
mv <- startVis t
let f p = do let p' = dirtToColour p
swapMVar mv p'
return ()
return f

dirtyvisualstream name = do cb <- visualcallback
streamcallback cb "127.0.0.1" "127.0.0.1" name "127.0.0.1" 7771 dirt


sound = makeS dirt "sound" sound = makeS dirt "sound"
offset = makeF dirt "offset" offset = makeF dirt "offset"
begin = makeF dirt "begin" begin = makeF dirt "begin"
Expand All @@ -59,4 +92,6 @@ striateO :: OscSequence -> Int -> Double -> OscSequence
striateO p n o = cat $ map (\x -> off (fromIntegral x) p) [0 .. n-1] striateO p n o = cat $ map (\x -> off (fromIntegral x) p) [0 .. n-1]
where off i p = p ~~ begin ((atom $ (fromIntegral i / fromIntegral n) + o) :: Sequence Double) ~~ end ((atom $ (fromIntegral (i+1) / fromIntegral n) + o) :: Sequence Double) where off i p = p ~~ begin ((atom $ (fromIntegral i / fromIntegral n) + o) :: Sequence Double) ~~ end ((atom $ (fromIntegral (i+1) / fromIntegral n) + o) :: Sequence Double)


metronome = slow 2 $ sound "[odx, [hh]*8]"

--metronome = slow 2 $ sound "[odx, [hh]*8]"
10 changes: 9 additions & 1 deletion Stream.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ stream client server name address port shape
return $ \p -> do swapMVar patternM p return $ \p -> do swapMVar patternM p
return () return ()


streamcallback :: (OscSequence -> IO ()) String -> String -> String -> String -> Int -> OscShape -> IO (OscSequence -> IO ()) streamcallback :: (OscSequence -> IO ()) -> String -> String -> String -> String -> Int -> OscShape -> IO (OscSequence -> IO ())
streamcallback callback client server name address port shape streamcallback callback client server name address port shape
= do f <- stream client server name address port shape = do f <- stream client server name address port shape
let f' p = do callback p let f' p = do callback p
Expand All @@ -128,6 +128,14 @@ onTick s shape patternM change ticks
catch (mapM_ (sendOSC s) messages) (\msg -> putStrLn $ "oops " ++ show msg) catch (mapM_ (sendOSC s) messages) (\msg -> putStrLn $ "oops " ++ show msg)
return () return ()


ticker :: IO (MVar Rational)
ticker = do mv <- newMVar 0
forkIO $ clocked "ticker" "127.0.0.1" "127.0.0.1" tpb (f mv)
return mv
where f mv change ticks = do swapMVar mv ((fromIntegral ticks) / (fromIntegral tpb))
return ()
tpb = 32

make :: ParseablePattern p => (a -> Datum) -> OscShape -> String -> p a -> (p OscMap) make :: ParseablePattern p => (a -> Datum) -> OscShape -> String -> p a -> (p OscMap)
make toOsc s nm p = fmap (\x -> Map.singleton nParam (defaultV x)) p make toOsc s nm p = fmap (\x -> Map.singleton nParam (defaultV x)) p
where nParam = param s nm where nParam = param s nm
Expand Down
60 changes: 33 additions & 27 deletions Visual.hs
Original file line number Original file line Diff line number Diff line change
@@ -1,16 +1,10 @@
{- module Visual where
Lines.hs (adapted from lines.c which is (c) Silicon Graphics, Inc.)
Copyright (c) Sven Panne 2002-2005 <sven.panne@aedion.de>
This file is part of HOpenGL and distributed under a BSD-style license
See the file libraries/GLUT/LICENSE
This program demonstrates geometric primitives and their attributes.
-}


import System.Exit ( exitWith, ExitCode(ExitSuccess) ) import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT import Graphics.UI.GLUT
import Parse import Parse
import Pattern import Pattern
import Control.Applicative
import Control.Concurrent import Control.Concurrent
import Control.Concurrent.MVar import Control.Concurrent.MVar
import Data.Colour import Data.Colour
Expand All @@ -20,13 +14,21 @@ import Data.Colour.SRGB
drawOneLine :: Vertex2 GLfloat -> Vertex2 GLfloat -> IO () drawOneLine :: Vertex2 GLfloat -> Vertex2 GLfloat -> IO ()
drawOneLine p1 p2 = renderPrimitive Lines $ do vertex p1; vertex p2 drawOneLine p1 p2 = renderPrimitive Lines $ do vertex p1; vertex p2


drawRect :: GLfloat -> GLfloat -> GLfloat -> GLfloat -> IO ()
drawRect x y x' y'
= renderPrimitive Polygon $ do vertex$Vertex2 x y
vertex$Vertex2 x y'
vertex$Vertex2 x' y'
vertex$Vertex2 x' y
return ()

myInit :: IO () myInit :: IO ()
myInit = do myInit = do
clearColor $= Color4 0 0 0 0 clearColor $= Color4 0 0 0 0
shadeModel $= Flat shadeModel $= Flat


w = 1024 w = 1024
lw = 400 lw = 150


translatef = translate :: Vector3 GLfloat -> IO () translatef = translate :: Vector3 GLfloat -> IO ()


Expand All @@ -40,17 +42,20 @@ drawEvent' _ (_, []) = return ()
drawEvent' lw' ((t, d), c:cs) drawEvent' lw' ((t, d), c:cs)
= do let (Data.Colour.SRGB.RGB r g b) = toSRGB c = do let (Data.Colour.SRGB.RGB r g b) = toSRGB c
color (Color3 r g b) color (Color3 r g b)
drawOneLine (Vertex2 (w * (fromRational t)) y) (Vertex2 (w * (fromRational $ t+d)) y) --drawOneLine (Vertex2 (w * (fromRational t)) y) (Vertex2 (w * (fromRational $ t+d)) y)
drawRect (w * (fromRational t)) 0 (w * (fromRational $ t+d)) lw'
translatef (Vector3 0 (lw') 0) translatef (Vector3 0 (lw') 0)
drawEvent' lw' ((t,d), cs) drawEvent' lw' ((t,d), cs)
where y = (lw'/2) where y = (lw'/2)


display :: (MVar (Sequence ColourD)) -> DisplayCallback display :: MVar Rational -> MVar (Sequence ColourD) -> DisplayCallback
display mv = do display t mv = do
clear [ ColorBuffer ] clear [ ColorBuffer ]
ticks <- readMVar t
p <-readMVar mv p <-readMVar mv
mapM_ drawEvent (range (segment p) (0, Just 1)) mapM_ drawEvent (map (mapFst (\(t,d) -> ((t - (ticks/2))/speed,d/speed))) $ range (segment p) ((ticks/2), Just speed))
flush flush
where speed = 4


reshape :: ReshapeCallback reshape :: ReshapeCallback
reshape size@(Size w h) = do reshape size@(Size w h) = do
Expand All @@ -71,17 +76,18 @@ animate = do


-- Request double buffer display mode. -- Request double buffer display mode.
-- Register mouse input callback functions -- Register mouse input callback functions
start :: IO (MVar (Sequence ColourD)) startVis :: MVar Rational -> IO (MVar (Sequence ColourD))
start = do initialize "smooth" [] startVis t =
initialDisplayMode $= [ SingleBuffered, RGBMode ] do initialize "smooth" []
initialWindowSize $= Size 1024 400 initialDisplayMode $= [ SingleBuffered, RGBMode ]
initialWindowPosition $= Position 100 100 initialWindowSize $= Size 1024 150
createWindow "smooth" initialWindowPosition $= Position 100 100
myInit createWindow "smooth"
mp <- newMVar (p "[white [red, [green orange] blue] red, tomato yellow]") myInit
displayCallback $= (display mp) mp <- newMVar (pure black)
addTimerCallback 100 $ animate -- refresh every 1/10sec displayCallback $= (display t mp)
reshapeCallback $= Just reshape addTimerCallback 80 $ animate
keyboardMouseCallback $= Just keyboard reshapeCallback $= Just reshape
forkIO $ mainLoop keyboardMouseCallback $= Just keyboard
return mp forkIO $ mainLoop
return mp

0 comments on commit afcd347

Please sign in to comment.