{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module Derive.EvalTrack (
TrackInfo(..)
, GetLastVal
, derive_control_track, derive_note_track
, defragment_track_signals, unwarp
, derive_event, context
#ifdef TESTING
, module Derive.EvalTrack
#endif
) where
import qualified Data.List as List
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import qualified Data.Text as Text
import qualified Util.Lists as Lists
import qualified Util.Log as Log
import qualified Util.Pretty as Pretty
import qualified App.Config as Config
import qualified Derive.Derive as Derive
import qualified Derive.Deriver.Internal as Internal
import qualified Derive.EnvKey as EnvKey
import qualified Derive.Eval as Eval
import qualified Derive.Parse as Parse
import qualified Derive.ParseTitle as ParseTitle
import qualified Derive.Score as Score
import qualified Derive.Slice as Slice
import qualified Derive.Stack as Stack
import qualified Derive.Stream as Stream
import qualified Derive.Warp as Warp
import qualified Perform.RealTime as RealTime
import qualified Perform.Signal as Signal
import qualified Ui.Event as Event
import qualified Ui.Events as Events
import qualified Ui.Track as Track
import qualified Ui.TrackTree as TrackTree
import Global
import Types
data TrackInfo d = TrackInfo {
forall d. TrackInfo d -> Track
tinfo_track :: !TrackTree.Track
, forall d. TrackInfo d -> EventsTree
tinfo_sub_tracks :: !TrackTree.EventsTree
, forall d. TrackInfo d -> Type
tinfo_type :: !ParseTitle.Type
, forall d. TrackInfo d -> GetLastVal d
tinfo_get_last_val :: GetLastVal d
}
tinfo_prev_val :: TrackInfo d -> Maybe d -> Stream.Stream d -> Maybe d
tinfo_prev_val :: forall d. TrackInfo d -> Maybe d -> Stream d -> Maybe d
tinfo_prev_val TrackInfo d
tinfo Maybe d
prev_val Stream d
levents =
forall d. TrackInfo d -> GetLastVal d
tinfo_get_last_val TrackInfo d
tinfo (forall a. Stream a -> [a]
Stream.events_of Stream d
levents) forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Maybe d
prev_val
instance Pretty (TrackInfo d) where
format :: TrackInfo d -> Doc
format (TrackInfo Track
track EventsTree
subs Type
ttype GetLastVal d
_) = Doc -> [(Text, Doc)] -> Doc
Pretty.record Doc
"TrackInfo"
[ (Text
"track", forall a. Pretty a => a -> Doc
Pretty.format Track
track)
, (Text
"sub_tracks", forall a. Pretty a => a -> Doc
Pretty.format EventsTree
subs)
, (Text
"type", forall a. Pretty a => a -> Doc
Pretty.format Type
ttype)
]
type GetLastVal d = [d] -> Maybe d
type DeriveResult d = ([Stream.Stream d], Derive.Threaded, Derive.Collect)
{-# SCC derive_control_track #-}
derive_control_track :: Derive.CallableExpr d => Derive.State -> TrackInfo d
-> DeriveResult d
derive_control_track :: forall d. CallableExpr d => State -> TrackInfo d -> DeriveResult d
derive_control_track State
state TrackInfo d
tinfo =
forall d a.
Taggable d =>
Track -> ((State, Maybe d), a) -> (a, Threaded, Collect)
post_track Track
track forall a b. (a -> b) -> a -> b
$ forall {a} {b} {b} {b}. ((a, b, b), b) -> ((a, b), b)
use_save_val forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
List.mapAccumL (forall d.
CallableExpr d =>
TrackInfo d
-> (State, Maybe d, Maybe d)
-> ([Event], [Event])
-> ((State, Maybe d, Maybe d), Stream d)
derive_control_track_stream TrackInfo d
tinfo) (State, Maybe d, Maybe d)
initial_state
(forall d. TrackInfo d -> [([Event], [Event])]
event_prev_nexts TrackInfo d
tinfo)
where
initial_state :: (State, Maybe d, Maybe d)
initial_state = (Track -> State -> State
record_track_dynamic Track
track State
state, Maybe d
val, Maybe d
val)
where val :: Maybe d
val = forall a. Taggable a => Track -> State -> Maybe a
lookup_prev_val Track
track State
state
track :: Track
track = forall d. TrackInfo d -> Track
tinfo_track TrackInfo d
tinfo
use_save_val :: ((a, b, b), b) -> ((a, b), b)
use_save_val ((a
state, b
_, b
save_val), b
result) = ((a
state, b
save_val), b
result)
{-# SCC derive_note_track #-}
derive_note_track :: (TrackTree.EventsTree -> Derive.NoteDeriver)
-> Derive.State -> TrackInfo Score.Event -> DeriveResult Score.Event
derive_note_track :: (EventsTree -> NoteDeriver)
-> State -> TrackInfo Event -> DeriveResult Event
derive_note_track EventsTree -> NoteDeriver
derive_tracks State
state TrackInfo Event
tinfo
| Track -> Sliced
TrackTree.track_sliced (forall d. TrackInfo d -> Track
tinfo_track TrackInfo Event
tinfo) forall a. Eq a => a -> a -> Bool
== Sliced
TrackTree.Inversion =
State -> TrackInfo Event -> Inversion -> DeriveResult Event
derive_inverted State
state TrackInfo Event
tinfo forall a b. (a -> b) -> a -> b
$
Dynamic -> Inversion
Derive.state_inversion (State -> Dynamic
Derive.state_dynamic State
state)
| Bool
otherwise = (EventsTree -> NoteDeriver)
-> State -> TrackInfo Event -> DeriveResult Event
derive_note_track_ EventsTree -> NoteDeriver
derive_tracks State
state TrackInfo Event
tinfo
derive_note_track_ :: (TrackTree.EventsTree -> Derive.NoteDeriver)
-> Derive.State -> TrackInfo Score.Event
-> ([Stream.Stream Score.Event], Derive.Threaded, Derive.Collect)
derive_note_track_ :: (EventsTree -> NoteDeriver)
-> State -> TrackInfo Event -> DeriveResult Event
derive_note_track_ EventsTree -> NoteDeriver
derive_tracks State
state TrackInfo Event
tinfo =
forall d a.
Taggable d =>
Track -> ((State, Maybe d), a) -> (a, Threaded, Collect)
post_track Track
track forall a b. (a -> b) -> a -> b
$
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
List.mapAccumL ((EventsTree -> NoteDeriver)
-> TrackInfo Event
-> (State, Maybe Event)
-> ([Event], [Event])
-> ((State, Maybe Event), Stream Event)
derive_note_track_stream EventsTree -> NoteDeriver
derive_tracks TrackInfo Event
tinfo) (State, Maybe Event)
initial_state
(forall d. TrackInfo d -> [([Event], [Event])]
event_prev_nexts TrackInfo Event
tinfo)
where
initial_state :: (State, Maybe Event)
initial_state = (Track -> State -> State
record_track_dynamic Track
track State
state, Maybe Event
val)
where val :: Maybe Event
val = forall a. Taggable a => Track -> State -> Maybe a
lookup_prev_val Track
track State
state
track :: Track
track = forall d. TrackInfo d -> Track
tinfo_track TrackInfo Event
tinfo
derive_inverted :: Derive.State -> TrackInfo Score.Event
-> Derive.Inversion -> DeriveResult Score.Event
derive_inverted :: State -> TrackInfo Event -> Inversion -> DeriveResult Event
derive_inverted State
state_ TrackInfo Event
tinfo Inversion
inversion =
([Stream Event
levents], Threaded
threaded, State -> Collect
Derive.state_collect State
next_state)
where
(Stream Event
levents, State
next_state) = forall d. State -> Deriver (Stream d) -> (Stream d, State)
run_derive State
state forall a b. (a -> b) -> a -> b
$ case Inversion
inversion of
Inversion
Derive.NotInverted ->
forall a. Stack => Text -> Deriver a
Derive.throw Text
"inverted track didn't set state_inversion"
Derive.InversionInProgress NoteDeriver
generator -> forall d a. TrackInfo d -> Deriver a -> Deriver a
with_inverted TrackInfo Event
tinfo forall a b. (a -> b) -> a -> b
$
Dynamic -> NoteDeriver -> NoteDeriver
Derive.state_under_invert (State -> Dynamic
Derive.state_dynamic State
state) NoteDeriver
generator
threaded :: Threaded
threaded = forall a. Taggable a => Track -> Maybe a -> Threaded -> Threaded
stash_prev_val Track
track Maybe Event
next_val forall a b. (a -> b) -> a -> b
$ State -> Threaded
Derive.state_threaded State
next_state
prev_val :: Maybe Event
prev_val = forall a. Taggable a => Track -> State -> Maybe a
lookup_prev_val Track
track State
state_
next_val :: Maybe Event
next_val = forall d. TrackInfo d -> Maybe d -> Stream d -> Maybe d
tinfo_prev_val TrackInfo Event
tinfo Maybe Event
prev_val Stream Event
levents
track :: Track
track = forall d. TrackInfo d -> Track
tinfo_track TrackInfo Event
tinfo
state :: State
state = Track -> State -> State
record_track_dynamic Track
track State
state_
with_inverted :: TrackInfo d -> Derive.Deriver a -> Derive.Deriver a
with_inverted :: forall d a. TrackInfo d -> Deriver a -> Deriver a
with_inverted TrackInfo d
tinfo = forall a. (Dynamic -> Dynamic) -> Deriver a -> Deriver a
Internal.local forall a b. (a -> b) -> a -> b
$ \Dynamic
state ->
forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id Frame -> Dynamic -> Dynamic
Internal.add_stack_frame Maybe Frame
frame forall a b. (a -> b) -> a -> b
$ Dynamic
state
{ state_inversion :: Inversion
Derive.state_inversion = Inversion
Derive.NotInverted }
where
frame :: Maybe Frame
frame = (\Event
e -> ScoreTime -> ScoreTime -> Frame
Stack.Region (Event -> ScoreTime
Event.min Event
e forall a. Num a => a -> a -> a
+ ScoreTime
shifted) (Event -> ScoreTime
Event.max Event
e forall a. Num a => a -> a -> a
+ ScoreTime
shifted))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Event
maybe_event
shifted :: ScoreTime
shifted = Track -> ScoreTime
TrackTree.track_shifted (forall d. TrackInfo d -> Track
tinfo_track TrackInfo d
tinfo)
maybe_event :: Maybe Event
maybe_event = Events -> Maybe Event
Events.head forall a b. (a -> b) -> a -> b
$ Track -> Events
TrackTree.track_events forall a b. (a -> b) -> a -> b
$ forall d. TrackInfo d -> Track
tinfo_track TrackInfo d
tinfo
post_track :: Derive.Taggable d => TrackTree.Track
-> ((Derive.State, Maybe d), a) -> (a, Derive.Threaded, Derive.Collect)
post_track :: forall d a.
Taggable d =>
Track -> ((State, Maybe d), a) -> (a, Threaded, Collect)
post_track Track
track ((State
state, Maybe d
save_val), a
result) =
( a
result
, forall a. Taggable a => Track -> Maybe a -> Threaded -> Threaded
stash_prev_val Track
track Maybe d
save_val forall a b. (a -> b) -> a -> b
$ State -> Threaded
Derive.state_threaded State
state
, State -> Collect
Derive.state_collect State
state
)
event_prev_nexts :: TrackInfo d -> [([Event.Event], [Event.Event])]
event_prev_nexts :: forall d. TrackInfo d -> [([Event], [Event])]
event_prev_nexts =
forall a. [a] -> [a] -> [([a], [a])]
Lists.zipper [] forall b c a. (b -> c) -> (a -> b) -> a -> c
. Events -> [Event]
Events.ascending forall b c a. (b -> c) -> (a -> b) -> a -> c
. Track -> Events
TrackTree.track_events forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall d. TrackInfo d -> Track
tinfo_track
derive_control_track_stream :: Derive.CallableExpr d
=> TrackInfo d
-> (Derive.State, Maybe d, Maybe d)
-> ([Event.Event], [Event.Event])
-> ((Derive.State, Maybe d, Maybe d), Stream.Stream d)
derive_control_track_stream :: forall d.
CallableExpr d =>
TrackInfo d
-> (State, Maybe d, Maybe d)
-> ([Event], [Event])
-> ((State, Maybe d, Maybe d), Stream d)
derive_control_track_stream TrackInfo d
tinfo (State
prev_state, Maybe d
prev_val, Maybe d
prev_save_val)
([Event]
prev_events, [Event]
cur_events) =
((State
state, Maybe d
next_val, Maybe d
save_val), Stream d
stream)
where
(Stream d
stream, State
state) = case Maybe (Deriver (Stream d))
derivers of
Just Deriver (Stream d)
deriver -> forall d. State -> Deriver (Stream d) -> (Stream d, State)
run_derive (State -> State
reset_event_serial State
prev_state) Deriver (Stream d)
deriver
Maybe (Deriver (Stream d))
Nothing -> (forall a. Stream a
Stream.empty, State
prev_state)
derivers :: Maybe (Deriver (Stream d))
derivers = case [Event]
cur_events of
Event
event : [Event]
next_events -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
forall d.
CallableExpr d =>
Context d -> Event -> Deriver (Stream d)
derive_event (forall a val.
TrackInfo a
-> Maybe val -> [Event] -> Event -> [Event] -> Context val
context TrackInfo d
tinfo Maybe d
prev_val [Event]
prev_events Event
event [Event]
next_events)
Event
event
[] -> forall a. Maybe a
Nothing
next_val :: Maybe d
next_val = forall d. TrackInfo d -> Maybe d -> Stream d -> Maybe d
tinfo_prev_val TrackInfo d
tinfo Maybe d
prev_val Stream d
stream
save_val :: Maybe d
save_val = if Bool
should_save_val then Maybe d
next_val else Maybe d
prev_save_val
should_save_val :: Bool
should_save_val = case [Event]
cur_events of
Event
_ : Event
next : [Event]
_ ->
Event -> ScoreTime
Event.start Event
next forall a. Ord a => a -> a -> Bool
<= Track -> ScoreTime
TrackTree.track_end (forall d. TrackInfo d -> Track
tinfo_track TrackInfo d
tinfo)
[Event]
_ -> Bool
False
{-# SCC derive_note_track_stream #-}
derive_note_track_stream :: (TrackTree.EventsTree -> Derive.NoteDeriver)
-> TrackInfo Score.Event
-> (Derive.State, Maybe Score.Event)
-> ([Event.Event], [Event.Event])
-> ((Derive.State, Maybe Score.Event), Stream.Stream Score.Event)
derive_note_track_stream :: (EventsTree -> NoteDeriver)
-> TrackInfo Event
-> (State, Maybe Event)
-> ([Event], [Event])
-> ((State, Maybe Event), Stream Event)
derive_note_track_stream EventsTree -> NoteDeriver
derive_tracks TrackInfo Event
tinfo (State
prev_state, Maybe Event
prev_val)
([Event]
prev_events, [Event]
cur_events) =
((State
next_state, Maybe Event
next_val), Stream Event
stream)
where
(Stream Event
stream, State
next_state)
| forall (t :: * -> *) a. Foldable t => t a -> Bool
null [NoteDeriver]
derivers = (forall a. Stream a
Stream.empty, State
prev_state)
| Bool
otherwise = forall d. State -> Deriver (Stream d) -> (Stream d, State)
run_derive (State -> State
reset_event_serial State
prev_state)
(forall a. Monoid a => [a] -> a
mconcat [NoteDeriver]
derivers)
derivers :: [NoteDeriver]
derivers = forall a. [Maybe a] -> [a]
Maybe.catMaybes forall a b. (a -> b) -> a -> b
$ case [Event]
cur_events of
Event
event : [Event]
next_events ->
[ Maybe Event -> Maybe Event -> Maybe NoteDeriver
derive_empty (forall a. [a] -> Maybe a
Lists.head [Event]
prev_events) (forall a. a -> Maybe a
Just Event
event)
, forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Event -> [Event] -> NoteDeriver
derive_note Event
event [Event]
next_events
]
[] -> [Maybe Event -> Maybe Event -> Maybe NoteDeriver
derive_empty (forall a. [a] -> Maybe a
Lists.head [Event]
prev_events) forall a. Maybe a
Nothing]
derive_note :: Event -> [Event] -> NoteDeriver
derive_note Event
event [Event]
next_events = forall d.
CallableExpr d =>
Context d -> Event -> Deriver (Stream d)
derive_event Context Event
ctx Event
event
where ctx :: Context Event
ctx = forall a val.
TrackInfo a
-> Maybe val -> [Event] -> Event -> [Event] -> Context val
context TrackInfo Event
tinfo Maybe Event
prev_val [Event]
prev_events Event
event [Event]
next_events
next_val :: Maybe Event
next_val = forall d. TrackInfo d -> Maybe d -> Stream d -> Maybe d
tinfo_prev_val TrackInfo Event
tinfo Maybe Event
prev_val Stream Event
stream
derive_empty :: Maybe Event -> Maybe Event -> Maybe NoteDeriver
derive_empty Maybe Event
prev Maybe Event
next =
(EventsTree -> NoteDeriver)
-> Maybe Event -> ScoreTime -> EventsTree -> Maybe NoteDeriver
derive_orphans EventsTree -> NoteDeriver
derive_tracks Maybe Event
prev ScoreTime
end (forall d. TrackInfo d -> EventsTree
tinfo_sub_tracks TrackInfo Event
tinfo)
where
end :: ScoreTime
end = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Track -> ScoreTime
TrackTree.track_end (forall d. TrackInfo d -> Track
tinfo_track TrackInfo Event
tinfo)) Event -> ScoreTime
Event.start Maybe Event
next
reset_event_serial :: Derive.State -> Derive.State
reset_event_serial :: State -> State
reset_event_serial State
state =
case Dynamic -> Inversion
Derive.state_inversion (State -> Dynamic
Derive.state_dynamic State
state) of
Inversion
Derive.NotInverted -> State
state
{ state_threaded :: Threaded
Derive.state_threaded = (State -> Threaded
Derive.state_threaded State
state)
{ state_event_serial :: Serial
Derive.state_event_serial = Serial
0 }
}
Inversion
_ -> State
state
lookup_prev_val :: Derive.Taggable a => TrackTree.Track -> Derive.State
-> Maybe a
lookup_prev_val :: forall a. Taggable a => Track -> State -> Maybe a
lookup_prev_val Track
track State
state = do
(BlockId, TrackId)
block_track <- Track -> Maybe (BlockId, TrackId)
TrackTree.block_track_id Track
track
Tagged
tagged <- forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (BlockId, TrackId)
block_track forall a b. (a -> b) -> a -> b
$
Threaded -> Map (BlockId, TrackId) Tagged
Derive.state_prev_val (State -> Threaded
Derive.state_threaded State
state)
forall a. Taggable a => Tagged -> Maybe a
Derive.from_tagged Tagged
tagged
stash_prev_val :: Derive.Taggable a => TrackTree.Track -> Maybe a
-> Derive.Threaded -> Derive.Threaded
stash_prev_val :: forall a. Taggable a => Track -> Maybe a -> Threaded -> Threaded
stash_prev_val Track
track Maybe a
prev_val Threaded
threaded = forall a. a -> Maybe a -> a
fromMaybe Threaded
threaded forall a b. (a -> b) -> a -> b
$ do
a
val <- Maybe a
prev_val
(BlockId, TrackId)
block_track <- Track -> Maybe (BlockId, TrackId)
TrackTree.block_track_id Track
track
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Threaded
threaded
{ state_prev_val :: Map (BlockId, TrackId) Tagged
Derive.state_prev_val = forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (BlockId, TrackId)
block_track (forall a. Taggable a => a -> Tagged
Derive.to_tagged a
val)
(Threaded -> Map (BlockId, TrackId) Tagged
Derive.state_prev_val Threaded
threaded)
}
run_derive :: Derive.State -> Derive.Deriver (Stream.Stream d)
-> (Stream.Stream d, Derive.State)
run_derive :: forall d. State -> Deriver (Stream d) -> (Stream d, State)
run_derive State
state Deriver (Stream d)
deriver = case Either Error (Stream d)
result of
Right Stream d
stream -> (forall e. [Msg] -> Stream e -> Stream e
Stream.merge_logs [Msg]
logs Stream d
stream, State
out_state)
Left Error
err -> (forall e. [Msg] -> Stream e -> Stream e
Stream.merge_logs [Msg]
logs (forall {a}. Error -> Stream a
error_to_stream Error
err), State
state)
where
error_to_stream :: Error -> Stream a
error_to_stream = forall a. [Msg] -> Stream a
Stream.from_logs forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. a -> [a] -> [a]
:[]) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> Msg
Derive.error_to_warn
(Either Error (Stream d)
result, State
out_state, [Msg]
logs) = forall st err a. st -> Deriver st err a -> RunResult st err a
Derive.run State
state Deriver (Stream d)
deriver
derive_orphans :: (TrackTree.EventsTree -> Derive.NoteDeriver)
-> Maybe Event.Event -> TrackTime -> TrackTree.EventsTree
-> Maybe Derive.NoteDeriver
derive_orphans :: (EventsTree -> NoteDeriver)
-> Maybe Event -> ScoreTime -> EventsTree -> Maybe NoteDeriver
derive_orphans EventsTree -> NoteDeriver
derive_tracks Maybe Event
prev ScoreTime
end EventsTree
subs
| ScoreTime
start forall a. Ord a => a -> a -> Bool
>= ScoreTime
end = forall a. Maybe a
Nothing
| Bool
otherwise = case Either Text EventsTree
checked of
Left Text
err -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *). (Stack, LogMonad m) => Text -> m ()
Log.warn Text
err forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Stream a
Stream.empty
Right [] -> forall a. Maybe a
Nothing
Right EventsTree
slices -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ EventsTree -> NoteDeriver
derive_tracks EventsTree
slices
where
checked :: Either Text EventsTree
checked = Bool
-> ScoreTime -> ScoreTime -> EventsTree -> Either Text EventsTree
Slice.slice_orphans Bool
exclude_start ScoreTime
start ScoreTime
end EventsTree
subs
exclude_start :: Bool
exclude_start = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False ((forall a. Eq a => a -> a -> Bool
==ScoreTime
0) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event -> ScoreTime
Event.duration) Maybe Event
prev
start :: ScoreTime
start = forall b a. b -> (a -> b) -> Maybe a -> b
maybe ScoreTime
0 Event -> ScoreTime
Event.end Maybe Event
prev
record_track_dynamic :: TrackTree.Track -> Derive.State -> Derive.State
record_track_dynamic :: Track -> State -> State
record_track_dynamic Track
track State
state =
State
state { state_collect :: Collect
Derive.state_collect = Collect
collect }
where
collect :: Collect
collect = case Dynamic -> Maybe TrackDynamic
Internal.record_track_dynamic (State -> Dynamic
Derive.state_dynamic State
state) of
Maybe TrackDynamic
Nothing -> forall a. Monoid a => a
mempty
Just TrackDynamic
track_dyn
| Track -> Sliced
TrackTree.track_sliced Track
track forall a. Eq a => a -> a -> Bool
== Sliced
TrackTree.Inversion -> forall a. Monoid a => a
mempty
{ collect_track_dynamic_inverted :: TrackDynamic
Derive.collect_track_dynamic_inverted = TrackDynamic
track_dyn }
| Bool
otherwise -> forall a. Monoid a => a
mempty { collect_track_dynamic :: TrackDynamic
Derive.collect_track_dynamic = TrackDynamic
track_dyn }
defragment_track_signals :: Warp.Warp -> Derive.Collect -> Derive.Collect
defragment_track_signals :: Warp -> Collect -> Collect
defragment_track_signals Warp
warp Collect
collect
| forall k a. Map k a -> Bool
Map.null SignalFragments
fragments = Collect
collect
| Bool
otherwise = Collect
collect
{ collect_track_signals :: TrackSignals
Derive.collect_track_signals = Collect -> TrackSignals
Derive.collect_track_signals Collect
collect
forall a. Semigroup a => a -> a -> a
<> forall a b k. (a -> b) -> Map k a -> Map k b
Map.map forall {k}. Map k Control -> TrackSignal
defragment SignalFragments
fragments
, collect_signal_fragments :: SignalFragments
Derive.collect_signal_fragments = forall a. Monoid a => a
mempty
}
where
fragments :: SignalFragments
fragments = Collect -> SignalFragments
Derive.collect_signal_fragments Collect
collect
defragment :: Map k Control -> TrackSignal
defragment = Warp -> Control -> TrackSignal
unwarp Warp
warp forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => [a] -> a
mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall k a. Map k a -> [a]
Map.elems
unwarp :: Warp.Warp -> Signal.Control -> Track.TrackSignal
unwarp :: Warp -> Control -> TrackSignal
unwarp Warp
warp Control
control = case Warp -> Maybe Linear
Warp.is_linear Warp
warp of
Just Linear
linear -> Track.TrackSignal
{ ts_signal :: Display
ts_signal = forall {k1} {k2} (kind1 :: k1) (kind2 :: k2).
Signal kind1 -> Signal kind2
Signal.coerce Control
control
, ts_shift :: ScoreTime
ts_shift = RealTime -> ScoreTime
RealTime.to_score (Linear -> RealTime
Warp._shift Linear
linear)
, ts_stretch :: ScoreTime
ts_stretch = RealTime -> ScoreTime
RealTime.to_score (Linear -> RealTime
Warp._stretch Linear
linear)
}
Maybe Linear
Nothing -> Track.TrackSignal
{ ts_signal :: Display
ts_signal = Warp -> Control -> Display
Warp.unwarp_signal Warp
warp Control
control
, ts_shift :: ScoreTime
ts_shift = ScoreTime
0
, ts_stretch :: ScoreTime
ts_stretch = ScoreTime
1
}
{-# SCC derive_event #-}
derive_event :: Derive.CallableExpr d => Derive.Context d -> Event.Event
-> Derive.Deriver (Stream.Stream d)
derive_event :: forall d.
CallableExpr d =>
Context d -> Event -> Deriver (Stream d)
derive_event Context d
ctx Event
event
| Text
Config.event_comment Text -> Text -> Bool
`Text.isPrefixOf` (Char -> Bool) -> Text -> Text
Text.dropWhile (forall a. Eq a => a -> a -> Bool
==Char
' ') Text
text =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Stream a
Stream.empty
| Bool
otherwise = forall a. ScoreTime -> Event -> Deriver a -> Deriver a
with_event_region (forall val. Context val -> ScoreTime
Derive.ctx_track_shifted Context d
ctx) Event
event forall a b. (a -> b) -> a -> b
$
case Text -> Either Text Expr
Parse.parse_expr Text
text of
Left Text
err -> forall (m :: * -> *). (Stack, LogMonad m) => Text -> m ()
Log.warn Text
err forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Stream a
Stream.empty
Right Expr
expr -> forall {a}. Deriver a -> Deriver a
with_note_start_end forall a b. (a -> b) -> a -> b
$ forall d. CallableExpr d => Context d -> Expr -> Deriver (Stream d)
Eval.eval_toplevel Context d
ctx Expr
expr
where
text :: Text
text = Event -> Text
Event.text Event
event
with_note_start_end :: Deriver a -> Deriver a
with_note_start_end = case forall val. Context val -> Maybe Type
Derive.ctx_track_type Context d
ctx of
Just Type
ParseTitle.NoteTrack -> forall val a. ToVal val => [(Text, val)] -> Deriver a -> Deriver a
Derive.with_vals
[ (Text
EnvKey.note_start, Event -> ScoreTime
Event.start Event
event)
, (Text
EnvKey.note_end, Event -> ScoreTime
Event.end Event
event)
]
where event :: Event
event = forall val. Context val -> Event
Derive.ctx_event Context d
ctx
Maybe Type
_ -> forall a. a -> a
id
with_event_region :: ScoreTime -> Event.Event -> Derive.Deriver a
-> Derive.Deriver a
with_event_region :: forall a. ScoreTime -> Event -> Deriver a -> Deriver a
with_event_region ScoreTime
track_shifted Event
event =
forall a. ScoreTime -> ScoreTime -> Deriver a -> Deriver a
Internal.with_stack_region (Event -> ScoreTime
Event.min Event
event forall a. Num a => a -> a -> a
+ ScoreTime
track_shifted)
(Event -> ScoreTime
Event.max Event
event forall a. Num a => a -> a -> a
+ ScoreTime
track_shifted)
context :: TrackInfo a -> Maybe val
-> [Event.Event]
-> Event.Event
-> [Event.Event]
-> Derive.Context val
context :: forall a val.
TrackInfo a
-> Maybe val -> [Event] -> Event -> [Event] -> Context val
context TrackInfo a
tinfo Maybe val
prev_val [Event]
prev Event
event [Event]
next = Derive.Context
{ ctx_prev_val :: Maybe val
Derive.ctx_prev_val = Maybe val
prev_val
, ctx_event :: Event
Derive.ctx_event = Event
event
, ctx_prev_events :: [Event]
Derive.ctx_prev_events = [Event]
tprev forall a. [a] -> [a] -> [a]
++ [Event]
prev
, ctx_next_events :: [Event]
Derive.ctx_next_events = [Event]
next forall a. [a] -> [a] -> [a]
++ [Event]
tnext
, ctx_event_end :: ScoreTime
Derive.ctx_event_end = case [Event]
next forall a. [a] -> [a] -> [a]
++ [Event]
tnext of
[] -> Track -> ScoreTime
TrackTree.track_end Track
track
Event
event : [Event]
_ -> Event -> ScoreTime
Event.start Event
event
, ctx_track_shifted :: ScoreTime
Derive.ctx_track_shifted = Track -> ScoreTime
TrackTree.track_shifted Track
track
, ctx_sub_tracks :: EventsTree
Derive.ctx_sub_tracks = EventsTree
subs
, ctx_sub_events :: Maybe [[(ScoreTime, ScoreTime, NoteDeriver)]]
Derive.ctx_sub_events = forall a. Maybe a
Nothing
, ctx_track_type :: Maybe Type
Derive.ctx_track_type = forall a. a -> Maybe a
Just Type
ttype
}
where
TrackInfo Track
track EventsTree
subs Type
ttype GetLastVal a
_ = TrackInfo a
tinfo
([Event]
tprev, [Event]
tnext) = Track -> ([Event], [Event])
TrackTree.track_around Track
track