module SFML.Audio.SoundRecorder
(
module SFML.Utils
, SoundRecorderException(..)
, SoundRecorderStartCallback
, SoundRecorderProcessCallback
, SoundRecorderStopCallback
, createSoundRecorder
, destroy
, startRecording
, stopRecording
, getSampleRate
, isSoundRecorderAvailable
)
where
import SFML.Audio.SFSampled
import SFML.Audio.SFSoundRecorder
import SFML.Audio.Types
import SFML.SFResource
import SFML.Utils
import Control.Exception
import Control.Monad ((>=>))
import Data.Typeable
import Data.Word (Word16)
import Foreign.C.Types
import Foreign.Ptr (Ptr, nullPtr)
checkNull :: SoundRecorder -> Maybe SoundRecorder
checkNull sr@(SoundRecorder ptr) = if ptr == nullPtr then Nothing else Just sr
data SoundRecorderException = SoundRecorderException String deriving (Show, Typeable)
instance Exception SoundRecorderException
type SoundRecorderStartCallback a = Ptr a -> IO CInt
type SoundRecorderProcessCallback a = Ptr Word16 -> CUInt -> Ptr a -> IO Bool
type SoundRecorderStopCallback a = Ptr a -> IO ()
createSoundRecorder
:: Ptr (SoundRecorderStartCallback a)
-> Ptr (SoundRecorderProcessCallback a)
-> Ptr (SoundRecorderStopCallback a)
-> Ptr a
-> IO (Either SoundRecorderException SoundRecorder)
createSoundRecorder c1 c2 c3 d =
let err = SoundRecorderException $
"Failed creating sound recorder: onStart = " ++ show c1 ++
" onProcess = " ++ show c2 ++
" onStop = " ++ show c3 ++
" userData = " ++ show d
in sfSoundRecorder_create c1 c2 c3 d >>= return . tagErr err . checkNull
foreign import ccall unsafe "sfSoundRecorder_create"
sfSoundRecorder_create
:: Ptr (SoundRecorderStartCallback a)
-> Ptr (SoundRecorderProcessCallback a)
-> Ptr (SoundRecorderStopCallback a)
-> Ptr a
-> IO SoundRecorder
instance SFResource SoundRecorder where
destroy = sfSoundRecorder_destroy
foreign import ccall unsafe "sfSoundRecorder_destroy"
sfSoundRecorder_destroy :: SoundRecorder -> IO ()
instance SFSoundRecorder SoundRecorder where
startRecording rec rate = sfSoundRecorder_start rec (fromIntegral rate)
stopRecording = sfSoundRecorder_stop
foreign import ccall unsafe "sfSoundRecorder_start"
sfSoundRecorder_start :: SoundRecorder -> CUInt -> IO ()
foreign import ccall unsafe "sfSoundRecorder_stop"
sfSoundRecorder_stop :: SoundRecorder -> IO ()
instance SFSampled SoundRecorder where
getSampleRate = sfSoundRecorder_getSampleRate >=> return . fromIntegral
foreign import ccall unsafe "sfSoundRecorder_getSampleRate"
sfSoundRecorder_getSampleRate :: SoundRecorder -> IO CUInt
isSoundRecorderAvailable :: IO Bool
isSoundRecorderAvailable = fmap (toEnum . fromIntegral) sfSoundRecorder_isAvailable
foreign import ccall unsafe "sfSoundRecorder_isAvailable"
sfSoundRecorder_isAvailable :: IO CInt