module Music.Score.Meta (
IsAttribute,
Attribute,
wrapAttr,
unwrapAttr,
Meta,
addMetaNote,
addGlobalMetaNote,
runMeta,
HasMeta(..),
) where
import Control.Applicative
import Control.Arrow
import Control.Lens
import Control.Monad.Plus
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import qualified Data.List as List
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe
import Data.Monoid.WithSemigroup
import Data.Semigroup
import Data.Set (Set)
import qualified Data.Set as Set
import Data.String
import Data.Traversable (Traversable)
import qualified Data.Traversable as T
import Data.Typeable
import Data.Void
import Music.Pitch.Literal
import Music.Score.Note
import Music.Score.Part
import Music.Score.Pitch
import Music.Score.Util
import Music.Score.Voice
import Music.Time
type IsAttribute a = (Typeable a, Monoid' a)
data Attribute :: * where
Attribute :: IsAttribute a => a -> Attribute
wrapAttr :: IsAttribute a => a -> Attribute
wrapAttr = Attribute
unwrapAttr :: IsAttribute a => Attribute -> Maybe a
unwrapAttr (Attribute a) = cast a
instance Semigroup Attribute where
(Attribute a1) <> a2 = case unwrapAttr a2 of
Nothing -> a2
Just a2' -> Attribute (a1 <> a2')
instance Delayable Attribute where
delay _ (Attribute a) = Attribute a
instance Stretchable Attribute where
stretch _ (Attribute a) = Attribute a
newtype Meta = Meta (Map String (Reactive Attribute))
deriving (Delayable, Stretchable)
inMeta :: (Map String (Reactive Attribute) -> Map String (Reactive Attribute)) -> Meta -> Meta
inMeta f (Meta s) = Meta (f s)
addGlobalMetaNote :: forall a b . (IsAttribute a, HasMeta b) => Note a -> b -> b
addGlobalMetaNote x = applyMeta $ addMeta' (Nothing::Maybe Int) $ noteToReactive x
addMetaNote :: forall a b . (IsAttribute a, HasMeta b, HasPart' b) => Note a -> b -> b
addMetaNote x y = (applyMeta $ addMeta' (Just y) $ noteToReactive x) y
addMetaChange :: forall a b . (IsAttribute a, HasMeta b, HasPart' b) => Time -> a -> b -> b
addMetaChange t x y = (applyMeta $ addMeta' (Just y) $ switch t mempty (pure x)) y
runMeta :: forall a b . (HasPart' a, IsAttribute b) => Maybe a -> Meta -> Reactive b
runMeta part = fromMaybe mempty . runMeta' part
addMeta' :: forall a b . (HasPart' a, IsAttribute b) => Maybe a -> Reactive b -> Meta
addMeta' part a = Meta $ Map.singleton key $ fmap wrapAttr a
where
key = ty ++ pt
pt = show $ fmap getPart part
ty = show $ typeOf (undefined :: b)
runMeta' :: forall a b . (HasPart' a, IsAttribute b) => Maybe a -> Meta -> Maybe (Reactive b)
runMeta' part (Meta s) = fmap (fmap (fromMaybe (error "runMeta'") . unwrapAttr)) $ Map.lookup key s
where
key = ty ++ pt
pt = show $ fmap getPart part
ty = show . typeOf $ (undefined :: b)
instance Semigroup Meta where
Meta s1 <> Meta s2 = Meta $ Map.unionWith (<>) s1 s2
instance Monoid Meta where
mempty = Meta Map.empty
mappend = (<>)
class HasMeta a where
meta :: Lens' a Meta
instance HasMeta Meta where
meta = ($)
applyMeta :: HasMeta a => Meta -> a -> a
applyMeta m = (meta <>~ m)
newtype RehearsalMark = RehearsalMark ()
deriving (Typeable, Monoid, Semigroup)
noteToReactive :: Monoid a => Note a -> Reactive a
noteToReactive n = (pure <$> n) `activate` pure mempty
splitReactive :: Reactive a -> Either a ((a, Time), [Note a], (Time, a))
splitReactive r = case updates r of
[] -> Left (initial r)
(t,x):[] -> Right ((initial r, t), [], (t, x))
(t,x):xs -> Right ((initial r, t), fmap note $ mrights (res $ (t,x):xs), head $ mlefts (res $ (t,x):xs))
where
note (t,u,x) = t <-> u =: x
res :: [(Time, a)] -> [Either (Time, a) (Time, Time, a)]
res rs = let (ts,xs) = unzip rs in
flip fmap (withNext ts `zip` xs) $
\ ((t, mu), x) -> case mu of
Nothing -> Left (t, x)
Just u -> Right (t, u, x)
withNext :: [a] -> [(a, Maybe a)]
withNext = go
where
go [] = []
go [x] = [(x, Nothing)]
go (x:y:rs) = (x, Just y) : withNext (y : rs)
activate :: Note (Reactive a) -> Reactive a -> Reactive a
activate (getNote -> (view range -> (start,stop), x)) y = y `turnOn` (x `turnOff` y)
where
turnOn = switch start
turnOff = switch stop