1 {-# LANGUAGE Arrows, FlexibleContexts #-}
3 module RCMA.Layer.Board ( boardSF
7 import Control.Concurrent
8 import Control.Concurrent.MVar
9 import Data.ReactiveValue
13 import RCMA.Auxiliary.Curry
14 import RCMA.Layer.Layer
16 import RCMA.Global.Clock
19 -- The state of the board is described by the list of the playheads
20 -- and the different actions onto the board.
21 -- It can then be modified discretly when a beat is received or
22 -- continuously when the user acts on it.
24 -> SF (Layer, [PlayHead], Event BeatNo)
25 (Event ([PlayHead], [Note]))
26 boardAction board = proc (Layer { relPitch = rp
30 ahSF <<^ arr propEvent -< (ebn, rp, s, pl)
32 ahSF :: SF (Event (BeatNo, RelPitch, Strength, [PlayHead]))
33 (Event ([PlayHead], [Note]))
34 ahSF = arr $ fmap (uncurry4 $ advanceHeads board)
35 propEvent (a,b,c,d) = if isEvent a
36 then Event (fromEvent a,b,c,d)
41 -> SF (Layer, Tempo) (Event ([PlayHead], [Note]))
42 boardSF' board ph = proc (l, t) -> do
43 (ebno, el) <- splitE ^<< layerMetronome -< (t, l)
44 boardAction board -< (l, ph, ebno)
46 boardSF :: Board -> SF (Layer, Tempo) (Event [Note])
47 boardSF board = boardSF'' board []
48 where boardSF'' :: Board -> [PlayHead] -> SF (Layer, Tempo) (Event [Note])
49 boardSF'' board ph = switch (splitE ^<< fmap swap ^<< boardSF' board ph)
53 -> ReactiveFieldReadWrite IO Tempo
54 -> ReactiveFieldReadWrite IO Layer
55 -> ReactiveFieldReadWrite IO [Note]
57 boardSetup board tempoRV layerRV outBoardRV = do
58 layer <- reactiveValueRead layerRV
59 tempo <- reactiveValueRead tempoRV
60 (inBoard, outBoard) <- yampaReactiveDual (layer, tempo) (boardSF board)
61 let inRV = pairRW layerRV tempoRV
65 reactiveValueOnCanRead outBoard
66 (reactiveValueRead outBoard >>= reactiveValueWrite outBoardRV . event [] id)
67 putStrLn "Board started."
72 (^:>) :: (ReactiveValueRead a b m, ReactiveValueReadWrite c d m) =>
74 not ^:> rv = reactiveValueOnCanRead not resync
75 where resync = reactiveValueRead rv >>= reactiveValueWrite rv