packages feed

wec (empty) → 0.1.0.0

raw patch · 7 files changed

+219/−0 lines, 7 filesdep +basedep +binarydep +bytestring

Dependencies added: base, binary, bytestring, optparse-applicative, rap, tasty, tasty-hunit, wec

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for wec++## 0.1.0.0 -- 11-25-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,16 @@+# wec+wav echo effect++Usage++```+tone -t 0.33 300 | wec -t 1 -c 3 -p 50 | aplay+```++Development++```+cabal build+cabal haddock+cabal run wec -- --help+```
+ exe/Main.hs view
@@ -0,0 +1,88 @@+module Main (main) where++import Control.Monad+import Data.Binary.Get+import Data.Binary.Put+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Lazy as BS+import Data.Int+import Data.Rap+import Data.Version+import Data.Word+import Options.Applicative+import Paths_wec+import Sound.Echo++main :: IO ()+main = do+  (s, t, p, c, iM, oM) <- customExecParser prefs' $+    info (parser <**> simpleVersioner (showVersion version) <**> helper) $+    progDesc "wav echo effect"+  r' <- either fail return . wec t p c =<< maybe BS.getContents BS.readFile iM+  case oM of+    Nothing | s -> print r'+            | otherwise -> BS.putStr $ runPut $ putRap r'+    Just o  | s -> writeFile o $ show r'+            | otherwise -> BS.writeFile o $ runPut $ putRap r'++prefs' :: ParserPrefs+prefs' = prefs $ showHelpOnError <> showHelpOnEmpty++parser :: Parser (Bool, Double, Double, Int, Maybe FilePath, Maybe FilePath)+parser = (,,,,,) <$> switch (short 's' <> help "show")+  <*> option auto (short 't' <> metavar "DBL" <> help "delay seconds")+  <*> option auto (short 'p' <> metavar "DBL" <> help "decay percent")+  <*> option auto (short 'c' <> metavar "INT" <> help "count")+  <*> optional (strOption $ short 'i' <> metavar "FILE" <> completer (bashCompleter "file")+      <> help "input wav file otherwise STDIN")+  <*> optional (strOption $ short 'o' <> metavar "FILE" <> completer (bashCompleter "file")+      <> help "output wav file otherwise STDOUT")++wec :: Double -> Double -> Int -> ByteString -> Either String Rap+wec t p c i+  | t < 0 = Left $ "delay seconds must be non-negative: " ++ show t+  | p < 0 || p > 100 = Left $ "decay percent out of range: " ++ show p+  | c < 0 = Left $ "echo count must be non-negative: " ++ show c+  | otherwise = do+    r <- case runGetOrFail getRap i of+      Left  (_, _, e) -> Left $ "getRap: " ++ e+      Right (_, _, a) -> Right a+    rs <- one "wave" [rs | Stanza "RIFF" "WAVE" rs <- r]+    (fmt, sr) <- join $ one "fmt" [(,) fmt <$> readSampleRate bs | fmt@(Verse "fmt " bs) <- rs]+    let n = round $ fromIntegral sr * t+    samples <- one "data" [runGet (many getInt16le) bs | Verse "data" bs <- rs]+    let n' = length samples + n * c+    when (fromIntegral n' > (maxBound :: Word32)) $ Left $ "echo sample count too large: " ++ show n'+    let samples' = take n' $ echo n c (1 - p / 100) $ map norm samples+    return [Stanza "RIFF" "WAVE"+           [fmt, Verse "data" $ runPut $ foldMap (putInt16le . denorm) samples']]++readSampleRate :: ByteString -> Either String Word32+readSampleRate bs =+  case runGetOrFail (getWord16le *> getWord16le *> getWord32le) bs of+    Left  (_, _, er) -> Left $ "readSampleRate: " ++ er+    Right (_, _, sr) -> Right sr++one :: String -> [a] -> Either String a+one lbl vs = case vs of+  []  -> Left $ "no " ++ lbl ++ " found"+  [v] -> Right v+  _   -> Left $ "more than one " ++ lbl ++ " found"++denorm :: Double -> Int16+denorm d+  | d > 0 = fromIntegral $ min (maxb :: Integer) $ round (d * maxb)+  | d < 0 = fromIntegral $ max (minb :: Integer) $ negate $ round (d * minb)+  | otherwise = 0++maxb :: Num a => a+maxb = fromIntegral (maxBound :: Int16)++minb :: Num a => a+minb = fromIntegral (minBound :: Int16)++norm :: Int16 -> Double+norm i+  | i > 0 = min   1  $ fromIntegral i / maxb+  | i < 0 = max (-1) $ negate $ fromIntegral i / minb+  | otherwise = 0
+ lib/Sound/Echo.hs view
@@ -0,0 +1,19 @@+module Sound.Echo (echo) where++import Data.List++echo+  :: Int      -- ^ delay sample count+  -> Int      -- ^ echo count+  -> Double   -- ^ volume decay [0,1]+  -> [Double] -- ^ input sound [0,1]+  -> [Double]+echo n c v s = map (clip . sum) $ transpose $+  s : [ replicate (n * i) 0 ++ map (a *) s+      | (i, a) <- zip [1..c] $ iterate (* v) v]++clip :: Double -> Double+clip x+  | x > 0 = min   1  x+  | x < 0 = max (-1) x+  | otherwise = 0
+ test/Main.hs view
@@ -0,0 +1,18 @@+module Main (main) where++import Sound.Echo+import Test.Tasty+import Test.Tasty.HUnit++main :: IO ()+main = defaultMain $ testGroup "Sound.Echo"+  [ testCase "empty" $ echo 0 0 0 [] @?= []+  , testCase "id" $ echo 5 0 1 [1, 1, 1] @?= [1, 1, 1]+  , testCase "single" $ echo 2 1 1 [1] @?= [1, 0, 1]+  , testCase "multi" $ echo 3 2 1 [0.25, 1] @?= [0.25, 1, 0, 0.25, 1, 0, 0.25, 1]+  , testCase "infinite" $ take 10 (echo 2 1 0.5 [1..]) @?= replicate 10 1+  , testCase "amps" $ echo 2 2 0.5 [1] @?= [1, 0, 0.5, 0, 0.25]+  , testCase "sum" $ echo 1 1 1 [0.5, 0.5] @?= [0.5, 1, 0.5]+  , testCase "clip" $ echo 1 1 1 [0.75, 0.75] @?= [0.75, 1, 0.75]+  , testCase "overlap" $ echo 0 1 1 [0.5, 0.25] @?= [1, 0.5]+  ]
+ wec.cabal view
@@ -0,0 +1,52 @@+cabal-version:      3.0+name:               wec+version:            0.1.0.0+synopsis:           wav echo effect+homepage:           https://github.com/dopamane/wec+license:            MIT+license-file:       LICENSE+author:             dopamane+maintainer:         dwc1295@gmail.com+copyright:          David Cox+category:           Sound+build-type:         Simple+extra-doc-files:    CHANGELOG.md+                    README.md++source-repository head+  type: git+  location: https://github.com/dopamane/wec++library+  default-language: Haskell2010+  ghc-options:      -Wall -O2+  hs-source-dirs:   lib+  exposed-modules:  Sound.Echo+  build-depends:    base < 5++executable wec+  default-language: Haskell2010+  ghc-options:      -Wall -O2 -threaded+  hs-source-dirs:   exe+  main-is:          Main.hs+  other-modules:    Paths_wec+  autogen-modules:  Paths_wec+  build-depends:+    base < 5,+    binary,+    bytestring,+    optparse-applicative,+    rap,+    wec++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,+    tasty,+    tasty-hunit,+    wec