module SFML.Audio.SoundRecorder
(
module SFML.Utils
, SoundRecorderStartCallback
, SoundRecorderProcessCallback
, SoundRecorderStopCallback
, createSoundRecorder
, destroy
, startRecording
, stopRecording
, getSampleRate
, isSoundRecorderAvailable
, setProcessingInterval
, getAvailableSoundRecordingDevices
, getDefaultSoundRecordingDevice
, setSoundRecordingDevice
, getSoundRecordingDevice
)
where
import SFML.Audio.SFSampled
import SFML.Audio.SFSoundRecorder
import SFML.Audio.Types
import SFML.SFException
import SFML.SFResource
import SFML.System.Time
import SFML.Utils
import Control.Applicative ((<$>), (<*>))
import Control.Monad ((>=>), forM)
import Data.Word (Word16)
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (advancePtr)
import Foreign.Storable
import Foreign.Ptr
checkNull :: SoundRecorder -> Maybe SoundRecorder
checkNull sr@(SoundRecorder ptr) = if ptr == nullPtr then Nothing else Just sr
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 SFException SoundRecorder)
createSoundRecorder c1 c2 c3 d =
let err = SFException $
"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 = ((/=0) . fromIntegral) <$> sfSoundRecorder_start rec (fromIntegral rate)
stopRecording = sfSoundRecorder_stop
foreign import ccall unsafe "sfSoundRecorder_start"
sfSoundRecorder_start :: SoundRecorder -> CUInt -> IO CInt
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
setProcessingInterval
:: SoundRecorder
-> Time
-> IO ()
setProcessingInterval = sfSoundRecorder_setProcessingInterval
foreign import ccall unsafe "sfSoundRecorder_setProcessingInterval"
sfSoundRecorder_setProcessingInterval :: SoundRecorder -> Time -> IO ()
getAvailableSoundRecordingDevices :: IO [String]
getAvailableSoundRecordingDevices = alloca $ \pCount -> do
pNames <- sfSoundRecorder_getAvailableDevices pCount
count <- fromIntegral <$> peek pCount :: IO Int
forM [1..count] $ peekCString . advancePtr (castPtr pNames)
foreign import ccall unsafe "sfSoundRecorder_getAvailableDevices"
sfSoundRecorder_getAvailableDevices :: Ptr CSize -> IO (Ptr CString)
getDefaultSoundRecordingDevice :: IO String
getDefaultSoundRecordingDevice = sfSoundRecorder_getDefaultDevice >>= peekCString
foreign import ccall unsafe "sfSoundRecorder_getDefaultDevice"
sfSoundRecorder_getDefaultDevice :: IO CString
setSoundRecordingDevice
:: SoundRecorder
-> String
-> IO Bool
setSoundRecordingDevice rec name = withCString name $ \cname ->
((/=0) . fromIntegral) <$> sfSoundRecorder_setDevice rec cname
foreign import ccall unsafe "sfSoundRecorder_setDevice"
sfSoundRecorder_setDevice :: SoundRecorder -> CString -> IO CInt
getSoundRecordingDevice :: SoundRecorder -> IO String
getSoundRecordingDevice rec = sfSoundRecorder_getDevice rec >>= peekCString
foreign import ccall unsafe "sfSoundRecorder_getDevice"
sfSoundRecorder_getDevice :: SoundRecorder -> IO CString