module Sound.MED.Raw.SampleInstr where

import Sound.MED.Basic.Amiga

data SampleInstr = SampleInstr
  { octaves :: WORD
  , chans :: Either [[BYTE]] [[WORD]]
  }
  deriving Show

peek :: ULONG -> WORD -> Bool -> Bool -> MEM -> PTR -> IO SampleInstr
peek len' stype' s16' stereo' m p = do
  let octaves' = [1,5,3,2,4,6,7,9]!!fromIntegral stype'
  case (s16',stereo') of
    (False, False) -> do { dat'' <- mapM (peekBYTE m) $ pointerRangeGen (p+6) 1 len'             ; return $ SampleInstr octaves' $ Left [dat''] }
    (True,  False) -> do { dat'' <- mapM (peekWORD m) $ pointerRangeGen (p+6) 2 (len'`div`2)     ; return $ SampleInstr octaves' $ Right [dat''] }
    (False, True ) -> do { dat'' <- mapM (peekBYTE m) $ pointerRangeGen (p+6) 1 (2*len')         ; return $ SampleInstr octaves' $ Left (chunk len' dat'') }
    (True,  True ) -> do { dat'' <- mapM (peekWORD m) $ pointerRangeGen (p+6) 2 (2*(len'`div`2)) ; return $ SampleInstr octaves' $ Right (chunk (len'`div`2) dat'') }