module Sound.MED.Raw.MMD0NoteData where

import Sound.MED.Basic.Amiga

import Data.Bits (shiftR, shiftL, (.&.), (.|.))

data MMD0NoteData = MMD0NoteData
  { note       :: UBYTE
  , instrument :: UBYTE
  , command    :: UBYTE
  , databyte   :: UBYTE
  }
  deriving (Show)

peek :: MEM -> PTR -> IO MMD0NoteData
peek m p = do
  byte1 <- peekUBYTE m (p + 0)
  byte2 <- peekUBYTE m (p + 1)
  byte3 <- peekUBYTE m (p + 2)
  let x           = (byte1 .&. 0x80) `shiftR` 7
  let y           = (byte1 .&. 0x40) `shiftR` 6
  let nnnnnn      = (byte1 .&. 0x3F)
  let iiii        = (byte2 .&. 0xF0) `shiftR` 4
  let cccc        = (byte2 .&. 0x0F)
  let dddddddd    = (byte3 .&. 0xFF)
  let note'       = nnnnnn
  let instrument' = (y `shiftL` 6) .|. (x `shiftL` 5) .|. iiii
  let command'    = cccc
  let databyte'   = dddddddd
  return $ MMD0NoteData
    note' instrument' command' databyte'