alsa-pcm-tests (empty) → 0.1
raw patch · 8 files changed
+199/−0 lines, 8 filesdep +alsadep +basesetup-changed
Dependencies added: alsa, base
Files
- LICENSE +7/−0
- Makefile +19/−0
- Setup.lhs +8/−0
- alsa-pcm-tests.cabal +33/−0
- duplex.hs +44/−0
- play.hs +27/−0
- record.hs +27/−0
- volume_meter.hs +34/−0
+ LICENSE view
@@ -0,0 +1,7 @@+Copyright (c) 2006 Iavor S. Diatchki++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.
+ Makefile view
@@ -0,0 +1,19 @@+.PHONY: all clean++all: volume_meter record play duplex++volume_meter: volume_meter.hs+ ghc -threaded --make -package alsa -o $@ $<++record: record.hs+ ghc -threaded --make -package alsa -o $@ $<++play: play.hs+ ghc -threaded --make -package alsa -o $@ $<++duplex: duplex.hs+ ghc -threaded --make -package alsa -o $@ $<++clean:+ -rm -f *o *.hi+ -rm -f volume_meter record play duplex
+ Setup.lhs view
@@ -0,0 +1,8 @@+#!/usr/bin/env runghc++> module Main where++> import Distribution.Simple++> main :: IO ()+> main = defaultMain
+ alsa-pcm-tests.cabal view
@@ -0,0 +1,33 @@+Name: alsa-pcm-tests+License: BSD3+License-file: LICENSE+Version: 0.1+Author: Iavor S. Diatchki <iavor.diatchki@gmail.com>+Maintainer: Henning Thielemann <alsa@henning-thielemann.de>+Category: Sound, Music+Synopsis: Tests for the ALSA audio signal library.+Description: Tests for the ALSA audio signal library.+Build-type: Simple+Cabal-Version: >= 1.2+Extra-Source-Files:+ Makefile++Executable duplex+ Main-Is: duplex.hs+ GHC-Options: -Wall -threaded+ Build-Depends: base >=3 && <6, alsa >=0.4 && <0.5++Executable play+ Main-Is: play.hs+ GHC-Options: -Wall -threaded+ Build-Depends: base >=3 && <6, alsa >=0.4 && <0.5++Executable record+ Main-Is: record.hs+ GHC-Options: -Wall -threaded+ Build-Depends: base >=3 && <6, alsa >=0.4 && <0.5++Executable volume_meter+ Main-Is: volume_meter.hs+ GHC-Options: -Wall -threaded+ Build-Depends: base >=3 && <6, alsa >=0.4 && <0.5
+ duplex.hs view
@@ -0,0 +1,44 @@+import Sound.Alsa+ (SoundFmt(SoundFmt), copySound, sampleFreq,+ fileSoundSink, fileSoundSource,+ alsaSoundSink, alsaSoundSource, )++import Control.Concurrent (forkOS, threadDelay, )+import System.Environment (getArgs, )+import System.Exit (exitFailure, )+import System.IO (hPutStrLn, stderr, )+import Data.Int (Int16, )++bufSize :: Int+bufSize = 4096++soundFormat :: SoundFmt Int16+soundFormat = SoundFmt { sampleFreq = 8000 }++main :: IO ()+main = do args <- getArgs+ case args of + [infile,outfile] -> duplex infile outfile+ _ -> + do hPutStrLn stderr "Usage: duplex <input.pcm> <output.pcm>"+ exitFailure++duplex :: FilePath -> FilePath -> IO ()+duplex infile outfile = + do forkOS (play infile)+ forkOS (record outfile)+ threadDelay 5000000+ return ()+++play :: FilePath -> IO ()+play file =+ do let source = fileSoundSource file soundFormat+ sink = alsaSoundSink "plughw:0,0" soundFormat+ copySound source sink bufSize++record :: FilePath -> IO ()+record file =+ do let source = alsaSoundSource "plughw:0,0" soundFormat+ sink = fileSoundSink file soundFormat+ copySound source sink bufSize
+ play.hs view
@@ -0,0 +1,27 @@+import Sound.Alsa+ (SoundFmt(SoundFmt), copySound, sampleFreq,+ fileSoundSource, alsaSoundSink, )++import System.Environment (getArgs, )+import System.Exit (exitFailure, )+import System.IO (hPutStrLn, stderr, )+import Data.Int (Int16, )++bufSize :: Int+bufSize = 8192++soundFormat :: SoundFmt Int16+soundFormat = SoundFmt { sampleFreq = 8000 }++main :: IO ()+main = do args <- getArgs+ case args of + [file] -> play file+ _ -> do hPutStrLn stderr "Usage: play <file.pcm>"+ exitFailure++play :: FilePath -> IO ()+play file =+ do let source = fileSoundSource file soundFormat+ sink = alsaSoundSink "plughw:0,0" soundFormat+ copySound source sink bufSize
+ record.hs view
@@ -0,0 +1,27 @@+import Sound.Alsa+ (SoundFmt(SoundFmt), copySound, sampleFreq,+ fileSoundSink, alsaSoundSource, )++import System.Environment (getArgs, )+import System.Exit (exitFailure, )+import System.IO (hPutStrLn, stderr, )+import Data.Int (Int16, )++bufSize :: Int+bufSize = 8192++soundFormat :: SoundFmt Int16+soundFormat = SoundFmt { sampleFreq = 8000 }++main :: IO ()+main = do args <- getArgs+ case args of + [file] -> record file+ _ -> do hPutStrLn stderr "Usage: record <file.pcm>"+ exitFailure++record :: FilePath -> IO ()+record file =+ do let source = alsaSoundSource "plughw:0,0" soundFormat+ sink = fileSoundSink file soundFormat+ copySound source sink bufSize
+ volume_meter.hs view
@@ -0,0 +1,34 @@+import Sound.Alsa+ (SoundFmt(SoundFmt), sampleFreq, soundSourceRead,+ SoundSource, alsaSoundSource, withSoundSource, )++import Foreign+ (allocaArray, peekArray,+ Storable, Ptr, castPtr, )+import Data.Int (Int16, )++bufSize :: Int+bufSize = 1000++inputFormat :: SoundFmt Int16+inputFormat = SoundFmt { sampleFreq = 8000 }+++main :: IO ()+main = let source = alsaSoundSource "plughw:0,0" inputFormat+ in allocaArray bufSize $ \buf -> + withSoundSource source $ \from ->+ loop source from bufSize buf++-- FIXME: assumes little-endian machine+loop :: SoundSource Int16 h -> h -> Int -> Ptr Int16 -> IO ()+loop src h n buf =+ do n' <- soundSourceRead src h (castPtr buf) n+ avg <- avgBuf buf n'+ putStrLn (replicate (avg `div` 20) '*')+ loop src h n buf++avgBuf :: (Storable a, Integral a) => Ptr a -> Int -> IO Int+avgBuf buf n = do xs <- peekArray n buf+ let xs' = map (fromIntegral . abs) xs :: [Int]+ return $ sum xs' `div` fromIntegral n