-- | Read a `Module` from the monad state.
getModule :: Get Module
getModule = label "IT" $ do
    header <- getHeader
    message <- getAtOffset (replicateM (fromIntegral $ messageLength header) getWord8) $ messageOffset header
    orders <- replicateM (fromIntegral (songLength header)) getWord8
    insOffsets <- replicateM (fromIntegral (numInstruments header)) getWord32le
    smpOffsets <- replicateM (fromIntegral (numSamples header)) getWord32le
    patOffsets <- replicateM (fromIntegral (numPatterns header)) getWord32le
    instruments <- mapM (getAtOffset getInstrument) insOffsets
    sampleHeaders <- mapM (getAtOffset getSampleHeader) smpOffsets
    patterns <- sequence [ if x == 0
                           then getEmptyPattern
                           else getAtOffset getPattern x
                         | x <- patOffsets ]
    return Module{..}

-- | Write a `Module` to the buffer.
getModule :: Get Module
getModule = label "IT" $ do
    header <- getHeader
    message <- getAtOffset (replicateM (fromIntegral $ messageLength header) getWord8) $ messageOffset header
    orders <- replicateM (fromIntegral (songLength header)) getWord8
    insOffsets <- replicateM (fromIntegral (numInstruments header)) getWord32le
    smpOffsets <- replicateM (fromIntegral (numSamples header)) getWord32le
    patOffsets <- replicateM (fromIntegral (numPatterns header)) getWord32le
    instruments <- mapM (getAtOffset getInstrument) insOffsets
    sampleHeaders <- mapM (getAtOffset getSampleHeader) smpOffsets
    patterns <- sequence [ if x == 0
                           then getEmptyPattern
                           else getAtOffset getPattern x
                         | x <- patOffsets ]
    return Module{..}

-- | Write a `Module` to the buffer.
getModule = label "IT" $ do
    header <- getHeader
    message <- getAtOffset (replicateM (fromIntegral $ messageLength header) getWord8) $ messageOffset header
    orders <- replicateM (fromIntegral (songLength header)) getWord8
    insOffsets <- replicateM (fromIntegral (numInstruments header)) getWord32le
    smpOffsets <- replicateM (fromIntegral (numSamples header)) getWord32le
    patOffsets <- replicateM (fromIntegral (numPatterns header)) getWord32le
    instruments <- mapM (getAtOffset getInstrument) insOffsets
    sampleHeaders <- mapM (getAtOffset getSampleHeader) smpOffsets
    patterns <- sequence [ if x == 0
                           then getEmptyPattern
                           else getAtOffset getPattern x
                         | x <- patOffsets ]
    return Module{..}

-- | Write a `Module` to the buffer.