#ifndef _MIDIDEFS
#define _MIDIDEFS

#include "misc/systemdefs.h"
//#include "misc/event.h"
#include <midi/camd.h>

#define PPQ 833.3		// Pulses per quarter note(?)
						// Camouflage reckons it's 768 but I'm not so sure!

// Track Chunk Types

#define META_EVENT				0xFF
#define SYSEX_TYPE1_EVENT		0xF0
#define SYSEX_TYPE2_EVENT		0xFC

// or any of these...
// Midi message types

#define NO_STATUS					0x00
#define NOTE_OFF              0x80
#define NOTE_ON               0x90
#define POLYPHONIC_AT         0xA0
#define CONTROL_CHANGE        0xB0
#define PROGRAM_CHANGE        0xC0
#define CHANNEL_PRESSURE      0xD0
#define PITCHBEND             0xE0

// Data sizes
#define NOTE_OFF_SIZE         2
#define NOTE_ON_SIZE          2
#define POLYPHONIC_AT_SIZE    2
#define CONTROL_CHANGE_SIZE   2
#define PROGRAM_CHANGE_SIZE   1
#define CHANNEL_PRESSURE_SIZE 1
#define PITCHBEND_SIZE        2

#define TYPE_MASK					0xF0	// Mask to find the type of message		
#define CHANNEL_MASK				0x0F	// Mask to find the msg channel
#define DIV_MASK              0x8000 
#define DIV_VAL_MASK          0x7FFF

// Chunk Types
#define CHUNK_UNKNOWN			0
#define CHUNK_HDR					0x4D546864	
#define CHUNK_TRK					0x4D54726B

// Event types
#define HI_BYTE_MASK				0x80
#define LO_BYTE_MASK				0x7F

// Meta Types
#define META_SEQ_NUMBER			0x00
#define META_TRACK_END			0x2F
#define META_TEMPO				0x51
#define META_SMPTE				0x54
#define META_TIME_SIG			0x58
#define META_KEY_SIG				0x59
#define META_SPECIFIC			0x7F

typedef struct {
	UINT ch_type;
	UINT ch_size;
} MidiChunk;

typedef struct {
	UINT identifier;
	UINT size;
	UWORD type;
	UWORD track_chunks;
	UWORD division;
} MidiHeader;

typedef struct {
	UINT identifier;
	UINT size;
} MidiTrack;

typedef struct{
	MidiMsg me_Msg;
//	ULONG me_deltaTime;
	ULONG me_absTime;
	ULONG me_Tempo;
//	UBYTE me_Type;
//	UBYTE me_Channel;
//	UBYTE me_Data[2];
} MidiEvent;

#endif
