| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Sound.Osc.Coding.Decode.Base ( |
| 5 |
decodeMessage, |
| 6 |
decodeBundle, |
| 7 |
decodePacket, |
| 8 |
) where |
| 9 |
|
| 10 |
import Data.Binary {- base -} |
| 11 |
import qualified Data.ByteString.Char8 as C {- bytestring -} |
| 12 |
import qualified Data.ByteString.Lazy as B {- bytestring -} |
| 13 |
import Data.List {- base -} |
| 14 |
import Data.Maybe {- base -} |
| 15 |
|
| 16 |
import Sound.Osc.Coding.Byte {- hosc -} |
| 17 |
import Sound.Osc.Coding.Convert {- hosc -} |
| 18 |
import Sound.Osc.Datum {- hosc -} |
| 19 |
import Sound.Osc.Packet {- hosc -} |
| 20 |
import Sound.Osc.Time {- hosc -} |
| 21 |
|
| 22 |
|
| 23 |
size :: DatumType -> B.ByteString -> Int |
| 24 |
size ty b = |
| 25 |
case ty of |
| 26 |
'i' -> 4 -- Int32 |
| 27 |
'f' -> 4 -- Float |
| 28 |
'd' -> 8 -- Double |
| 29 |
't' -> 8 -- Time (NTP) |
| 30 |
'm' -> 4 -- MIDI |
| 31 |
's' -> |
| 32 |
int64_to_int |
| 33 |
( fromMaybe |
| 34 |
(error ("size: no terminating zero: " ++ show b)) |
| 35 |
(B.elemIndex 0 b) |
| 36 |
) |
| 37 |
'b' -> decode_i32 (B.take 4 b) |
| 38 |
_ -> error "size: illegal type" |
| 39 |
|
| 40 |
|
| 41 |
storage :: DatumType -> B.ByteString -> Int |
| 42 |
storage ty b = |
| 43 |
case ty of |
| 44 |
's' -> let n = size 's' b + 1 in n + align n |
| 45 |
'b' -> let n = size 'b' b in n + align n + 4 |
| 46 |
_ -> size ty B.empty |
| 47 |
|
| 48 |
|
| 49 |
decode_datum :: DatumType -> B.ByteString -> Datum |
| 50 |
decode_datum ty b = |
| 51 |
case ty of |
| 52 |
'i' -> Int32 (decode b) |
| 53 |
'h' -> Int64 (decode b) |
| 54 |
'f' -> Float (decode_f32 b) |
| 55 |
'd' -> Double (decode_f64 b) |
| 56 |
's' -> AsciiString (decode_ascii (b_take (size 's' b) b)) |
| 57 |
'b' -> Blob (b_take (size 'b' b) (B.drop 4 b)) |
| 58 |
't' -> TimeStamp (ntpi_to_ntpr (decode_word64 b)) |
| 59 |
'm' -> |
| 60 |
case B.unpack (B.take 4 b) of |
| 61 |
[b0, b1, b2, b3] -> midi (b0, b1, b2, b3) |
| 62 |
_ -> error "decode_datum: illegal midi data" |
| 63 |
_ -> error ("decode_datum: illegal type (" ++ [ty] ++ ")") |
| 64 |
|
| 65 |
|
| 66 |
decode_datum_seq :: Ascii -> B.ByteString -> [Datum] |
| 67 |
decode_datum_seq cs b = |
| 68 |
let swap (x, y) = (y, x) |
| 69 |
cs' = C.unpack cs |
| 70 |
f b' c = swap (B.splitAt (int_to_int64 (storage c b')) b') |
| 71 |
in zipWith decode_datum cs' (snd (mapAccumL f b cs')) |
| 72 |
|
| 73 |
|
| 74 |
decodeMessage :: B.ByteString -> Message |
| 75 |
decodeMessage b = |
| 76 |
let n = storage 's' b |
| 77 |
cmd = decode_datum 's' b |
| 78 |
m = storage 's' (b_drop n b) |
| 79 |
in case (cmd, decode_datum 's' (b_drop n b)) of |
| 80 |
(AsciiString cmd', AsciiString dsc) -> |
| 81 |
let arg = decode_datum_seq (descriptor_tags dsc) (b_drop (n + m) b) |
| 82 |
in Message (C.unpack cmd') arg |
| 83 |
_ -> error "decodeMessage" |
| 84 |
|
| 85 |
|
| 86 |
decode_message_seq :: B.ByteString -> [Message] |
| 87 |
decode_message_seq b = |
| 88 |
let s = decode_i32 b |
| 89 |
m = decodeMessage (b_drop 4 b) |
| 90 |
nxt = decode_message_seq (b_drop (4 + s) b) |
| 91 |
in if B.length b == 0 then [] else m : nxt |
| 92 |
|
| 93 |
|
| 94 |
decodeBundle :: B.ByteString -> BundleOf Message |
| 95 |
decodeBundle b = |
| 96 |
let h = storage 's' b -- header (should be '#bundle') |
| 97 |
t = storage 't' (b_drop h b) -- time |
| 98 |
in case decode_datum 't' (b_drop h b) of |
| 99 |
TimeStamp timeStamp -> Bundle timeStamp (decode_message_seq (b_drop (h + t) b)) |
| 100 |
_ -> error "decodeBundle" |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
decodePacket :: B.ByteString -> PacketOf Message |
| 109 |
decodePacket b = |
| 110 |
if bundleHeader `B.isPrefixOf` b |
| 111 |
then Packet_Bundle (decodeBundle b) |
| 112 |
else Packet_Message (decodeMessage b) |
| 113 |
|
| 114 |
-- * Util |
| 115 |
|
| 116 |
|
| 117 |
b_take :: Int -> B.ByteString -> B.ByteString |
| 118 |
b_take = B.take . int_to_int64 |
| 119 |
|
| 120 |
|
| 121 |
b_drop :: Int -> B.ByteString -> B.ByteString |
| 122 |
b_drop = B.drop . int_to_int64 |