module Sound.SC3.Server.Synthdef where
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy as B
import Data.Default
import Data.List
import Data.Maybe
import Sound.OSC.Coding.Byte
import Sound.OSC.Coding.Cast
import System.FilePath
import Sound.SC3.Server.Synthdef.Internal
import Sound.SC3.Server.Synthdef.Type
import Sound.SC3.UGen.Graph
import Sound.SC3.UGen.Type
synth :: UGen -> Graph
synth u =
let (_,g) = mk_node (prepare_root u) empty_graph
g' = g {ugens = reverse (ugens g)}
in add_implicit g'
data Synthdef = Synthdef {synthdefName :: String
,synthdefGraph :: Graph}
deriving (Eq,Show)
instance Default Synthdef where def = defaultSynthdef
synthdef :: String -> UGen -> Synthdef
synthdef s u = Synthdef s (synth u)
defaultSynthdef :: Synthdef
defaultSynthdef = synthdef "default" default_ugen_graph
synthdefParam :: Synthdef -> [String]
synthdefParam = map node_k_name . controls . synthdefGraph
graphdef :: Graph -> Graphdef
graphdef = encode_graphdef
ugenIndices :: String -> Graph -> [Integer]
ugenIndices nm =
let f (k,nd) =
case nd of
NodeU _ _ nm' _ _ _ _ -> if nm == nm' then Just k else Nothing
_ -> Nothing
in mapMaybe f . zip [0..] . ugens
synthdefData :: Synthdef -> Graphdef
synthdefData (Synthdef s g) =
B.concat [encode_str (C.pack "SCgf")
,encode_i32 0
,encode_i16 1
,B.pack (str_pstr s)
,encode_graphdef g]
synthdefWrite :: Synthdef -> FilePath -> IO ()
synthdefWrite s dir =
let nm = dir </> synthdefName s <.> "scsyndef"
in B.writeFile nm (synthdefData s)
synthstat :: UGen -> String
synthstat u =
let s = synth u
cs = constants s
ks = controls s
us = ugens s
f g = let h (x:xs) = (x,length (x:xs))
h [] = error "synthstat"
in show . map h . group . sort . map g
in unlines ["number of constants : " ++ show (length cs)
,"number of controls : " ++ show (length ks)
,"control rates : " ++ f node_k_rate ks
,"number of unit generators : " ++ show (length us)
,"unit generator rates : " ++ f node_u_rate us]