#include "Setting.h"
#include "Gdi.h"
#include "DefOrg.h"
#include "OrgData.h"
#include "resource.h"
#include <memory.h>
#include <math.h>

#include "Sound.h"
#include "rxoFunction.h"
#include "Scroll.h"

#define DEFVOLUME	200//255偼VOLDUMMY丅MAX偼254
#define DEFPAN		6
extern HWND hDlgTrack;
extern char *dram_name[];
extern int iKeyPushDown[];
//巜掕偺悢偩偗NoteData偺椞堟傪妋曐(弶婜壔)
BOOL OrgData::NoteAlloc(unsigned short alloc)
{
	int i,j;
	for(j = 0; j < MAXTRACK; j++){
		info.tdata[j].wave_no = 0;
		info.tdata[j].note_list = NULL;//僐儞僗僩儔僋僞偵傗傜偣偨偄
		info.tdata[j].note_p = new NOTELIST[alloc];
		if(info.tdata[j].note_p == NULL)return FALSE;
		//	info.alloc_note = alloc;
		for(i = 0; i < alloc; i++){
			(info.tdata[j].note_p + i)->from = NULL;
			(info.tdata[j].note_p + i)->to = NULL;
			(info.tdata[j].note_p + i)->length = 0;
			(info.tdata[j].note_p + i)->pan = PANDUMMY;
			(info.tdata[j].note_p + i)->volume = VOLDUMMY;
			(info.tdata[j].note_p + i)->y = KEYDUMMY;
		}
	}
	for(j = 0; j < MAXMELODY; j++)
		MakeOrganyaWave(j,info.tdata[j].wave_no, info.tdata[j].pipi);
	for(j = 0; j < MAXDRAM; j++)
		InitDramObject(0,j);

	track = 0;//////////崱偼偙偙偵彂偄偰偍偔
	return TRUE;
}
//NoteData傪奐曻
void OrgData::ReleaseNote(void)
{
	for(int i = 0; i < MAXTRACK; i++){
		if(info.tdata[i].note_p != NULL)delete info.tdata[i].note_p;
	}
}
//嬋忣曬傪庢摼
void OrgData::GetMusicInfo(MUSICINFO *mi, int mode) {
	mi->dot = info.dot;
	mi->line = info.line;
	mi->alloc_note = info.alloc_note;
	mi->wait = info.wait;
	mi->repeat_x = info.repeat_x;
	mi->end_x = info.end_x;
	for(int i = 0; i < MAXTRACK; i++){
		mi->tdata[i].freq = info.tdata[i].freq;
		mi->tdata[i].wave_no = info.tdata[i].wave_no;
		mi->tdata[i].pipi = info.tdata[i].pipi;
		if (mode == 1) {
			mi->tdata[i].note_list = info.tdata[i].note_list;
			mi->tdata[i].note_p = info.tdata[i].note_p;
		}
	}
}

unsigned short OrgData::GetWait(void) {
	return info.wait;
}

bool OrgData::PutBackGround(void)
{
	//if(!MakeMusicParts(info.line,info.dot))return false;//僷乕僣傪惗惉
	//MakePanParts(info.line,info.dot);
	//MessageBox(hWnd,"僌儕僢僪","",MB_OK);
	return true;
}

//嬋忣曬傪愝掕丅flag偼傾僀僥儉傪巜掕
BOOL OrgData::SetMusicInfo(MUSICINFO *mi,unsigned long flag)
{
	char str[32];
	int i;
	if(flag & SETGRID){//僌儕僢僪傪桳岠偵
		info.dot = mi->dot;
		info.line = mi->line;
		scr_data.ChangeHorizontalRange(info.dot * info.line * MAXHORZMEAS);
		//MakeMusicParts(info.line,info.dot);//僷乕僣傪惗惉
		//MakePanParts(info.line,info.dot);
//		MessageBox(hWnd,"僌儕僢僪","",MB_OK);

	}
	if(flag & SETALLOC){//椞堟妋曐
		info.alloc_note = mi->alloc_note;
		ReleaseNote();
		NoteAlloc(info.alloc_note);
//		MessageBox(hWnd,"垹儘僢僋","",MB_OK);
	}
	if(flag & SETWAIT){
		info.wait = mi->wait;
		itoa(mi->wait,str,10);
		SetDlgItemText(hDlgTrack,IDE_VIEWWAIT,str);
//		MessageBox(hWnd,"偆偊偄偲","",MB_OK);
	}
	if(flag & SETREPEAT){
		info.repeat_x = mi->repeat_x;
		info.end_x = mi->end_x;
	}
	if(flag & SETFREQ){
		for(int i = 0; i < MAXMELODY; i++){
			info.tdata[i].freq = mi->tdata[i].freq;
			info.tdata[i].pipi = info.tdata[i].pipi;
		}
	}
	if(flag & SETWAVE){
		for(i = 0; i < MAXTRACK; i++)
			info.tdata[i].wave_no = mi->tdata[i].wave_no;
	}
	if(flag & SETPIPI){
		for(i = 0; i < MAXTRACK; i++)
			info.tdata[i].pipi = mi->tdata[i].pipi;
	}

	return TRUE;
}
//枹巊梡壒晞傪専嶕
NOTELIST *OrgData::SearchNote(NOTELIST *np)
{
	int i;
	for(i = 0; i < info.alloc_note; i++,np++){
		if(np->length == 0){
			return np;
		}
	}
	return NULL;
}
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//埲壓偼壒晞乮僉乕乯偺攝抲丄嶍彍
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//壒晞傪攝抲(嵍僋儕僢僋偺張棟)
void OrgData::TouchKeyboard(unsigned char y)
{
	Rxo_PlayKey(y,track,info.tdata[track].freq,0);
	iKeyPushDown[y] = 1;
}
void OrgData::StopKeyboard(unsigned char y)
{
	Rxo_StopKey(y,track,0);
	iKeyPushDown[y] = 0;
}
BOOL OrgData::SetNote(long x,unsigned char y, int DragMode)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	NOTELIST *cut_p;//偦傟傪抲偔帠偵傛偭偰僇僢僩偡傋偒壒晞
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	//弶壒晞側傜儕僗僩偵搊榐
	if(info.tdata[track].note_list == NULL){
		PlayOrganKey(y,track,info.tdata[track].freq,100);//仭
		info.tdata[track].note_list = note;
		note->from = NULL;
		note->to = NULL;
		note->length = 1;
		note->pan = def_pan[track];
		note->volume = def_volume[track];
		note->x = x;
		note->y = y;
		return TRUE;
	}
	//摢偐傜専嶕
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	//憓擖
	if(p->x > x){
		PlayOrganKey(y,track,info.tdata[track].freq,100);//仭
		note->to = p;
		note->from = p->from;
		if(p->from == NULL){
			info.tdata[track].note_list = note;//愭摢
		}
		else p->from->to = note;//偠傖側偗傟偽慜偺傗偮偺師偵
		p->from = note;
		note->length = 1;
		note->pan = def_pan[track];
		note->volume = def_volume[track];
		note->x = x;
		note->y = y;
		//僇僢僩偡傋偒壒晞傪専嶕
		cut_p = note->from;
		while(cut_p != NULL && cut_p->y == KEYDUMMY)cut_p = cut_p->from;
		if(cut_p == NULL)return TRUE;
		if(note->x < cut_p->x + cut_p->length)//僇僢僩
			cut_p->length = (unsigned char)(note->x - cut_p->x);
	}
	//僷儔儊乕僞曄峏
	else if(p->x == x){
		if(p->y == y){
			if(p->length == MAXNOTELENGTH)return FALSE;
			//if(DragMode==0)
			if(iPushStratch)p->length++;//挿偔
			else p->length = iLastEditNoteLength; //摢傪...僆僾僔儑儞偩偭偨傜怢偽偡偺偱偼側偔嵟屻偺挿偝傪巊偆
			//僇僢僩偡傋偒壒晞傪専嶕
			cut_p = p->to;
			while(cut_p != NULL && cut_p->y == KEYDUMMY){
				cut_p = cut_p->to;
			}
			if(cut_p != NULL && p->length + p->x > cut_p->x){//怢偽偟偡偓傞偲師偺傪堸傒崬傓
				if(cut_p->to == NULL){//偦偄偮偑嵟屻旜側傜
					cut_p->from->to = NULL;//堦偮慜偺恖偵嵟屻旜傪擟偣傞丅
				}
				else{
					cut_p->from->to = cut_p->to;
					cut_p->to->from = cut_p->from;
				}
				cut_p->length = 0;
				cut_p->from = NULL;
			}
		}else{
			PlayOrganKey(y,track,info.tdata[track].freq,100);//仭
			p->y = y;//倄曄峏
			//僇僢僩偡傋偒壒晞傪専嶕
			cut_p = p->from;
			while(cut_p != NULL && cut_p->y == KEYDUMMY)cut_p = cut_p->from;
			if(cut_p == NULL)return TRUE;
			if(p->x < cut_p->x + cut_p->length)//僇僢僩
				cut_p->length = (unsigned char)(p->x - cut_p->x);
		}
	}
	//嵟屻旜捛壛
	else if(p->to == NULL){
		PlayOrganKey(y,track,info.tdata[track].freq,100);//仭
		note->from = p;
		p->to = note;
		note->to = NULL;
		note->length = 1;
		note->pan = def_pan[track];
		note->volume = def_volume[track];
		note->x = x;
		note->y = y;
		//僇僢僩偡傋偒壒晞傪専嶕
		cut_p = note->from;
		while(cut_p != NULL && cut_p->y == KEYDUMMY)cut_p = cut_p->from;
		if(cut_p == NULL)return TRUE;
		if(note->x < cut_p->x + cut_p->length)//僇僢僩
			cut_p->length = (unsigned char)(note->x - cut_p->x);
	}
	return TRUE;
}
//壒晞偺僇僢僩(塃僋儕僢僋偺張棟)
BOOL OrgData::CutNote(long x,unsigned char y)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	if(info.tdata[track].note_list == NULL)return FALSE;
	p = info.tdata[track].note_list;
	while(p != NULL && p->x < x)p = p->to;
	if(p == NULL)return FALSE;
	//僷儔儊乕僞曄峏
	if(p->x == x && p->y == y){
		if(iPushStratch)p->length--;//抁偔
		else p->length = 0; //摢傪...僆僾僔儑儞偩偭偨傜抁偔偡傞偺偱偼側偔徚偡丅
		if(p->length == 0){
			if(p->from == NULL)info.tdata[track].note_list = p->to;
			else p->from->to = p->to;
			if(p->to == NULL);
			else p->to->from = p->from;
		}
		return TRUE;
	}
	return FALSE;
}
//偳偺僩儔僢僋偵偁傞壒晞偐丠偺専嶕(曉傝寣偼僩儔僢僋)
int OrgData::SearchNote(long x, unsigned char y, int YuusenTrack)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	int i;
	int is,ie;

	p = info.tdata[YuusenTrack].note_list;
	while(p != NULL && !(p->x <= x && x < (p->x+p->length)))p = p->to;
	if(p != NULL){
		//hit!!
		if((p->x <= x && x < (p->x+p->length)) &&  p->y == y){
			return YuusenTrack;
		}
	}

	if(track<MAXDRAM){
		is = 0; ie = MAXDRAM;
	}else{
		is  =MAXDRAM; ie = MAXTRACK;
	}
	for(i=ie-1;i>=is;i--){
		if(info.tdata[i].note_list != NULL){
			p = info.tdata[i].note_list;
			//while(p != NULL && p->x < x)p = p->to;
			while(p != NULL && !(p->x <= x && x < (p->x+p->length)))p = p->to;
			if(p != NULL){
				//hit!!
				if((p->x <= x && x < (p->x+p->length)) &&  p->y == y){
					return i;
				}
			}
		}
	}
	return -1;
}
//偳偺僩儔僢僋偵偁傞壒晞偐丠偺専嶕(曉傝寣偼僩儔僢僋)
//忋偲摨偠偩偑丄奩摉偡傞壒晞偺嵟弶偲嵟屻傪億僀儞僞偱曉偡丅伀  偲       伀
//YuusenTrack>=0偺偲偒丄偦偺僩儔僢僋傪桪愭偟偰専嶕偡傞丅
int OrgData::SearchNoteB(long x,unsigned char y, long *lStartx, long *lLastx, int YuusenTrack)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	int i;
	int is,ie;
	if(YuusenTrack>=0){
		p = info.tdata[YuusenTrack].note_list;
		while(p != NULL && !(p->x <= x && x < (p->x+p->length)))p = p->to;
		if(p != NULL){
			//hit!!
			if((p->x <= x && x < (p->x+p->length)) &&  p->y != KEYDUMMY){
				*lStartx = p->x;
				*lLastx  = p->x + p->length - 1;
				return YuusenTrack;
			}
		}
	}
	if(track<MAXDRAM){
		is = 0; ie = MAXDRAM;
	}else{
		is  =MAXDRAM; ie = MAXTRACK;
	}
	for(i=ie-1;i>=is;i--){
		if(info.tdata[i].note_list != NULL){
			p = info.tdata[i].note_list;
			//while(p != NULL && p->x < x)p = p->to;
			while(p != NULL && !(p->x <= x && x < (p->x+p->length)))p = p->to;
			if(p != NULL){
				//hit!!
				if((p->x <= x && x < (p->x+p->length)) &&  p->y == y){
					*lStartx = p->x;
					*lLastx  = p->x + p->length - 1;
					return i;
				}
			}
		}
	}
	return -1;
}

//弅彫昞帵帪偺専嶕丅
int OrgData::SearchNoteC(long x,unsigned char y, long xWidth, long xMod)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	int i;

	for(p = info.tdata[track].note_list; p != NULL ; p = p->to){
		//if(p != NULL && p->y == y){	// 2014.05.28 D
		if(p != NULL){	// 2014.05.28 A
			for(i = 0; i <= xWidth; i++){
				if( p->x == x - i && x < (p->x + p->length) ){
					return i;
				}
			}
			if(p->x > x + xWidth)break;
		}
	}

	return -1;
}

//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//埲壓偼壒晞乮僷儞乯偺攝抲丄嶍彍
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//壒晞傪攝抲(嵍僋儕僢僋偺張棟)
BOOL OrgData::SetPan(long x,unsigned char y)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	//弶壒晞側傜儕僗僩偵搊榐
	if(info.tdata[track].note_list == NULL){
		info.tdata[track].note_list = note;
		note->from = NULL;
		note->to = NULL;
		note->length = 1;
		note->pan = y;
		note->volume = VOLDUMMY;
		note->x = x;
		note->y = KEYDUMMY;
		return TRUE;
	}
	//摢偐傜専嶕
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	//憓擖
	if(p->x > x){
		note->to = p;
		note->from = p->from;
		if(p->from == NULL){
			info.tdata[track].note_list = note;//愭摢
		}
		else p->from->to = note;//偠傖側偗傟偽慜偺傗偮偺師偵
		p->from = note;
		note->length = 1;
		note->pan = y;
		note->volume = 255;
		note->x = x;
		note->y = KEYDUMMY;
	}
	//僷儔儊乕僞曄峏
	else if(p->x == x){
		p->pan = y;//倄曄峏
	}
	//嵟屻旜捛壛
	else if(p->to == NULL){
		note->from = p;
		p->to = note;
		note->to = NULL;
		note->length = 1;
		note->pan = y;
		note->volume = VOLDUMMY;
		note->x = x;
		note->y = KEYDUMMY;
	}
	return TRUE;
}
BOOL OrgData::SetPan2(long x,unsigned char y)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	//弶壒晞側傜儕僗僩偵搊榐
	if(info.tdata[track].note_list == NULL){
		return FALSE;
	}
	//摢偐傜専嶕
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	//憓擖
	if(p->x == x){
		if(p->pan != y){ // 2010.08.14 A
			p->pan = y;//倄曄峏
		}else{
			return FALSE;	// 2010.08.14 A
		}
	}
	return TRUE;
}
//壒晞偺僇僢僩(塃僋儕僢僋偺張棟)
BOOL OrgData::CutPan(long x,unsigned char y)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	if(info.tdata[track].note_list == NULL)return FALSE;
	p = info.tdata[track].note_list;
	while(p != NULL && p->x < x)p = p->to;
	if(p == NULL)return FALSE;
	//僷儔儊乕僞曄峏
	if(p->x == x){
		p->length = 0;
		if(p->from == NULL)info.tdata[track].note_list = p->to;
		else p->from->to = p->to;
		if(p->to == NULL);
		else p->to->from = p->from;
	}
	return TRUE;
}
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//埲壓偼壒晞乮儃儕儏乕儉乯偺攝抲丄嶍彍
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
//壒晞傪攝抲(嵍僋儕僢僋偺張棟)
BOOL OrgData::SetVolume(long x,unsigned char y)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	//弶壒晞側傜儕僗僩偵搊榐
	if(info.tdata[track].note_list == NULL){
		info.tdata[track].note_list = note;
		note->from = NULL;
		note->to = NULL;
		note->length = 1;
		note->pan = PANDUMMY;
		note->volume = y;
		note->x = x;
		note->y = KEYDUMMY;
		return TRUE;
	}
	//摢偐傜専嶕
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	//憓擖
	if(p->x > x){
		note->to = p;
		note->from = p->from;
		if(p->from == NULL){
			info.tdata[track].note_list = note;//愭摢
		}
		else p->from->to = note;//偠傖側偗傟偽慜偺傗偮偺師偵
		p->from = note;
		note->length = 1;
		note->pan = PANDUMMY;
		note->volume = y;
		note->x = x;
		note->y = KEYDUMMY;
//		if(note->from != NULL && note->x < note->from->x + note->from->length)
//			note->length = (unsigned char)(note->from->length - (note->x - note->from->x));
	}
	//僷儔儊乕僞曄峏
	else if(p->x == x){
		if(p->volume != y){	// 2010.08.14 A
			p->volume = y;//倄曄峏
		}else{
			return FALSE;
		}
	}
	//嵟屻旜捛壛
	else if(p->to == NULL){
		note->from = p;
		p->to = note;
		note->to = NULL;
		note->length = 1;
		note->pan = PANDUMMY;
		note->volume = y;
		note->x = x;
		note->y = KEYDUMMY;
//		if(note->from != NULL && note->x < note->from->x + note->from->length)
//			note->length = (unsigned char)(note->from->length - (note->x - note->from->x));
	}
	return TRUE;
}
//
BOOL OrgData::SetVolume2(long x,unsigned char y,long fade)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	unsigned char lastlength = 1;
	unsigned char vv;
	double dv;
	long lastx = 0;
	int i;
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	//弶壒晞側傜儕僗僩偵搊榐
	if(info.tdata[track].note_list == NULL){
		info.tdata[track].note_list = note;
		note->from = NULL;
		note->to = NULL;
		note->length = 1;
		note->pan = PANDUMMY;
		note->volume = y;
		note->x = x;
		note->y = KEYDUMMY;
		return TRUE;
	}
	//摢偐傜専嶕
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		if(p->y != KEYDUMMY){
			lastx = p->x;
			lastlength = p->length;
		}
		p = p->to;
	}
	//憓擖
	if(p->x > x){
		note->to = p;
		note->from = p->from;
		if(p->from == NULL){
			info.tdata[track].note_list = note;//愭摢
		}
		else p->from->to = note;//偠傖側偗傟偽慜偺傗偮偺師偵
		p->from = note;
		note->length = 1;
		note->pan = PANDUMMY;
		switch(fade){
		case 0: //捈慄揑尭悐
			dv = (double)(p->x - lastx)/(double)lastlength;
			dv = 200 - dv * 200;
			if(dv < 0)dv = 0; else if(dv > 255)dv = 255;
			vv = (unsigned char)dv;
			break;
		case 1: //偡偖偵尭悐
			dv = 200.0f;
			for(i=0;i<(p->x - lastx);i++){
				dv = dv / 2.0f;
			}
			if(dv < 0)dv = 0; else if(dv > 255)dv = 255;
			vv = (unsigned char)dv;
			break;
		case 2: //備偭偔傝偲尭悐
		case 3: //備偭偔傝偲尭悐(價僽儔乕僩乯
			dv = sqrt(sqrt(sqrt(((double)lastlength-(double)p->x)/(double)lastlength))) * 200;
			i = (p->x - lastx) % 2;
			if(i==1 && fade==3)dv = dv - 10.0;
			if(dv < 0)dv = 0; else if(dv > 255)dv = 255;
			vv = (unsigned char)dv;
			break;
		}
		note->volume = vv;
		note->x = x;
		note->y = KEYDUMMY;
//		if(note->from != NULL && note->x < note->from->x + note->from->length)
//			note->length = (unsigned char)(note->from->length - (note->x - note->from->x));
	}
	//僷儔儊乕僞曄峏
	else if(p->x == x){
		if(p->volume != y){	// 2010.08.14 A
			p->volume = y;//倄曄峏
		}else{
			return FALSE;
		}
	}
	//嵟屻旜捛壛
	else if(p->to == NULL){
		note->from = p;
		p->to = note;
		note->to = NULL;
		note->length = 1;
		note->pan = PANDUMMY;
		note->volume = y;
		note->x = x;
		note->y = KEYDUMMY;
//		if(note->from != NULL && note->x < note->from->x + note->from->length)
//			note->length = (unsigned char)(note->from->length - (note->x - note->from->x));
	}
	return TRUE;
}
//壒晞偺僇僢僩(塃僋儕僢僋偺張棟)
BOOL OrgData::CutVolume(long x,unsigned char y)
{
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//摢偐傜専嶕
	if(info.tdata[track].note_list == NULL)return FALSE;
	p = info.tdata[track].note_list;
	while(p != NULL && p->x < x)p = p->to;
	if(p == NULL)return FALSE;
	//僷儔儊乕僞曄峏
	if(p->x == x){
		p->length = 0;
		if(p->from == NULL)info.tdata[track].note_list = p->to;
		else p->from->to = p->to;
		if(p->to == NULL);
		else p->to->from = p->from;
	}else{
		return FALSE;	// 2010.08.14 A
	}
	return TRUE;
}
//仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭仭
void OrgData::InitOrgData(void)
{
//	MUSICINFO mi;
	track = 0;
	info.alloc_note = ALLOCNOTE;//偲傝偁偊偢10000屄妋曐
	info.dot = 4;
	info.line = 4;
	info.wait = 128;
	info.repeat_x = info.dot * info.line * 0;
	info.end_x = info.dot * info.line * 100; // updated
	int i;
	for(i = 0; i < MAXTRACK; i++){
		info.tdata[i].freq = 1000;
		info.tdata[i].wave_no = 0;
		info.tdata[i].pipi = 0;
		def_pan[i] = DEFPAN;
		def_volume[i] = DEFVOLUME;
	}
	SetMusicInfo(&info,SETALL);

	// 埲壓 2014.05.07 捛壛
	for(i=0; i<MAXMELODY; i++){
		info.tdata[i].wave_no = i*11;
		MakeOrganyaWave(i, info.tdata[i].wave_no, info.tdata[i].pipi);
	}
	info.tdata[ 8].wave_no = 0;
	info.tdata[ 9].wave_no = 2;
	info.tdata[10].wave_no = 5;
	info.tdata[11].wave_no = 6;
	info.tdata[12].wave_no = 4;
	info.tdata[13].wave_no = 8;
	info.tdata[14].wave_no = 0;
	info.tdata[15].wave_no = 0;
	for(i = MAXMELODY; i < MAXTRACK; i++){
		InitDramObject(info.tdata[i].wave_no,i-MAXMELODY);
	}
}
void OrgData::GetNoteUsed(long *use,long*left,char track)
{
	NOTELIST *np;
	long u,l;
	int i;
	u = 0;
	l = 0;
	np = info.tdata[track].note_p;
	for(i = 0; i < ALLOCNOTE; i++){
		if(np->length)u++;
		else l++;
		np++;
	}
	*use = u;
	*left = l;
}
OrgData::OrgData()
{
	int i;
	for(i = 0; i < MAXTRACK; i++){
		info.tdata[i].note_list = NULL;
		info.tdata[i].note_p = NULL;
	}
	CurrentUndoCursor = 0;
	MinimumUndoCursor = 0;
	MaximumUndoCursor = 0;
	RedoEnable = false;
	UndoEnable = false;
	//noteon = new unsigned char[65536];
}
OrgData::~OrgData() //僨僗僩儔僋僞
{
	//delete [] noteon;
}



int OrgData::SetUndoData()
{
	int j,cc;
	cc = (CurrentUndoCursor % 32);
	memcpy(&ud_tdata[cc] , &info, sizeof(MUSICINFO));
	for(j=0;j<16;j++){
		memcpy(&ud_note[cc][j] , info.tdata[j].note_p , sizeof(NOTELIST)*4096);
	}
	CurrentUndoCursor++;
	MaximumUndoCursor = CurrentUndoCursor;
	if(CurrentUndoCursor - 32+1 > MinimumUndoCursor)MinimumUndoCursor = CurrentUndoCursor - 32+1;
	
	if(!UndoEnable){
		UndoEnable = true;
		return 1;
	}
	if(RedoEnable){
		RedoEnable = false;
		return 2;
	}
	return 0;
}

void OrgData::ClearUndoData()
{
	CurrentUndoCursor = 0;
	MinimumUndoCursor = 0;
	MaximumUndoCursor = 0;
	RedoEnable = false;
	UndoEnable = false;
}

//傾儞僪僁偐傜暅婣
int OrgData::ReplaceFromUndoData()
{
	if(!UndoEnable)return 1;
	if(MaximumUndoCursor == CurrentUndoCursor){
		SetUndoData();
		CurrentUndoCursor--;
	}
	int j,cc,r;
	MUSICINFO mi;
	CurrentUndoCursor--;
	r=0;
	RedoEnable = true;
	if(CurrentUndoCursor <= MinimumUndoCursor){
		CurrentUndoCursor = MinimumUndoCursor;
		r=2; //傕偆偙傟埲忋尦偵栠偣側偄偧丅
		UndoEnable = false;
	}
	cc = (CurrentUndoCursor % 32);
	memcpy(&info, &ud_tdata[cc] ,  sizeof(MUSICINFO));
	for(j=0;j<16;j++){
		memcpy(info.tdata[j].note_p , &ud_note[cc][j] ,  sizeof(NOTELIST)*4096);
	}
	org_data.GetMusicInfo(&mi);
	for (j = 0; j < MAXMELODY; j++) {
		MakeOrganyaWave(j, mi.tdata[j].wave_no, mi.tdata[j].pipi);
	}
	for (j = MAXMELODY; j < MAXTRACK; j++) {
		InitDramObject(mi.tdata[j].wave_no, j - MAXMELODY);
	}
	return r;
}

//儕僪僁
int OrgData::ReplaceFromRedoData()
{
	if(!RedoEnable)return 1;
	int j,cc,r;
	MUSICINFO mi;
	CurrentUndoCursor++;
	r=0;
	UndoEnable = true;
	if(CurrentUndoCursor >= MaximumUndoCursor-1){
		CurrentUndoCursor = MaximumUndoCursor-1;
		r=2; //傕偆偙傟埲忋尦偵栠偣側偄偧丅
		RedoEnable = false;
	}
	cc = (CurrentUndoCursor % 32);
	memcpy(&info, &ud_tdata[cc] ,  sizeof(MUSICINFO));
	for(j=0;j<16;j++){
		memcpy(info.tdata[j].note_p , &ud_note[cc][j] ,  sizeof(NOTELIST)*4096);
	}
	org_data.GetMusicInfo(&mi);
	for (j = 0; j < MAXMELODY; j++) {
		MakeOrganyaWave(j, mi.tdata[j].wave_no, mi.tdata[j].pipi);
	}
	for (j = MAXMELODY; j < MAXTRACK; j++) {
		InitDramObject(mi.tdata[j].wave_no, j - MAXMELODY);
	}
	return r;

}


int OrgData::ResetLastUndo()
{
	int r = 0;
	CurrentUndoCursor--;
	MaximumUndoCursor = CurrentUndoCursor;
	if(CurrentUndoCursor <= MinimumUndoCursor){
		CurrentUndoCursor = MinimumUndoCursor;
		r=2; //傕偆偙傟埲忋尦偵栠偣側偄偧丅
		UndoEnable = false;
	}
	return r;

}

//嵟屻偺敾掕傪偟側偄
BOOL OrgData::SetNote_onlyLength(long x, long Length)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	if(info.tdata[track].note_list == NULL)return FALSE;
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	if(p->x == x){
		if(Length < MAXNOTELENGTH && Length != 0){
			if(p->length != (char)Length){ //A 2010.08.14 
				p->length = (char)Length;
			}else{
				return FALSE; //A 2010.08.14 
			}
		}
	}
	return TRUE ;
}

BOOL OrgData::SetNote_afterSetLength(long x)
{
	NOTELIST *note;//惗惉NOTE
	NOTELIST *p;//儕僗僩傪巜偡億僀儞僞乕
	NOTELIST *cut_p;//偦傟傪抲偔帠偵傛偭偰僇僢僩偡傋偒壒晞
	//枹巊梡NOTE傪専嶕
	if((note = SearchNote(info.tdata[track].note_p)) == NULL)return FALSE;
	if(info.tdata[track].note_list == NULL)return FALSE;
	p = info.tdata[track].note_list;
	while(p->x < x && p->to != NULL){
		p = p->to;
	}
	if(p->x == x){
		cut_p = p->to;
		while(cut_p != NULL && cut_p->y == KEYDUMMY){
			cut_p = cut_p->to;
		}
		if(cut_p != NULL && p->length + p->x > cut_p->x){//怢偽偟偡偓傞偲師偺傪堸傒崬傓
			if(cut_p->to == NULL){//偦偄偮偑嵟屻旜側傜
				cut_p->from->to = NULL;//堦偮慜偺恖偵嵟屻旜傪擟偣傞丅
			}
			else{
				cut_p->from->to = cut_p->to;
				cut_p->to->from = cut_p->from;
			}
			cut_p->length = 0;
			cut_p->from = NULL;
		}

	}
	return TRUE;

}

