Debian Codegraph @

Sound/Osc/Coding/Encode/Builder.hs

raw symbols SCIP
1 -- | Optimised encode function for Osc packets.
2 module Sound.Osc.Coding.Encode.Builder (
3 build_packet,
4 encodeMessage,
5 encodeBundle,
6 encodePacket,
7 encodePacket_strict,
8 ) where
9
10 import Data.Word {- base -}
11
12 import qualified Blaze.ByteString.Builder as B {- bytestring -}
13 import qualified Blaze.ByteString.Builder.Char8 as B {- bytestring -}
14 import qualified Data.ByteString as S {- bytestring -}
15 import qualified Data.ByteString.Lazy as L {- bytestring -}
16
17 import qualified Sound.Osc.Coding.Byte as Byte {- hosc -}
18 import qualified Sound.Osc.Coding.Cast as Cast {- hosc -}
19 import qualified Sound.Osc.Coding.Convert as Convert {- hosc -}
20 import Sound.Osc.Datum {- hosc -}
21 import Sound.Osc.Packet {- hosc -}
22 import Sound.Osc.Time {- hosc -}
23
24 -- | Generate a list of zero bytes for padding.
25 padding :: Int -> [Word8]
26 padding n = replicate n 0
27
28 -- | Nul byte (0) and then zero padding.
29 nul_and_padding :: Int -> B.Builder
30 nul_and_padding n = B.fromWord8s (0 : padding (Byte.align n))
31
32 -- Encode a string with zero padding.
33 build_ascii :: Ascii -> B.Builder
34 build_ascii s = B.fromByteString s <> nul_and_padding (S.length s + 1)
35
36 -- Encode a string with zero padding.
37 build_string :: String -> B.Builder
38 build_string s = B.fromString s <> nul_and_padding (length s + 1)
39
40 -- Encode a byte string with prepended length and zero padding.
41 build_bytes :: L.ByteString -> B.Builder
42 build_bytes s =
43 B.fromInt32be (Convert.int64_to_int32 (L.length s))
44 <> B.fromLazyByteString s
45 <> B.fromWord8s (padding (Convert.int64_to_int (Byte.align (L.length s))))
46
47 -- Encode an Osc datum.
48 build_datum :: Datum -> B.Builder
49 build_datum d =
50 case d of
51 Int32 i -> B.fromInt32be i
52 Int64 i -> B.fromInt64be i
53 Float n -> B.fromWord32be (Cast.f32_w32 n)
54 Double n -> B.fromWord64be (Cast.f64_w64 n)
55 TimeStamp t -> B.fromWord64be (ntpr_to_ntpi t)
56 AsciiString s -> build_ascii s
57 Midi (MidiData b0 b1 b2 b3) -> B.fromWord8s [b0, b1, b2, b3]
58 Blob b -> build_bytes b
59
60 -- Encode an Osc 'Message'.
61 build_message :: Message -> B.Builder
62 build_message (Message c l) =
63 mconcat
64 [ build_string c
65 , build_ascii (descriptor l)
66 , mconcat (map build_datum l)
67 ]
68
69 -- Encode an Osc 'Bundle'.
70 build_bundle_ntpi :: Ntp64 -> [Message] -> B.Builder
71 build_bundle_ntpi t l =
72 mconcat
73 [ B.fromLazyByteString Byte.bundleHeader
74 , B.fromWord64be t
75 , mconcat (map (build_bytes . B.toLazyByteString . build_message) l)
76 ]
77
78 -- | Builder for an Osc 'Packet'.
79 build_packet :: PacketOf Message -> B.Builder
80 build_packet o =
81 case o of
82 Packet_Message m -> build_message m
83 Packet_Bundle (Bundle t m) -> build_bundle_ntpi (ntpr_to_ntpi t) m
84
85 {-# INLINE encodePacket #-}
86 {-# INLINE encodeMessage #-}
87 {-# INLINE encodeBundle #-}
88 {-# INLINE encodePacket_strict #-}
89
90 -- | Encode an Osc 'Packet'.
91 encodePacket :: PacketOf Message -> L.ByteString
92 encodePacket = B.toLazyByteString . build_packet
93
94 {- | Encode an Osc 'Message', ie. 'encodePacket' of 'Packet_Message'.
95
96 >>> let m = [47,103,95,102,114,101,101,0,44,105,0,0,0,0,0,0]
97 >>> encodeMessage (Message "/g_free" [Int32 0]) == L.pack m
98 True
99 -}
100 encodeMessage :: Message -> L.ByteString
101 encodeMessage = encodePacket . Packet_Message
102
103 {- | Encode an Osc 'Bundle', ie. 'encodePacket' of 'Packet_Bundle'.
104
105 >>> let m = [47,103,95,102,114,101,101,0,44,105,0,0,0,0,0,0]
106 >>> let b = [35,98,117,110,100,108,101,0,0,0,0,0,0,0,0,1,0,0,0,16] ++ m
107 >>> encodeBundle (Bundle immediately [Message "/g_free" [Int32 0]]) == L.pack b
108 True
109 -}
110 encodeBundle :: BundleOf Message -> L.ByteString
111 encodeBundle = encodePacket . Packet_Bundle
112
113 -- | Encode an Osc 'Packet' to a strict 'S.ByteString'.
114 encodePacket_strict :: PacketOf Message -> S.ByteString
115 encodePacket_strict = B.toByteString . build_packet