5 module Codec.Audio.WavPack.Unpack
9 import Codec.Audio.WavPack.Internal
11 import Data.Bitstream.Generic (Bitstream)
12 import qualified Data.Bitstream.Generic as B
15 -- Read a single unsigned value from the specified bitstream with a
16 -- value from 0 to maxCode. If there are exactly a power of two number
17 -- of possible codes then this will read a fixed number of bits;
18 -- otherwise it reads the minimum number of bits and then determines
19 -- whether another bit is needed to define the code.
20 readCode ∷ Bitstream bs ⇒ bs → Word32 → (Word32, bs)
21 {-# INLINEABLE readCode #-}
22 readCode bs 0 = (0, bs)
23 readCode bs 1 = (b2n $ B.head bs, B.tail bs)
25 = let !bitCount = countBits maxCode
26 !extras = (1 `shiftL` bitCount) - maxCode - 1