/* =================================
     StdMIDIデータを実際に書き込む
   ================================= */

#include "VarLen.h"

#define   USERUNSTAT 0xFE
#define NOUSERUNSTAT 0xFF

#include "config.h"
void status_init(UBYTE initdata);
int Putcode(UBYTE *MTrk,DWORD DELTA,UBYTE p1,UBYTE p2,UBYTE p3);
int Putcode2(UBYTE *MTrk,DWORD DELTA,UBYTE p1,UBYTE p2);

static UBYTE CC,status;


/* ======================
     ステータスの初期化
   ====================== */

void status_init(UBYTE initdata)
{
	CC = status = initdata;
}

/* =============================================
     StdMIDIデータ(３バイト系)を実際に書き込む
   ============================================= */

int Putcode(UBYTE *MTrk,DWORD DELTA,UBYTE p1,UBYTE p2,UBYTE p3)
{
	UBYTE *MTrk_ = MTrk;

	MTrk += PutVarLen( MTrk,DELTA);
	if ((p1 != CC) || (status == NOUSERUNSTAT)) {
		*MTrk++ = CC = p1;
	}
	*MTrk++ = p2;
	*MTrk++ = p3;
	return MTrk - MTrk_;
}


/* =============================================
     StdMIDIデータ(２バイト系)を実際に書き込む
   ============================================= */

int Putcode2(UBYTE *MTrk,DWORD DELTA,UBYTE p1,UBYTE p2)
{
	UBYTE *MTrk_ = MTrk;

	MTrk += PutVarLen(MTrk,DELTA);
	if ((p1 != CC) || (status == NOUSERUNSTAT)) {
		*MTrk++ = CC = p1;
	}
	*MTrk++ = p2;
	return MTrk - MTrk_;
}
