module Sound.File.Decode ( decode
, deinterleave ) where
import Data.Int
import qualified Data.ByteString.Lazy as B
import Sound.OSC.Coding.Byte
import Sound.File.Encoding
setsOf :: Int -> [a] -> [[a]]
setsOf n l =
case l of
[] -> []
_ -> let (x, y) = splitAt n l
in x : setsOf n y
channel :: [[a]] -> Int -> [a]
channel l n = map (!!n) l
deinterleave :: Int -> [a] -> [[a]]
deinterleave n l = map (channel (setsOf n l)) [0..n1]
bSetsOf :: Int64 -> B.ByteString -> [B.ByteString]
bSetsOf _ b | B.null b = []
bSetsOf n b =
if B.null b
then []
else let (x, y) = B.splitAt n b
in x : bSetsOf n y
decodef32 :: (Real n,Floating n) => Int -> B.ByteString -> [[n]]
decodef32 n = deinterleave n . map (realToFrac . decode_f32) . bSetsOf 4
decodef64 :: (Real n,Floating n) => Int -> B.ByteString -> [[n]]
decodef64 n = deinterleave n . map (realToFrac . decode_f64) . bSetsOf 8
decodei32 :: Int -> B.ByteString -> [[Int]]
decodei32 n = deinterleave n . map decode_i32 . bSetsOf 4
decodei16 :: Int -> B.ByteString -> [[Int]]
decodei16 n = deinterleave n . map decode_i16 . bSetsOf 2
decodei8 :: Int -> B.ByteString -> [[Int]]
decodei8 n = deinterleave n . map decode_i8 . bSetsOf 1
i8_f :: (Real n,Floating n) => [Int] -> [n]
i8_f = map ((/ 128.0) . fromIntegral)
i16_f :: (Real n,Floating n) => [Int] -> [n]
i16_f = map ((/ 32768.0) . fromIntegral)
i32_f :: (Real n,Floating n) => [Int] -> [n]
i32_f = map ((/ 2147483648.0) . fromIntegral)
decode :: (Real n,Floating n) => Encoding -> Int -> B.ByteString -> [[n]]
decode enc nc b =
case enc of
Linear8 -> map i8_f (decodei8 nc b)
Linear16 -> map i16_f (decodei16 nc b)
Linear32 -> map i32_f (decodei32 nc b)
Float -> decodef32 nc b
Double -> decodef64 nc b