{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveFunctor #-}
module Sound.Tidal.Time where
import Control.Applicative
import GHC.Generics
import Control.DeepSeq (NFData)
type Time = Rational
data ArcF a = Arc
{ forall a. ArcF a -> a
start :: a
, forall a. ArcF a -> a
stop :: a
} deriving (ArcF a -> ArcF a -> Bool
forall a. Eq a => ArcF a -> ArcF a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ArcF a -> ArcF a -> Bool
$c/= :: forall a. Eq a => ArcF a -> ArcF a -> Bool
== :: ArcF a -> ArcF a -> Bool
$c== :: forall a. Eq a => ArcF a -> ArcF a -> Bool
Eq, ArcF a -> ArcF a -> Bool
ArcF a -> ArcF a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (ArcF a)
forall a. Ord a => ArcF a -> ArcF a -> Bool
forall a. Ord a => ArcF a -> ArcF a -> Ordering
forall a. Ord a => ArcF a -> ArcF a -> ArcF a
min :: ArcF a -> ArcF a -> ArcF a
$cmin :: forall a. Ord a => ArcF a -> ArcF a -> ArcF a
max :: ArcF a -> ArcF a -> ArcF a
$cmax :: forall a. Ord a => ArcF a -> ArcF a -> ArcF a
>= :: ArcF a -> ArcF a -> Bool
$c>= :: forall a. Ord a => ArcF a -> ArcF a -> Bool
> :: ArcF a -> ArcF a -> Bool
$c> :: forall a. Ord a => ArcF a -> ArcF a -> Bool
<= :: ArcF a -> ArcF a -> Bool
$c<= :: forall a. Ord a => ArcF a -> ArcF a -> Bool
< :: ArcF a -> ArcF a -> Bool
$c< :: forall a. Ord a => ArcF a -> ArcF a -> Bool
compare :: ArcF a -> ArcF a -> Ordering
$ccompare :: forall a. Ord a => ArcF a -> ArcF a -> Ordering
Ord, forall a b. a -> ArcF b -> ArcF a
forall a b. (a -> b) -> ArcF a -> ArcF b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ArcF b -> ArcF a
$c<$ :: forall a b. a -> ArcF b -> ArcF a
fmap :: forall a b. (a -> b) -> ArcF a -> ArcF b
$cfmap :: forall a b. (a -> b) -> ArcF a -> ArcF b
Functor, Int -> ArcF a -> ShowS
forall a. Show a => Int -> ArcF a -> ShowS
forall a. Show a => [ArcF a] -> ShowS
forall a. Show a => ArcF a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ArcF a] -> ShowS
$cshowList :: forall a. Show a => [ArcF a] -> ShowS
show :: ArcF a -> String
$cshow :: forall a. Show a => ArcF a -> String
showsPrec :: Int -> ArcF a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> ArcF a -> ShowS
Show, forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (ArcF a) x -> ArcF a
forall a x. ArcF a -> Rep (ArcF a) x
$cto :: forall a x. Rep (ArcF a) x -> ArcF a
$cfrom :: forall a x. ArcF a -> Rep (ArcF a) x
Generic)
type Arc = ArcF Time
instance Applicative ArcF where
pure :: forall a. a -> ArcF a
pure a
t = forall a. a -> a -> ArcF a
Arc a
t a
t
<*> :: forall a b. ArcF (a -> b) -> ArcF a -> ArcF b
(<*>) (Arc a -> b
sf a -> b
ef) (Arc a
sx a
ex) = forall a. a -> a -> ArcF a
Arc (a -> b
sf a
sx) (a -> b
ef a
ex)
instance NFData a => NFData (ArcF a)
instance Num a => Num (ArcF a) where
negate :: ArcF a -> ArcF a
negate = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
negate
+ :: ArcF a -> ArcF a -> ArcF a
(+) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(+)
* :: ArcF a -> ArcF a -> ArcF a
(*) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Num a => a -> a -> a
(*)
fromInteger :: Integer -> ArcF a
fromInteger = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger
abs :: ArcF a -> ArcF a
abs = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
abs
signum :: ArcF a -> ArcF a
signum = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Num a => a -> a
signum
instance (Fractional a) => Fractional (ArcF a) where
recip :: ArcF a -> ArcF a
recip = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Fractional a => a -> a
recip
fromRational :: Time -> ArcF a
fromRational = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Fractional a => Time -> a
fromRational
sam :: Time -> Time
sam :: Time -> Time
sam = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. (RealFrac a, Integral b) => a -> b
floor :: Time -> Int)
toTime :: Real a => a -> Rational
toTime :: forall a. Real a => a -> Time
toTime = forall a. Real a => a -> Time
toRational
fromTime :: Fractional a => Time -> a
fromTime :: forall a. Fractional a => Time -> a
fromTime = forall a. Fractional a => Time -> a
fromRational
nextSam :: Time -> Time
nextSam :: Time -> Time
nextSam = (Time
1forall a. Num a => a -> a -> a
+) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Time -> Time
sam
cyclePos :: Time -> Time
cyclePos :: Time -> Time
cyclePos Time
t = Time
t forall a. Num a => a -> a -> a
- Time -> Time
sam Time
t
hull :: Arc -> Arc -> Arc
hull :: Arc -> Arc -> Arc
hull (Arc Time
s Time
e) (Arc Time
s' Time
e') = forall a. a -> a -> ArcF a
Arc (forall a. Ord a => a -> a -> a
min Time
s Time
s') (forall a. Ord a => a -> a -> a
max Time
e Time
e')
subArc :: Arc -> Arc -> Maybe Arc
subArc :: Arc -> Arc -> Maybe Arc
subArc a :: Arc
a@(Arc Time
s Time
e) b :: Arc
b@(Arc Time
s' Time
e')
| forall (t :: * -> *). Foldable t => t Bool -> Bool
and [Time
s'' forall a. Eq a => a -> a -> Bool
== Time
e'', Time
s'' forall a. Eq a => a -> a -> Bool
== Time
e, Time
s forall a. Ord a => a -> a -> Bool
< Time
e] = forall a. Maybe a
Nothing
| forall (t :: * -> *). Foldable t => t Bool -> Bool
and [Time
s'' forall a. Eq a => a -> a -> Bool
== Time
e'', Time
s'' forall a. Eq a => a -> a -> Bool
== Time
e', Time
s' forall a. Ord a => a -> a -> Bool
< Time
e'] = forall a. Maybe a
Nothing
| Time
s'' forall a. Ord a => a -> a -> Bool
<= Time
e'' = forall a. a -> Maybe a
Just (forall a. a -> a -> ArcF a
Arc Time
s'' Time
e'')
| Bool
otherwise = forall a. Maybe a
Nothing
where (Arc Time
s'' Time
e'') = Arc -> Arc -> Arc
sect Arc
a Arc
b
subMaybeArc :: Maybe Arc -> Maybe Arc -> Maybe (Maybe Arc)
subMaybeArc :: Maybe Arc -> Maybe Arc -> Maybe (Maybe Arc)
subMaybeArc (Just Arc
a) (Just Arc
b) = do Arc
sa <- Arc -> Arc -> Maybe Arc
subArc Arc
a Arc
b
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just Arc
sa
subMaybeArc Maybe Arc
_ Maybe Arc
_ = forall a. a -> Maybe a
Just forall a. Maybe a
Nothing
sect :: Arc -> Arc -> Arc
sect :: Arc -> Arc -> Arc
sect (Arc Time
s Time
e) (Arc Time
s' Time
e') = forall a. a -> a -> ArcF a
Arc (forall a. Ord a => a -> a -> a
max Time
s Time
s') (forall a. Ord a => a -> a -> a
min Time
e Time
e')
timeToCycleArc :: Time -> Arc
timeToCycleArc :: Time -> Arc
timeToCycleArc Time
t = forall a. a -> a -> ArcF a
Arc (Time -> Time
sam Time
t) (Time -> Time
sam Time
t forall a. Num a => a -> a -> a
+ Time
1)
cycleArc :: Arc -> Arc
cycleArc :: Arc -> Arc
cycleArc (Arc Time
s Time
e) = forall a. a -> a -> ArcF a
Arc (Time -> Time
cyclePos Time
s) (Time -> Time
cyclePos Time
s forall a. Num a => a -> a -> a
+ (Time
eforall a. Num a => a -> a -> a
-Time
s))
cyclesInArc :: Integral a => Arc -> [a]
cyclesInArc :: forall a. Integral a => Arc -> [a]
cyclesInArc (Arc Time
s Time
e)
| Time
s forall a. Ord a => a -> a -> Bool
> Time
e = []
| Time
s forall a. Eq a => a -> a -> Bool
== Time
e = [forall a b. (RealFrac a, Integral b) => a -> b
floor Time
s]
| Bool
otherwise = [forall a b. (RealFrac a, Integral b) => a -> b
floor Time
s .. forall a b. (RealFrac a, Integral b) => a -> b
ceiling Time
eforall a. Num a => a -> a -> a
-a
1]
cycleArcsInArc :: Arc -> [Arc]
cycleArcsInArc :: Arc -> [Arc]
cycleArcsInArc = forall a b. (a -> b) -> [a] -> [b]
map (Time -> Arc
timeToCycleArc forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Real a => a -> Time
toTime :: Int -> Time)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Integral a => Arc -> [a]
cyclesInArc
arcCycles :: Arc -> [Arc]
arcCycles :: Arc -> [Arc]
arcCycles (Arc Time
s Time
e) | Time
s forall a. Ord a => a -> a -> Bool
>= Time
e = []
| Time -> Time
sam Time
s forall a. Eq a => a -> a -> Bool
== Time -> Time
sam Time
e = [forall a. a -> a -> ArcF a
Arc Time
s Time
e]
| Bool
otherwise = forall a. a -> a -> ArcF a
Arc Time
s (Time -> Time
nextSam Time
s) forall a. a -> [a] -> [a]
: Arc -> [Arc]
arcCycles (forall a. a -> a -> ArcF a
Arc (Time -> Time
nextSam Time
s) Time
e)
arcCyclesZW :: Arc -> [Arc]
arcCyclesZW :: Arc -> [Arc]
arcCyclesZW (Arc Time
s Time
e) | Time
s forall a. Eq a => a -> a -> Bool
== Time
e = [forall a. a -> a -> ArcF a
Arc Time
s Time
e]
| Bool
otherwise = Arc -> [Arc]
arcCycles (forall a. a -> a -> ArcF a
Arc Time
s Time
e)
mapCycle :: (Time -> Time) -> Arc -> Arc
mapCycle :: (Time -> Time) -> Arc -> Arc
mapCycle Time -> Time
f (Arc Time
s Time
e) = forall a. a -> a -> ArcF a
Arc (Time
sam' forall a. Num a => a -> a -> a
+ Time -> Time
f (Time
s forall a. Num a => a -> a -> a
- Time
sam')) (Time
sam' forall a. Num a => a -> a -> a
+ Time -> Time
f (Time
e forall a. Num a => a -> a -> a
- Time
sam'))
where sam' :: Time
sam' = Time -> Time
sam Time
s
isIn :: Arc -> Time -> Bool
isIn :: Arc -> Time -> Bool
isIn (Arc Time
s Time
e) Time
t = Time
t forall a. Ord a => a -> a -> Bool
>= Time
s Bool -> Bool -> Bool
&& Time
t forall a. Ord a => a -> a -> Bool
< Time
e