module Sound.SC3.Server.NRT.Edit where
import Data.List
import qualified Data.List.Ordered as L
import Sound.OSC
import Sound.SC3.Server.Command
import Sound.SC3.Server.NRT
insertBy_post :: (a -> a -> Ordering) -> a -> [a] -> [a]
insertBy_post :: (a -> a -> Ordering) -> a -> [a] -> [a]
insertBy_post a -> a -> Ordering
cmp a
e [a]
l =
case [a]
l of
[] -> [a
e]
a
h:[a]
l' -> case a -> a -> Ordering
cmp a
e a
h of
Ordering
LT -> a
e a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
l
Ordering
_ -> a
h a -> [a] -> [a]
forall a. a -> [a] -> [a]
: (a -> a -> Ordering) -> a -> [a] -> [a]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
insertBy_post a -> a -> Ordering
cmp a
e [a]
l'
insert_post :: Bundle -> [Bundle] -> [Bundle]
insert_post :: Bundle -> [Bundle] -> [Bundle]
insert_post = (Bundle -> Bundle -> Ordering) -> Bundle -> [Bundle] -> [Bundle]
forall a. (a -> a -> Ordering) -> a -> [a] -> [a]
insertBy_post Bundle -> Bundle -> Ordering
forall a. Ord a => a -> a -> Ordering
compare
at_last :: (a -> b) -> (a -> b) -> [a] -> [b]
at_last :: (a -> b) -> (a -> b) -> [a] -> [b]
at_last a -> b
f a -> b
g [a]
x =
case [a]
x of
[] -> []
[a
i] -> [a -> b
g a
i]
a
i:[a]
x' -> a -> b
f a
i b -> [b] -> [b]
forall a. a -> [a] -> [a]
: (a -> b) -> (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> (a -> b) -> [a] -> [b]
at_last a -> b
f a -> b
g [a]
x'
nrt_merge :: NRT -> NRT -> NRT
nrt_merge :: NRT -> NRT -> NRT
nrt_merge (NRT [Bundle]
p) (NRT [Bundle]
q) = [Bundle] -> NRT
NRT ([Bundle] -> [Bundle] -> [Bundle]
forall a. Ord a => [a] -> [a] -> [a]
L.merge [Bundle]
p [Bundle]
q)
nrt_merge_set :: [NRT] -> NRT
nrt_merge_set :: [NRT] -> NRT
nrt_merge_set = (NRT -> NRT -> NRT) -> NRT -> [NRT] -> NRT
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr NRT -> NRT -> NRT
nrt_merge NRT
nrt_empty
nrt_empty :: NRT
nrt_empty :: NRT
nrt_empty = [Bundle] -> NRT
NRT []
nrt_insert_pre :: Bundle -> NRT -> NRT
nrt_insert_pre :: Bundle -> NRT -> NRT
nrt_insert_pre Bundle
p (NRT [Bundle]
q) = [Bundle] -> NRT
NRT (Bundle -> [Bundle] -> [Bundle]
forall a. Ord a => a -> [a] -> [a]
insert Bundle
p [Bundle]
q)
nrt_insert_post :: Bundle -> NRT -> NRT
nrt_insert_post :: Bundle -> NRT -> NRT
nrt_insert_post Bundle
p (NRT [Bundle]
q) = [Bundle] -> NRT
NRT (Bundle -> [Bundle] -> [Bundle]
insert_post Bundle
p [Bundle]
q)
nrt_end_time :: NRT -> Time
nrt_end_time :: NRT -> Time
nrt_end_time = Bundle -> Time
bundleTime (Bundle -> Time) -> (NRT -> Bundle) -> NRT -> Time
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bundle] -> Bundle
forall a. [a] -> a
last ([Bundle] -> Bundle) -> (NRT -> [Bundle]) -> NRT -> Bundle
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NRT -> [Bundle]
nrt_bundles
bundle_map :: (Time -> Time) -> ([Message] -> [Message]) -> Bundle -> Bundle
bundle_map :: (Time -> Time) -> ([Message] -> [Message]) -> Bundle -> Bundle
bundle_map Time -> Time
t_f [Message] -> [Message]
m_f (Bundle Time
t [Message]
m) = Time -> [Message] -> Bundle
Bundle (Time -> Time
t_f Time
t) ([Message] -> [Message]
m_f [Message]
m)
nrt_close :: NRT -> NRT
nrt_close :: NRT -> NRT
nrt_close (NRT [Bundle]
l) =
let is_nrt_end_msg :: Message -> Bool
is_nrt_end_msg = ([Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"/nrt_end") ([Char] -> Bool) -> (Message -> [Char]) -> Message -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> [Char]
messageAddress
rem_end_msg :: Bundle -> Bundle
rem_end_msg = (Time -> Time) -> ([Message] -> [Message]) -> Bundle -> Bundle
bundle_map Time -> Time
forall a. a -> a
id ((Message -> Bool) -> [Message] -> [Message]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Message -> Bool) -> Message -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> Bool
is_nrt_end_msg))
req_end_msg :: Bundle -> Bundle
req_end_msg = let f :: [Message] -> [Message]
f [Message]
m = if (Message -> Bool) -> [Message] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Message -> Bool
is_nrt_end_msg [Message]
m
then [Message]
m
else [Message]
m [Message] -> [Message] -> [Message]
forall a. [a] -> [a] -> [a]
++ [Message
nrt_end]
in (Time -> Time) -> ([Message] -> [Message]) -> Bundle -> Bundle
bundle_map Time -> Time
forall a. a -> a
id [Message] -> [Message]
f
in [Bundle] -> NRT
NRT ((Bundle -> Bundle) -> (Bundle -> Bundle) -> [Bundle] -> [Bundle]
forall a b. (a -> b) -> (a -> b) -> [a] -> [b]
at_last Bundle -> Bundle
rem_end_msg Bundle -> Bundle
req_end_msg [Bundle]
l)
nrt_append :: NRT -> NRT -> NRT
nrt_append :: NRT -> NRT -> NRT
nrt_append (NRT [Bundle]
p) (NRT [Bundle]
q) = [Bundle] -> NRT
NRT ([Bundle]
p [Bundle] -> [Bundle] -> [Bundle]
forall a. [a] -> [a] -> [a]
++ [Bundle]
q)