]> Git — Sourcephile - tmp/julm/arpeggigon.git/blob - RMCA/Translator/Filter.hs
Tiles are removable by dragging them outside.
[tmp/julm/arpeggigon.git] / RMCA / Translator / Filter.hs
1 -- Contains function for scheduling and filtering events given the
2 -- correct informations.
3
4 module RMCA.Translator.Filter where
5
6 import Data.Bifunctor as BF
7 import Data.Function (on)
8 import Data.List (group, groupBy, sortBy)
9 import Data.Ord
10 import FRP.Yampa
11 import RMCA.Semantics
12 import RMCA.Translator.Message
13 import Sound.JACK (NFrames (NFrames))
14
15 -- Takes a list of time stamped "things", a sample rate and a buffer
16 -- size. The function argument is a function that needs to tell which
17 -- arguments are kept in the case where two would come into
18 -- contact. On the left are the events that can be thrown into the
19 -- buffer, on the right are the events that will need to wait. Both
20 -- list are sorted.
21 --
22 -- /!\ The frame number is relative. A preprocessing operation
23 -- removing all events too soon to be happening and shifting them is
24 -- necessary.
25 schedule :: Frames
26 -> [(Frames, a)]
27 -> ([(Frames,a)], [(Frames,a)])
28 schedule size = BF.first scatterEvents
29 . break ((>= size) . fst) . sortBy (comparing fst)
30
31 {- Rendered useless
32 -- The function choose between the event in case two are in conflict.
33 --
34 -- /!\ That functional argument is a bit unsatisfying, it would be
35 -- probably better if we'd try to push events to the next frame if
36 -- they conflict and only remove them if it's impossible to do
37 -- otherwise.
38 nubDuplicate :: (Eq a) => ([a] -> a) -> [(Frames, a)] -> [(Frames, a)]
39 nubDuplicate f = map (BF.second f) . scatterEvents
40 . map (\l@((n,_):_) -> (n,map snd l)) . group
41 -}
42 -- When to events are at the same frame, shift them so that they are
43 -- all separated by one frame. Then take every list and make sure that
44 -- the first frame of the next list is at least one frame after the
45 -- last frame of that list.
46 scatterEvents :: [(Frames, a)] -> [(Frames, a)]
47 scatterEvents (x@(n,a):(m,b):xs) = x:scatterEvents ((m',b):xs)
48 where m' = m + max 0 (1 + n - m)
49 scatterEvents [x] = [x]
50 scatterEvents _ = []