packages feed

hommage-ds-0.0.5: Sound/Hommage/DSPlayer/AudioSample.hs

{- 2009 Daniel van den Eijkel <dvde@gmx.net> -}

module Sound.Hommage.DSPlayer.AudioSample
 ( SamplePool
 , loadAudioSample
 , getAudioSample
 , AudioSample (..)
 , openWavSample
 )
 where

--import Sound.Hommage.WavFile
import Sound.Hommage.Signal
import Sound.Hommage.Sample

import Control.Monad 
import Data.Array.Storable
import Data.Array.IO
import GHC.IOBase

-- | A list of AudioSamples. The list can be extended at runtime. 
type SamplePool = IORef [AudioSample]

-- | Load a Sample into the SamplePool
loadAudioSample :: SamplePool -> FilePath -> IO Int
loadAudioSample ref fp = do
 a <- openWavSample fp
 let s = either MonoSample StereoSample a
 aus <- readIORef ref
 writeIORef ref (aus ++ [s])
 return $! length aus
 
-- | Get the nth AudioSample. Nothing if not defined.
getAudioSample :: SamplePool -> Int -> IO (Maybe AudioSample)
getAudioSample ref i = do
 aus <- readIORef ref
 if i < 0 || i >= length aus
  then return Nothing
  else return (Just (aus !! i))

---------------------------------------------------------------------------------------------------
-- Sample Mono | Sample Stereo
data AudioSample = MonoSample   { theMonoSample   :: Sample Mono } 
                 | StereoSample { theStereoSample :: Sample Stereo }
---------------------------------------------------------------------------------------------------