packages feed

wav-0.1.0.0 / lib / Data / Wav.hs

-- | wav file formatmodule Data.Wav (Wav(..), Format(..), toWav, toRap, denorm, norm) whereimport Control.Applicativeimport Control.Monadimport Data.Binaryimport Data.Binary.Getimport Data.Binary.Putimport Data.Bitsimport Data.ByteString.Lazy (ByteString)import qualified Data.ByteString.Lazy as BSimport Data.Intimport Data.Maybeimport Data.Rap-- | wav languagedata Wav  = Wav [Wav]  | Fmt Format  | Aud [Double]  | Chk String ByteString  | Lst String String [Wav]  deriving (Eq, Read, Show)data Format = Format{ccode :: Word16, nchans :: Word16, sampRate :: Word32  , avgBytesPerSec :: Word32, balign :: Word16, bitsPerSamp :: Word16  , extraFmt :: Maybe ByteString} deriving (Eq, Read, Show)-- | put 'extraFmt' byte length not checkedinstance Binary Format where  get = Format <$> getWord16le <*> getWord16le <*> getWord32le <*> getWord32le    <*> getWord16le <*> getWord16le    <*> optional (getLazyByteString . fromIntegral =<< getWord16le)  put (Format c n sr avgbs bali bps extM) = do    putWord16le c >> putWord16le n >> putWord32le sr >> putWord32le avgbs    putWord16le bali >> putWord16le bps    maybe mempty (mappend (putWord16le l) . putLazyByteString) extM    where      l = fromIntegral $ maybe 0 BS.length extM-- | decompile rap to wavtoWav :: Rap -> Either String WavtoWav r = do  w <- decodeFormat =<< fromRiff (fromRap r)  case findFormat w of    Just Format{ccode=1, bitsPerSamp=bps} -> decodeAudio bps w    Just  _ -> Right w    Nothing -> Left "toWav: no format found"fromRap :: Rap -> WavfromRap (Stanza n t s) = Lst n t $ fromRap <$> sfromRap (Verse n b) = Chk n bfromRiff :: Wav -> Either String WavfromRiff w = case w of  Lst "RIFF" "WAVE" cs -> Right $ Wav cs  _ -> Left "fromRiff: must begin with RIFF WAVE list"decodeFormat :: Wav -> Either String WavdecodeFormat w = case w of  Wav cs -> Wav <$> mapM decodeFormat cs  Chk "fmt " bs -> Fmt <$> case runGetOrFail get bs of    Left  (_, _, e) -> Left $ "decodeFormat: " ++ e    Right (_, _, f) -> Right f  _ -> Right wfindFormat :: Wav -> Maybe FormatfindFormat w = case w of  Wav cs -> listToMaybe [f | Fmt f <- cs]  _ -> NothingdecodeAudio :: Word16 -> Wav -> Either String WavdecodeAudio bps w = case w of  Wav cs -> Wav <$> mapM (decodeAudio bps) cs  Chk "data" bs -> Aud <$> norm bps bs  _ -> Right w-- | compile wav to rifftoRap :: Wav -> Either String RaptoRap c = case c of  Wav cs -> Stanza "RIFF" "WAVE" <$> mapM (toRiff fmtM) cs  _ -> Left "toRap: must begin with RIFF WAVE"  where    fmtM = findFormat ctoRiff :: Maybe Format -> Wav -> Either String RaptoRiff fmtM w = case w of  Wav{} -> Left "toRiff: only one RIFF WAVE allowed"  Fmt f@Format{extraFmt=extM} -> do    when (BS.compareLength (fromMaybe mempty extM) (fromIntegral (maxBound :: Word16)) == GT) $      Left "toRiff: extra format length > Word16"    Right $ Verse "fmt " $ runPut $ put f  Aud s -> case fmtM of    Just Format{ccode=1, bitsPerSamp=bps} ->      Verse "data" <$> denorm bps s    Just  _ -> Left "toRiff: unsupported audio format"    Nothing -> Left "toRiff: unknown audio format"  Chk n bs -> Right $ Verse n bs  Lst n ty cs -> Stanza n ty <$> mapM (toRiff fmtM) cs-- | normalize PCMnorm :: Word16 -> ByteString -> Either String [Double]norm bps  | bps >= 1 && bps <= 8  = (fmap . map) (normU bps . fromIntegral . (`shiftR` (8 - bps')))    . fromGet . runGetOrFail (many getWord8)  | bps >  8 && bps <= 16 = (fmap . map) (normS bps . fromIntegral . (`shiftR` (16 - bps')))    . fromGet . runGetOrFail (many getInt16le)  | bps > 16 && bps <= 24 = (fmap . map) (normS bps . fromIntegral . (`shiftR` (24 - bps')))    . fromGet . runGetOrFail (many getInt24le)  | bps > 24 && bps <= 32 = (fmap . map) (normS bps . fromIntegral . (`shiftR` (32 - bps')))    . fromGet . runGetOrFail (many getInt32le)  | otherwise = const $ Left $ "norm: unsupported bits-per-sample " ++ show bps  where    bps' = fromIntegral bps    fromGet (Left  (_, _, e)) = Left $ "norm: " ++ e    fromGet (Right (_, _, a)) = Right anormU :: Word16 -> Integer -> DoublenormU bps s = (fromIntegral s - 2 ^ (bps - 1)) / 2 ^ (bps - 1)normS :: Word16 -> Integer -> DoublenormS bps s = fromIntegral s / 2 ^ (bps - 1)-- | denormalize PCMdenorm :: Word16 -> [Double] -> Either String ByteStringdenorm bps  | bps >= 1 && bps <= 8 = Right . runPut .    foldMap (putWord8 . (`shiftL` (8 - bps')) . fromIntegral . denormU bps)  | bps >  8 && bps <= 16 = Right . runPut .    foldMap (putInt16le . (`shiftL` (16 - bps')) . fromIntegral . denormS bps)  | bps > 16 && bps <= 24 = Right . runPut .    foldMap (putInt24le . (`shiftL` (24 - bps')) . fromIntegral . denormS bps)  | bps > 24 && bps <= 32 = Right . runPut .    foldMap (putInt32le . (`shiftL` (32 - bps')) . fromIntegral . denormS bps)  | otherwise = const $ Left $ "denorm: unsupported bits-per-sample " ++ show bps  where    bps' = fromIntegral bpsdenormU :: Word16 -> Double -> IntegerdenormU bps s = min (2 ^ bps - 1) $ round $ s * 2 ^ (bps - 1) + 2 ^ (bps - 1)denormS :: Word16 -> Double -> IntegerdenormS bps s = min (2 ^ (bps - 1) - 1) $ round $ s * 2 ^ (bps - 1)putInt24le :: Int32 -> PutputInt24le i32 = foldMap (putWord8 . fromIntegral) [i32, i32 `shiftR` 8, i32 `shiftR` 16]getInt24le :: Get Int32getInt24le = getWord8 >>= \a -> getWord8 >>= \b -> getWord8 >>= \c -> do  let i32 = fromIntegral a .|. fromIntegral b `shiftL` 8 .|. fromIntegral c `shiftL` 16  return $ i32 `shiftL` 8 `shiftR` 8