Debian Codegraph @

Sound/Osc/Coding/Decode/Binary.hs

raw symbols SCIP
1 -- | Optimised decode function for Osc packets.
2 module Sound.Osc.Coding.Decode.Binary (
3 get_packet,
4 decodeMessage,
5 decodeBundle,
6 decodePacket,
7 decodePacket_strict,
8 decodeMessageOr,
9 decodeBundleOr,
10 decodePacketOr,
11 ) where
12
13 import Control.Applicative {- base -}
14 import Control.Monad {- base -}
15 import Data.Word {- base -}
16
17 import qualified Data.Binary.Get as Binary {- binary -}
18
19 import qualified Data.ByteString.Char8 as ByteString.Char8 {- bytestring -}
20 import qualified Data.ByteString.Lazy as ByteString.Lazy {- bytestring -}
21 import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8 {- bytestring -}
22
23 import qualified Sound.Osc.Coding.Byte as Byte {- hosc -}
24 import qualified Sound.Osc.Coding.Convert as Convert {- hosc -}
25 import Sound.Osc.Datum {- hosc -}
26 import Sound.Osc.Packet {- hosc -}
27 import qualified Sound.Osc.Time as Time {- hosc -}
28
29 -- | Get an aligned Osc string.
30 get_string :: Binary.Get String
31 get_string = do
32 s <- Binary.getLazyByteStringNul
33 Binary.skip (Convert.int64_to_int (Byte.align (ByteString.Lazy.length s + 1)))
34 return (ByteString.Lazy.Char8.unpack s)
35
36 -- | Get an aligned Osc string.
37 get_ascii :: Binary.Get Ascii
38 get_ascii = do
39 s <- Binary.getLazyByteStringNul
40 Binary.skip (Convert.int64_to_int (Byte.align (ByteString.Lazy.length s + 1)))
41 return (ByteString.Char8.pack (ByteString.Lazy.Char8.unpack s))
42
43 -- | Get binary data prefixed by byte count.
44 get_bytes :: Word32 -> Binary.Get ByteString.Lazy.ByteString
45 get_bytes n = do
46 b <- Binary.getLazyByteString (Convert.word32_to_int64 n)
47 if n /= Convert.int64_to_word32 (ByteString.Lazy.length b)
48 then fail "get_bytes: end of stream"
49 else Binary.skip (Convert.word32_to_int (Byte.align n))
50 return b
51
52 -- | Get an Osc datum.
53 get_datum :: DatumType -> Binary.Get Datum
54 get_datum ty =
55 case ty of
56 'i' -> fmap Int32 Binary.getInt32be
57 'h' -> fmap Int64 Binary.getInt64be
58 'f' -> fmap Float Binary.getFloatbe
59 'd' -> fmap Double Binary.getDoublebe
60 's' -> fmap AsciiString get_ascii
61 'b' -> fmap Blob (get_bytes =<< Binary.getWord32be)
62 't' -> fmap (TimeStamp . Time.ntpi_to_ntpr) Binary.getWord64be
63 'm' -> fmap Midi (liftM4 MidiData Binary.getWord8 Binary.getWord8 Binary.getWord8 Binary.getWord8)
64 _ -> fail ("get_datum: illegal type " ++ show ty)
65
66 -- | Get an Osc 'Message', fail if type descriptor is invalid.
67 get_message :: Binary.Get Message
68 get_message = do
69 cmd <- get_string
70 dsc <- get_ascii
71 case ByteString.Char8.unpack dsc of
72 ',' : tags -> do
73 arg <- mapM get_datum tags
74 return (Message cmd arg)
75 e -> fail ("get_message: invalid type descriptor string: " ++ e)
76
77 -- | Get a sequence of Osc 'Message's, each one headed by its length.
78 get_message_seq :: Binary.Get [Message]
79 get_message_seq = do
80 b <- Binary.isEmpty
81 if b
82 then return []
83 else do
84 p <- flip Binary.isolate get_message . Convert.word32_to_int =<< Binary.getWord32be
85 ps <- get_message_seq
86 return (p : ps)
87
88 -- | Get a bundle. Fail if bundle header is not found in packet.
89 get_bundle :: Binary.Get (BundleOf Message)
90 get_bundle = do
91 h <- Binary.getByteString (ByteString.Char8.length Byte.bundleHeader_strict)
92 when (h /= Byte.bundleHeader_strict) (fail "get_bundle: not a bundle")
93 t <- fmap Time.ntpi_to_ntpr Binary.getWord64be
94 fmap (Bundle t) get_message_seq
95
96 -- | Get an Osc 'Packet'.
97 get_packet :: Binary.Get (PacketOf Message)
98 get_packet = fmap Packet_Bundle get_bundle <|> fmap Packet_Message get_message
99
100 {-# INLINE decodeMessage #-}
101 {-# INLINE decodeBundle #-}
102 {-# INLINE decodePacket #-}
103 {-# INLINE decodePacket_strict #-}
104
105 {- | Decode an Osc 'Message' from a lazy ByteString.
106
107 >>> let b = ByteString.Lazy.pack [47,103,95,102,114,101,101,0,44,105,0,0,0,0,0,0]
108 >>> decodeMessage b == Message "/g_free" [Int32 0]
109 True
110 -}
111 decodeMessage :: ByteString.Lazy.ByteString -> Message
112 decodeMessage = Binary.runGet get_message
113
114 -- | Decode an Osc 'Bundle' from a lazy ByteString.
115 decodeBundle :: ByteString.Lazy.ByteString -> BundleOf Message
116 decodeBundle = Binary.runGet get_bundle
117
118 -- | Run decoder and report any error.
119 runDecoder :: Binary.Get t -> ByteString.Lazy.Char8.ByteString -> Either String t
120 runDecoder f p =
121 case Binary.runGetOrFail f p of
122 Left (_, _, err) -> Left err
123 Right (_, _, decoded) -> Right decoded
124
125 {- | Decode an Osc packet from a lazy ByteString.
126
127 >>> let b = ByteString.Lazy.pack [47,103,95,102,114,101,101,0,44,105,0,0,0,0,0,0]
128 >>> decodePacket b == Packet_Message (Message "/g_free" [Int32 0])
129 True
130 -}
131 decodePacket :: ByteString.Lazy.ByteString -> PacketOf Message
132 decodePacket = Binary.runGet get_packet
133
134 -- | Decode an Osc packet from a strict Char8 ByteString.
135 decodePacket_strict :: ByteString.Char8.ByteString -> PacketOf Message
136 decodePacket_strict = Binary.runGet get_packet . ByteString.Lazy.fromChunks . (: [])
137
138 {- | Either decode Osc message or return an error message.
139 Prevents application halt for non-valid message/bundle/packet arrives.
140
141 >>> let b = ByteString.Lazy.pack [1,2,3,2,1]
142 >>> decodePacketOr b
143 Left "not enough bytes"
144 -}
145 decodeMessageOr :: ByteString.Lazy.ByteString -> Either String Message
146 decodeMessageOr = runDecoder get_message
147
148 decodeBundleOr :: ByteString.Lazy.ByteString -> Either String Bundle
149 decodeBundleOr = runDecoder get_bundle
150
151 decodePacketOr :: ByteString.Lazy.ByteString -> Either String Packet
152 decodePacketOr = runDecoder get_packet