Skip to content

Commit

Permalink
etc
Browse files Browse the repository at this point in the history
  • Loading branch information
ejconlon committed Dec 9, 2024
1 parent 99f84db commit 6a7bd95
Show file tree
Hide file tree
Showing 10 changed files with 352 additions and 36 deletions.
14 changes: 7 additions & 7 deletions minipat-live/src/Minipat/Live/Attrs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Minipat.Live.Attrs
, attrsTryLookup
, attrsUnalias
, Attr (..)
, IsAttrs (..)
, ToAttrs (..)
, attrsMerge
)
where
Expand Down Expand Up @@ -110,24 +110,24 @@ attrsUnalias as m0 = foldM go m0 as
Nothing -> pure m
Just v -> attrsTryInsert y v (attrsDelete x m)

-- | For when you need something anonymous with an 'IsAttrs' instance.
-- | For when you need something anonymous with an 'ToAttrs' instance.
data Attr a = Attr
{ attrKey :: !Text
, attrVal :: !a
}
deriving stock (Eq, Ord, Show, Functor, Foldable, Traversable)

class IsAttrs a where
class ToAttrs a where
toAttrs :: a -> Attrs

instance (IsDatum a) => IsAttrs (Attr a) where
instance (IsDatum a) => ToAttrs (Attr a) where
toAttrs (Attr k v) = attrsSingleton k (toDatum v)

instance IsAttrs Attrs where
instance ToAttrs Attrs where
toAttrs = id

instance (IsAttrs a) => IsAttrs (Maybe a) where
instance (ToAttrs a) => ToAttrs (Maybe a) where
toAttrs = maybe mempty toAttrs

attrsMerge :: (IsAttrs a, IsAttrs b) => a -> b -> Attrs
attrsMerge :: (ToAttrs a, ToAttrs b) => a -> b -> Attrs
attrsMerge a b = toAttrs a <> toAttrs b
8 changes: 4 additions & 4 deletions minipat-live/src/Minipat/Live/Boot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import Data.Default (Default (..))
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import Data.Kind (Type)
import Minipat.EStream (EStream)
import Minipat.Live.Attrs (Attrs, IsAttrs)
import Minipat.Live.Attrs (Attrs, ToAttrs)
import Minipat.Live.Backend qualified as B
import Minipat.Live.Combinators
import Minipat.Live.Core qualified as C
Expand Down Expand Up @@ -116,7 +116,7 @@ setCycle x = readLiveSt >>= \st -> C.setCycle st x
setTempo :: (LiveSt) => Rational -> IO ()
setTempo x = readLiveSt >>= \st -> C.setTempo st x

setOrbit :: (LiveSt, IsAttrs a) => Integer -> EStream a -> IO ()
setOrbit :: (LiveSt, ToAttrs a) => Integer -> EStream a -> IO ()
setOrbit x y = readLiveSt >>= \st -> C.setOrbit st x y

clearOrbit :: (LiveSt) => Integer -> IO ()
Expand Down Expand Up @@ -147,10 +147,10 @@ checkTasks = readLiveSt >>= void . C.checkTasks
peek :: (LiveSt, Pretty a) => EStream a -> IO ()
peek x = readLiveSt >>= \st -> C.peek st x

d :: (LiveSt, IsAttrs a) => Integer -> EStream a -> IO ()
d :: (LiveSt, ToAttrs a) => Integer -> EStream a -> IO ()
d = setOrbit

d0, d1, d2, d3, d4, d5, d6, d7 :: (LiveSt, IsAttrs a) => EStream a -> IO ()
d0, d1, d2, d3, d4, d5, d6, d7 :: (LiveSt, ToAttrs a) => EStream a -> IO ()
d0 = d 0
d1 = d 1
d2 = d 2
Expand Down
4 changes: 2 additions & 2 deletions minipat-live/src/Minipat/Live/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import Data.Sequence qualified as Seq
import Minipat.Classes (Flow (..))
import Minipat.EStream (EStream)
import Minipat.EStream qualified as E
import Minipat.Live.Attrs (Attrs, IsAttrs, attrsMerge)
import Minipat.Live.Attrs (Attrs, ToAttrs, attrsMerge)
import Minipat.Time (CycleDelta, CycleTime)

type S = EStream

setIn, (#) :: (IsAttrs a, IsAttrs b) => S a -> S b -> S Attrs
setIn, (#) :: (ToAttrs a, ToAttrs b) => S a -> S b -> S Attrs
setIn = flowInnerApply attrsMerge
(#) = setIn

Expand Down
6 changes: 3 additions & 3 deletions minipat-live/src/Minipat/Live/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import Data.Sequence qualified as Seq
import Data.Text (Text)
import Data.Text qualified as T
import Minipat.EStream (EStream (..))
import Minipat.Live.Attrs (Attrs, IsAttrs (..))
import Minipat.Live.Attrs (Attrs, ToAttrs (..))
import Minipat.Live.Backend (Backend (..), Callback (..), UninitErr (..), WithPlayMeta)
import Minipat.Live.Exception (catchUserErr)
import Minipat.Live.Logger (LogAction, logDebug, logException, logInfo, logWarn, nullLogger)
Expand Down Expand Up @@ -273,13 +273,13 @@ updateOrbits st f = atomically $ do
let z = foldl' (\x (o, y) -> x <> fmap (WithOrbit o) y) mempty (Map.toList m')
writeTVar (domStream dom) z

setOrbit :: (IsAttrs a) => St i -> Integer -> EStream a -> IO ()
setOrbit :: (ToAttrs a) => St i -> Integer -> EStream a -> IO ()
setOrbit st o es =
case unEStream es of
Left e -> logException (stLogger st) ("Error setting orbit " <> T.pack (show o)) e
Right s -> setOrbit' st o s

setOrbit' :: (IsAttrs a) => St i -> Integer -> Stream a -> IO ()
setOrbit' :: (ToAttrs a) => St i -> Integer -> Stream a -> IO ()
setOrbit' st o s = updateOrbits st (Map.insert o (fmap toAttrs s))

clearOrbit :: St i -> Integer -> IO ()
Expand Down
6 changes: 3 additions & 3 deletions minipat-live/src/Minipat/Live/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Looksee qualified as L
import Minipat.Ast (Ident (..), Select (..))
import Minipat.EStream (EStream (..))
import Minipat.Eval (evalPat)
import Minipat.Live.Attrs (Attr (..), IsAttrs (..), attrsInsert, attrsSingleton)
import Minipat.Live.Attrs (Attr (..), ToAttrs (..), attrsInsert, attrsSingleton)
import Minipat.Live.Combinators (S)
import Minipat.Live.Datum (DatumProxy (..))
import Minipat.Live.Notes
Expand Down Expand Up @@ -108,7 +108,7 @@ data Sound = Sound
instance Pretty Sound where
pretty (Sound so mn) = pretty so <> maybe mempty ((":" <>) . pretty) mn

instance IsAttrs Sound where
instance ToAttrs Sound where
toAttrs (Sound so mn) = attrsInsert "sound" (DatumString (unIdent so)) (toAttrs mn)

soundP :: P Sound
Expand All @@ -125,7 +125,7 @@ newtype Note = Note {unNote :: Int32}
deriving stock (Show)
deriving newtype (Eq, Ord, Pretty)

instance IsAttrs Note where
instance ToAttrs Note where
toAttrs (Note x) = attrsSingleton "note" (DatumInt32 x)

noteLinOffset :: Integer
Expand Down
32 changes: 19 additions & 13 deletions minipat-live/src/Minipat/Live/OscRpc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import Data.Sequence (Seq (..))
import Data.Sequence qualified as Seq
import Data.Text (Text)
import Data.Text qualified as T
import Minipat.Live.Attrs (Attrs, IsAttrs (..), attrsSingleton, attrsToList)
import Minipat.Live.Attrs (Attrs, ToAttrs (..), attrsSingleton, attrsToList)
import Minipat.Live.Convert (ConvErr, ConvM, runConvM)
import Minipat.Live.EnumString (EnumString, allEnumStrings)
import Nanotime (PosixTime, TimeDelta)
Expand Down Expand Up @@ -77,18 +77,24 @@ newtype RequestId = RequestId {unRequestId :: Int32}
deriving stock (Show)
deriving newtype (Eq, Ord, Num, Enum)

instance IsAttrs RequestId where
instance ToAttrs RequestId where
toAttrs (RequestId x) = attrsSingleton "!requestId" (DatumInt32 x)

lookupRequestId :: Attrs -> Maybe RequestId
lookupRequestId = error "TODO"

newtype RemoteErr = RemoteErr {unRemoteErr :: Text}
deriving stock (Show)
deriving newtype (Eq, Ord)

instance Exception RemoteErr

instance IsAttrs RemoteErr where
instance ToAttrs RemoteErr where
toAttrs (RemoteErr x) = attrsSingleton "!error" (DatumString x)

lookupRemoteErr :: Attrs -> Maybe RemoteErr
lookupRemoteErr = undefined

data RpcErr
= -- | Remote side signaled an error
RpcErrRemote !RemoteErr
Expand Down Expand Up @@ -130,13 +136,13 @@ data Waiter c where
Waiter :: RequestId -> c r -> RawAddrPat -> PosixTime -> WaitVar r -> Waiter c

waiterMatches :: Msg -> Waiter c -> Bool
waiterMatches = error "TODO"
waiterMatches m w = error "TODO"

waiterExpired :: PosixTime -> Waiter c -> Bool
waiterExpired = error "TODO"
waiterExpired t w = error "TODO"

waiterRun :: Msg -> Waiter c -> IO (Either RpcErr ())
waiterRun = error "TODO"
waiterRun m w = error "TODO"

data OscProtoEnv c = OscProtoEnv
{ opeTimeout :: !TimeDelta
Expand All @@ -147,16 +153,16 @@ data OscProtoEnv c = OscProtoEnv
newOscProtoEnvIO :: TimeDelta -> RequestId -> IO (OscProtoEnv c)
newOscProtoEnvIO to rid = OscProtoEnv to <$> newTVarIO rid <*> newTVarIO Empty

-- TODO finish implementing
expireWaiters :: OscProtoEnv c -> PosixTime -> IO ()
expireWaiters (OscProtoEnv _to _ _wes) _now = error "TODO"

-- expireWaiters :: OscProtoEnv c -> PosixTime -> IO Int
-- expireWaiters (OscProtoEnv _to _ _wes) _now = error "TODO"
handleRecvMsg :: OscProtoEnv c -> Msg -> IO (Either RpcErr a)
handleRecvMsg = error "TODO"

-- handleRecvMsg :: OscProtoEnv c -> Msg -> IO (Either RpcErr a)
-- handleRecvMsg = error "TODO"
sendMsgWith :: (RpcCmd t c) => (Msg -> IO ()) -> OscProtoEnv c -> c r -> IO (WaitVar r)
sendMsgWith _send _ope _cmd = error "TODO"

-- sendMsgWith :: (RpcCmd t c) => (Msg -> IO ()) -> OscProtoEnv c -> c r -> IO (WaitVar r)
-- sendMsgWith send ope cmd = error "TODO"
-- TODO finish implementing

-- data OscTaskEnv c a = OscTaskEnv
-- { oteSend :: !(Msg -> IO ())
Expand Down
4 changes: 2 additions & 2 deletions minipat-midi/src/Minipat/Midi/Convert.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Dahdit.Midi.Osc (Datum (..))
import Data.Default (def)
import Data.Functor ((<&>))
import Data.Int (Int32)
import Minipat.Live.Attrs (Attrs, IsAttrs (..), attrsSingleton)
import Minipat.Live.Attrs (Attrs, ToAttrs (..), attrsSingleton)
import Minipat.Live.Convert (Branch (..), ConvErr, ConvM, branchM, defaultM, lookupM, runConvM)
import Minipat.Live.Datum (DatumProxy (..))
import Minipat.Midi.Midi (PortData (..), psFromText)
Expand All @@ -17,7 +17,7 @@ newtype Vel = Vel {unVel :: Int32}
deriving stock (Show)
deriving newtype (Eq, Ord, Num, Integral, Real, Enum)

instance IsAttrs Vel where
instance ToAttrs Vel where
toAttrs (Vel x) = attrsSingleton "vel" (DatumInt32 x)

-- TODO support more:
Expand Down
4 changes: 2 additions & 2 deletions minipat-midi/src/Minipat/Midi/Midi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import Libremidi.Common
)
import Libremidi.Foreign qualified as LMF
import Libremidi.Simple qualified as LMS
import Minipat.Live.Attrs (IsAttrs (..), attrsSingleton)
import Minipat.Live.Attrs (ToAttrs (..), attrsSingleton)
import Minipat.Live.Logger (LogAction, logError, logWarn)
import Minipat.Midi.Count (CountM, throwErrM)
import Nanotime (PosixTime, TimeDelta, threadDelayDelta)
Expand Down Expand Up @@ -110,7 +110,7 @@ psFromText t =
instance Pretty PortSel where
pretty = pretty . psToText

instance IsAttrs PortSel where
instance ToAttrs PortSel where
toAttrs = attrsSingleton "port" . DatumString . psToText

instance Default PortSel where
Expand Down
1 change: 1 addition & 0 deletions minipat-rns/minipat-rns.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ source-repository head
library
exposed-modules:
Minipat.Rns
Minipat.Rns.Song
other-modules:
Paths_minipat_rns
hs-source-dirs:
Expand Down
Loading

0 comments on commit 6a7bd95

Please sign in to comment.