Skip to content

Commit

Permalink
bump nanotime
Browse files Browse the repository at this point in the history
  • Loading branch information
ejconlon committed Jan 26, 2024
1 parent 42b547a commit f490782
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 41 deletions.
2 changes: 1 addition & 1 deletion minipat-dirt/minipat-dirt.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ library
, dahdit-network ==0.5.*
, minipat ==0.1.*
, mtl ==2.3.*
, nanotime ==0.1.*
, nanotime ==0.2.*
, network ==3.1.*
, optics ==0.4.*
, resourcet ==1.3.*
Expand Down
2 changes: 1 addition & 1 deletion minipat-dirt/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies:
- dahdit-network >= 0.5 && < 0.6
- minipat >= 0.1 && < 0.2
- mtl >= 2.3 && < 2.4
- nanotime >= 0.1 && < 0.2
- nanotime >= 0.2 && < 0.3
- network >= 3.1 && < 3.2
- optics >= 0.4 && < 0.5
- resourcet >= 1.3 && < 1.4
Expand Down
79 changes: 59 additions & 20 deletions minipat-dirt/src/Minipat/Dirt/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

module Minipat.Dirt.Prelude where

import Control.Applicative (empty)
import Control.Concurrent (forkFinally)
import Control.Concurrent.Async (async, cancel, waitCatch)
import Control.Concurrent.STM.TVar (newTVarIO)
import Control.Concurrent.STM (atomically)
import Control.Concurrent.STM.TVar (TVar, newTVarIO, readTVar, writeTVar)
import Control.Exception (SomeException, bracket, throwIO)
import Control.Monad.IO.Class (liftIO)
import Dahdit.Midi.Osc (Datum (..), Packet)
import Dahdit.Network (Conn (..), HostPort (..), runDecoder, runEncoder, udpServerConn)
import Dahdit.Network (Conn (..), HostPort (..), resolveAddr, runDecoder, runEncoder, udpServerConn)
import Data.Acquire (Acquire)
import Data.IORef (IORef, newIORef)
import Data.Map.Strict qualified as Map
import Data.Ratio ((%))
import Minipat.Base qualified as B
import Minipat.Dirt.Osc qualified as O
import Minipat.Dirt.Ref (Ref, ReleaseVar)
import Minipat.Dirt.Ref qualified as R
import Minipat.Time qualified as T
import Nanotime (PosixTime, TimeDelta, currentTime, threadDelayDelta, timeDeltaFromFracSecs)
import Nanotime (PosixTime (..), TimeDelta, currentTime, threadDelayDelta, timeDeltaFromFracSecs)
import Network.Socket qualified as NS

data Env = Env
Expand All @@ -38,11 +39,58 @@ defaultEnv =
, envOscTimeout = timeDeltaFromFracSecs @Double 0.1
}

data Clock = Clock
{ clDawn :: !PosixTime
, clCps :: !Rational
data Domain = Domain
{ domDawn :: !(TVar PosixTime)
, domCps :: !(TVar Rational)
, domAhead :: !(TVar TimeDelta)
, domPlaying :: !(TVar Bool)
, domCycle :: !(TVar Integer)
, domPat :: !(TVar (B.Pat O.OscMap))
}
deriving stock (Eq, Ord, Show)

newDomain :: IO Domain
newDomain =
Domain
<$> newTVarIO (PosixTime 0)
<*> newTVarIO 0
<*> newTVarIO 0
<*> newTVarIO False
<*> newTVarIO 0
<*> newTVarIO empty

initDomain :: Env -> IO Domain
initDomain env = newDomain >>= \d -> d <$ reinitDomain env d

reinitDomain :: Env -> Domain -> IO ()
reinitDomain env dom = do
now <- currentTime
let cps = envCps env
td = timeDeltaFromFracSecs (1 / cps)
atomically $ do
writeTVar (domDawn dom) now
writeTVar (domCps dom) cps
writeTVar (domAhead dom) td
writeTVar (domPlaying dom) False
writeTVar (domCycle dom) 0
writeTVar (domPat dom) empty

setCps :: St -> Rational -> IO ()
setCps = undefined -- NOTE have to change dawn + ahead

setPlaying :: St -> Bool -> IO ()
setPlaying st x = atomically (writeTVar (domPlaying (stDom st)) x)

setPat :: St -> B.Pat O.OscMap -> IO ()
setPat st x = atomically (writeTVar (domPat (stDom st)) x)

setCycle :: St -> Integer -> IO ()
setCycle st x =
let dom = stDom st
in atomically $ do
dawn <- readTVar (domDawn dom)
cps <- readTVar (domCps dom)
cyc <- readTVar (domCycle dom)
error "TODO"

data OscConn = OscConn
{ ocTargetAddr :: !NS.SockAddr
Expand All @@ -52,18 +100,10 @@ data OscConn = OscConn
data St = St
{ stEnv :: !Env
, stRel :: !ReleaseVar
, stClock :: !(IORef Clock)
, stDom :: !Domain
, stConn :: !(Ref OscConn)
}

-- TODO export this from dahdit-network
resolveAddr :: HostPort -> IO NS.SockAddr
resolveAddr hp@(HostPort host port) = do
infos <- NS.getAddrInfo Nothing host (Just (show port))
case infos of
[] -> fail ("Could not resolve address: " ++ show hp)
info : _ -> pure (NS.addrAddress info)

acqConn :: Env -> Acquire OscConn
acqConn (Env targetHp listenHp _ _) = do
targetAddr <- liftIO (resolveAddr targetHp)
Expand All @@ -74,9 +114,8 @@ initSt :: Env -> IO St
initSt env = do
rv <- R.releaseVarCreate
ref <- R.refCreate rv (acqConn env)
now <- currentTime
cv <- newIORef (Clock now (envCps env))
pure (St env rv cv ref)
dom <- initDomain env
pure (St env rv dom ref)

reinitSt :: St -> IO ()
reinitSt st = R.refReplace (stConn st) (acqConn (stEnv st))
Expand Down
6 changes: 3 additions & 3 deletions minipat-dirt/src/Minipat/Dirt/Ref.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Control.Monad.Trans.Resource (createInternalState)
import Control.Monad.Trans.Resource.Internal (ReleaseMap, registerType, stateCleanup)
import Data.Acquire.Internal (Acquire (..), Allocated (..), ReleaseType (..), mkAcquire)
import Data.IORef (IORef, atomicModifyIORef', newIORef, readIORef, writeIORef)
import Nanotime (MonoTime (..), TimeDelta (..), awaitDelta, currentTime)
import Nanotime (MonoTime (..), TimeDelta, awaitDelta, currentTime)

type ReleaseVar = IORef ReleaseMap

Expand Down Expand Up @@ -156,8 +156,8 @@ refLoop :: ReleaseVar -> TVar TimeDelta -> IO (Maybe a) -> IO (Ref (Async a))
refLoop rv tdv act = do
tv <- newIORef (MonoTime 0)
let act' = do
td@(TimeDelta x) <- readTVarIO tdv
unless (x > 0) (throwIO NonPosTimeDeltaErr)
td <- readTVarIO tdv
unless (td > 0) (throwIO NonPosTimeDeltaErr)
awaitTime td tv
act >>= maybe act' pure
refAsync rv act'
Expand Down
4 changes: 2 additions & 2 deletions minipat/minipat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ library
, heaps ==0.4.*
, looksee ==0.5.*
, mtl ==2.3.*
, nanotime ==0.1.*
, nanotime ==0.2.*
, nonempty-containers ==0.3.*
, prettyprinter ==1.7.*
, text ==2.0.*
Expand Down Expand Up @@ -127,7 +127,7 @@ test-suite minipat-test
, looksee ==0.5.*
, minipat
, mtl ==2.3.*
, nanotime ==0.1.*
, nanotime ==0.2.*
, nonempty-containers ==0.3.*
, prettyprinter ==1.7.*
, tasty ==1.4.*
Expand Down
2 changes: 1 addition & 1 deletion minipat/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:
- heaps >= 0.4 && < 0.5
- looksee >= 0.5 && < 0.6
- mtl >= 2.3 && < 2.4
- nanotime >= 0.1 && < 0.2
- nanotime >= 0.2 && < 0.3
- nonempty-containers >= 0.3 && < 0.4
- prettyprinter >= 1.7 && < 1.8
- text >= 2.0 && < 2.1
Expand Down
6 changes: 3 additions & 3 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ packages:
extra-deps:
- bowtie-0.2.0
- dahdit-0.5.1
- dahdit-network-0.5.1
- dahdit-midi-0.5.1
- dahdit-network-0.5.2
- dahdit-midi-0.5.2
- data-sword-0.2.0.3
- daytripper-0.3.1
- falsify-0.2.0
- looksee-0.5.2
- nanotime-0.1.0
- nanotime-0.2.0

allow-newer: true
allow-newer-deps:
Expand Down
20 changes: 10 additions & 10 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ packages:
original:
hackage: dahdit-0.5.1
- completed:
hackage: dahdit-network-0.5.1@sha256:9a5aed2baed0f5122e613f5533416151fe894478a480cfa274a95dbe781324af,3000
hackage: dahdit-network-0.5.2@sha256:1c75b1a3eb74a194ea9a0bc6606703735eb803e899727a72ef4de29aab235641,2968
pantry-tree:
sha256: af524794fb60872f93d23d31f305ddc6835e91d1952858c8acff95702453af07
sha256: 3ef6365a3aea81140e56b2034d4f5954ce03a1d8d589c11e80ac2f8b79b1edc0
size: 227
original:
hackage: dahdit-network-0.5.1
hackage: dahdit-network-0.5.2
- completed:
hackage: dahdit-midi-0.5.1@sha256:106700dd9a1c00506f63825c2e8418954d35a36c7acfa5faf7c73e4ee7d84c9f,3395
hackage: dahdit-midi-0.5.2@sha256:912393c22a45a3a793c296dac216f822c6eae99fa3430d051f320c7da39821c9,3395
pantry-tree:
sha256: 6ff9ec8f4ec494b2816c2e5f1bb6a9c304216e12cbe18d854f717993823e2058
sha256: eabbbc4d3312eb1049f09cf33389a1fe7c5f08963eec1371acefbaaf97e5171f
size: 562
original:
hackage: dahdit-midi-0.5.1
hackage: dahdit-midi-0.5.2
- completed:
hackage: data-sword-0.2.0.3@sha256:953cca4b4533a388df0ef17e06ac5530652d1d73decfe8b656242ccdf4b40999,1568
pantry-tree:
Expand Down Expand Up @@ -61,12 +61,12 @@ packages:
original:
hackage: looksee-0.5.2
- completed:
hackage: nanotime-0.1.0@sha256:394c44f44fcb0dab9abe267e490ceb836501147abd92fd97a2b9218b1e293700,2827
hackage: nanotime-0.2.0@sha256:79aa8dd2d0776173d98dad4447e238f200d3638d6318991852a19d5357f086ed,2827
pantry-tree:
sha256: 63b85e0fa5eb344d72e41b9e3e998c46bea374d022e4ca93793a67a068e26c80
size: 167
sha256: 7b9aca6ec0a989e7d7862c0827c90376420fd274843d0157fc6aae0b122996c9
size: 168
original:
hackage: nanotime-0.1.0
hackage: nanotime-0.2.0
snapshots:
- completed:
sha256: 7b975b104cb3dbf0c297dfd01f936a4d2ee523241dd0b1ae960522b833fe3027
Expand Down

0 comments on commit f490782

Please sign in to comment.