Skip to content

Commit

Permalink
move tickAction outside of config
Browse files Browse the repository at this point in the history
  • Loading branch information
polymorphicengine committed Dec 30, 2023
1 parent e73b570 commit 95b1763
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Sound/Tidal/Stream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ startStream config oscmap
return $ Cx {cxUDP = u, cxAddr = remote_addr, cxBusAddr = remote_bus_addr, cxTarget = target, cxOSCs = os}
) oscmap

clockRef <- Clock.clocked ((cClockConfig config) {Clock.cTickAction = doTick sMapMV bussesMV pMapMV globalFMV cxs listen})
clockRef <- Clock.clocked (cClockConfig config) (doTick sMapMV bussesMV pMapMV globalFMV cxs listen)

let stream = Stream {sConfig = config,
sBusses = bussesMV,
Expand Down
31 changes: 15 additions & 16 deletions tidal-link/src/hs/Sound/Tidal/Clock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ data ClockMemory
= ClockMemory
{clockConfig :: ClockConfig
,clockRef :: ClockRef
,clockAction :: TickAction
}

-- | internal mutable state of the clock
Expand Down Expand Up @@ -51,7 +52,6 @@ data ClockConfig
,cEnableLink :: Bool
,cSkipTicks :: Int64
,cProcessAhead :: Double
,cTickAction :: TickAction
}

-- | action to be executed on a tick,
Expand Down Expand Up @@ -90,26 +90,25 @@ defaultConfig = ClockConfig
,cSkipTicks = 10
,cQuantum = 4
,cBeatsPerCycle = 4
,cTickAction = \_ _ _ -> return ()
}

-- | creates a clock according to the config and runs it
-- | in a seperate thread
clocked :: ClockConfig -> IO ClockRef
clocked config = runClock config clockCheck
clocked :: ClockConfig -> TickAction -> IO ClockRef
clocked config ac = runClock config ac clockCheck

-- | runs the clock on the initial state and memory as given
-- | by initClock, hands the ClockRef for interaction from outside
runClock :: ClockConfig -> Clock () -> IO ClockRef
runClock config clock = do
(mem, st) <- initClock config
runClock :: ClockConfig -> TickAction -> Clock () -> IO ClockRef
runClock config ac clock = do
(mem, st) <- initClock config ac
_ <- forkIO $ evalStateT (runReaderT clock mem) st
return (clockRef mem)

-- | creates a ableton link instance and an MVar for interacting
-- | with the clock from outside and computes the initial clock state
initClock :: ClockConfig -> IO (ClockMemory, ClockState)
initClock config = do
initClock :: ClockConfig -> TickAction -> IO (ClockMemory, ClockState)
initClock config ac = do
abletonLink <- Link.create bpm
when (cEnableLink config) $ Link.enable abletonLink
sessionState <- Link.createAndCaptureAppSessionState abletonLink
Expand All @@ -123,7 +122,7 @@ initClock config = do
nowArc = (0,0),
nudged = 0
}
return (ClockMemory config (ClockRef clockMV abletonLink), st)
return (ClockMemory config (ClockRef clockMV abletonLink) ac, st)
where processAhead = round $ (cProcessAhead config) * 1000000
bpm = (coerce defaultCps) * 60 * (cBeatsPerCycle config)

Expand All @@ -138,7 +137,7 @@ initClock config = do
-- previously called checkArc
clockCheck :: Clock ()
clockCheck = do
(ClockMemory config (ClockRef clockMV abletonLink)) <- ask
(ClockMemory config (ClockRef clockMV abletonLink) _) <- ask

action <- liftIO $ swapMVar clockMV NoAction
processAction action
Expand All @@ -161,7 +160,7 @@ clockCheck = do
-- tick delays the thread when logical time is ahead of Link time.
tick :: Clock ()
tick = do
(ClockMemory config (ClockRef _ abletonLink)) <- ask
(ClockMemory config (ClockRef _ abletonLink) _) <- ask
st <- get
now <- liftIO $ Link.clock abletonLink
let processAhead = round $ (cProcessAhead config) * 1000000
Expand All @@ -187,7 +186,7 @@ tick = do
-- hands the current link operations to the TickAction
clockProcess :: Clock ()
clockProcess = do
(ClockMemory config (ClockRef _ abletonLink)) <- ask
(ClockMemory config (ClockRef _ abletonLink) action) <- ask
st <- get
let logicalEnd = logicalTime config (start st) $ ticks st + 1
startCycle = arcEnd $ nowArc st
Expand All @@ -210,7 +209,7 @@ clockProcess = do
cyclesToBeat = \cyc -> cyc * (cBeatsPerCycle config)
}

liftIO $ (cTickAction config) (nowArc st') (nudged st') ops
liftIO $ action (nowArc st') (nudged st') ops

liftIO $ Link.commitAndDestroyAppSessionState abletonLink sessionState

Expand All @@ -221,13 +220,13 @@ processAction :: ClockAction -> Clock ()
processAction NoAction = return ()
processAction (SetNudge n) = modify (\st -> st {nudged = n})
processAction (SetTempo bpm) = do
(ClockMemory _ (ClockRef _ abletonLink)) <- ask
(ClockMemory _ (ClockRef _ abletonLink) _) <- ask
sessionState <- liftIO $ Link.createAndCaptureAppSessionState abletonLink
now <- liftIO $ Link.clock abletonLink
liftIO $ Link.setTempo sessionState (fromRational bpm) now
liftIO $ Link.commitAndDestroyAppSessionState abletonLink sessionState
processAction (SetCycle cyc) = do
(ClockMemory config (ClockRef _ abletonLink)) <- ask
(ClockMemory config (ClockRef _ abletonLink) _) <- ask
sessionState <- liftIO $ Link.createAndCaptureAppSessionState abletonLink

now <- liftIO $ Link.clock abletonLink
Expand Down

0 comments on commit 95b1763

Please sign in to comment.