module Sound.DF.Audition where

import Sound.DF.CGen
import Sound.DF.Node
import Sound.OpenSoundControl
import System.Directory
import System.FilePath

-- * Interaction with jack.dl server

-- | Allocate buffer.
b_alloc :: Int -> Int -> OSC
b_alloc b n = Message "/b_alloc" [Int b, Int n]

-- | Load graph.
g_load :: Int -> String -> OSC
g_load i s = Message "/g_load" [Int i, String s]

-- | Load graph.
g_unload :: Int -> OSC
g_unload i = Message "/g_unload" [Int i]

-- | Run action with UDP link to jack.dl.
with_jack_dl :: (UDP -> IO a) -> IO a
with_jack_dl = withTransport (openUDP "127.0.0.1" 57190)

-- | Audition graph 'n' after sending initialisation messages 'is'.
audition :: [OSC] -> Node -> IO ()
audition is n = 
    do t <- getTemporaryDirectory
       k <- generateID
       let fn = t </> ("audition" ++ show k)
       dl_gen fn n
       with_jack_dl (\fd -> mapM (send fd) is)
       with_jack_dl (\fd -> send fd (g_load 0 (fn <.> "so")))