module Sound.MED.Raw.MMD1Block where

import qualified Sound.MED.Raw.BlockInfo as BlockInfo
import Sound.MED.Raw.BlockInfo(BlockInfo)
import qualified Sound.MED.Raw.MMD1NoteData as MMD1NoteData
import Sound.MED.Raw.MMD1NoteData(MMD1NoteData)

import Sound.MED.Basic.Amiga

data MMD1Block = MMD1Block
  { numtracks :: UWORD
  , lines     :: UWORD -- NOTE: actual number of lines is one greater
  , info      :: Maybe BlockInfo
  , notedata  :: [ [ MMD1NoteData ] ]
  }
  deriving (Show)

peek :: MEM -> PTR -> IO MMD1Block
peek m p = do
  numtracks'    <- peekUWORD m (p + 0)
  lines'        <- peekUWORD m (p + 2)
  info'''       <- peekPTR   m (p + 4)
  info'         <- BlockInfo.peek numtracks' (lines'+1) m $? info'''
  notedata''    <- mapM (MMD1NoteData.peek m) $ pointerRangeGen2 (p+8) 4 numtracks' (lines'+1)
  let notedata' =  chunk numtracks' notedata''
  return $ MMD1Block
    numtracks' lines' info' notedata'