module Sound.OSC.Coding.Encode.Base where
import Data.Binary
import qualified Data.ByteString.Char8 as C
import qualified Data.ByteString.Lazy as B
import Sound.OSC.Coding.Byte
import Sound.OSC.Coding.Convert
import Sound.OSC.Datum
import Sound.OSC.Packet
import Sound.OSC.Time
extend :: Word8 -> B.ByteString -> B.ByteString
extend :: Word8 -> ByteString -> ByteString
extend Word8
p ByteString
s = ByteString -> ByteString -> ByteString
B.append ByteString
s (Int64 -> Word8 -> ByteString
B.replicate (Int64 -> Int64
forall i. (Num i, Bits i) => i -> i
align (ByteString -> Int64
B.length ByteString
s)) Word8
p)
encode_datum :: Datum -> B.ByteString
encode_datum :: Datum -> ByteString
encode_datum Datum
dt =
case Datum
dt of
Int32 Int32
i -> Int32 -> ByteString
forall a. Binary a => a -> ByteString
encode Int32
i
Int64 Int64
i -> Int64 -> ByteString
forall a. Binary a => a -> ByteString
encode Int64
i
Float Float
f -> Float -> ByteString
encode_f32 Float
f
Double Double
d -> Double -> ByteString
encode_f64 Double
d
TimeStamp Double
t -> Word64 -> ByteString
encode_word64 (Word64 -> ByteString) -> Word64 -> ByteString
forall a b. (a -> b) -> a -> b
$ Double -> Word64
ntpr_to_ntpi Double
t
ASCII_String ASCII
s -> Word8 -> ByteString -> ByteString
extend Word8
0 (ByteString -> Word8 -> ByteString
B.snoc (ASCII -> ByteString
encode_ascii ASCII
s) Word8
0)
Midi (MIDI Word8
b0 Word8
b1 Word8
b2 Word8
b3) -> [Word8] -> ByteString
B.pack [Word8
b0,Word8
b1,Word8
b2,Word8
b3]
Blob ByteString
b -> let n :: ByteString
n = Int32 -> ByteString
forall a. Binary a => a -> ByteString
encode (Int64 -> Int32
int64_to_int32 (ByteString -> Int64
B.length ByteString
b))
in ByteString -> ByteString -> ByteString
B.append ByteString
n (Word8 -> ByteString -> ByteString
extend Word8
0 ByteString
b)
encodeMessage :: Message -> B.ByteString
encodeMessage :: Message -> ByteString
encodeMessage (Message Address_Pattern
c [Datum]
l) =
[ByteString] -> ByteString
B.concat [Datum -> ByteString
encode_datum (ASCII -> Datum
ASCII_String (Address_Pattern -> ASCII
C.pack Address_Pattern
c))
,Datum -> ByteString
encode_datum (ASCII -> Datum
ASCII_String ([Datum] -> ASCII
descriptor [Datum]
l))
,[ByteString] -> ByteString
B.concat ((Datum -> ByteString) -> [Datum] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map Datum -> ByteString
encode_datum [Datum]
l) ]
encode_message_blob :: Message -> Datum
encode_message_blob :: Message -> Datum
encode_message_blob = ByteString -> Datum
Blob (ByteString -> Datum)
-> (Message -> ByteString) -> Message -> Datum
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> ByteString
encodeMessage
encodeBundle :: Bundle -> B.ByteString
encodeBundle :: Bundle -> ByteString
encodeBundle (Bundle Double
t [Message]
m) =
[ByteString] -> ByteString
B.concat [ByteString
bundleHeader
,Word64 -> ByteString
encode_word64 (Double -> Word64
ntpr_to_ntpi Double
t)
,[ByteString] -> ByteString
B.concat ((Message -> ByteString) -> [Message] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map (Datum -> ByteString
encode_datum (Datum -> ByteString)
-> (Message -> Datum) -> Message -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Message -> Datum
encode_message_blob) [Message]
m)]
encodePacket :: Packet -> B.ByteString
encodePacket :: Packet -> ByteString
encodePacket Packet
o =
case Packet
o of
Packet_Message Message
m -> Message -> ByteString
encodeMessage Message
m
Packet_Bundle Bundle
b -> Bundle -> ByteString
encodeBundle Bundle
b