wav (empty) → 0.1.0.0
raw patch · 7 files changed · hide whitespace changes · browse wav-0.1.0.0
+340/−0 lines, 7 filesdep +basedep +binarydep +bytestring
Dependencies added: base, binary, bytestring, rap, tasty, tasty-hunit, wav
Files
- CHANGELOG.md +5/−0
- LICENSE +21/−0
- README.md +11/−0
- exe/Main.hs +4/−0
- lib/Data/Wav.hs +152/−0
- test/Main.hs +94/−0 · folded
- wav.cabal +53/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for wav++## 0.1.0.0 -- 12-4-2026++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,21 @@+MIT License++Copyright (c) 2026 David Cox++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ README.md view
@@ -0,0 +1,11 @@+# wav++[wav file format](https://www.loc.gov/preservation/digital/formats/fdd/fdd000001.shtml)++development++```+cabal build+cabal haddock+cabal run test+```
+ exe/Main.hs view
@@ -0,0 +1,4 @@+module Main (main) where++main :: IO ()+main = putStrLn "Hello, Haskell!"
+ lib/Data/Wav.hs view
@@ -0,0 +1,152 @@+-- | wav file format+module Data.Wav (Wav(..), Format(..), toWav, toRap, denorm, norm) where++import Control.Applicative+import Control.Monad+import Data.Binary+import Data.Binary.Get+import Data.Binary.Put+import Data.Bits+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Lazy as BS+import Data.Int+import Data.Maybe+import Data.Rap++-- | wav language+data 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 checked+instance 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 wav+toWav :: Rap -> Either String Wav+toWav 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 -> Wav+fromRap (Stanza n t s) = Lst n t $ fromRap <$> s+fromRap (Verse n b) = Chk n b++fromRiff :: Wav -> Either String Wav+fromRiff w = case w of+ Lst "RIFF" "WAVE" cs -> Right $ Wav cs+ _ -> Left "fromRiff: must begin with RIFF WAVE list"++decodeFormat :: Wav -> Either String Wav+decodeFormat 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 w++findFormat :: Wav -> Maybe Format+findFormat w = case w of+ Wav cs -> listToMaybe [f | Fmt f <- cs]+ _ -> Nothing++decodeAudio :: Word16 -> Wav -> Either String Wav+decodeAudio bps w = case w of+ Wav cs -> Wav <$> mapM (decodeAudio bps) cs+ Chk "data" bs -> Aud <$> norm bps bs+ _ -> Right w++-- | compile wav to riff+toRap :: Wav -> Either String Rap+toRap c = case c of+ Wav cs -> Stanza "RIFF" "WAVE" <$> mapM (toRiff fmtM) cs+ _ -> Left "toRap: must begin with RIFF WAVE"+ where+ fmtM = findFormat c++toRiff :: Maybe Format -> Wav -> Either String Rap+toRiff 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 PCM+norm :: 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 a++normU :: Word16 -> Integer -> Double+normU bps s = (fromIntegral s - 2 ^ (bps - 1)) / 2 ^ (bps - 1)++normS :: Word16 -> Integer -> Double+normS bps s = fromIntegral s / 2 ^ (bps - 1)++-- | denormalize PCM+denorm :: Word16 -> [Double] -> Either String ByteString+denorm 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 bps++denormU :: Word16 -> Double -> Integer+denormU bps s = min (2 ^ bps - 1) $ round $ s * 2 ^ (bps - 1) + 2 ^ (bps - 1)++denormS :: Word16 -> Double -> Integer+denormS bps s = min (2 ^ (bps - 1) - 1) $ round $ s * 2 ^ (bps - 1)++putInt24le :: Int32 -> Put+putInt24le i32 = foldMap (putWord8 . fromIntegral) [i32, i32 `shiftR` 8, i32 `shiftR` 16]++getInt24le :: Get Int32+getInt24le = 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
+ test/Main.hs view
@@ -0,0 +1,94 @@+{-# LANGUAGE BinaryLiterals #-}+module Main (main) where++import Data.Binary.Put+import qualified Data.ByteString.Lazy as BS+import Data.Rap+import Data.Wav+import Test.Tasty+import Test.Tasty.HUnit++main :: IO ()+main = defaultMain $ testGroup "Data.Wav" [toWavTest, toRapTest, denormTest+ , normTest]++toWavTest :: TestTree+toWavTest = testCaseSteps "toWav" $ \step -> do+ step "basic"+ toWav basicRap @?= Right basicWav++toRapTest :: TestTree+toRapTest = testCaseSteps "toRap" $ \step -> do+ step "basic"+ toRap basicWav @?= Right basicRap++basicRap :: Rap+basicRap = Stanza "RIFF" "WAVE"+ [ Verse "fmt " $ runPut $ do+ putWord16le 1 -- PCM+ putWord16le 1 -- MONO+ putWord32le 44100 -- sample rate+ putWord32le 2 -- bytes per second+ putWord16le 2 -- block align+ putWord16le 16 -- bits per sample+ , Verse "data" $ BS.pack [0x00, 0x00, 0x00, 0x00]+ ]++basicWav :: Wav+basicWav = Wav [Fmt $ Format 1 1 44100 2 2 16 Nothing, Aud [0, 0]]++denormTest :: TestTree+denormTest = testCaseSteps "denorm" $ \step -> do+ step "3-bit"+ BS.unpack <$> denorm 3 [-1] @?= Right [0x00]+ BS.unpack <$> denorm 3 [-0.5] @?= Right [0x40]+ BS.unpack <$> denorm 3 [1] @?= Right [0xE0]+ step "8-bit"+ BS.unpack <$> denorm 8 [-1] @?= Right [0x00]+ BS.unpack <$> denorm 8 [0] @?= Right [128]+ BS.unpack <$> denorm 8 [1] @?= Right [0xFF]+ step "16-bit"+ denorm 16 [-1] @?= Right (runPut $ putInt16le minBound)+ denorm 16 [0] @?= Right (runPut $ putInt16le 0)+ denorm 16 [1] @?= Right (runPut $ putInt16le maxBound)+ step "20-bit"+ -- 0x12345 / (2^19 - 1)+ BS.unpack <$> denorm 20 [0.1422214508] @?= Right [0x50, 0x34, 0x12]+ BS.unpack <$> denorm 20 [-0.1422214508] @?= Right [0xB0, 0xCB, 0xED]+ step "24-bit"+ BS.unpack <$> denorm 24 [-1] @?= Right [0x00, 0x00, 0x80]+ BS.unpack <$> denorm 24 [0] @?= Right [0x00, 0x00, 0x00]+ BS.unpack <$> denorm 24 [1] @?= Right [0xFF, 0xFF, 0x7F]+ BS.unpack <$> denorm 24 [1193046 / 8388608] @?= Right [0x56, 0x34, 0x12]+ step "32-bit"+ denorm 32 [-1] @?= Right (runPut $ putInt32le minBound)+ denorm 32 [0] @?= Right (runPut $ putInt32le 0)+ denorm 32 [1] @?= Right (runPut $ putInt32le maxBound)++normTest :: TestTree+normTest = testCaseSteps "norm" $ \step -> do+ step "3-bit"+ norm 3 (BS.pack [0x00]) @?= Right [-1]+ norm 3 (BS.pack [0x40]) @?= Right [-0.5]+ norm 3 (BS.pack [0xE0]) @?= Right [0.75]+ step "8-bit"+ norm 8 (BS.pack [0x00]) @?= Right [-1]+ norm 8 (BS.pack [128]) @?= Right [0]+ norm 8 (BS.pack [0xFF]) @?= Right [0.9921875]+ step "16-bit"+ norm 16 (runPut $ putInt16le minBound) @?= Right [-1]+ norm 16 (runPut $ putInt16le 0) @?= Right [0]+ norm 16 (runPut $ putInt16le maxBound) @?= Right [0.999969482421875]+ step "20-bit"+ -- 0x12345 / (2^19 - 1)+ norm 20 (BS.pack [0x50, 0x32, 0x12]) @?= Right [0.14216041564941406]+ norm 20 (BS.pack [0xB0, 0xCB, 0xED]) @?= Right [-0.14222145080566406]+ step "24-bit"+ norm 24 (BS.pack [0x00, 0x00, 0x80]) @?= Right [-1]+ norm 24 (BS.pack [0x00, 0x00, 0x00]) @?= Right [0]+ norm 24 (BS.pack [0xFF, 0xFF, 0x7F]) @?= Right [0.9999998807907104]+ norm 24 (BS.pack [0x56, 0x34, 0x12]) @?= Right [1193046 / 8388608]+ step "32-bit"+ norm 32 (runPut $ putInt32le minBound) @?= Right [-1]+ norm 32 (runPut $ putInt32le 0) @?= Right [0]+ norm 32 (runPut $ putInt32le maxBound) @?= Right [0.9999999995343387]
+ wav.cabal view
@@ -0,0 +1,53 @@+cabal-version: 3.0+name: wav+version: 0.1.0.0+synopsis: wav file format+homepage: https://github.com/dopamane/wav+license: MIT+license-file: LICENSE+author: dopamane+maintainer: dwc1295@gmail.com+copyright: David Cox+category: Data+build-type: Simple+extra-doc-files: CHANGELOG.md+ README.md++source-repository head+ type: git+ location: https://github.com/dopamane/wav++library+ default-language: Haskell2010+ ghc-options: -Wall -O2+ hs-source-dirs: lib+ exposed-modules: Data.Wav+ build-depends:+ base < 5,+ binary,+ bytestring,+ rap++executable wav+ default-language: Haskell2010+ ghc-options: -Wall -O2 -threaded+ hs-source-dirs: exe+ main-is: Main.hs+ build-depends:+ base < 5,+ wav++test-suite test+ default-language: Haskell2010+ ghc-options: -Wall -O2 -threaded+ hs-source-dirs: test+ type: exitcode-stdio-1.0+ main-is: Main.hs+ build-depends:+ base < 5,+ binary,+ bytestring,+ rap,+ tasty,+ tasty-hunit,+ wav