 ///include.h

#include <xtl.h>
#include <process.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <xgraphics.h>
#include <xfont.h>
#include <fcntl.h>
#include <memory.h>
#include <malloc.h>
#include <math.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>

#define far
#define interrupt __stdcall

#define sleep Sleep
#define random(x) (rand()%x)
//#define sound(x)
//#define nosound()
#define textcolor(x) 
#define clrscr()
#define farmalloc(x) malloc(x)
#define _fmemset memset
#define farfree(x) free(x)
#define delay sleep

//#define kbhit() ;
//#define getch() ;

#include "sound.h"

int kbhit() { return 1; }
int getch() { return 1; }

void reboot()
{
	LD_LAUNCH_DASHBOARD LaunchData = { XLD_LAUNCH_DASHBOARD_MAIN_MENU };
	XLaunchNewImage( NULL, (LAUNCH_DATA*) &LaunchData );
}

void nosound();

void sound( unsigned freq )
{
	const double R = 8000;
	const double C = (double) freq;
	const double F = R / 256;
	const double V = 127;

	/* stop the buffer if it's still playing */
//	nosound();

	/* generate tone */
	for( int i = 0; i < sndbuf_size; i++ )
	{
		sndbuf[i] = (byte) (sin(i*2*(6.2831/2.0)/R*C)+1)*V;
	}

//	SetEvent( play_event );

	/* now set the buffer data in the ds buffer, and play it */
	/* FIX: this only needs to be done once so long as the data buffer exists */
//	dsb->SetBufferData( sndbuf, sndbuf_size );

	DWORD status;
	dsb->GetStatus( &status );
	if( status != DSBSTATUS_PLAYING )
		dsb->Play( 0, 0, 0 );
}

void nosound()
{
	/* stop the buffer */

//	SetEvent( stop_event );

	DWORD status;
	
#if 0
	HRESULT hr = dsb->GetStatus( &status );

	if( status == DSBSTATUS_PLAYING )
		dsb->Stop();
#else
	dsb->GetStatus( &status );

	if( status == DSBSTATUS_PLAYING )
	{
		dsb->StopEx( 0, DSBSTOPEX_IMMEDIATE );
		do
		{
			dsb->GetStatus( &status );
		} while( status == DSBSTATUS_PLAYING );
	}
#endif
}
