-- GENERATED by C->Haskell Compiler, version 0.28.1 Switcheroo, 1 April 2016 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "./Sound/ProteaAudio.chs" #-}
{-|
    ProteaAudio is a stereo audio mixer/playback library
    for Linux (native ALSA, Jack, and OSS),
    Macintosh OS X (CoreAudio and Jack),
    and Windows (DirectSound and ASIO) operating systems.
-}
{-#LANGUAGE ForeignFunctionInterface#-}

module Sound.ProteaAudio (
    initAudio,
    finishAudio,
    loaderAvailable,
    volume,
    sampleFromMemoryWav,
    sampleFromMemoryOgg,
    sampleFromFile,
    soundActive,
    soundStopAll,
    soundLoop,
    soundPlay,
    soundUpdate,
    soundStop,
    Sample()
 ) where
import qualified Foreign.C.String as C2HSImp
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Marshal.Utils as C2HSImp
import qualified Foreign.Ptr as C2HSImp



import Foreign
import Foreign.C
import Data.ByteString (ByteString, useAsCStringLen)

-- | audio sample handle
newtype Sample = Sample (C2HSImp.CInt)
{-# LINE 31 "./Sound/ProteaAudio.chs" #-}


toSample s = Sample s
fromSample (Sample s) = s

-- | initializes the audio system
initAudio :: (Int) -- ^ the maximum number of sounds that are played parallely. Computation time is linearly correlated to this factor.
 -> (Int) -- ^ sample frequency of the playback in Hz. 22050 corresponds to FM radio 44100 is CD quality. Computation time is linearly correlated to this factor.
 -> (Int) -- ^ the number of bytes that are sent to the sound card at once. Low numbers lead to smaller latencies but need more computation time (thread switches). If a too small number is chosen, the sounds might not be played continuously. The default value 512 guarantees a good latency below 40 ms at 22050 Hz sample frequency.
 -> IO ((Bool)) -- ^ returns True on success

initAudio a1 a2 a3 =
  let {a1' = fromIntegral a1} in 
  let {a2' = fromIntegral a2} in 
  let {a3' = fromIntegral a3} in 
  initAudio'_ a1' a2' a3' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 42 "./Sound/ProteaAudio.chs" #-}


-- | releases the audio device and cleans up resources
finishAudio :: IO ()
finishAudio =
  finishAudio'_ >>
  return ()

{-# LINE 45 "./Sound/ProteaAudio.chs" #-}


-- | checks if loader for this file type is available
loaderAvailable :: (String) -- ^ file extension (e.g. ogg)
 -> IO ((Bool))
loaderAvailable a1 =
  C2HSImp.withCString a1 $ \a1' -> 
  loaderAvailable'_ a1' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 51 "./Sound/ProteaAudio.chs" #-}


-- | loads wav sound sample from memory buffer
_sampleFromMemoryWav :: (Ptr CChar) -- ^ memory buffer pointer
 -> (Int) -- ^ memory buffer size in bytes
 -> (Float) -- ^ volume
 -> IO ((Sample)) -- ^ returns handle

_sampleFromMemoryWav a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  let {a3' = realToFrac a3} in 
  _sampleFromMemoryWav'_ a1' a2' a3' >>= \res ->
  let {res' = toSample res} in
  return (res')

{-# LINE 59 "./Sound/ProteaAudio.chs" #-}


-- | loads ogg sound sample from memory buffer
_sampleFromMemoryOgg :: (Ptr CChar) -- ^ memory buffer pointer
 -> (Int) -- ^ memory buffer size in bytes
 -> (Float) -- ^ volume
 -> IO ((Sample)) -- ^ returns handle

_sampleFromMemoryOgg a1 a2 a3 =
  let {a1' = id a1} in 
  let {a2' = fromIntegral a2} in 
  let {a3' = realToFrac a3} in 
  _sampleFromMemoryOgg'_ a1' a2' a3' >>= \res ->
  let {res' = toSample res} in
  return (res')

{-# LINE 67 "./Sound/ProteaAudio.chs" #-}


-- | loads wav sound sample from memory buffer
sampleFromMemoryWav :: ByteString -- ^ wav sample data
                    -> Float -- ^ volume
                    -> IO Sample -- ^ return sample handle
sampleFromMemoryWav wavData volume = useAsCStringLen wavData $ \(ptr, size) -> _sampleFromMemoryWav ptr size volume

-- | loads ogg sound sample from memory buffer
sampleFromMemoryOgg :: ByteString -- ^ ogg sample data
                    -> Float -- ^ volume
                    -> IO Sample -- ^ return sample handle
sampleFromMemoryOgg oggData volume = useAsCStringLen oggData $ \(ptr, size) -> _sampleFromMemoryOgg ptr size volume

-- | loads a sound sample from file
sampleFromFile :: (String) -- ^ sample filepath
 -> (Float) -- ^ volume
 -> IO ((Sample)) -- ^ returns handle

sampleFromFile a1 a2 =
  C2HSImp.withCString a1 $ \a1' -> 
  let {a2' = realToFrac a2} in 
  sampleFromFile'_ a1' a2' >>= \res ->
  let {res' = toSample res} in
  return (res')

{-# LINE 86 "./Sound/ProteaAudio.chs" #-}


-- | set mixer volume
volume :: (Float) -- ^ left
 -> (Float) -- ^ right
 -> IO ()
volume a1 a2 =
  let {a1' = realToFrac a1} in 
  let {a2' = realToFrac a2} in 
  volume'_ a1' a2' >>
  return ()

{-# LINE 93 "./Sound/ProteaAudio.chs" #-}


-- | number of currently active sounds
soundActive :: IO ((Int))
soundActive =
  soundActive'_ >>= \res ->
  let {res' = fromIntegral res} in
  return (res')

{-# LINE 96 "./Sound/ProteaAudio.chs" #-}


-- | stops all sounds immediately
soundStopAll :: IO ()
soundStopAll =
  soundStopAll'_ >>
  return ()

{-# LINE 99 "./Sound/ProteaAudio.chs" #-}


-- | plays a specified sound sample continuously and sets its parameters
soundLoop :: (Sample) -- ^ handle of a previously loaded sample
 -> (Float) -- ^ left volume
 -> (Float) -- ^ right volume
 -> (Float) -- ^ time difference between left and right channel in seconds. Use negative values to specify a delay for the left channel, positive for the right
 -> (Float) -- ^ pitch factor for playback. 0.5 corresponds to one octave below, 2.0 to one above the original sample
 -> IO ()
soundLoop a1 a2 a3 a4 a5 =
  let {a1' = fromSample a1} in 
  let {a2' = realToFrac a2} in 
  let {a3' = realToFrac a3} in 
  let {a4' = realToFrac a4} in 
  let {a5' = realToFrac a5} in 
  soundLoop'_ a1' a2' a3' a4' a5' >>
  return ()

{-# LINE 109 "./Sound/ProteaAudio.chs" #-}


-- | plays a specified sound sample once and sets its parameters
soundPlay :: (Sample) -- ^ handle of a previously loaded sample
 -> (Float) -- ^ left volume
 -> (Float) -- ^ right volume
 -> (Float) -- ^ time difference between left and right channel in seconds. Use negative values to specify a delay for the left channel, positive for the right
 -> (Float) -- ^ pitch factor for playback. 0.5 corresponds to one octave below, 2.0 to one above the original sample
 -> IO ()
soundPlay a1 a2 a3 a4 a5 =
  let {a1' = fromSample a1} in 
  let {a2' = realToFrac a2} in 
  let {a3' = realToFrac a3} in 
  let {a4' = realToFrac a4} in 
  let {a5' = realToFrac a5} in 
  soundPlay'_ a1' a2' a3' a4' a5' >>
  return ()

{-# LINE 119 "./Sound/ProteaAudio.chs" #-}


-- | updates parameters of a specified sound
soundUpdate :: (Sample) -- ^ handle of a currently active sound
 -> (Float) -- ^ left volume
 -> (Float) -- ^ right volume
 -> (Float) -- ^ time difference between left and right channel in seconds. Use negative values to specify a delay for the left channel, positive for the right
 -> (Float) -- ^ pitch factor for playback. 0.5 corresponds to one octave below, 2.0 to one above the original sample
 -> IO ((Bool)) -- ^ return True in case the parameters have been updated successfully

soundUpdate a1 a2 a3 a4 a5 =
  let {a1' = fromSample a1} in 
  let {a2' = realToFrac a2} in 
  let {a3' = realToFrac a3} in 
  let {a4' = realToFrac a4} in 
  let {a5' = realToFrac a5} in 
  soundUpdate'_ a1' a2' a3' a4' a5' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 129 "./Sound/ProteaAudio.chs" #-}


-- | stops a specified sound immediately
soundStop :: (Sample) -> IO ((Bool))
soundStop a1 =
  let {a1' = fromSample a1} in 
  soundStop'_ a1' >>= \res ->
  let {res' = C2HSImp.toBool res} in
  return (res')

{-# LINE 132 "./Sound/ProteaAudio.chs" #-}


foreign import ccall safe "Sound/ProteaAudio.chs.h initAudio"
  initAudio'_ :: (C2HSImp.CInt -> (C2HSImp.CInt -> (C2HSImp.CInt -> (IO C2HSImp.CInt))))

foreign import ccall safe "Sound/ProteaAudio.chs.h finishAudio"
  finishAudio'_ :: (IO ())

foreign import ccall safe "Sound/ProteaAudio.chs.h loaderAvailable"
  loaderAvailable'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (IO C2HSImp.CInt))

foreign import ccall safe "Sound/ProteaAudio.chs.h _sampleFromMemoryWav"
  _sampleFromMemoryWav'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> (C2HSImp.CFloat -> (IO C2HSImp.CInt))))

foreign import ccall safe "Sound/ProteaAudio.chs.h _sampleFromMemoryOgg"
  _sampleFromMemoryOgg'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CInt -> (C2HSImp.CFloat -> (IO C2HSImp.CInt))))

foreign import ccall safe "Sound/ProteaAudio.chs.h sampleFromFile"
  sampleFromFile'_ :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CFloat -> (IO C2HSImp.CInt)))

foreign import ccall safe "Sound/ProteaAudio.chs.h volume"
  volume'_ :: (C2HSImp.CFloat -> (C2HSImp.CFloat -> (IO ())))

foreign import ccall safe "Sound/ProteaAudio.chs.h soundActive"
  soundActive'_ :: (IO C2HSImp.CInt)

foreign import ccall safe "Sound/ProteaAudio.chs.h soundStopAll"
  soundStopAll'_ :: (IO ())

foreign import ccall safe "Sound/ProteaAudio.chs.h soundLoop"
  soundLoop'_ :: (C2HSImp.CInt -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (IO ()))))))

foreign import ccall safe "Sound/ProteaAudio.chs.h soundPlay"
  soundPlay'_ :: (C2HSImp.CInt -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (IO ()))))))

foreign import ccall safe "Sound/ProteaAudio.chs.h soundUpdate"
  soundUpdate'_ :: (C2HSImp.CInt -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (C2HSImp.CFloat -> (IO C2HSImp.CInt))))))

foreign import ccall safe "Sound/ProteaAudio.chs.h soundStop"
  soundStop'_ :: (C2HSImp.CInt -> (IO C2HSImp.CInt))