git.iko.soy/rowtype-yoga/purescript-abcjs
on main help sign in
78 lines 2.7 kB history raw
1module Abcjs.Timing
2 ( TimingCallbacks
3 , TimingOptions
4 , newTimingCallbacks
5 , start
6 , stop
7 , pause
8 , reset
9 , setProgress
10 ) where
12import Prelude
14import Abcjs.Types (BeatNumber, Fraction, Qpm, TimingEvent, TotalBeats, TuneObject)
15import Data.Nullable (Nullable)
16import Data.Time.Duration (Seconds)
17import Effect (Effect)
18import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn4, mkEffectFn1, mkEffectFn3, runEffectFn1, runEffectFn2, runEffectFn4)
19import Prim.Row (class Union)
21-- | A standalone animation timer over a rendered tune. Drives the cursor and
22-- | game logic off note timing, independent of whether audio is playing.
23foreign import data TimingCallbacks :: Type
25-- | Timer options as a row, so a caller supplies only what they need; anything
26-- | omitted falls back to abcjs (no `qpm` means the tune's written tempo).
27-- | `eventCallback` fires per note/rest/bar; abcjs passes `null` at the end of
28-- | the tune, hence `Nullable`. `beatCallback` receives
29-- | (beatNumber, totalBeats, totalTime).
30type TimingOptions =
31 ( qpm :: Qpm
32 , eventCallback :: Nullable TimingEvent -> Effect Unit
33 , beatCallback :: BeatNumber -> TotalBeats -> Seconds -> Effect Unit
34 )
36newTimingCallbacks
37 :: forall provided rest
38 . Union provided rest TimingOptions
39 => TuneObject -> { | provided } -> Effect TimingCallbacks
40newTimingCallbacks tune options =
41 runEffectFn4 newTimingCallbacksImpl mkEffectFn1 mkEffectFn3 tune options
43-- | The FFI gets the converters and the partial options, and converts each
44-- | callback only where supplied — omitted fields are left off the abcjs object.
45foreign import newTimingCallbacksImpl
46 :: forall provided
47 . EffectFn4
48 ((Nullable TimingEvent -> Effect Unit) -> EffectFn1 (Nullable TimingEvent) Unit)
49 ((BeatNumber -> TotalBeats -> Seconds -> Effect Unit) -> EffectFn3 BeatNumber TotalBeats Seconds Unit)
50 TuneObject
51 { | provided }
52 TimingCallbacks
54start :: TimingCallbacks -> Effect Unit
55start = runEffectFn1 startImpl
57foreign import startImpl :: EffectFn1 TimingCallbacks Unit
59stop :: TimingCallbacks -> Effect Unit
60stop = runEffectFn1 stopImpl
62foreign import stopImpl :: EffectFn1 TimingCallbacks Unit
64pause :: TimingCallbacks -> Effect Unit
65pause = runEffectFn1 pauseImpl
67foreign import pauseImpl :: EffectFn1 TimingCallbacks Unit
69reset :: TimingCallbacks -> Effect Unit
70reset = runEffectFn1 resetImpl
72foreign import resetImpl :: EffectFn1 TimingCallbacks Unit
74-- | Jump the cursor to a fraction of the tune (0.0 to 1.0).
75setProgress :: Fraction -> TimingCallbacks -> Effect Unit
76setProgress = runEffectFn2 setProgressImpl
78foreign import setProgressImpl :: EffectFn2 Fraction TimingCallbacks Unit