module Sound.SC3.Server.NRT where
import qualified Data.ByteString.Lazy as B
import Sound.OSC.Core
import Sound.OSC.Coding.Byte
import System.IO
oscWithSize :: Bundle -> B.ByteString
oscWithSize o =
let b = encodeBundle o
l = encode_i32 (fromIntegral (B.length b))
in B.append l b
data NRT = NRT {nrt_bundles :: [Bundle]} deriving (Show)
encodeNRT :: NRT -> B.ByteString
encodeNRT = B.concat . map oscWithSize . nrt_bundles
writeNRT :: FilePath -> NRT -> IO ()
writeNRT fn = B.writeFile fn . encodeNRT
putNRT :: Handle -> NRT -> IO ()
putNRT h = B.hPut h . encodeNRT
decode_nrt_bundles :: B.ByteString -> [Bundle]
decode_nrt_bundles s =
let (p,q) = B.splitAt 4 s
n = fromIntegral (decode_i32 p)
(r,s') = B.splitAt n q
r' = decodeBundle r
in if B.null s'
then [r']
else r' : decode_nrt_bundles s'
decodeNRT :: B.ByteString -> NRT
decodeNRT = NRT . decode_nrt_bundles
readNRT :: FilePath -> IO NRT
readNRT = fmap decodeNRT . B.readFile