#ifndef _MIDIFILE_H
#define _MIDIFILE_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
  int len;
  unsigned char data[];
} MIDITRACK;

typedef struct MIDIFILE {
  // Type 0: single track  (-> no_tracks==1) 
  // Type 1: possible more simultaneous tracks
  // Type 2: the multiple tracks are meant sequential
  unsigned short format;
  unsigned short no_tracks;
  unsigned short ppq; // pulses per quarter
  unsigned char smtpe_fps,smtpe_subframes; // SMTPE, iff ppq==0
  MIDITRACK **tracks;
} MIDIFILE;



// will fill >mf
int read_midifile(MIDIFILE *mf, const char *filename); // 0 on success

void init_midifile(MIDIFILE *mf);
int write_midifile(MIDIFILE *mf, const char *filename);
void free_midifile(MIDIFILE *mf); // only contents

// end-pos==4 is always large enough
int midi_read_vl(unsigned char **pos,unsigned char *end);  // -1 on error
int midi_write_vl(unsigned char *pos,unsigned char *end,unsigned int val); // returns length  or 0;  allows pos==0

// extern int midi_debug;

#ifdef __cplusplus
};
#endif

#endif
