/*
			MOV SOUND DATA SAVE

			Hiroshi TODA
			1991 7/28
*/

#include <stdio.h>
#include <stdlib.h>
#include <egb.h>
#include <mos.h>
#include <snd.h>
#include "video.h"

/*#define C_WT 0x7fff*/		/* カーソル color */
/*#define C_PL 24311*/		/* 初期プレーン color */
/*#define C_FL 0x303030*/		/* files back color */

#define C_COLOR 15		/* Command color palette */
#define W_COLOR 8		/* Window color palette */
#define B_COLOR 7		/* Back color palette */
#define E_COLOR 10		/* ERROR color palette */
#define OK_COLOR 12		/* OK color palette */

extern int windget();
extern int gprint( char *, int x, int y );
extern int gprint2( char *, int x, int y );
extern int mouse( int mpara[], int, int, int );
extern int windput();
extern int error_check( int );
extern int look_files( char *path, char *fullname );
extern int itochar( int var, int n, char *dsp );

extern int sw,cx,cy;	/* MOUSE data */

extern int mpara[];		/* mouse para */
extern char para[];		/* PARAM */
extern char gwork[];	/* graph work */
extern char ework[];	/* EGB work */
extern char bbuf[];	/* BACK-UP buf. */

/* mov sound save */

mov_sound_save( namemov )
char namemov[];
{
	FILE *fp;
	FILE *fps;
	char head[256];
	char path[100];		/* path */
	char fullname[80];	/* file name */
	char name[13];
	char fmbname[13], pmbname[13];
	char pcmname[8][13];
	int ch[14], data[14];
	int temp,min,max,i,j,k,k1,k2,wt;
	int point[][2] = {	{  48, 447 },
				{ 192, 447 },
				{ 336, 447 },
				{ 480, 447 },
				{  48, 479 },
				{ 192, 479 },
				{ 336, 479 },
				{ 480, 479 }
			 };
	char dsp0[] = "<<SOUND DATA>>   WRITE                                                      EXIT";
	char dsp1[] = "FMB <<        >> ch0=    ch1=    ch2=    ch3=    ch4=    ch5=                   ";
	char dsp2[] = "PMB <<        >> ch64=   ch65=   ch66=   ch67=   ch68=   ch69=   ch70=   ch71=  ";
	char dsp3[] = "PCM0<<        >>  PCM1<<        >>  PCM2<<        >>  PCM3<<        >>";
	char dsp4[] = "PCM4<<        >>  PCM5<<        >>  PCM6<<        >>  PCM7<<        >>";

	windput();
		/* MOV DATA head read */
	if( ( fp = fopen( namemov, "rb" ) ) == NULL )return 1;
	temp = fread( head, 1, 256, fp );
	fclose( fp );
	if( temp < 256 )return 58;
	if( DWORD(head+0) != 0x32564f4d )return 58;
	if( DWORD(head+4) != 16 )return 58;
	if( DWORD(head+16) != 320 )return 58;
	if( DWORD(head+20) != 240 )return 58;
		/* CHANEL read */
	for( i=0 ; i<6 ; i++ )ch[i] = BYTE( head + 48 + i );
	for( i=0 ; i<8 ; i++ )ch[ 6 + i ] = BYTE( head + 64 + i );
		/* パネル表示 */
snd01:	EGB_displayPage(ework,1,3);		/* mode3が前側 */
	EGB_writePage(ework,1);		/* mode3に書き込み */
	EGB_clearScreen(ework);
	EGB_writeMode(ework,0);
	EGB_color(ework,0,W_COLOR);
	EGB_color(ework,2,W_COLOR);
	EGB_paintMode(ework,0x22);
	EGB_writeMode(ework,0);
	WORD(para+0) = 0;
	WORD(para+2) = 336;
	WORD(para+4) = 639;
	WORD(para+6) = 479;
	EGB_rectangle(ework,para);
	EGB_color(ework,0,C_COLOR);
	gprint( dsp0, 0, 351 );
	gprint( dsp1, 0, 383 );
	gprint( dsp2, 0, 415 );
	gprint( dsp3, 0, 447 );
	gprint( dsp4, 0, 479 );
		/* FMB data name */
	for( i=0 ; i<8 ; i++ ){
		fmbname[i] = head[ 40 + i ];
		if( fmbname[i] == (char)0 )break;
	}
	if( i ){
		fmbname[i] = (char)0;
		gprint( fmbname, 48, 383 );
		DWORD( fmbname+i )='.'+0x100*'F'+0x10000*'M'+0x1000000*'B';
		fmbname[i+4]=(char)0;
	}
		/* PMB data name */
	for( i=0 ; i<8 ; i++ ){
		pmbname[i] = head[ 56 + i ];
		if( pmbname[i] == (char)0 )break;
	}
	if( i ){
		pmbname[i] = (char)0;
		gprint( pmbname, 48, 415 );
		DWORD( pmbname+i )='.'+0x100*'P'+0x10000*'M'+0x1000000*'B';
		pmbname[i+4]=(char)0;
	}
		/* PCM data name */
	for( j=0 ; j<8 ; j++ ){
		for( i=0 ; i<8 ; i++ ){
			pcmname[j][i] = head[ 72 + 8*j + i ];
			if( pcmname[j][i] == (char)0 )break;
		}
		if( i ){
			pcmname[j][i] = (char)0;
			gprint( *(pcmname+j), point[j][0], point[j][1] );
			DWORD( *(pcmname+j)+i )
			= '.' + 0x100*'S' + 0x10000*'N' + 0x1000000*'D';
			pcmname[j][i+4]=(char)0;
		}
	}
	for( i = 0 ; i < 14 ; i++ )data[i] = -1;
	mov_sound_save_dsp( ch, data );
snd02:	MOS_disp(1);
	mouse(mpara,0,0,1);
	if( cy < 335 )goto snd02;
	if( cy < 351 ){
		if( cx < 128 )goto snd02;
		if( cx < 184 ){		/* write */
			j = 10;
			goto snd05;
		}
		if( cx < 600 )goto snd02;
		if( cx < 640 ){		/* exit */
			j = 11;
			goto snd05;
		}
		goto snd02;
	}
	if( cy < 367 )goto snd02;
	if( cy < 383 ){
		if( cx < 132 ){		/* fmb file name */
			j = 0;
			goto snd03;
		}
		if( cx < 196 ){
			i = 0; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 260 ){
			i = 1; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 324 ){
			i = 2; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 388 ){
			i = 3; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 452 ){
			i = 4; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 516 ){
			i = 5; min = 0; max = 127;
			goto snd07;
		}
		goto snd02;
	}
	if( cy < 399 )goto snd02;
	if( cy < 415 ){
		if( cx < 132 ){		/* fmb file name */
			j = 1;
			goto snd03;
		}
		if( cx < 196 ){
			i = 6; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 260 ){
			i = 7; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 324 ){
			i = 8; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 388 ){
			i = 9; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 452 ){
			i = 10; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 516 ){
			i = 11; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 580 ){
			i = 12; min = 0; max = 31;
			goto snd07;
		}
		if( cx < 640 ){
			i = 13; min = 0; max = 31;
			goto snd07;
		}
		goto snd02;
	}
	if( cy < 431 )goto snd02;
	if( cy < 447 ){
		if( cx < 136 ){		/* pcm file name */
			j = 2;
			goto snd03;
		}
		if( cx < 280 ){		/* pcm file name */
			j = 3;
			goto snd03;
		}
		if( cx < 424 ){		/* pcm file name */
			j = 4;
			goto snd03;
		}
		if( cx < 568 ){		/* pcm file name */
			j = 5;
			goto snd03;
		}
		goto snd02;
	}
	if( cy < 463 )goto snd02;
	if( cy < 479 ){
		if( cx < 136 ){		/* pcm file name */
			j = 6;
			goto snd03;
		}
		if( cx < 280 ){		/* pcm file name */
			j = 7;
			goto snd03;
		}
		if( cx < 424 ){		/* pcm file name */
			j = 8;
			goto snd03;
		}
		if( cx < 568 ){		/* pcm file name */
			j = 9;
			goto snd03;
		}
		goto snd02;
	}
	goto snd02;
snd03:	mouse(mpara,0,0,0);
	if( sw > 1 )goto snd02;
	MOS_disp(0);
	windput();
	for( k=0 ; k<80 ; k++ ){
		path[k] = namemov[k];
		if( path[k] == (char)0 )break;
	}
	for( i=k ; i>=0 ; i-- ){			/* path name 抽出 */
		if( path[i] == '\\' || path[i] == ':' ){
			path[i+1] = '*';
			path[i+2] = '.';
			if(j == 0)DWORD( path+i+3 )='F'+0x100*'M'+0x10000*'B';
			if(j == 1)DWORD( path+i+3 )='P'+0x100*'M'+0x10000*'B';
			if(j >= 2)DWORD( path+i+3 )='S'+0x100*'N'+0x10000*'D';
			break;
		}
	}
	for( i=0 ; i<8 ; i++ )name[i]=(char)0;
	if( look_files( path, fullname ) == 0 ){
		k1 = -1;
		for( i=0 ; i<80 ; i++ ){
			if(fullname[i] == '\\' || fullname[i] == ':')k1 = i;
			if(fullname[i] == (char)0 || fullname[i] == '.')break;
		}
		k = 0; k1++; k2 = i;
		if( k2-k1 > 8 )goto snd04;
		for( i=k1 ; i<k2 ; i++ )name[k++] = fullname[i];
	}
snd04:	if( j == 0 )for( i=0 ; i<8 ; i++ )head[40+i] = name[i];
	if( j == 1 )for( i=0 ; i<8 ; i++ )head[56+i] = name[i];
	if( j > 1 && j < 10 ){
		k = j - 2;
		for( i=0 ; i<8 ; i++ )head[72+8*k+i] = name[i];
	}
	goto snd01;

snd05:	mouse(mpara,0,0,0);
	if( sw > 1 )goto snd02;
	MOS_disp(0);
	windput();
	switch( j ){
	   case 10: for( i=0 ; i<6 ; i++ ){ /* write protect の場合6回必要 */ 
			if(
			    ( fps = fopen( namemov, "r+b" ) ) != NULL
			)goto snd06;
		    }
		    return 2;
	     snd06: for( i=0 ; i<6 ; i++ )BYTE( head + 48 + i ) = ch[i];
		    for( i=0 ; i<8 ; i++ )BYTE( head + 64 + i ) = ch[ 6 + i ];
		    fseek( fps, 0, SEEK_SET );
		    fwrite( head, 1, 256, fps );
		    temp = ferror( fps );
		    fclose( fps );
		    if( temp ){
			/*remove( namemov );*/		/* error */
			return 2;
		    }
		    return
		    mov_sound_page_save(namemov,fmbname,pmbname,pcmname,ch);
	   case 11: return 0;
	}
	goto snd01;

snd07:	wt = 125000;
snd08:	if( sw == 1 )ch[i] = ch[i] + 1;
	if( sw == 2 )ch[i] = ch[i] - 1;
	if( ch[i] < min )ch[i] = max;
	if( ch[i] > max )ch[i] = min;
	mov_sound_save_dsp( ch, data );
	for( k = 0 ; k < wt ; k++ );
	wt = 10000;
	MOS_rdpos(&sw,&cx,&cy);
	if( sw > 0 )goto snd08;
	goto snd02;
}

mov_sound_save_dsp(ch,data)
int ch[], data[];
{
	int i, x, y;
	int point[][3] = {	{ 168,383,3 },
				{ 232,383,3 },
				{ 296,383,3 },
				{ 360,383,3 },
				{ 424,383,3 },
				{ 488,383,3 },

				{ 176,415,2 },
				{ 240,415,2 },
				{ 304,415,2 },
				{ 368,415,2 },
				{ 432,415,2 },
				{ 496,415,2 },
				{ 560,415,2 },
				{ 624,415,2 }
			  };
	char dsp[10];

	for( i = 0 ; i < 14 ; i++ ){
		if( data[i] != ch[i] ){
			MOS_disp(0);
			itochar( ch[i], point[i][2], dsp );
			EGB_color(ework,0,C_COLOR);
			x = point[i][0];  y = point[i][1];
			gprint2( dsp, x, y );
			data[i] = ch[i];
		}
	}
	return 0;
}

/* mov sound page save */

mov_sound_page_save( namemov, fmbname, pmbname, pcmname, ch )
char namemov[], fmbname[], pmbname[], pcmname[][13];
int ch[];
{
	FILE *fp;
	FILE *fps;

	char head[256];		/* head */
	char bank[8];
	char path[100];
	char p[80];
	int var[16];
	int data[16];
	int page,n,total,size,top,temp,min,max,i,j,k,wt;
	char dsp[10];
	char dsp0[] = "<< Page =     >>  WRITE  NEXT-PAGE  DISPLAY                                 EXIT";
	char dsp1[] = " FMB/PMB1 << switch=   ch=    note=     velo=     TEST >>    OFFSET(x=    y=   )";
	char dsp2[] = " FMB/PMB2 << switch=   ch=    note=     velo=     TEST >>    WAIT=     LOOP=   ";
	char dsp3[] = "      PCM << switch=   ch=    note=     velo=     TEST >>";

	windput();
		/* fmb, pmb data load */
	make_path_name( path, namemov, fmbname );
	SND_fm_bank_load( path, bank );
	make_path_name( path, namemov, pmbname );
	SND_pcm_bank_load( path, bank );
	SND_elevol_mute( 0x03 ) ;
	for( i=0 ; i<6 ; i++ )SND_inst_change( i, ch[i] );
	for( i=0 ; i<8 ; i++ )SND_inst_change( 64+i, ch[6+i] );
		/* mov data load */
	for( i=0 ; i<6 ; i++ ){ /* write protect の場合6回必要 */ 
		if( ( fp = fopen( namemov, "r+b" ) ) != NULL )goto snd0A;
	}
	return 2;
snd0A:	fseek( fp, 0, SEEK_SET );
	if( fread( head, 1, 256, fp ) < 256 ){
		fclose( fp );
		return 58;
	}
	total = 256; n = 0; page = DWORD( head + 12 );
		/* mov data display */
	DWORD( p+0 ) = (unsigned int)bbuf;
	DWORD( p+4 ) = (unsigned int)bbuf+0x40000;
	DWORD( p+8 ) = 0;
	DWORD( p+12) = 0;
snd00:	if( fread( bbuf, 1, 32, fp ) < 32 ){
		fclose( fp );
		return 58;
	}
	top = total; total += 32; size = DWORD( bbuf + 4 );
	if( fread( bbuf+32, 1, size, fp ) < size ){
		fclose( fp );
		return 58;
	}
	if( (unsigned int)size > 500000 ){
		fclose( fp );
		return 58;
	}
	total += size;
	move32( p );
	EGB_clearScreen(ework);	/* page1を消さないとパネル表示の時見える */
	n++;
		/* read data from page head */
	for( i=0 ; i<12 ; i++ )var[i] = BYTE( bbuf+20+i );	/* sound */
	for( i=0 ; i<4 ; i++ )var[12+i] = WORD( bbuf+8+2*i );	/* etc. */
	if( var[15] & 0x8000 )var[15] = -1;
		/* パネル表示 */
snd01:	EGB_displayPage(ework,1,3);		/* mode3が前側 */
	EGB_writePage(ework,1);		/* mode3に書き込み */
	EGB_clearScreen(ework);
	EGB_writeMode(ework,0);
	EGB_color(ework,0,W_COLOR);
	EGB_color(ework,2,W_COLOR);
	EGB_paintMode(ework,0x22);
	EGB_writeMode(ework,0);
	WORD(para+0) = 0;
	WORD(para+2) = 367;
	WORD(para+4) = 639;
	WORD(para+6) = 479;
	EGB_rectangle(ework,para);
	EGB_color(ework,0,C_COLOR);
	gprint( dsp0, 0, 383 );
	gprint( dsp1, 0, 415 );
	gprint( dsp2, 0, 447 );
	gprint( dsp3, 0, 479 );
	itochar( n, 3, dsp );
	gprint( dsp, 80, 383 );
	for( i = 0 ; i < 16 ; i++ )data[i] = -2;
	mov_sound_page_save_dsp( var, data );
		/* command loop */
snd02:	MOS_disp(1);
	mouse(mpara,0,0,1);
	if( cy < 367 )goto snd02;
	if( cy < 383 ){
		if( cx < 136 )goto snd02;
		if( cx < 192 ){		/* write */
			j = 0;
			goto snd05;
		}
		if( cx < 280 ){		/* next page */
			j = 1;
			goto snd05;
		}
		if( cx < 352 ){		/* display */
			j = 2;
			goto snd05;
		}
		if( cx < 608 )goto snd02;
		if( cx < 640 ){		/* exit */
			j = 3;
			goto snd05;
		}
		goto snd02;
	}
	if( cy < 399 )goto snd02;
	if( cy < 415 ){
		if( cx < 96 )goto snd02;
		if( cx < 176 ){			/* fmb switch */
			i = 0; min = 0; max = 1;
			goto snd07;
		}
		if( cx < 232 ){
			i = 1; min = 0; max = 71;
			goto snd07;
		}
		if( cx < 312 ){
			i = 2; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 392 ){
			i = 3; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 440 ){		/* fmb sound test */
			j = 0;
			goto snd03;
		}
		if( cx < 540 )goto snd02;
		if( cx < 588 ){		/* x */
			i = 12; min = 0; max = 319;
			goto snd07;
		}
		if( cx < 640 ){		/* y */
			i = 13; min = 0; max = 239;
			goto snd07;
		}
		goto snd02;
	}
	if( cy < 431 )goto snd02;
	if( cy < 447 ){
		if( cx < 96 )goto snd02;
		if( cx < 176 ){			/* pmb switch */
			i = 4; min = 0; max = 1;
			goto snd07;
		}
		if( cx < 232 ){
			i = 5; min = 0; max = 71;
			goto snd07;
		}
		if( cx < 312 ){
			i = 6; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 392 ){
			i = 7; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 440 ){		/* pmb sound test */
			j = 1;
			goto snd03;
		}
		if( cx < 480 )goto snd02;
		if( cx < 564 ){		/* wait */
			i = 14; min = 0; max = 999;
			goto snd07;
		}
		if( cx < 640 ){		/* loop */
			i = 15; min = -1; max = 999;
			goto snd07;
		}
		goto snd02;
	}
	if( cy < 453 )goto snd02;
	if( cy < 479 ){
		if( cx < 96 )goto snd02;
		if( cx < 176 ){			/* pcm switch */
			i = 8; min = 0; max = 2;
			goto snd07;
		}
		if( cx < 232 ){
			i = 9; min = 0; max = 7;
			goto snd07;
		}
		if( cx < 312 ){
			i = 10; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 392 ){
			i = 11; min = 0; max = 127;
			goto snd07;
		}
		if( cx < 440 ){		/* pcm sound test */
			j = 2;
			goto snd03;
		}
		goto snd02;
	}
	goto snd02;
snd03:	if( sw > 1 )goto snd02;
	switch( j ){
		case 0:	SND_key_off( var[1] );
			if( !var[0] )break;
			SND_key_on( var[1], var[2], var[3] );
			while( sw )MOS_rdpos(&sw,&cx,&cy);
			SND_key_off( var[1] );
			break;
		case 1:	SND_key_off( var[5] );
			if( !var[4] )break;
			SND_key_on( var[5], var[6], var[7] );
			while( sw )MOS_rdpos(&sw,&cx,&cy);
			SND_key_off( var[5] );
			break;
		case 2:	mouse(mpara,0,0,0);
			if( pcmname[var[9]][0] == (char)0 )break;
			make_path_name( path, namemov, *(pcmname+var[9]) );
			if(
			  ( fps = fopen( path, "rb" ) ) == NULL
			){
				error_check(1);
				goto snd01;
			}
			if( fread( bbuf+32, 1, 32, fps ) < 32 ){
				fclose( fps );
				error_check(1);
				goto snd01;
			}
			temp = DWORD( bbuf+32+12 );
			if( temp > 0x40000-100 ){
				fclose( fps );
				error_check(7);
				goto snd01;
			}
			if( fread( bbuf+64, 1, temp, fps ) < temp ){
				fclose( fps );
				error_check(1);
				goto snd01;
			}
			fclose( fps );
			if( SND_pcm_mode_set( 1 ) ){
				SND_pcm_sound_delete( -1 );
				SND_pcm_mode_set( 1 );
			}
			SND_pcm_play_stop( 71 );
			if( !var[8] )break;
			if( var[8] == 1 ){
				SND_pcm_play( 71, var[10], var[11], bbuf+32 );
			}
			else SND_pcm_play2( 71, var[10], var[11], bbuf+32 );
			break;
	}
	goto snd02;
snd05:	mouse(mpara,0,0,0);
	if( sw > 1 )goto snd02;
	MOS_disp(0);
	windput();
	switch( j ){
		case 0:	for( i=0 ; i<12 ; i++ )BYTE( bbuf+20+i ) = var[i];
			for( i=0 ; i<4 ; i++ )WORD( bbuf+8+2*i ) = var[12+i];
			fseek( fp, top, SEEK_SET );
			fwrite( bbuf, 1, 32, fp );
			fseek( fp, total, SEEK_SET );
			goto snd09;
		case 1:	goto snd09;
		case 2:	mouse(mpara,0,0,0);
			break;
		case 3:	goto snd10;
	}
	goto snd01;

snd07:	wt = 125000;
snd08:	if( sw == 1 )var[i] = var[i] + 1;
	if( sw == 2 )var[i] = var[i] - 1;
	if( var[i] < min )var[i] = max;
	if( var[i] > max )var[i] = min;
	mov_sound_page_save_dsp( var, data );
	for( k = 0 ; k < wt ; k++ );
	wt = 10000;
	MOS_rdpos(&sw,&cx,&cy);
	if( sw > 0 )goto snd08;
	goto snd02;

snd09:	if( n < page )goto snd00;
	else goto snd0A;
snd10:	fclose( fp );
	return 0;
}

mov_sound_page_save_dsp(var,data)
int var[], data[];
{
	int i, x, y;
	int point[][3] = {	{ 160,415,1 },	/* sond1 */
				{ 208,415,2 },
				{ 280,415,3 },
				{ 360,415,3 },

				{ 160,447,1 },	/* sound2 */
				{ 208,447,2 },
				{ 280,447,3 },
				{ 360,447,3 },

				{ 160,479,1 },	/* pcm */
				{ 208,479,2 },
				{ 280,479,3 },
				{ 360,479,3 },

				{ 560,415,3 },	/* offset */
				{ 608,415,3 },
				{ 528,447,3 },	/* wait */
				{ 608,447,3 }	/* loop */
			  };
	char dsp[10];

	for( i = 0 ; i < 16 ; i++ ){
		if( data[i] != var[i] ){
			MOS_disp(0);
			itochar( var[i], point[i][2], dsp );
			EGB_color(ework,0,C_COLOR);
			x = point[i][0];  y = point[i][1];
			if( (i == 15) && (var[i] < 0) ){
				gprint2( "END", x, y );
			}
			else {
				gprint2( dsp, x, y );
			}
			data[i] = var[i];
		}
	}
	return 0;
}

/* path = fullname(path) + name 合成 */

make_path_name( path, fulname, name )
char path[], fulname[], name[];
{
	int i, k;

	for( k=0 ; k<80 ; k++ ){
		path[k] = fulname[k];
		if( path[k] == (char)0 )break;
	}
	for( i=k ; i>=0 ; i-- ){	/* path name 抽出 */
		if( path[i] == '\\' || path[i] == ':' )break;
	}
	for( k=0 ; k<13 ; k++ ){
		path[i+1+k] = name[k];
		if( name[k] == (char)0 )break;
	}
	return 0;
}

