Debian Codegraph @

Sound/Osc/Coding/Encode/Base.hs

raw symbols SCIP
1 {- | Base-level encode function for Osc packets (slow).
2 For ordinary use see 'Sound.Osc.Coding.Encode.Builder'.
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 -- | Align byte string, if required.
17 extend :: Word8 -> B.ByteString -> B.ByteString
18 extend p s = B.append s (B.replicate (align (B.length s)) p)
19
20 {- | Encode Osc 'Datum'.
21
22 MidiData: Bytes from MSB to LSB are: port id, status byte, data1, data2.
23
24 >>> encode_datum (blob [1, 2, 3, 4]) == B.pack [0, 0, 0, 4, 1, 2, 3, 4]
25 True
26
27 >>> encode_datum (Float 1) == B.pack [63, 128, 0, 0]
28 True
29
30 >>> encode_datum (Float 2) == B.pack [64, 0, 0, 0]
31 True
32
33 >>> encode_datum (Float 3) == B.pack [64, 64, 0, 0]
34 True
35
36 >>> encode_datum (Float 4) == B.pack [64, 128, 0, 0]
37 True
38
39 >>> encode_datum (Float 5) == B.pack [64, 160, 0, 0]
40 True
41
42 >>> encode_datum (Int32 65536) == B.pack [0, 1, 0, 0]
43 True
44
45 >>> encode_datum (Int32 (-65536)) == B.pack [255, 255, 0, 0]
46 True
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 {- | Encode Osc 'Message'.
63
64 >>> blob_unpack (encodeMessage (Message "/x" []))
65 [47,120,0,0,44,0,0,0]
66
67 >>> blob_unpack (encodeMessage (Message "/y" [float 3.141]))
68 [47,121,0,0,44,102,0,0,64,73,6,37]
69
70 >>> let m = Message "/n_set" [int32 (-1), string "freq", float 440, string "amp", float 0.1]
71 >>> let e = blob_unpack (encodeMessage m)
72 >>> length e
73 40
74
75 >>> take 20 e
76 [47,110,95,115,101,116,0,0,44,105,115,102,115,102,0,0,255,255,255,255]
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 -- | Encode Osc 'Message' as an Osc blob.
87 encode_message_blob :: Message -> Datum
88 encode_message_blob = Blob . encodeMessage
89
90 {- | Encode Osc 'Bundle'.
91
92 >>> blob_unpack (encodeBundle (Bundle immediately [Message "/x" []]))
93 [35,98,117,110,100,108,101,0,0,0,0,0,0,0,0,1,0,0,0,8,47,120,0,0,44,0,0,0]
94
95 >>> let m = Message "/n_set" [int32 (-1), string "freq", float 440, string "amp", float 0.1]
96 >>> let b = Bundle 0.0 [m]
97 >>> let e = blob_unpack (encodeBundle b)
98 >>> length e
99 60
100
101 >> take 20 e
102 [35,98,117,110,100,108,101,0,0,0,0,0,0,0,0,0,0,0,0,40]
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 -- | Encode Osc 'Packet'.
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