    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Graphics.UI.SDL.Mixer.Samples
    -- Copyright   :  (c) David Himmelstrup 2005
    -- License     :  BSD-like
    --
    -- Maintainer  :  lemmih@gmail.com
    -- Stability   :  provisional
    -- Portability :  portable
    --
    -----------------------------------------------------------------------------
    module Graphics.UI.SDL.Mixer.Samples
        ( mkFinalizedChunk
        , maxVolume
        , tryLoadWAV
        , loadWAV
        , volumeChunk
        ) where

    import Foreign(Ptr, FunPtr, nullPtr, withForeignPtr, newForeignPtr)
    import Foreign.C(withCString, CString)

    import Graphics.UI.SDL.Mixer.Types(Chunk, ChunkStruct)
    import Graphics.UI.SDL.General(unwrapMaybe)

    maxVolume :: Int
    maxVolume :: Int
    maxVolume = 128

    -- void Mix_FreeChunk(Mix_Chunk *chunk)
    foreign import ccall unsafe "&Mix_FreeChunk" mixFreeChunkFinal :: FunPtr (Ptr ChunkStruct -> IO ())
    mkFinalizedChunk :: Ptr ChunkStruct -> IO Chunk
    mkFinalizedChunk :: Ptr ChunkStruct -> IO Chunk
    mkFinalizedChunk = FinalizerPtr ChunkStruct -> Ptr ChunkStruct -> IO Chunk
    forall a. FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
    newForeignPtr FinalizerPtr ChunkStruct
    mixFreeChunkFinal

    -- Mix_Chunk *Mix_LoadWAV(char *file)
    -- foreign import ccall unsafe "Mix_LoadWAV" mixLoadWAV :: CString -> IO (Ptr ChunkStruct)
    foreign import ccall unsafe "macros.h HS_Mix_LoadWAV"
        mixLoadWAV :: CString -> IO (Ptr ChunkStruct)

    tryLoadWAV :: FilePath -> IO (Maybe Chunk)
    tryLoadWAV :: FilePath -> IO (Maybe Chunk)
    tryLoadWAV string :: FilePath
    string
        = FilePath -> (CString -> IO (Maybe Chunk)) -> IO (Maybe Chunk)
    forall a. FilePath -> (CString -> IO a) -> IO a
    withCString FilePath
    string ((CString -> IO (Maybe Chunk)) -> IO (Maybe Chunk))
    -> (CString -> IO (Maybe Chunk)) -> IO (Maybe Chunk)
    forall a b. (a -> b) -> a -> b
    $ \cString :: CString
    cString ->
          do Ptr ChunkStruct
    chunk <- CString -> IO (Ptr ChunkStruct)
    mixLoadWAV CString
    cString
             if Ptr ChunkStruct
    chunk Ptr ChunkStruct -> Ptr ChunkStruct -> Bool
    forall a. Eq a => a -> a -> Bool
    == Ptr ChunkStruct
    forall a. Ptr a
    nullPtr
                then Maybe Chunk -> IO (Maybe Chunk)
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe Chunk
    forall a. Maybe a
    Nothing
                else (Chunk -> Maybe Chunk) -> IO Chunk -> IO (Maybe Chunk)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Chunk -> Maybe Chunk
    forall a. a -> Maybe a
    Just (Ptr ChunkStruct -> IO Chunk
    mkFinalizedChunk Ptr ChunkStruct
    chunk)

    loadWAV :: FilePath -> IO Chunk
    loadWAV :: FilePath -> IO Chunk
    loadWAV path :: FilePath
    path = FilePath -> IO (Maybe Chunk) -> IO Chunk
    forall a. FilePath -> IO (Maybe a) -> IO a
    unwrapMaybe "Mix_LoadWAV" (FilePath -> IO (Maybe Chunk)
    tryLoadWAV FilePath
    path)

    -- int Mix_VolumeChunk(Mix_Chunk *chunk, int volume)
    foreign import ccall unsafe "Mix_VolumeChunk" mixVolumeChunk :: Ptr ChunkStruct -> Int -> IO Int
    volumeChunk :: Chunk -> Int -> IO Int
    volumeChunk :: Chunk -> Int -> IO Int
    volumeChunk chunk :: Chunk
    chunk volume :: Int
    volume = Chunk -> (Ptr ChunkStruct -> IO Int) -> IO Int
    forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
    withForeignPtr Chunk
    chunk (\ptr :: Ptr ChunkStruct
    ptr -> Ptr ChunkStruct -> Int -> IO Int
    mixVolumeChunk Ptr ChunkStruct
    ptr Int
    volume)