module Sound.MED.Raw.MMD0Song where

import qualified Sound.MED.Raw.MMD0Sample as MMD0Sample
import Sound.MED.Raw.MMD0Sample(MMD0Sample)
import Sound.MED.Basic.Amiga

data MMD0Song = MMD0Song
  { sample     :: [ MMD0Sample ]
  , numblocks  :: UWORD
  , songlen    :: UWORD
  , playseq    :: [ UBYTE ]
  , deftempo   :: UWORD
  , playtransp :: BYTE
  , flags      :: UBYTE
  , flags2     :: UBYTE
  , tempo2     :: UBYTE
  , trkvol     :: [ UBYTE ]
  , mastervol  :: UBYTE
  , numsamples :: UBYTE
  }
  deriving (Show)

peek :: MEM -> PTR -> IO MMD0Song
peek m p = do
  sample'      <- mapM (MMD0Sample.peek m) $ pointerRange p 8 63
  numblocks'   <- peekUWORD m (p+504)
  songlen'     <- peekUWORD m (p+506)
  playseq'     <- mapM (peekUBYTE m) $ pointerRange (p+508) 1 256
  deftempo'    <- peekUWORD m (p+764)
  playtransp'  <- peekBYTE  m (p+766)
  flags'       <- peekUBYTE m (p+767)
  flags2'      <- peekUBYTE m (p+768)
  tempo2'      <- peekUBYTE m (p+769)
  trkvol'      <- mapM (peekUBYTE m) $ pointerRange (p+770) 1 16
  mastervol'   <- peekUBYTE m (p+786)
  numsamples'  <- peekUBYTE m (p+787)
  return $ MMD0Song
    sample' numblocks' songlen' playseq' deftempo' playtransp' flags'
    flags2' tempo2' trkvol' mastervol' numsamples'