| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Sound.Osc.Coding.Encode.Base where |
| 5 |
|
| 6 |
import Data.Binary {- base -} |
| 7 |
import qualified Data.ByteString.Char8 as C {- bytestring -} |
| 8 |
import qualified Data.ByteString.Lazy as B {- bytestring -} |
| 9 |
|
| 10 |
import Sound.Osc.Coding.Byte {- hosc -} |
| 11 |
import Sound.Osc.Coding.Convert {- hosc -} |
| 12 |
import Sound.Osc.Datum {- hosc -} |
| 13 |
import Sound.Osc.Packet {- hosc -} |
| 14 |
import Sound.Osc.Time {- hosc -} |
| 15 |
|
| 16 |
|
| 17 |
extend :: Word8 -> B.ByteString -> B.ByteString |
| 18 |
extend p s = B.append s (B.replicate (align (B.length s)) p) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
encode_datum :: Datum -> B.ByteString |
| 49 |
encode_datum dt = |
| 50 |
case dt of |
| 51 |
Int32 i -> encode i |
| 52 |
Int64 i -> encode i |
| 53 |
Float f -> encode_f32 f |
| 54 |
Double d -> encode_f64 d |
| 55 |
TimeStamp t -> encode_word64 $ ntpr_to_ntpi t |
| 56 |
AsciiString s -> extend 0 (B.snoc (encode_ascii s) 0) |
| 57 |
Midi (MidiData b0 b1 b2 b3) -> B.pack [b0, b1, b2, b3] |
| 58 |
Blob b -> |
| 59 |
let n = encode (int64_to_int32 (B.length b)) |
| 60 |
in B.append n (extend 0 b) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
encodeMessage :: Message -> B.ByteString |
| 79 |
encodeMessage (Message c l) = |
| 80 |
B.concat |
| 81 |
[ encode_datum (AsciiString (C.pack c)) |
| 82 |
, encode_datum (AsciiString (descriptor l)) |
| 83 |
, B.concat (map encode_datum l) |
| 84 |
] |
| 85 |
|
| 86 |
|
| 87 |
encode_message_blob :: Message -> Datum |
| 88 |
encode_message_blob = Blob . encodeMessage |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
encodeBundle :: BundleOf Message -> B.ByteString |
| 105 |
encodeBundle (Bundle t m) = |
| 106 |
B.concat |
| 107 |
[ bundleHeader |
| 108 |
, encode_word64 (ntpr_to_ntpi t) |
| 109 |
, B.concat (map (encode_datum . encode_message_blob) m) |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
encodePacket :: PacketOf Message -> B.ByteString |
| 114 |
encodePacket o = |
| 115 |
case o of |
| 116 |
Packet_Message m -> encodeMessage m |
| 117 |
Packet_Bundle b -> encodeBundle b |