]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - src/RMCA/Auxiliary/Yampa.hs
Repeat button waits for the next beat before restarting.
[tmp/julm/arpeggigon.git] / src / RMCA / Auxiliary / Yampa.hs
1 {-# LANGUAGE Arrows #-}
2
3 module RMCA.Auxiliary.Yampa where
4
5 import FRP.Yampa
6 import Data.Maybe
7 import RMCA.Auxiliary.Misc
8
9 import Debug.Trace
10
11 -- | = Yampa
12
13 countTo :: (Integral b) => b -> SF (Event a) (Event b)
14 countTo n = count >>^ filterE (== n)
15
16 -- | Synchonizes two event sources. An event on the first source will be delayed until an event occurs on the second.
17 --
18 -- Ex:
19 -- Event a => . . 1 . . . . 2 . . . 3 . . 4 . . . . . 5 . . 6 . . . . .
20 -- Event b => . a . . . b . . . c . . . . . . d . e . f . . . . . g . .
21 -- wairFor => . . . . . 1 . . . 2 . . . . . . 4 . . . 5 . . . . . 6 . .
22
23 waitForEvent :: (Show a, Show b) => SF (Event a, Event b) (Event a)
24 waitForEvent = proc (ea,eb) -> do
25 em <- arr $ uncurry $ mapMerge Left Right (\_ b -> Right b) -< let a = (ea,eb) in traceShow a a
26 hob <- dAccumHoldBy accumulator NoEvent -< em
27 returnA -< let a = eb *> (ea `lMerge` hob) in traceShow (a,eb) a
28 where accumulator :: Event a -> Either a b -> Event a
29 accumulator _ (Left a) = Event a
30 accumulator _ (Right _) = NoEvent
31 --accumulator _ (Right b) =
32
33 {-
34 waitForEvent :: SF (Event b, Event a) (Event b)
35 waitForEvent = proc (eb,ea) -> do
36 rec
37 es' <- iPre NoEvent -< es
38 es <- rSwitch waitAux -< ((eb,ea),es' `tag` waitAux)
39 returnA -< es
40 where waitAux = proc (eb,ea) -> do
41 --ea' <- (if b then notYet else identity) -< ea
42 eb' <- accumHoldBy (\_ b -> Event b) NoEvent -< eb
43 returnA -< ea *> eb'
44 -}
45 -- | 'stepBack' contains its previous argument as its output. Because it's hard to define it at time 0, it's wrapped up in a 'Maybe'.
46 stepBack :: SF a (Maybe a)
47 stepBack = sscan f (Nothing, Nothing) >>^ snd
48 where f :: (Maybe a, Maybe a) -> a -> (Maybe a, Maybe a)
49 f (Nothing,_) x' = (Just x', Nothing)
50 f (Just x, _) x' = (Just x', Just x)
51
52 -- | Like 'stepBack' but the output value is always defined and is equal to the input at time 0.
53 stepBack' :: SF a a
54 stepBack' = proc x -> do
55 x' <- stepBack -< x
56 returnA -< fromMaybe x x'
57
58 -- | Throws an 'Event' when the incoming signal change. The 'Event' is tagged with the new value.
59 onChange :: (Eq a) => SF a (Event a)
60 onChange = proc x -> do
61 x' <- stepBack -< x
62 let makeEvent x x'
63 | isNothing x' = NoEvent
64 | otherwise = let x'' = fromJust x' in
65 if x'' == x then NoEvent else Event x
66 returnA -< makeEvent x x'
67
68 -- | Similar to 'onChange' but contains its initial value in the first
69 -- event.
70 onChange' :: (Eq a) => SF a (Event a)
71 onChange' = proc x -> do
72 x' <- stepBack -< x
73 -- If it's the first value, throw an Event, else behave like onChange.
74 let makeEvent x x'
75 | isNothing x' = Event x
76 | otherwise = let x'' = fromJust x' in
77 if x'' == x then NoEvent else Event x
78 returnA -< makeEvent x x'
79
80 -- | Integrates some variable modulo something.
81 integralMod :: (Real a, VectorSpace a s) => a -> SF a a
82 integralMod x = intMod' 0
83 where intMod' x0 = switch (intMod'' x0) (\y -> intMod' (y - x))
84 intMod'' x0 = proc t -> do
85 it <- (+ x0) ^<< integral -< t
86 es <- edgeBy (\_ y -> maybeIf (y > x) $> y) 0 -< it
87 returnA -< (it,es)
88
89
90
91 -- | Generates a sine function whose period is given as a varying input.
92 varFreqSine :: SF DTime Double
93 varFreqSine = sin ^<< (2*pi*) ^<< integralMod 1 <<^ (1/)
94
95 -- | Generates an 'Event' with a regular period, which is given as an input to the signal function.
96 repeatedlyS :: a -> SF DTime (Event a)
97 repeatedlyS x = edgeBy (\a b -> maybeIf (a * b < 0) $> x) 0
98 <<< varFreqSine <<^ (2*)
99
100 repeatedlyS' :: a -> SF DTime (Event a)
101 repeatedlyS' x = (repeatedlyS x &&& now x) >>> arr (uncurry lMerge)
102
103 -- |
104 -- = Curry and uncurry functions