module Music.Score.Meta.Attribution (
Attribution,
attribution,
attributions,
getAttribution,
attribute,
attributeDuring,
composer,
composerDuring,
lyricist,
lyricistDuring,
arranger,
arrangerDuring,
withAttribution,
withAttribution',
) where
import Control.Lens (view)
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.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 Music.Pitch.Literal
import Music.Score.Meta
import Music.Score.Part
import Music.Score.Pitch
import Music.Score.Internal.Util
import Music.Time
import Music.Time.Reactive
newtype Attribution = Attribution (Map String (Option (Last String)))
deriving (Typeable, Monoid, Semigroup)
instance Show Attribution where
show (Attribution a) = "attributions " ++ show (Map.toList (fmap (fromJust . fmap getLast . getOption) $ a))
attributions :: [(String, String)] -> Attribution
attributions = Attribution . fmap (Option . Just . Last) . Map.fromList
attribution :: String -> String -> Attribution
attribution k v = Attribution . fmap (Option . Just . Last) $ Map.singleton k v
getAttribution :: Attribution -> String -> Maybe String
getAttribution (Attribution a) k = join $ k `Map.lookup` (fmap (fmap getLast . getOption) $ a)
attribute :: (HasMeta a, HasPosition a) => Attribution -> a -> a
attribute a x = attributeDuring (_era x) a x
attributeDuring :: (HasMeta a) => Span -> Attribution -> a -> a
attributeDuring s a = addMetaNote (view event (s, a))
composer :: (HasMeta a, HasPosition a) => String -> a -> a
composer t x = composerDuring (_era x) t x
composerDuring :: HasMeta a => Span -> String -> a -> a
composerDuring s x = attributeDuring s ("composer" `attribution` x)
lyricist :: (HasMeta a, HasPosition a) => String -> a -> a
lyricist t x = lyricistDuring (_era x) t x
lyricistDuring :: HasMeta a => Span -> String -> a -> a
lyricistDuring s x = attributeDuring s ("lyricist" `attribution` x)
arranger :: (HasMeta a, HasPosition a) => String -> a -> a
arranger t x = arrangerDuring (_era x) t x
arrangerDuring :: HasMeta a => Span -> String -> a -> a
arrangerDuring s x = attributeDuring s ("arranger" `attribution` x)
withAttribution :: String -> (String -> Score a -> Score a) -> Score a -> Score a
withAttribution name f = withAttribution' (fromMaybe id . fmap f . flip getAttribution name)
withAttribution' :: (Attribution -> Score a -> Score a) -> Score a -> Score a
withAttribution' = withMetaAtStart