Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ejconlon committed Feb 5, 2024
1 parent 6be5cdf commit c425cee
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 382 deletions.
18 changes: 9 additions & 9 deletions minipat-dirt/src/Minipat/Dirt/EStream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Data.Semigroup (Semigroup (..))
import Data.String (IsString (..))
import Data.Text (Text)
import Data.Typeable (Typeable)
import Minipat.Eval (EvalEnv, evalPat)
-- import Minipat.Eval (evalPat)
import Minipat.Stream (Stream)
import Minipat.Stream qualified as S

Expand Down Expand Up @@ -46,11 +46,11 @@ estreamThrow = EStream . Left . SomeException
estreamFilter :: (a -> Bool) -> EStream k a -> EStream j a
estreamFilter = estreamMap . S.streamFilter

class EStreamEval k e a | k -> e a where
estreamEvalEnv :: Proxy k -> EvalEnv e a

instance (EStreamEval k e a, Show e, Typeable e) => IsString (EStream k a) where
fromString = estreamEval @e (estreamEvalEnv (Proxy :: Proxy k)) . fromString

estreamEval :: (Show e, Typeable e) => EvalEnv e a -> Text -> EStream k a
estreamEval ee txt = EStream (evalPat ee txt)
-- class EStreamEval k e a | k -> e a where
-- estreamEvalEnv :: Proxy k -> EvalEnv e a
--
-- instance (EStreamEval k e a, Show e, Typeable e) => IsString (EStream k a) where
-- fromString = estreamEval @e (estreamEvalEnv (Proxy :: Proxy k)) . fromString
--
-- estreamEval :: (Show e, Typeable e) => EvalEnv e a -> Text -> EStream k a
-- estreamEval ee txt = EStream (evalPat ee txt)
129 changes: 79 additions & 50 deletions minipat-dirt/src/Minipat/Dirt/Eval.hs
Original file line number Diff line number Diff line change
@@ -1,54 +1,83 @@
{-# LANGUAGE OverloadedStrings #-}
-- {-# LANGUAGE OverloadedStrings #-}

module Minipat.Dirt.Eval
( liveEvalPat
, liveEvalSoundPat
)
-- ( liveEvalPat
-- , liveEvalSoundPat
-- , liveEvalNotePat
-- )
where

import Dahdit.Midi.Osc (Datum (..), DatumType (..))
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Data.Void (Void)
import Looksee (intP, sciP)
import Minipat.Ast (Ident (..), Pattern (..), Select (..))
import Minipat.Dirt.Osc (Attrs)
import Minipat.Eval (EvalEnv (..), evalPat)
import Minipat.Interp (InterpEnv (..), InterpErr (..), forbidInterpEnv)
import Minipat.Parser (P, identP)

datumP :: DatumType -> P Datum
datumP = \case
DatumTypeInt32 -> fmap (DatumInt32 . fromInteger) intP
DatumTypeInt64 -> fmap (DatumInt64 . fromInteger) intP
DatumTypeFloat -> fmap (DatumFloat . realToFrac) sciP
DatumTypeDouble -> fmap (DatumDouble . realToFrac) sciP
DatumTypeString -> fmap (DatumString . unIdent) identP
dt -> fail ("Datum type is not parseable: " <> show dt)

forbidEvalEnv :: DatumType -> EvalEnv e Datum
forbidEvalEnv dt = EvalEnv forbidInterpEnv (datumP dt)

liveEvalPat :: (Pattern f) => DatumType -> Text -> f Datum
liveEvalPat dt txt = either (pure patEmpty) id (evalPat @_ @Void (forbidEvalEnv dt) txt)

data SoundSelectErr = SoundSelectErr
deriving stock (Eq, Ord, Show)

soundSelFn :: (Pattern f) => Select -> f Attrs -> Either (InterpErr SoundSelectErr) (f Attrs)
soundSelFn sel attrs =
case sel of
SelectSample n -> Right (fmap (Map.insert "note" (DatumInt32 (fromIntegral n))) attrs)
_ -> Left (InterpErrEmbed SoundSelectErr)

soundProjFn :: Ident -> Attrs
soundProjFn = Map.singleton "sound" . DatumString . unIdent

soundInterpEnv :: InterpEnv SoundSelectErr Ident Attrs
soundInterpEnv = InterpEnv soundSelFn soundProjFn

soundEvalEnv :: EvalEnv SoundSelectErr Attrs
soundEvalEnv = EvalEnv soundInterpEnv identP

liveEvalSoundPat :: (Pattern f) => Text -> f Attrs
liveEvalSoundPat txt = either (pure patEmpty) id (evalPat soundEvalEnv txt)
-- import Control.Exception (Exception)
-- import Dahdit.Midi.Osc (Datum (..), DatumType (..))
-- import Data.Map.Strict qualified as Map
-- import Data.Text (Text)
-- import Data.Void (Void)
-- import Looksee (intP, sciP)
-- import Minipat.Ast (Ident (..), Pattern (..), Select (..))
-- import Minipat.Dirt.Notes (LinNote)
-- import Minipat.Dirt.Osc (Attrs)
-- import Minipat.Eval (evalPat)
-- import Minipat.Interp (InterpErr (..))
-- import Minipat.Parser (P, identP)
--
-- datumP :: DatumType -> P Datum
-- datumP = \case
-- DatumTypeInt32 -> fmap (DatumInt32 . fromInteger) intP
-- DatumTypeInt64 -> fmap (DatumInt64 . fromInteger) intP
-- DatumTypeFloat -> fmap (DatumFloat . realToFrac) sciP
-- DatumTypeDouble -> fmap (DatumDouble . realToFrac) sciP
-- DatumTypeString -> fmap (DatumString . unIdent) identP
-- dt -> fail ("Datum type is not parseable: " <> show dt)
--
-- forbidEvalEnv :: DatumType -> EvalEnv e Datum
-- forbidEvalEnv dt = EvalEnv forbidInterpEnv (datumP dt)
--
-- liveEvalPat :: (Pattern f) => DatumType -> Text -> f Datum
-- liveEvalPat dt txt = either (pure patEmpty) id (evalPat @_ @Void (forbidEvalEnv dt) txt)
--
-- data SoundSelectErr = SoundSelectErr
-- deriving stock (Eq, Ord, Show)
--
-- instance Exception SoundSelectErr
--
-- soundSel :: (Pattern f) => Select -> f Attrs -> Either (InterpErr SoundSelectErr) (f Attrs)
-- soundSel sel attrs =
-- case sel of
-- SelectSample n -> Right (fmap (Map.insert "note" (DatumInt32 (fromIntegral n))) attrs)
-- _ -> Left (InterpErrEmbed SoundSelectErr)
--
-- soundProj :: Ident -> Attrs
-- soundProj = Map.singleton "sound" . DatumString . unIdent
--
-- soundInterpEnv :: InterpEnv SoundSelectErr Ident Attrs
-- soundInterpEnv = InterpEnv soundSel soundProj
--
-- soundEvalEnv :: EvalEnv SoundSelectErr Attrs
-- soundEvalEnv = EvalEnv soundInterpEnv identP
--
-- -- TODO parse sound with note selector here instead of having it in the grammar
-- liveEvalSoundPat :: (Pattern f) => Text -> f Attrs
-- liveEvalSoundPat txt = either (pure patEmpty) id (evalPat soundEvalEnv txt)
--
-- noteProjFn :: Integer -> Attrs
-- noteProjFn = Map.singleton "note" . DatumInt32 . fromInteger
--
-- data NoteErr = NoteErr
-- deriving stock (Eq, Ord, Show)
--
-- instance Exception NoteErr
--
-- linNoteP :: P LinNote
-- linNoteP = error "TODO"
--
-- noteProj :: LinNote -> Attrs
-- noteProj = error "TODO"
--
-- noteInterpEnv :: InterpEnv NoteErr LinNote Attrs
-- noteInterpEnv = InterpEnv forbidSel noteProj
--
-- noteEvalEnv :: EvalEnv NoteErr Attrs
-- noteEvalEnv = EvalEnv noteInterpEnv linNoteP
--
-- liveEvalNotePat :: (Pattern f) => Text -> f Attrs
-- liveEvalNotePat txt = either (pure patEmpty) id (evalPat noteEvalEnv txt)
23 changes: 13 additions & 10 deletions minipat-dirt/src/Minipat/Dirt/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Minipat.Dirt.Prelude where
import Dahdit.Midi.Osc (Datum (..), DatumType (..), IsDatum (..))
import Data.Map.Strict qualified as Map
import Data.Text (Text)
import Minipat.Dirt.Eval (liveEvalPat, liveEvalSoundPat)
-- import Minipat.Dirt.Eval (liveEvalNotePat, liveEvalPat, liveEvalSoundPat)
import Minipat.Dirt.Osc (Attrs)
import Minipat.Stream (Stream (..), streamInnerBind)

Expand All @@ -19,20 +19,23 @@ pF k = fmap (Map.singleton k . DatumFloat . realToFrac)
pI :: (Integral a) => Text -> Stream a -> Stream Attrs
pI k = fmap (Map.singleton k . DatumInt32 . fromIntegral)

pat :: DatumType -> Text -> Text -> Stream Attrs
pat dt k t = stream k (liveEvalPat dt t)
-- pat :: DatumType -> Text -> Text -> Stream Attrs
-- pat dt k t = stream k (liveEvalPat dt t)

stream :: (IsDatum a) => Text -> Stream a -> Stream Attrs
stream k = fmap (Map.singleton k . toDatum)

sound, s :: Text -> Stream Attrs
sound = liveEvalSoundPat
s = sound
-- sound, s :: Text -> Stream Attrs
-- sound = liveEvalSoundPat
-- s = sound
--
-- note, n :: Text -> Stream Attrs
-- note = liveEvalNotePat
-- n = note

-- TODO n is midinote? what is difference between note and midinote?
note, n :: Text -> Stream Attrs
note = pat DatumTypeInt32 "note"
n = note
-- TODO essentially - midinote = note . fmap (- 60)
-- default note is c5, so we subtract 60 to get to note 0
-- midinote :: Text -> Stream Attrs

-- Basic effect parameters
accelerate
Expand Down
Loading

0 comments on commit c425cee

Please sign in to comment.