module Sound.SFML (
PolySound,
newPolySound,
freePolySound,
triggerPolySound,
) where
import Data.IORef
import Control.Monad
import Foreign.Ptr
import Sound.SFML.LowLevel
data PolySound = PolySound FilePath (Ptr SoundBuffer) [Ptr Sound] (IORef Int)
instance Show PolySound where
show (PolySound file _ _ _) = "PolySound " ++ show file
newPolySound :: FilePath -> Int -> IO PolySound
newPolySound path numberOfVoices = do
buffer <- sfSoundBuffer_CreateFromFile path
sounds <- forM [1 .. numberOfVoices] $ \ _ -> do
sound <- sfSound_Create
sfSound_SetBuffer sound buffer
return sound
ref <- newIORef 0
return $ PolySound path buffer sounds ref
freePolySound :: PolySound -> IO ()
freePolySound (PolySound _ buffer sounds _) = do
sfSoundBuffer_Destroy buffer
mapM_ sfSound_Destroy sounds
triggerPolySound :: PolySound -> IO ()
triggerPolySound (PolySound _ _ sounds ref) = do
i <- readIORef ref
let i' = (i + 1) `mod` length sounds
writeIORef ref i'
sfSound_Play (sounds !! i)