module Sound.Sc3.Server.Nrt where
import Data.Maybe
import System.IO
import qualified Data.ByteString.Lazy as B
import qualified Sound.Osc.Coding.Byte as Byte
import qualified Sound.Osc.Coding.Decode.Binary as Decode
import qualified Sound.Osc.Coding.Encode.Builder as Encode
import Sound.Osc.Datum
import Sound.Osc.Packet
oscWithSize :: Bundle -> B.ByteString
oscWithSize :: Bundle -> ByteString
oscWithSize Bundle
o =
let b :: ByteString
b = Bundle -> ByteString
Encode.encodeBundle Bundle
o
l :: ByteString
l = Int -> ByteString
Byte.encode_i32 (forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int64
B.length ByteString
b))
in ByteString -> ByteString -> ByteString
B.append ByteString
l ByteString
b
newtype Nrt = Nrt {Nrt -> [Bundle]
nrt_bundles :: [Bundle]} deriving (Int -> Nrt -> ShowS
[Nrt] -> ShowS
Nrt -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Nrt] -> ShowS
$cshowList :: [Nrt] -> ShowS
show :: Nrt -> String
$cshow :: Nrt -> String
showsPrec :: Int -> Nrt -> ShowS
$cshowsPrec :: Int -> Nrt -> ShowS
Show)
nrt_span :: (Time -> Bool) -> Nrt -> ([Bundle],[Bundle])
nrt_span :: (Time -> Bool) -> Nrt -> ([Bundle], [Bundle])
nrt_span Time -> Bool
f = forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Time -> Bool
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bundle -> Time
bundleTime) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nrt -> [Bundle]
nrt_bundles
encodeNrt :: Nrt -> B.ByteString
encodeNrt :: Nrt -> ByteString
encodeNrt = [ByteString] -> ByteString
B.concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Bundle -> ByteString
oscWithSize forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nrt -> [Bundle]
nrt_bundles
writeNrt :: FilePath -> Nrt -> IO ()
writeNrt :: String -> Nrt -> IO ()
writeNrt String
fn = String -> ByteString -> IO ()
B.writeFile String
fn forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nrt -> ByteString
encodeNrt
putNrt :: Handle -> Nrt -> IO ()
putNrt :: Handle -> Nrt -> IO ()
putNrt Handle
h = Handle -> ByteString -> IO ()
B.hPut Handle
h forall b c a. (b -> c) -> (a -> b) -> a -> c
. Nrt -> ByteString
encodeNrt
decode_nrt_bundles :: B.ByteString -> [Bundle]
decode_nrt_bundles :: ByteString -> [Bundle]
decode_nrt_bundles ByteString
s =
let (ByteString
p,ByteString
q) = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
4 ByteString
s
n :: Int64
n = forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
Byte.decode_i32 ByteString
p)
(ByteString
r,ByteString
s') = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
n ByteString
q
r' :: Bundle
r' = ByteString -> Bundle
Decode.decodeBundle ByteString
r
in if ByteString -> Bool
B.null ByteString
s'
then [Bundle
r']
else Bundle
r' forall a. a -> [a] -> [a]
: ByteString -> [Bundle]
decode_nrt_bundles ByteString
s'
decodeNrt :: B.ByteString -> Nrt
decodeNrt :: ByteString -> Nrt
decodeNrt = [Bundle] -> Nrt
Nrt forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Bundle]
decode_nrt_bundles
readNrt :: FilePath -> IO Nrt
readNrt :: String -> IO Nrt
readNrt = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Nrt
decodeNrt forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> IO ByteString
B.readFile
nrt_non_ascending :: Nrt -> [(Bundle, Bundle)]
nrt_non_ascending :: Nrt -> [(Bundle, Bundle)]
nrt_non_ascending (Nrt [Bundle]
b) =
let p :: [(Bundle, Bundle)]
p = forall a b. [a] -> [b] -> [(a, b)]
zip [Bundle]
b (forall a. [a] -> [a]
tail [Bundle]
b)
f :: (Bundle, Bundle) -> Maybe (Bundle, Bundle)
f (Bundle
i,Bundle
j) = if Bundle -> Time
bundleTime Bundle
i forall a. Ord a => a -> a -> Bool
> Bundle -> Time
bundleTime Bundle
j then forall a. a -> Maybe a
Just (Bundle
i,Bundle
j) else forall a. Maybe a
Nothing
in forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Bundle, Bundle) -> Maybe (Bundle, Bundle)
f [(Bundle, Bundle)]
p