| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
module Sound.Osc.Coding.Byte where |
| 5 |
|
| 6 |
import Data.Bits {- base -} |
| 7 |
import Data.Int {- base -} |
| 8 |
import Data.Word {- base -} |
| 9 |
import System.IO {- base -} |
| 10 |
|
| 11 |
import qualified GHC.ByteOrder {- base -} |
| 12 |
|
| 13 |
import qualified Data.Binary as Binary {- binary -} |
| 14 |
import qualified Data.Binary.Get as Binary.Get {- binary -} |
| 15 |
import qualified Data.Binary.Put as Binary.Put {- binary -} |
| 16 |
|
| 17 |
import qualified Data.ByteString as ByteString {- bytestring -} |
| 18 |
import qualified Data.ByteString.Char8 as ByteString.Char8 {- bytestring -} |
| 19 |
import qualified Data.ByteString.Internal as ByteString.Internal {- bytestring -} |
| 20 |
import qualified Data.ByteString.Lazy as ByteString.Lazy {- bytestring -} |
| 21 |
import qualified Data.ByteString.Lazy.Char8 as ByteString.Lazy.Char8 {- bytestring -} |
| 22 |
import qualified Data.ByteString.Unsafe as ByteString.Unsafe {- bytestring -} |
| 23 |
|
| 24 |
import qualified Sound.Osc.Coding.Convert as Convert {- hosc -} |
| 25 |
|
| 26 |
-- * Encode |
| 27 |
|
| 28 |
|
| 29 |
encode_int8 :: Int8 -> ByteString.Lazy.ByteString |
| 30 |
encode_int8 = Binary.encode |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
encode_int16 :: Int16 -> ByteString.Lazy.ByteString |
| 38 |
encode_int16 = Binary.encode |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
encode_int16_le :: Int16 -> ByteString.Lazy.ByteString |
| 46 |
encode_int16_le = Binary.Put.runPut . Binary.Put.putInt16le |
| 47 |
|
| 48 |
|
| 49 |
encode_int64 :: Int64 -> ByteString.Lazy.ByteString |
| 50 |
encode_int64 = Binary.encode |
| 51 |
|
| 52 |
|
| 53 |
encode_word8 :: Word8 -> ByteString.Lazy.ByteString |
| 54 |
encode_word8 = Binary.encode |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
encode_word16 :: Word16 -> ByteString.Lazy.ByteString |
| 62 |
encode_word16 = Binary.encode |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
encode_word16_le :: Word16 -> ByteString.Lazy.ByteString |
| 70 |
encode_word16_le = Binary.Put.runPut . Binary.Put.putWord16le |
| 71 |
|
| 72 |
|
| 73 |
encode_word32 :: Word32 -> ByteString.Lazy.ByteString |
| 74 |
encode_word32 = Binary.encode |
| 75 |
|
| 76 |
|
| 77 |
encode_word32_le :: Word32 -> ByteString.Lazy.ByteString |
| 78 |
encode_word32_le = Binary.Put.runPut . Binary.Put.putWord32le |
| 79 |
|
| 80 |
|
| 81 |
encode_word64 :: Word64 -> ByteString.Lazy.ByteString |
| 82 |
encode_word64 = Binary.encode |
| 83 |
|
| 84 |
-- * Encode/Int |
| 85 |
|
| 86 |
|
| 87 |
encode_i8 :: Int -> ByteString.Lazy.ByteString |
| 88 |
encode_i8 = encode_int8 . Convert.int_to_int8 |
| 89 |
|
| 90 |
|
| 91 |
encode_u8 :: Int -> ByteString.Lazy.ByteString |
| 92 |
encode_u8 = encode_word8 . Convert.int_to_word8 |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
encode_u16 :: Int -> ByteString.Lazy.ByteString |
| 100 |
encode_u16 = encode_word16 . Convert.int_to_word16 |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
encode_u16_le :: Int -> ByteString.Lazy.ByteString |
| 108 |
encode_u16_le = encode_word16_le . Convert.int_to_word16 |
| 109 |
|
| 110 |
|
| 111 |
encode_i16 :: Int -> ByteString.Lazy.ByteString |
| 112 |
encode_i16 = Binary.encode . Convert.int_to_int16 |
| 113 |
|
| 114 |
|
| 115 |
encode_i32 :: Int -> ByteString.Lazy.ByteString |
| 116 |
encode_i32 = Binary.encode . Convert.int_to_int32 |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
encode_u32 :: Int -> ByteString.Lazy.ByteString |
| 124 |
encode_u32 = encode_word32 . Convert.int_to_word32 |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
encode_u32_le :: Int -> ByteString.Lazy.ByteString |
| 132 |
encode_u32_le = encode_word32_le . Convert.int_to_word32 |
| 133 |
|
| 134 |
-- * Encode/Float |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
encode_f32 :: Float -> ByteString.Lazy.ByteString |
| 142 |
encode_f32 = Binary.Put.runPut . Binary.Put.putFloatbe |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
encode_f32_le :: Float -> ByteString.Lazy.ByteString |
| 150 |
encode_f32_le = Binary.Put.runPut . Binary.Put.putFloatle |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
encode_f64 :: Double -> ByteString.Lazy.ByteString |
| 158 |
encode_f64 = Binary.Put.runPut . Binary.Put.putDoublebe |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
encode_f64_le :: Double -> ByteString.Lazy.ByteString |
| 166 |
encode_f64_le = Binary.Put.runPut . Binary.Put.putDoublele |
| 167 |
|
| 168 |
-- * Encode/Ascii |
| 169 |
|
| 170 |
|
| 171 |
encode_ascii :: ByteString.Char8.ByteString -> ByteString.Lazy.ByteString |
| 172 |
encode_ascii = ByteString.Lazy.pack . ByteString.unpack |
| 173 |
|
| 174 |
-- * Decode |
| 175 |
|
| 176 |
|
| 177 |
decode_word16 :: ByteString.Lazy.ByteString -> Word16 |
| 178 |
decode_word16 = Binary.decode |
| 179 |
|
| 180 |
|
| 181 |
decode_word16_le :: ByteString.Lazy.ByteString -> Word16 |
| 182 |
decode_word16_le = Binary.Get.runGet Binary.Get.getWord16le |
| 183 |
|
| 184 |
|
| 185 |
decode_int16 :: ByteString.Lazy.ByteString -> Int16 |
| 186 |
decode_int16 = Binary.decode |
| 187 |
|
| 188 |
|
| 189 |
decode_word32 :: ByteString.Lazy.ByteString -> Word32 |
| 190 |
decode_word32 = Binary.decode |
| 191 |
|
| 192 |
|
| 193 |
decode_word32_le :: ByteString.Lazy.ByteString -> Word32 |
| 194 |
decode_word32_le = Binary.Get.runGet Binary.Get.getWord32le |
| 195 |
|
| 196 |
|
| 197 |
decode_int64 :: ByteString.Lazy.ByteString -> Int64 |
| 198 |
decode_int64 = Binary.decode |
| 199 |
|
| 200 |
|
| 201 |
decode_word64 :: ByteString.Lazy.ByteString -> Word64 |
| 202 |
decode_word64 = Binary.decode |
| 203 |
|
| 204 |
-- * Decode/Int |
| 205 |
|
| 206 |
|
| 207 |
decode_u8 :: ByteString.Lazy.ByteString -> Int |
| 208 |
decode_u8 = Convert.word8_to_int . ByteString.Lazy.head |
| 209 |
|
| 210 |
|
| 211 |
decode_i8 :: ByteString.Lazy.ByteString -> Int |
| 212 |
decode_i8 = Convert.int8_to_int . Binary.decode |
| 213 |
|
| 214 |
|
| 215 |
decode_u16 :: ByteString.Lazy.ByteString -> Int |
| 216 |
decode_u16 = Convert.word16_to_int . decode_word16 |
| 217 |
|
| 218 |
|
| 219 |
decode_u16_le :: ByteString.Lazy.ByteString -> Int |
| 220 |
decode_u16_le = Convert.word16_to_int . decode_word16_le |
| 221 |
|
| 222 |
|
| 223 |
decode_i16 :: ByteString.Lazy.ByteString -> Int |
| 224 |
decode_i16 = Convert.int16_to_int . decode_int16 |
| 225 |
|
| 226 |
|
| 227 |
decode_i16_le :: ByteString.Lazy.ByteString -> Int |
| 228 |
decode_i16_le = decode_i16 . ByteString.Lazy.reverse |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
decode_i32 :: ByteString.Lazy.ByteString -> Int |
| 236 |
decode_i32 = Convert.int32_to_int . Binary.decode |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
decode_i32_le :: ByteString.Lazy.ByteString -> Int |
| 244 |
decode_i32_le = decode_i32 . ByteString.Lazy.reverse |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
decode_u32 :: ByteString.Lazy.ByteString -> Int |
| 252 |
decode_u32 = Convert.word32_to_int . decode_word32 |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
decode_u32_le :: ByteString.Lazy.ByteString -> Int |
| 260 |
decode_u32_le = Convert.word32_to_int . decode_word32_le |
| 261 |
|
| 262 |
-- * Decode/Float |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
decode_f32 :: ByteString.Lazy.ByteString -> Float |
| 270 |
decode_f32 = Binary.Get.runGet Binary.Get.getFloatbe |
| 271 |
|
| 272 |
|
| 273 |
decode_f32_le :: ByteString.Lazy.ByteString -> Float |
| 274 |
decode_f32_le = Binary.Get.runGet Binary.Get.getFloatle |
| 275 |
|
| 276 |
|
| 277 |
decode_f64 :: ByteString.Lazy.ByteString -> Double |
| 278 |
decode_f64 = Binary.Get.runGet Binary.Get.getDoublebe |
| 279 |
|
| 280 |
-- * Decode/Ascii |
| 281 |
|
| 282 |
|
| 283 |
decode_ascii :: ByteString.Lazy.ByteString -> ByteString.Char8.ByteString |
| 284 |
{-# INLINE decode_ascii #-} |
| 285 |
decode_ascii = ByteString.Char8.pack . ByteString.Lazy.Char8.unpack |
| 286 |
|
| 287 |
-- * IO |
| 288 |
|
| 289 |
|
| 290 |
read_decode :: (ByteString.Lazy.ByteString -> t) -> Int -> Handle -> IO t |
| 291 |
read_decode f n = fmap f . flip ByteString.Lazy.hGet n |
| 292 |
|
| 293 |
|
| 294 |
read_word32 :: Handle -> IO Word32 |
| 295 |
read_word32 = read_decode Binary.decode 4 |
| 296 |
|
| 297 |
|
| 298 |
read_word32_le :: Handle -> IO Word32 |
| 299 |
read_word32_le = read_decode decode_word32_le 4 |
| 300 |
|
| 301 |
|
| 302 |
write_word32 :: Handle -> Word32 -> IO () |
| 303 |
write_word32 h = ByteString.Lazy.hPut h . encode_word32 |
| 304 |
|
| 305 |
|
| 306 |
write_word32_le :: Handle -> Word32 -> IO () |
| 307 |
write_word32_le h = ByteString.Lazy.hPut h . encode_word32_le |
| 308 |
|
| 309 |
-- * Io/Int |
| 310 |
|
| 311 |
|
| 312 |
read_i8 :: Handle -> IO Int |
| 313 |
read_i8 = read_decode decode_i8 1 |
| 314 |
|
| 315 |
|
| 316 |
read_i16 :: Handle -> IO Int |
| 317 |
read_i16 = read_decode decode_i16 2 |
| 318 |
|
| 319 |
|
| 320 |
read_i32 :: Handle -> IO Int |
| 321 |
read_i32 = read_decode decode_i32 4 |
| 322 |
|
| 323 |
|
| 324 |
read_i32_le :: Handle -> IO Int |
| 325 |
read_i32_le = read_decode decode_i32_le 4 |
| 326 |
|
| 327 |
|
| 328 |
read_u32 :: Handle -> IO Int |
| 329 |
read_u32 = read_decode decode_u32 4 |
| 330 |
|
| 331 |
|
| 332 |
read_u32_le :: Handle -> IO Int |
| 333 |
read_u32_le = read_decode decode_u32_le 4 |
| 334 |
|
| 335 |
|
| 336 |
write_u32 :: Handle -> Int -> IO () |
| 337 |
write_u32 h = ByteString.Lazy.hPut h . encode_u32 |
| 338 |
|
| 339 |
|
| 340 |
write_u32_le :: Handle -> Int -> IO () |
| 341 |
write_u32_le h = ByteString.Lazy.hPut h . encode_u32_le |
| 342 |
|
| 343 |
-- * Io/Float |
| 344 |
|
| 345 |
|
| 346 |
read_f32 :: Handle -> IO Float |
| 347 |
read_f32 = read_decode decode_f32 4 |
| 348 |
|
| 349 |
|
| 350 |
read_f32_le :: Handle -> IO Float |
| 351 |
read_f32_le = read_decode decode_f32_le 4 |
| 352 |
|
| 353 |
-- * Io/Ascii |
| 354 |
|
| 355 |
|
| 356 |
read_pstr :: Handle -> IO ByteString.Char8.ByteString |
| 357 |
read_pstr h = do |
| 358 |
n <- fmap decode_u8 (ByteString.Lazy.hGet h 1) |
| 359 |
fmap decode_ascii (ByteString.Lazy.hGet h n) |
| 360 |
|
| 361 |
-- * Util |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
bundleHeader_strict :: ByteString.Char8.ByteString |
| 369 |
bundleHeader_strict = ByteString.Char8.pack "#bundle\0" |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
bundleHeader :: ByteString.Lazy.ByteString |
| 377 |
{-# INLINE bundleHeader #-} |
| 378 |
bundleHeader = ByteString.Lazy.Char8.fromChunks [bundleHeader_strict] |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
align :: (Num i, Bits i) => i -> i |
| 389 |
{-# INLINE align #-} |
| 390 |
align n = ((n + 3) .&. complement 3) - n |
| 391 |
|
| 392 |
-- * ByteString |
| 393 |
|
| 394 |
|
| 395 |
isLittleEndian :: Bool |
| 396 |
isLittleEndian = GHC.ByteOrder.targetByteOrder == GHC.ByteOrder.LittleEndian |
| 397 |
|
| 398 |
|
| 399 |
byteStringSwap32BitWords :: ByteString.ByteString -> ByteString.ByteString |
| 400 |
byteStringSwap32BitWords xs = |
| 401 |
ByteString.Internal.unsafePackLenBytes |
| 402 |
(ByteString.length xs `quot` 4 * 4) |
| 403 |
[ ByteString.Unsafe.unsafeIndex xs $ i * 4 + j |
| 404 |
| i <- [0 .. ByteString.length xs `quot` 4 - 1] |
| 405 |
, j <- [3, 2, 1, 0] |
| 406 |
] |
| 407 |
|
| 408 |
|
| 409 |
byteString32BitNetworkOrder :: ByteString.ByteString -> ByteString.ByteString |
| 410 |
byteString32BitNetworkOrder x = if isLittleEndian then byteStringSwap32BitWords x else x |