module SFML.Audio.SoundBuffer
(
module SFML.Utils
, soundBufferFromFile
, soundBufferFromMemory
, soundBufferFromStream
, soundBufferFromSamples
, copySoundBuffer
, destroy
, saveSoundBufferToFile
, getSamples
, getSampleCount
, getSampleRate
, getChannelCount
, getDuration
)
where
import SFML.Audio.SFSampled
import SFML.Audio.SFSoundBuffer
import SFML.Audio.Types
import SFML.SFException
import SFML.SFResource
import SFML.System.InputStream
import SFML.System.Time
import SFML.Utils
import Control.Monad ((>=>))
import Foreign.C.String
import Foreign.C.Types
import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (Ptr, nullPtr)
import Foreign.Storable (peek)
checkNull :: SoundBuffer -> Maybe SoundBuffer
checkNull buf@(SoundBuffer ptr) = if ptr == nullPtr then Nothing else Just buf
soundBufferFromFile :: FilePath -> IO (Either SFException SoundBuffer)
soundBufferFromFile path =
let err = SFException $ "Failed loading sound buffer from file: " ++ show path
in fmap (tagErr err . checkNull) $ withCAString path sfSoundBuffer_createFromFile
foreign import ccall unsafe "sfSoundBuffer_createFromFile"
sfSoundBuffer_createFromFile :: CString -> IO SoundBuffer
soundBufferFromMemory
:: Ptr a
-> Int
-> IO (Either SFException SoundBuffer)
soundBufferFromMemory ptr size =
let err = SFException $ "Failed loading sound buffer from memory address " ++ show ptr
in sfSoundBuffer_createFromMemory ptr (fromIntegral size) >>= return . tagErr err . checkNull
foreign import ccall unsafe "sfSoundBuffer_createFromMemory"
sfSoundBuffer_createFromMemory :: Ptr a -> CUInt -> IO SoundBuffer
soundBufferFromStream
:: InputStream
-> IO (Either SFException SoundBuffer)
soundBufferFromStream is =
let err = SFException $ "Failed loading sound buffer from input stream " ++ show is
in with is sfSoundBuffer_createFromStream >>= return . tagErr err . checkNull
foreign import ccall unsafe "sfSoundBuffer_createFromStream"
sfSoundBuffer_createFromStream :: Ptr InputStream -> IO SoundBuffer
soundBufferFromSamples
:: Ptr a
-> Int
-> Int
-> Int
-> IO (Maybe SoundBuffer)
soundBufferFromSamples ptr i j k =
sfSoundBuffer_createFromSamples ptr (fromIntegral i) (fromIntegral j) (fromIntegral k) >>= return . checkNull
foreign import ccall unsafe "sfSoundBuffer_createFromSamples"
sfSoundBuffer_createFromSamples :: Ptr a -> CUInt -> CUInt -> CUInt -> IO SoundBuffer
copySoundBuffer :: SoundBuffer -> IO SoundBuffer
copySoundBuffer = sfSoundBuffer_copy
foreign import ccall unsafe "sfSoundBuffer_copy"
sfSoundBuffer_copy :: SoundBuffer -> IO SoundBuffer
instance SFResource SoundBuffer where
destroy = sfSoundBuffer_destroy
foreign import ccall unsafe "sfSoundBuffer_destroy"
sfSoundBuffer_destroy :: SoundBuffer -> IO ()
saveSoundBufferToFile
:: SoundBuffer
-> FilePath
-> IO Bool
saveSoundBufferToFile sb path = fmap (toEnum . fromIntegral) . withCAString path $ sfSoundBuffer_saveToFile sb
foreign import ccall unsafe "sfSoundBuffer_saveToFile"
sfSoundBuffer_saveToFile :: SoundBuffer -> CString -> IO CInt
getSamples
:: SoundBuffer
-> IO (Ptr a)
getSamples = sfSoundBuffer_getSamples
foreign import ccall unsafe "sfSoundBuffer_getSamples"
sfSoundBuffer_getSamples :: SoundBuffer -> IO (Ptr a)
getSampleCount :: SoundBuffer -> IO Int
getSampleCount = sfSoundBuffer_getSampleCount >=> return . fromIntegral
foreign import ccall unsafe "sfSoundBuffer_getSampleCount"
sfSoundBuffer_getSampleCount :: SoundBuffer -> IO CUInt
instance SFSoundBuffer SoundBuffer where
getChannelCount = sfSoundBuffer_getChannelCount >=> return . fromIntegral
getDuration sb = alloca $ \ptr -> sfSoundBuffer_getDuration_helper sb ptr >> peek ptr
foreign import ccall unsafe "sfSoundBuffer_getChannelCount"
sfSoundBuffer_getChannelCount :: SoundBuffer -> IO CUInt
foreign import ccall unsafe "sfSoundBuffer_getDuration_helper"
sfSoundBuffer_getDuration_helper :: SoundBuffer -> Ptr Time -> IO ()
instance SFSampled SoundBuffer where
getSampleRate = sfSoundBuffer_getSampleRate >=> return . fromIntegral
foreign import ccall unsafe "sfSoundBuffer_getSampleRate"
sfSoundBuffer_getSampleRate :: SoundBuffer -> IO CUInt