packages feed

tone (empty) → 0.1.0.0

raw patch · 7 files changed

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

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

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for tone++## 0.1.0.0 -- 11-23-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,24 @@+# Tone+tone wav synthesizer++Usage++```+tone --help+tone 440 | aplay+```++Install++```+cabal install tone+```++Development++```+cabal build+cabal haddock+cabal run test+cabal run tone -- --help+```
+ exe/Main.hs view
@@ -0,0 +1,66 @@+module Main (main) where++import Control.Monad+import Data.Binary.Put+import qualified Data.ByteString.Lazy as BS+import Data.Int+import Data.Rap+import Data.Version+import Data.Word+import Options.Applicative+import Paths_tone+import Sound.Tone++main :: IO ()+main = do+  (t, a, sr, p, f) <- customExecParser prefs' $+    info (parser <**> simpleVersioner (showVersion version) <**> helper)+    (progDesc "tone synthesizer")+  when (a < 0 || a > 100) $ fail $ "amp percent out of range: " ++ show a+  when (sr <= 0) $ fail $ "sample rate not positive: " ++ show sr+  when (p < 0 || p >= 2 * pi) $ fail $ "phase offset rads out of range: " ++ show p+  when (f < 0 || f >= fromIntegral sr / 2) $ fail $ "frequency out of range: " ++ show f+  let smpCnt = round (fromIntegral sr * t) :: Integer+  when (smpCnt > fromIntegral (maxBound :: Word32)) $+    fail $ "sample count out of range: " ++ show smpCnt+  let sound = tone (fromIntegral sr) (a / 100) (p / (2 * pi)) f+  BS.putStr $ runPut $ putRap $ wav sr $ take (fromIntegral smpCnt) sound++prefs' :: ParserPrefs+prefs' = prefs $ showHelpOnError <> showHelpOnEmpty++parser :: Parser (Double, Double, Word32, Double, Double)+parser = (,,,,)+  <$> option auto (short 't' <> long "time" <> metavar "DBL" <> value 1+      <> showDefault <> help "duration sec")+  <*> option auto (short 'a' <> long "amp" <> metavar "DBL" <> value 80+      <> showDefault <> help "amplitude percent")+  <*> option auto (short 's' <> long "samp" <> metavar "INT" <> value 44100+      <> showDefault <> help "sample rate")+  <*> option auto (short 'p' <> long "phase" <> metavar "DBL" <> value 0+      <> showDefault <> help "phase offset rads")+  <*> argument auto (metavar "DBL" <> help "frequency Hz")++wav :: Word32 -> Tone -> Rap+wav sr sound = [Stanza "RIFF" "WAVE"+  [ Verse "fmt " $ runPut $ do+    putWord16le 0x0001+    putWord16le 1+    putWord32le sr+    putWord32le $ sr * 2+    putWord16le 2+    putWord16le 16+    putWord16le 0+  , Verse "data" $ runPut $ foldMap (putInt16le . norm) sound]]++norm :: Double -> Int16+norm d+  | d > 0 = fromIntegral $ min (maxb :: Integer) $ round (d * maxb)+  | d < 0 = fromIntegral $ max (minb :: Integer) $ round (d * minb)+  | otherwise = 0++maxb :: Num a => a+maxb = fromIntegral (maxBound :: Int16)++minb :: Num a => a+minb = fromIntegral (minBound :: Int16)
+ lib/Sound/Tone.hs view
@@ -0,0 +1,18 @@+module Sound.Tone (Tone, tone) where++import Data.List++type Tone = [Double]++tone+  :: Double -- ^ sample rate+  -> Double -- ^ amplitude [0,1]+  -> Double -- ^ offset [0,1)+  -> Double -- ^ frequency+  -> Tone+tone sr a o f = [a * sin (2 * pi * phase) | phase <- iterate' acc o]+  where+    acc phi | phi' >= 1 = phi' - 1+            | otherwise = phi'+      where+        phi' = phi + f / sr
+ test/Main.hs view
@@ -0,0 +1,24 @@+module Main (main) where++import Sound.Tone+import Test.Tasty+import Test.Tasty.HUnit++main :: IO ()+main = defaultMain $ testGroup "Sound.Tone"+  [testCase "100 samples" $ do+    let sound = tone 48000 1 0 480+    sound !! 0 @?= 0+    sound !! 25 @?= 1+    rnd12p (sound !! 50) @?= 0+    sound !! 75 @?= (-1)+  , testCase "4 samples" $ do+    let sound = tone 48000 1 0 12000+    (map rnd12p . take 4) sound @?= [0, 1, 0, -1]+  , testCase "phase" $ do+    let sound = tone 48000 1 ((pi / 2) / (2 * pi)) 12000+    (map rnd12p . take 4) sound @?= [1, 0, -1, 0]+  ]++rnd12p :: Double -> Double+rnd12p x = fromIntegral (round (x * 1e12) :: Integer) / 1e12
+ tone.cabal view
@@ -0,0 +1,53 @@+cabal-version:      3.0+name:               tone+version:            0.1.0.0+synopsis:           tone synthesizer+homepage:           https://github.com/dopamane/tone+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/tone++library+  default-language: Haskell2010+  ghc-options:      -Wall -O2+  hs-source-dirs:   lib+  exposed-modules:  Sound.Tone+  build-depends:    base < 5++executable tone+  default-language: Haskell2010+  ghc-options:      -Wall -O2 -threaded+  hs-source-dirs:   exe+  main-is:          Main.hs+  other-modules:    Paths_tone+  autogen-modules:  Paths_tone+  build-depends:+    base < 5,+    binary,+    bytestring,+    optparse-applicative,+    rap,+    tone+++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,+    tone