module Sound.Frame
(C(..),
numberOfChannelsFoldable,
sizeOfElementFoldable,
sizeOfElementType,
paddedSizeOf,
withSignal,
) where
import Data.Word (Word8, Word16, Word32, )
import Data.Int (Int8, Int16, Int32, )
import Foreign.Storable (Storable, sizeOf, alignment, )
import qualified Data.Foldable as Fold
class C y where
numberOfChannels :: y -> Int
sizeOfElement :: y -> Int
numberOfChannelsFoldable ::
(C y, Fold.Foldable f) => f y -> Int
numberOfChannelsFoldable =
Fold.foldl' (\n y -> n + numberOfChannels y) 0
sizeOfElementFoldable ::
(C y, Fold.Foldable f) => f y -> Int
sizeOfElementFoldable =
sizeOfElement . head . Fold.toList
sizeOfElementType ::
(C y) => f y -> Int
sizeOfElementType =
sizeOfElement . elementType
elementType :: f a -> a
elementType _ =
error "Sound.Frame.sizeOfElement may not depend on element values"
instance C Word8 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Int8 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Word16 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Int16 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Word32 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Int32 where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Float where
numberOfChannels _ = 1
sizeOfElement = sizeOf
instance C Double where
numberOfChannels _ = 1
sizeOfElement = sizeOf
paddedSizeOf :: Storable a => a -> Int
paddedSizeOf x =
sizeOf x + mod ( sizeOf x) (alignment x)
withSignal :: (y -> a) -> (sig y -> a)
withSignal f _ = f (error "Frame: dummy signal parameter touched")