| 1 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
decodeMessage :: ByteString.Lazy.ByteString -> Message |
| 112 |
decodeMessage = Binary.runGet get_message |
| 113 |
|
| 114 |
|
| 115 |
decodeBundle :: ByteString.Lazy.ByteString -> BundleOf Message |
| 116 |
decodeBundle = Binary.runGet get_bundle |
| 117 |
|
| 118 |
|
| 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 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
decodePacket :: ByteString.Lazy.ByteString -> PacketOf Message |
| 132 |
decodePacket = Binary.runGet get_packet |
| 133 |
|
| 134 |
|
| 135 |
decodePacket_strict :: ByteString.Char8.ByteString -> PacketOf Message |
| 136 |
decodePacket_strict = Binary.runGet get_packet . ByteString.Lazy.fromChunks . (: []) |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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 |