{-# LANGUAGE ForeignFunctionInterface #-}
module Sound.ALSA.PCM
    (Class.SampleFmt(..),
     SampleFreq,
     Time,
     Size,
     SoundFmt(..),
     SoundSource(..),
     SoundSink(..),
     SoundBufferTime(..),
     Pcm, File,
     withSoundSource,
     withSoundSourceRunning,
     withSoundSink,
     withSoundSinkRunning,
     soundFmtMIME,
     audioBytesPerSample,
     audioBytesPerFrame,
     soundSourceBytesPerFrame,
     soundSinkBytesPerFrame,
     copySound,
     alsaSoundSource,
     alsaSoundSink,
     alsaSoundSourceTime,
     alsaSoundSinkTime,
     alsaSoundSourceParams,
     alsaSoundSinkParams,
     fileSoundSource,
     fileSoundSink,
    ) where

import qualified Sound.ALSA.PCM.Node.ALSA as PCM
import qualified Sound.ALSA.PCM.Node.File as File

import Sound.ALSA.PCM.Parameters.Hardware (Time, SampleFreq, Size, )
import Sound.ALSA.PCM.Core.Handle (arraySize, )
import qualified Sound.ALSA.PCM.Parameters.Software as SwParam
import qualified Sound.ALSA.PCM.Parameters.Hardware as HwParam
import qualified Sound.ALSA.PCM.Core.Class as Class
import qualified Sound.ALSA.Exception as AlsaExc
import qualified Sound.ALSA.PCM.Debug as Debug

import qualified Sound.Frame as Frame

import Control.Exception (bracket, bracket_, )
import Control.Monad (when, liftM2, liftM4, )

import qualified Foreign.C.Types as C
import Foreign.Marshal.Array (allocaArray, )
import Foreign.Ptr (Ptr, )
import Foreign.Storable (Storable, )
import qualified System.IO as IO

--
-- * Generic sound API
--

data SoundFmt y = SoundFmt {
        forall y. SoundFmt y -> SampleFreq
sampleFreq :: SampleFreq
        }
  deriving (SampleFreq -> SoundFmt y -> ShowS
[SoundFmt y] -> ShowS
SoundFmt y -> String
(SampleFreq -> SoundFmt y -> ShowS)
-> (SoundFmt y -> String)
-> ([SoundFmt y] -> ShowS)
-> Show (SoundFmt y)
forall y. SampleFreq -> SoundFmt y -> ShowS
forall y. [SoundFmt y] -> ShowS
forall y. SoundFmt y -> String
forall a.
(SampleFreq -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall y. SampleFreq -> SoundFmt y -> ShowS
showsPrec :: SampleFreq -> SoundFmt y -> ShowS
$cshow :: forall y. SoundFmt y -> String
show :: SoundFmt y -> String
$cshowList :: forall y. [SoundFmt y] -> ShowS
showList :: [SoundFmt y] -> ShowS
Show)

data SoundBufferTime = SoundBufferTime {
        SoundBufferTime -> SampleFreq
bufferTime, SoundBufferTime -> SampleFreq
periodTime :: Time
        }
  deriving (SampleFreq -> SoundBufferTime -> ShowS
[SoundBufferTime] -> ShowS
SoundBufferTime -> String
(SampleFreq -> SoundBufferTime -> ShowS)
-> (SoundBufferTime -> String)
-> ([SoundBufferTime] -> ShowS)
-> Show SoundBufferTime
forall a.
(SampleFreq -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: SampleFreq -> SoundBufferTime -> ShowS
showsPrec :: SampleFreq -> SoundBufferTime -> ShowS
$cshow :: SoundBufferTime -> String
show :: SoundBufferTime -> String
$cshowList :: [SoundBufferTime] -> ShowS
showList :: [SoundBufferTime] -> ShowS
Show)


type Pcm = PCM.Handle PCM.Interleaved

type File = File.Handle


-- | Counts are in samples, not bytes. Multi-channel data is interleaved.
data SoundSource handle y =
   SoundSource {
      forall (handle :: * -> *) y. SoundSource handle y -> IO (handle y)
soundSourceOpen  :: IO (handle y),
      forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceClose :: handle y -> IO (),
      forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceStart :: handle y -> IO (),
      forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceStop  :: handle y -> IO (),
      forall (handle :: * -> *) y.
SoundSource handle y
-> handle y -> Ptr y -> SampleFreq -> IO SampleFreq
soundSourceRead  :: handle y -> Ptr y -> Size -> IO Size
   }

data SoundSink handle y =
   SoundSink {
      forall (handle :: * -> *) y. SoundSink handle y -> IO (handle y)
soundSinkOpen  :: IO (handle y),
      forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkClose :: handle y -> IO (),
      forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> Ptr y -> SampleFreq -> IO ()
soundSinkWrite :: handle y -> Ptr y -> Size -> IO (),
      forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkStart :: handle y -> IO (),
      forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkStop  :: handle y -> IO ()
   }


withSoundSource :: SoundSource handle y -> (handle y -> IO a) -> IO a
withSoundSource :: forall (handle :: * -> *) y a.
SoundSource handle y -> (handle y -> IO a) -> IO a
withSoundSource SoundSource handle y
source =
    IO (handle y) -> (handle y -> IO ()) -> (handle y -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (SoundSource handle y -> IO (handle y)
forall (handle :: * -> *) y. SoundSource handle y -> IO (handle y)
soundSourceOpen SoundSource handle y
source) (SoundSource handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceClose SoundSource handle y
source)

withSoundSourceRunning :: SoundSource handle y -> handle y -> IO a -> IO a
withSoundSourceRunning :: forall (handle :: * -> *) y a.
SoundSource handle y -> handle y -> IO a -> IO a
withSoundSourceRunning SoundSource handle y
src handle y
h =
    IO () -> IO () -> IO a -> IO a
forall a b c. IO a -> IO b -> IO c -> IO c
bracket_ (SoundSource handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceStart SoundSource handle y
src handle y
h) (SoundSource handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSource handle y -> handle y -> IO ()
soundSourceStop SoundSource handle y
src handle y
h)

withSoundSink :: SoundSink handle y -> (handle y -> IO a) -> IO a
withSoundSink :: forall (handle :: * -> *) y a.
SoundSink handle y -> (handle y -> IO a) -> IO a
withSoundSink SoundSink handle y
sink =
    IO (handle y) -> (handle y -> IO ()) -> (handle y -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (SoundSink handle y -> IO (handle y)
forall (handle :: * -> *) y. SoundSink handle y -> IO (handle y)
soundSinkOpen SoundSink handle y
sink) (SoundSink handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkClose SoundSink handle y
sink)

withSoundSinkRunning :: SoundSink handle y -> handle y -> IO a -> IO a
withSoundSinkRunning :: forall (handle :: * -> *) y a.
SoundSink handle y -> handle y -> IO a -> IO a
withSoundSinkRunning SoundSink handle y
src handle y
h =
    IO () -> IO () -> IO a -> IO a
forall a b c. IO a -> IO b -> IO c -> IO c
bracket_ (SoundSink handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkStart SoundSink handle y
src handle y
h) (SoundSink handle y -> handle y -> IO ()
forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> IO ()
soundSinkStop SoundSink handle y
src handle y
h)


withSampleFmt :: (y -> a) -> (SoundFmt y -> a)
withSampleFmt :: forall y a. (y -> a) -> SoundFmt y -> a
withSampleFmt y -> a
f SoundFmt y
_ = y -> a
f y
forall a. HasCallStack => a
undefined

withNodeSample :: (y -> a) -> (node y -> a)
withNodeSample :: forall y a (node :: * -> *). (y -> a) -> node y -> a
withNodeSample y -> a
f node y
_ = y -> a
f y
forall a. HasCallStack => a
undefined


soundFmtMIME :: Class.SampleFmt y => SoundFmt y -> String
soundFmtMIME :: forall y. SampleFmt y => SoundFmt y -> String
soundFmtMIME SoundFmt y
fmt = String
t String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
r String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
c
  where t :: String
t = String
"audio/basic"
{-
        t = case sampleFmt fmt of
                SampleFmtLinear16BitSignedLE -> "audio/L16"
                SampleFmtMuLaw8Bit           -> "audio/basic"
-}
        r :: String
r = String
";rate=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ SampleFreq -> String
forall a. Show a => a -> String
show (SoundFmt y -> SampleFreq
forall y. SoundFmt y -> SampleFreq
sampleFreq SoundFmt y
fmt)
        c :: String
c =
           if SoundFmt y -> SampleFreq
forall y. SampleFmt y => SoundFmt y -> SampleFreq
numChannels SoundFmt y
fmt SampleFreq -> SampleFreq -> Bool
forall a. Eq a => a -> a -> Bool
== SampleFreq
1
             then String
""
             else String
";channels=" String -> ShowS
forall a. [a] -> [a] -> [a]
++ SampleFreq -> String
forall a. Show a => a -> String
show (SoundFmt y -> SampleFreq
forall y. SampleFmt y => SoundFmt y -> SampleFreq
numChannels SoundFmt y
fmt)

numChannels :: Class.SampleFmt y => SoundFmt y -> Int
numChannels :: forall y. SampleFmt y => SoundFmt y -> SampleFreq
numChannels = (y -> SampleFreq) -> SoundFmt y -> SampleFreq
forall y a. (y -> a) -> SoundFmt y -> a
withSampleFmt y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.numberOfChannels

audioBytesPerSample :: Class.SampleFmt y => SoundFmt y -> Int
audioBytesPerSample :: forall y. SampleFmt y => SoundFmt y -> SampleFreq
audioBytesPerSample = (y -> SampleFreq) -> SoundFmt y -> SampleFreq
forall y a. (y -> a) -> SoundFmt y -> a
withSampleFmt y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.sizeOfElement

{-
assumes interleaved data

Due to alignment constraints
a frame might occupy more than the calculated size
in an array in memory.
-}
audioBytesPerFrame :: Class.SampleFmt y => SoundFmt y -> Int
audioBytesPerFrame :: forall y. SampleFmt y => SoundFmt y -> SampleFreq
audioBytesPerFrame SoundFmt y
fmt = SoundFmt y -> SampleFreq
forall y. SampleFmt y => SoundFmt y -> SampleFreq
numChannels SoundFmt y
fmt SampleFreq -> SampleFreq -> SampleFreq
forall a. Num a => a -> a -> a
* SoundFmt y -> SampleFreq
forall y. SampleFmt y => SoundFmt y -> SampleFreq
audioBytesPerSample SoundFmt y
fmt

soundSourceBytesPerFrame :: Class.SampleFmt y => SoundSource handle y -> Int
soundSourceBytesPerFrame :: forall y (handle :: * -> *).
SampleFmt y =>
SoundSource handle y -> SampleFreq
soundSourceBytesPerFrame =
   (y -> SampleFreq) -> SoundSource handle y -> SampleFreq
forall y a (node :: * -> *). (y -> a) -> node y -> a
withNodeSample ((y -> SampleFreq) -> SoundSource handle y -> SampleFreq)
-> (y -> SampleFreq) -> SoundSource handle y -> SampleFreq
forall a b. (a -> b) -> a -> b
$ \y
y -> y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.numberOfChannels y
y SampleFreq -> SampleFreq -> SampleFreq
forall a. Num a => a -> a -> a
* y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.sizeOfElement y
y

soundSinkBytesPerFrame :: Class.SampleFmt y => SoundSink handle y -> Int
soundSinkBytesPerFrame :: forall y (handle :: * -> *).
SampleFmt y =>
SoundSink handle y -> SampleFreq
soundSinkBytesPerFrame =
   (y -> SampleFreq) -> SoundSink handle y -> SampleFreq
forall y a (node :: * -> *). (y -> a) -> node y -> a
withNodeSample ((y -> SampleFreq) -> SoundSink handle y -> SampleFreq)
-> (y -> SampleFreq) -> SoundSink handle y -> SampleFreq
forall a b. (a -> b) -> a -> b
$ \y
y -> y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.numberOfChannels y
y SampleFreq -> SampleFreq -> SampleFreq
forall a. Num a => a -> a -> a
* y -> SampleFreq
forall y. C y => y -> SampleFreq
Frame.sizeOfElement y
y

copySound ::
   Class.SampleFmt y =>
      SoundSource handleIn y
   -> SoundSink handleOut y
   -> Size -- ^ Buffer size (in sample frames) to use
   -> IO ()
copySound :: forall y (handleIn :: * -> *) (handleOut :: * -> *).
SampleFmt y =>
SoundSource handleIn y
-> SoundSink handleOut y -> SampleFreq -> IO ()
copySound SoundSource handleIn y
source SoundSink handleOut y
sink SampleFreq
bufSize =
    SampleFreq -> (Ptr y -> IO ()) -> IO ()
forall a b. Storable a => SampleFreq -> (Ptr a -> IO b) -> IO b
allocaArray     (SampleFreq -> SampleFreq
forall a b. (Integral a, Num b) => a -> b
fromIntegral SampleFreq
bufSize) ((Ptr y -> IO ()) -> IO ()) -> (Ptr y -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr y
buf ->
    SoundSource handleIn y -> (handleIn y -> IO ()) -> IO ()
forall (handle :: * -> *) y a.
SoundSource handle y -> (handle y -> IO a) -> IO a
withSoundSource SoundSource handleIn y
source  ((handleIn y -> IO ()) -> IO ()) -> (handleIn y -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \handleIn y
from ->
    SoundSink handleOut y -> (handleOut y -> IO ()) -> IO ()
forall (handle :: * -> *) y a.
SoundSink handle y -> (handle y -> IO a) -> IO a
withSoundSink   SoundSink handleOut y
sink    ((handleOut y -> IO ()) -> IO ())
-> (handleOut y -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \handleOut y
to ->
       let loop :: IO ()
loop = do SampleFreq
n <- SoundSource handleIn y
-> handleIn y -> Ptr y -> SampleFreq -> IO SampleFreq
forall (handle :: * -> *) y.
SoundSource handle y
-> handle y -> Ptr y -> SampleFreq -> IO SampleFreq
soundSourceRead SoundSource handleIn y
source handleIn y
from Ptr y
buf SampleFreq
bufSize
                     Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SampleFreq
n SampleFreq -> SampleFreq -> Bool
forall a. Ord a => a -> a -> Bool
> SampleFreq
0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do SoundSink handleOut y
-> handleOut y -> Ptr y -> SampleFreq -> IO ()
forall (handle :: * -> *) y.
SoundSink handle y -> handle y -> Ptr y -> SampleFreq -> IO ()
soundSinkWrite SoundSink handleOut y
sink handleOut y
to Ptr y
buf SampleFreq
n
                                       IO ()
loop
        in IO ()
loop

--
-- * Alsa stuff
--


{- |
The buffer is initialized with an empty block
which means that the zero bit pattern
should equal the number zero in the Class.SampleFmt type.
-}
alsaOpen :: Class.SampleFmt y =>
   PCM.Stream ->
   HwParam.T PCM.Interleaved y a ->
   (a -> SwParam.T PCM.Interleaved y ()) ->
   String {- ^ device, e.g @"default"@ -} ->
   IO (Pcm y)
alsaOpen :: forall y a.
SampleFmt y =>
Stream
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> String
-> IO (Pcm y)
alsaOpen Stream
stream T Interleaved y a
hwParams a -> T Interleaved y ()
swParams String
dev = IO (Pcm y) -> IO (Pcm y)
forall a. IO a -> IO a
AlsaExc.rethrow (IO (Pcm y) -> IO (Pcm y)) -> IO (Pcm y) -> IO (Pcm y)
forall a b. (a -> b) -> a -> b
$ do
   String -> IO ()
Debug.put String
"alsaOpen"
   {-
   Debug.put $ "requested bufferTime = " ++ show (bufferTime time)
   Debug.put $ "requested periodTime = " ++ show (periodTime time)
   -}
   ((SampleFreq
bufferTime_,(SampleFreq, Ordering)
bufferSize_,SampleFreq
periodTime_,SampleFreq
periodSize_), Pcm y
h) <-
      Mode
-> Stream
-> T Interleaved
     y
     (a, (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
-> ((a,
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
    -> T Interleaved
         y
         (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
-> String
-> IO
     ((SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq),
      Pcm y)
forall i y a b.
(Access i, SampleFmt y) =>
Mode
-> Stream
-> T i y a
-> (a -> T i y b)
-> String
-> IO (b, Handle i y)
PCM.open ([Mode] -> Mode
PCM.modes []) Stream
stream
         ((a
 -> (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
 -> (a,
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)))
-> T Interleaved y a
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
-> T Interleaved
     y
     (a, (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 (,) T Interleaved y a
hwParams (T Interleaved
   y
   (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
 -> T Interleaved
      y
      (a, (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)))
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
-> T Interleaved
     y
     (a, (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
forall a b. (a -> b) -> a -> b
$
          (SampleFreq
 -> (SampleFreq, Ordering)
 -> SampleFreq
 -> SampleFreq
 -> (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq))
-> T Interleaved y SampleFreq
-> T Interleaved y (SampleFreq, Ordering)
-> T Interleaved y SampleFreq
-> T Interleaved y SampleFreq
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
forall (m :: * -> *) a1 a2 a3 a4 r.
Monad m =>
(a1 -> a2 -> a3 -> a4 -> r) -> m a1 -> m a2 -> m a3 -> m a4 -> m r
liftM4 (,,,)
             T Interleaved y SampleFreq
forall i y. T i y SampleFreq
HwParam.getBufferSize
             T Interleaved y (SampleFreq, Ordering)
forall i y. T i y (SampleFreq, Ordering)
HwParam.getBufferTime
             (((SampleFreq, Ordering) -> SampleFreq)
-> T Interleaved y (SampleFreq, Ordering)
-> T Interleaved y SampleFreq
forall a b. (a -> b) -> T Interleaved y a -> T Interleaved y b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SampleFreq, Ordering) -> SampleFreq
forall a b. (a, b) -> a
fst T Interleaved y (SampleFreq, Ordering)
forall i y. T i y (SampleFreq, Ordering)
HwParam.getPeriodSize)
             (((SampleFreq, Ordering) -> SampleFreq)
-> T Interleaved y (SampleFreq, Ordering)
-> T Interleaved y SampleFreq
forall a b. (a -> b) -> T Interleaved y a -> T Interleaved y b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (SampleFreq, Ordering) -> SampleFreq
forall a b. (a, b) -> a
fst T Interleaved y (SampleFreq, Ordering)
forall i y. T i y (SampleFreq, Ordering)
HwParam.getPeriodTime))
         (\(a
a, (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
params) -> a -> T Interleaved y ()
swParams a
a T Interleaved y ()
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
forall a b.
T Interleaved y a -> T Interleaved y b -> T Interleaved y b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
-> T Interleaved
     y
     (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
forall a. a -> T Interleaved y a
forall (m :: * -> *) a. Monad m => a -> m a
return (SampleFreq, (SampleFreq, Ordering), SampleFreq, SampleFreq)
params)
         String
dev
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.prepare Pcm y
h
   String -> IO ()
Debug.put (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"bufferTime = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SampleFreq -> String
forall a. Show a => a -> String
show SampleFreq
bufferTime_
   String -> IO ()
Debug.put (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"bufferSize = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ (SampleFreq, Ordering) -> String
forall a. Show a => a -> String
show (SampleFreq, Ordering)
bufferSize_
   String -> IO ()
Debug.put (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"periodTime = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SampleFreq -> String
forall a. Show a => a -> String
show SampleFreq
periodTime_
   String -> IO ()
Debug.put (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ String
"periodSize = " String -> ShowS
forall a. [a] -> [a] -> [a]
++ SampleFreq -> String
forall a. Show a => a -> String
show SampleFreq
periodSize_
   Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Stream
stream Stream -> Stream -> Bool
forall a. Eq a => a -> a -> Bool
== Stream
PCM.StreamPlayback) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$
      SampleFreq -> (Ptr y -> IO ()) -> IO ()
forall a b. Storable a => SampleFreq -> (Ptr a -> IO b) -> IO b
callocaArray SampleFreq
periodSize_ ((Ptr y -> IO ()) -> IO ()) -> (Ptr y -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr y
buf ->
         Pcm y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
PCM.writei Pcm y
h Ptr y
buf (SampleFreq -> SampleFreq
forall a b. (Integral a, Num b) => a -> b
fromIntegral SampleFreq
periodSize_) IO SampleFreq -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
   Pcm y -> IO (Pcm y)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Pcm y
h

alsaClose :: Pcm y -> IO ()
alsaClose :: forall y. Pcm y -> IO ()
alsaClose Pcm y
h = IO () -> IO ()
forall a. IO a -> IO a
AlsaExc.rethrow (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
   String -> IO ()
Debug.put String
"alsaClose"
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.drain Pcm y
h
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.close Pcm y
h


alsaStart :: Pcm y -> IO ()
alsaStart :: forall y. Pcm y -> IO ()
alsaStart Pcm y
h = IO () -> IO ()
forall a. IO a -> IO a
AlsaExc.rethrow (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
   String -> IO ()
Debug.put String
"alsaStart"
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.prepare Pcm y
h
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.start Pcm y
h

-- FIXME: use PCM.drain for sinks?
alsaStop :: Pcm y -> IO ()
alsaStop :: forall y. Pcm y -> IO ()
alsaStop Pcm y
h = IO () -> IO ()
forall a. IO a -> IO a
AlsaExc.rethrow (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
   String -> IO ()
Debug.put String
"alsaStop"
   Pcm y -> IO ()
forall i y. Handle i y -> IO ()
PCM.drain Pcm y
h


alsaRead ::
   Class.SampleFmt y =>
   Pcm y -> Ptr y -> Size -> IO Size
alsaRead :: forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
alsaRead Pcm y
h Ptr y
buf0 SampleFreq
n =
   IO SampleFreq -> IO SampleFreq
forall a. IO a -> IO a
AlsaExc.rethrow (IO SampleFreq -> IO SampleFreq) -> IO SampleFreq -> IO SampleFreq
forall a b. (a -> b) -> a -> b
$ Pcm y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
PCM.readiRetry Pcm y
h Ptr y
buf0 SampleFreq
n

alsaWrite ::
   Class.SampleFmt y =>
   Pcm y -> Ptr y -> Size -> IO Size
alsaWrite :: forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
alsaWrite Pcm y
h Ptr y
buf0 SampleFreq
n =
   IO SampleFreq -> IO SampleFreq
forall a. IO a -> IO a
AlsaExc.rethrow (IO SampleFreq -> IO SampleFreq) -> IO SampleFreq -> IO SampleFreq
forall a b. (a -> b) -> a -> b
$ Pcm y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
PCM.writeiRetry Pcm y
h Ptr y
buf0 SampleFreq
n


defaultBufferTime :: SoundBufferTime
defaultBufferTime :: SoundBufferTime
defaultBufferTime =
   SoundBufferTime {
      bufferTime :: SampleFreq
bufferTime = SampleFreq
500000, -- 0.5s
      periodTime :: SampleFreq
periodTime = SampleFreq
100000  -- 0.1s
   }

bufferTimeParams ::
   SoundFmt y ->
   SoundBufferTime ->
   (HwParam.T PCM.Interleaved y (Size,Size),
    (Size,Size) -> SwParam.T PCM.Interleaved y ())
bufferTimeParams :: forall y.
SoundFmt y
-> SoundBufferTime
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
bufferTimeParams SoundFmt y
fmt SoundBufferTime
time =
   (SampleFreq
-> SampleFreq
-> SampleFreq
-> T Interleaved y (SampleFreq, SampleFreq)
forall y.
SampleFreq
-> SampleFreq
-> SampleFreq
-> T Interleaved y (SampleFreq, SampleFreq)
HwParam.setRateBufferTime
       (SoundFmt y -> SampleFreq
forall y. SoundFmt y -> SampleFreq
sampleFreq SoundFmt y
fmt)
       (SoundBufferTime -> SampleFreq
bufferTime SoundBufferTime
time)
       (SoundBufferTime -> SampleFreq
periodTime SoundBufferTime
time),
    (SampleFreq -> SampleFreq -> T Interleaved y ())
-> (SampleFreq, SampleFreq) -> T Interleaved y ()
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry SampleFreq -> SampleFreq -> T Interleaved y ()
forall i y. SampleFreq -> SampleFreq -> T i y ()
SwParam.setBufferSize)

alsaSoundSource ::
   Class.SampleFmt y =>
   String -> SoundFmt y -> SoundSource Pcm y
alsaSoundSource :: forall y. SampleFmt y => String -> SoundFmt y -> SoundSource Pcm y
alsaSoundSource String
dev SoundFmt y
fmt =
   String -> SoundFmt y -> SoundBufferTime -> SoundSource Pcm y
forall y.
SampleFmt y =>
String -> SoundFmt y -> SoundBufferTime -> SoundSource Pcm y
alsaSoundSourceTime String
dev SoundFmt y
fmt SoundBufferTime
defaultBufferTime

alsaSoundSink ::
   Class.SampleFmt y =>
   String -> SoundFmt y -> SoundSink Pcm y
alsaSoundSink :: forall y. SampleFmt y => String -> SoundFmt y -> SoundSink Pcm y
alsaSoundSink String
dev SoundFmt y
fmt =
   String -> SoundFmt y -> SoundBufferTime -> SoundSink Pcm y
forall y.
SampleFmt y =>
String -> SoundFmt y -> SoundBufferTime -> SoundSink Pcm y
alsaSoundSinkTime String
dev SoundFmt y
fmt SoundBufferTime
defaultBufferTime

alsaSoundSourceTime ::
   Class.SampleFmt y =>
   String -> SoundFmt y -> SoundBufferTime -> SoundSource Pcm y
alsaSoundSourceTime :: forall y.
SampleFmt y =>
String -> SoundFmt y -> SoundBufferTime -> SoundSource Pcm y
alsaSoundSourceTime String
dev SoundFmt y
fmt SoundBufferTime
time =
   (T Interleaved y (SampleFreq, SampleFreq)
 -> ((SampleFreq, SampleFreq) -> T Interleaved y ())
 -> SoundSource Pcm y)
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSource Pcm y
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (String
-> T Interleaved y (SampleFreq, SampleFreq)
-> ((SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSource Pcm y
forall y a.
SampleFmt y =>
String
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> SoundSource Pcm y
alsaSoundSourceParams String
dev) ((T Interleaved y (SampleFreq, SampleFreq),
  (SampleFreq, SampleFreq) -> T Interleaved y ())
 -> SoundSource Pcm y)
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSource Pcm y
forall a b. (a -> b) -> a -> b
$
   SoundFmt y
-> SoundBufferTime
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
forall y.
SoundFmt y
-> SoundBufferTime
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
bufferTimeParams SoundFmt y
fmt SoundBufferTime
time

alsaSoundSinkTime ::
   Class.SampleFmt y =>
   String -> SoundFmt y -> SoundBufferTime -> SoundSink Pcm y
alsaSoundSinkTime :: forall y.
SampleFmt y =>
String -> SoundFmt y -> SoundBufferTime -> SoundSink Pcm y
alsaSoundSinkTime String
dev SoundFmt y
fmt SoundBufferTime
time =
   (T Interleaved y (SampleFreq, SampleFreq)
 -> ((SampleFreq, SampleFreq) -> T Interleaved y ())
 -> SoundSink Pcm y)
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSink Pcm y
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (String
-> T Interleaved y (SampleFreq, SampleFreq)
-> ((SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSink Pcm y
forall y a.
SampleFmt y =>
String
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> SoundSink Pcm y
alsaSoundSinkParams String
dev) ((T Interleaved y (SampleFreq, SampleFreq),
  (SampleFreq, SampleFreq) -> T Interleaved y ())
 -> SoundSink Pcm y)
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
-> SoundSink Pcm y
forall a b. (a -> b) -> a -> b
$
   SoundFmt y
-> SoundBufferTime
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
forall y.
SoundFmt y
-> SoundBufferTime
-> (T Interleaved y (SampleFreq, SampleFreq),
    (SampleFreq, SampleFreq) -> T Interleaved y ())
bufferTimeParams SoundFmt y
fmt SoundBufferTime
time

alsaSoundSourceParams ::
   Class.SampleFmt y =>
   String ->
   HwParam.T PCM.Interleaved y a ->
   (a -> SwParam.T PCM.Interleaved y ()) ->
   SoundSource Pcm y
alsaSoundSourceParams :: forall y a.
SampleFmt y =>
String
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> SoundSource Pcm y
alsaSoundSourceParams String
dev T Interleaved y a
hwParams a -> T Interleaved y ()
swParams =
   SoundSource {
      soundSourceOpen :: IO (Handle Interleaved y)
soundSourceOpen  = Stream
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> String
-> IO (Handle Interleaved y)
forall y a.
SampleFmt y =>
Stream
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> String
-> IO (Pcm y)
alsaOpen Stream
PCM.StreamCapture T Interleaved y a
hwParams a -> T Interleaved y ()
swParams String
dev,
      soundSourceClose :: Handle Interleaved y -> IO ()
soundSourceClose = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaClose,
      soundSourceStart :: Handle Interleaved y -> IO ()
soundSourceStart = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaStart,
      soundSourceStop :: Handle Interleaved y -> IO ()
soundSourceStop  = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaStop,
      soundSourceRead :: Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
soundSourceRead  = Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
alsaRead
   }

alsaSoundSinkParams ::
   Class.SampleFmt y =>
   String ->
   HwParam.T PCM.Interleaved y a ->
   (a -> SwParam.T PCM.Interleaved y ()) ->
   SoundSink Pcm y
alsaSoundSinkParams :: forall y a.
SampleFmt y =>
String
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> SoundSink Pcm y
alsaSoundSinkParams String
dev T Interleaved y a
hwParams a -> T Interleaved y ()
swParams =
   SoundSink {
      soundSinkOpen :: IO (Handle Interleaved y)
soundSinkOpen  = Stream
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> String
-> IO (Handle Interleaved y)
forall y a.
SampleFmt y =>
Stream
-> T Interleaved y a
-> (a -> T Interleaved y ())
-> String
-> IO (Pcm y)
alsaOpen Stream
PCM.StreamPlayback T Interleaved y a
hwParams a -> T Interleaved y ()
swParams String
dev,
      soundSinkClose :: Handle Interleaved y -> IO ()
soundSinkClose = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaClose,
      soundSinkStart :: Handle Interleaved y -> IO ()
soundSinkStart = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaStart,
      soundSinkStop :: Handle Interleaved y -> IO ()
soundSinkStop  = Handle Interleaved y -> IO ()
forall y. Pcm y -> IO ()
alsaStop,
      soundSinkWrite :: Handle Interleaved y -> Ptr y -> SampleFreq -> IO ()
soundSinkWrite = \Handle Interleaved y
h Ptr y
buf SampleFreq
n -> Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle Interleaved y -> Ptr y -> SampleFreq -> IO SampleFreq
alsaWrite Handle Interleaved y
h Ptr y
buf SampleFreq
n IO SampleFreq -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
   }

--
-- * File stuff
--

fileSoundSource ::
   Class.SampleFmt y =>
   FilePath -> SoundSource File y
fileSoundSource :: forall y. SampleFmt y => String -> SoundSource File y
fileSoundSource String
file =
   SoundSource {
      soundSourceOpen :: IO (Handle y)
soundSourceOpen  = IOMode -> String -> IO (Handle y)
forall y. SampleFmt y => IOMode -> String -> IO (Handle y)
File.open IOMode
IO.ReadMode String
file,
      soundSourceClose :: Handle y -> IO ()
soundSourceClose = Handle y -> IO ()
forall y. SampleFmt y => Handle y -> IO ()
File.close,
      soundSourceRead :: Handle y -> Ptr y -> SampleFreq -> IO SampleFreq
soundSourceRead  = Handle y -> Ptr y -> SampleFreq -> IO SampleFreq
forall y.
SampleFmt y =>
Handle y -> Ptr y -> SampleFreq -> IO SampleFreq
File.read,
      soundSourceStart :: Handle y -> IO ()
soundSourceStart = IO () -> Handle y -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle y -> IO ()) -> IO () -> Handle y -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (),
      soundSourceStop :: Handle y -> IO ()
soundSourceStop  = IO () -> Handle y -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle y -> IO ()) -> IO () -> Handle y -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
   }

fileSoundSink ::
   Class.SampleFmt y =>
   FilePath -> SoundSink File y
fileSoundSink :: forall y. SampleFmt y => String -> SoundSink File y
fileSoundSink String
file =
   SoundSink {
      soundSinkOpen :: IO (Handle y)
soundSinkOpen  = IOMode -> String -> IO (Handle y)
forall y. SampleFmt y => IOMode -> String -> IO (Handle y)
File.open IOMode
IO.WriteMode String
file,
      soundSinkClose :: Handle y -> IO ()
soundSinkClose = Handle y -> IO ()
forall y. SampleFmt y => Handle y -> IO ()
File.close,
      soundSinkWrite :: Handle y -> Ptr y -> SampleFreq -> IO ()
soundSinkWrite = Handle y -> Ptr y -> SampleFreq -> IO ()
forall y. SampleFmt y => Handle y -> Ptr y -> SampleFreq -> IO ()
File.write,
      soundSinkStart :: Handle y -> IO ()
soundSinkStart = IO () -> Handle y -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle y -> IO ()) -> IO () -> Handle y -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (),
      soundSinkStop :: Handle y -> IO ()
soundSinkStop  = IO () -> Handle y -> IO ()
forall a b. a -> b -> a
const (IO () -> Handle y -> IO ()) -> IO () -> Handle y -> IO ()
forall a b. (a -> b) -> a -> b
$ () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
   }

--
-- * Marshalling utilities
--

callocaArray :: Storable y => Size -> (Ptr y -> IO b) -> IO b
callocaArray :: forall a b. Storable a => SampleFreq -> (Ptr a -> IO b) -> IO b
callocaArray SampleFreq
n0 Ptr y -> IO b
f =
   case SampleFreq -> SampleFreq
forall a b. (Integral a, Num b) => a -> b
fromIntegral SampleFreq
n0 of
      SampleFreq
n ->
         SampleFreq -> (Ptr y -> IO b) -> IO b
forall a b. Storable a => SampleFreq -> (Ptr a -> IO b) -> IO b
allocaArray SampleFreq
n ((Ptr y -> IO b) -> IO b) -> (Ptr y -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \Ptr y
p ->
            Ptr y -> SampleFreq -> IO ()
forall a. Ptr a -> SampleFreq -> IO ()
clearBytes Ptr y
p (Ptr y -> SampleFreq -> SampleFreq
forall y. Storable y => Ptr y -> SampleFreq -> SampleFreq
arraySize Ptr y
p SampleFreq
n) IO () -> IO b -> IO b
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
            Ptr y -> IO b
f Ptr y
p

clearBytes :: Ptr a -> Int -> IO ()
clearBytes :: forall a. Ptr a -> SampleFreq -> IO ()
clearBytes Ptr a
p SampleFreq
n = Ptr a -> CInt -> CSize -> IO (Ptr a)
forall a. Ptr a -> CInt -> CSize -> IO (Ptr a)
memset Ptr a
p CInt
0 (SampleFreq -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral SampleFreq
n) IO (Ptr a) -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

foreign import ccall unsafe "string.h" memset :: Ptr a -> C.CInt -> C.CSize -> IO (Ptr a)