/* BGM、効果音、PCMボイス再生 */
/* by 松原 


・このソフトウェアの著作権・所有権はWonderWitch.Comに帰属します。
・このソフトウェアを使用することによって生じたあらゆる損害に対しては責任を負いかねます。
・このソフトウェアを営利目的に利用することを禁止します。
*/

#include "il_sound.h"
#include "sys/bios.h"
#include "stdio.h"
#include "bsvilwp.h" /*===== ← ☆ ヘッダーファイルインクルード =====*/

#define BGM_MAX 400	/*バッファサイズ*/
#define SE_MAX 100
#define VOICE_MAX 11000

static	BYTE	bBgm[BGM_MAX];
static	BYTE	bSe[SE_MAX];
static	BYTE	bVoice[VOICE_MAX];

SoundIL		soundIL;

/*-----------PCMボイスの読み込み*/
void subLoadVoice(char *fname ,char *buf) {
	int    iFd;

	if((iFd = open(fname, FMODE_R, 0)) >= 0) {
		read(iFd ,buf ,VOICE_MAX );
		close(iFd);
	}else{
		text_screen_init();
		text_put_string(1 ,1 ,fname);
		text_put_string(1,2 ,"file open error!");
		key_wait();
	}
}

void main(void)
{
	int iKey;
	int iFlg;

	if ( open_sound_il( &soundIL ) != E_FS_SUCCESS ) {
		text_screen_init();
		text_put_string(1,2 ,"sound.il open error!");
		key_wait();
		return;
	}

    /*--------------- IL 初期化 */
    if (ilibIL->_open("@bmpsaver", (IL far *)&bsvIL) != E_FS_SUCCESS) {
        /* error */
    }else{
		bs_set_target( KH_BS_SCREEN2 );
	}

	sounddrv_init();
	sound_open();

	text_screen_init();
	text_put_string(1 ,1 ,"DOWN:BGM  (#1&#4)START/STOP");
	text_put_string(1 ,2 ,"   B:VOICE(#2)PLAY");
	text_put_string(1 ,3 ,"   A:SE   (#3)PLAY");
	text_put_string(1 ,6 ,"START:END");


/*Sound.ILの使い方参照"C:\wwitch\doc\appendix\il_sound.html"*/
/*MMLの使い方参照"C:\wwitch\doc\appendix\mml.html"*/

	parse_mml( bBgm, "#1 V10O3G1A2G1F2B2C1A1B2B3 #4 V10O2G1F1G23B2C1D2D1", 0 );

	parse_mml( bSe, "#3 O4 @E5 E16C4", 1 );

	subLoadVoice("voice.fr" ,bVoice);

	bgm_play( bBgm, PLAY_LOOP );	/*BGM演奏*/
	iFlg = 1;
	for(;;) {
	    iKey = key_wait();
		if(iKey == KEY_START)
			break;
		
		switch(iKey) {
		case KEY_Y1:
			bs_save_screen_xmodem("sample.bmp",0,0,224,144);
			break;

		case KEY_DOWN1:
			iFlg ^= 1;
			if(iFlg != 0)
				bgm_play( bBgm, PLAY_LOOP);	/*BGM演奏*/
			else
				bgm_stop();		/*BGMの停止*/
			break;

		case KEY_A:
			se_play(bSe, 0);	/*効果音の演奏(ポインタ,効果音番号)*/
			break;

		case KEY_B:
			voice_play (bVoice, 0);	/*PCMボイス再生*/
			break;

		}
	}
	sound_close();
	sounddrv_release();	/* ドライバ用ワークを解放 */
}



