#pragma once

#include "xm/xm.h"

#ifdef __cplusplus
extern "C" {
#endif

#define xm_max_patterns 64
#define xm_max_instruments 128

typedef struct xm_state {
  xm_header_t * header;
  xm_pattern_header_t * pattern_header[xm_max_patterns];
  xm_instrument_header_t * instrument_header[xm_max_instruments];
  xm_sample_header_t * sample_header[xm_max_instruments]; // array
  int sample_data_offset[xm_max_instruments];

  int number_of_channels;
  int song_length;
  xm_pattern_format_t * pattern[xm_max_patterns];
  int pattern_note_count[xm_max_patterns];
} xm_state_t;

int xm_init(xm_state_t * xm,
            int buf,
            uint8_t * sample_data,
            int sample_data_length);

static inline int s16(void * buf)
{
  uint8_t * b = (uint8_t *)buf;
  int16_t v = (b[0] << 0) | (b[1] << 8);
  return v;
}

static inline int s32(void * buf)
{
  uint8_t * b = (uint8_t *)buf;
  int32_t v = (b[0] << 0) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  return v;
}

#ifdef __cplusplus
}
#endif
