3 , ExistentialQuantification
6 -- | WavPack metadata sub-blocks
7 module Codec.Audio.WavPack.Metadata
17 import Data.Binary.Get
18 import Data.Binary.Put
20 import qualified Data.ByteString.Lazy as L
22 import Prelude.Unicode
24 -- | Type class for every metadata sub-blocks.
25 class (Binary α, Eq α, Show α, Typeable α) ⇒ Metadata α where
26 -- | Get the metadata ID without odd-size bit nor large-block bit
29 -- | Get the size of metadata, excluding the metadata header
32 metaSize = fromIntegral ∘ L.length ∘ runPut ∘ put
33 -- | Cast a 'SubBlock' to this type of metadata (optional).
34 fromSubBlock ∷ SubBlock → Maybe α
35 fromSubBlock (SubBlock a) = cast a
36 -- | Wrap the metadata into 'SubBlock' (optional).
37 toSubBlock ∷ α → SubBlock
40 -- | An opaque sub-block container.
41 data SubBlock = ∀α. Metadata α ⇒ SubBlock α
44 instance Metadata SubBlock where
45 metaID (SubBlock a) = metaID a
46 metaSize (SubBlock a) = metaSize a
50 instance Binary SubBlock where
52 = let size = metaSize a
54 oddBit = if odd size then 0x40 else 0
55 largeBit = if size > 0x1FE then 0x80 else 0
56 idWord = metaID a .|. oddBit .|. largeBit
60 -- Don't forget about the endianness.
61 do putWord8 $ fromIntegral $ (size' `shiftR` 1) .&. 0xFF
62 putWord8 $ fromIntegral $ (size' `shiftR` 9) .&. 0xFF
63 putWord8 $ fromIntegral $ (size' `shiftR` 17) .&. 0xFF
65 putWord8 $ fromIntegral $ (size' `shiftR` 1) .&. 0xFF
67 when (odd size) $ putWord8 0
69 get = do idWord ← getWord8
70 let isOdd = idWord .&. 0x40 ≢ 0
71 isLarge = idWord .&. 0x80 ≢ 0
72 rawID = idWord .&. (complement 0x40) .&. (complement 0x80)
73 adj = if isOdd then -1 else 0
74 size ← if isLarge then
78 return $ ( (fromIntegral sz2 `shiftL` 17) .|.
79 (fromIntegral sz1 `shiftL` 9) .|.
80 (fromIntegral sz0 `shiftL` 1)
83 fmap ((+ adj) ∘ (`shiftL` 1) ∘ fromIntegral) getWord8
84 subb ← getLazyByteString $ fromIntegral (size ∷ Word32)
85 return $ runGet (getSubBlock rawID) subb
87 getSubBlock ∷ Word8 → Get SubBlock
88 getSubBlock 0x00 = fmap SubBlock (get ∷ Get Dummy)
90 = if unknownID .&. 0x20 ≡ 0 then
91 fail ("Unknown WavPack metadata ID: " ⧺ show unknownID)
93 -- It's unknown but optional. We can safely ignore it.
94 fmap (SubBlock ∘ Unknown unknownID) getRemainingLazyByteString
96 instance Eq SubBlock where
97 (SubBlock a) == (SubBlock b)
100 instance Show SubBlock where
101 show (SubBlock a) = show a
103 -- | Dummy metadata to pad WavPack blocks.
106 -- | Must be less than 2^25 bytes long due to the limitation
107 -- of WavPack specification.
110 deriving (Eq, Show, Typeable)
112 instance Metadata Dummy where
116 instance Binary Dummy where
117 put = putLazyByteString ∘ flip L.replicate 0x00 ∘ fromIntegral ∘ dumSize
118 get = fmap (Dummy ∘ fromIntegral) remaining
120 -- | Unknown but optional metadata found in the WavPack block.
123 -- | The ID of this unknown metadata without odd-size bit nor
126 -- | Raw data; must be less than 2^25 bytes long.
127 , unkData ∷ L.ByteString
129 deriving (Eq, Show, Typeable)
131 instance Metadata Unknown where
133 metaSize = fromIntegral ∘ L.length ∘ unkData
135 instance Binary Unknown where
136 put = putLazyByteString ∘ unkData