/**** TEST16.C		Demonstrate all DIGPAK capable play modes.				*******/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#include "doscalls.h"
#include "loader.h"
#include "digplay.h"
#include "t16.h"

// Define memory allocation functions.  If using DOS memory allocation
// functions, provided through DOSCALLS, then set the conditional compilation
// 'DOSALLOC' to true.  If using C compiler library function memory allocation
// set 'DOSALLOC' to zero.

#define DOSALLOC 0
// Redirect memory allocation to either DOS memory allocate functions located
// in DOSCALLS or to C library far memory allocation functions.
unsigned char far * far memalloc(long int siz)
{
	#if DOSALLOC
		return(fmalloc(siz));  // DOS far memory allocation functions
	#else
		return(farmalloc(siz)); // C's far memory allocation functions.
	#endif
}

void far memfree(char far *memory)
{
	#if DOSALLOC
		ffree(memory);
	#else
		farfree(memory);
	#endif
}

void main(void)
{
	int cap;
	SNDSTRUC snd;
	long int siz;
	char far *music;
	char far *sound;
	char far *workspace;

	if ( !LoadDigPak("SOUNDRV.COM") )
	{
		printf("Unable to load SOUNDRV.COM\n");
		exit(1);
	}
	if ( !InitDigPak() )
	{
		UnLoadDigPak();
		printf("Unable to initialize digitized sound hardware.\n");
		exit(1);
	}
	sound = fload("TEST.SND", &siz);
	if ( sound )
	{
		cap = AudioCapabilities();
		workspace = memalloc(64000); //allocate lots of memory for workspace.
		if ( siz*4 > 64000L ) siz = 64000/4;
		printf("Press a key to play test sound effect in 8 bit mono mode.\n");
		printf("Press ESC to go to next step in the test suite.\n");

		farmov(workspace,sound,siz);
		snd.sound = workspace;
		snd.frequency = 11000;
		snd.sndlen = siz;
		MassageAudio(&snd);

		do
		{
			DigPlay2(&snd);
		} while ( getkey() != 27 );
		if ( cap & STEREOPLAY )
		{
			printf("Press a key to play test sound effect in 8 bit stereo mode.\n");
			printf("JUST out of the LEFT speaker.\n");
			printf("Press ESC to go to next step in the test suite.\n");
			Left8(sound,workspace,siz);
			snd.sound = workspace;
			snd.frequency = 11000;
			snd.sndlen = siz*2;

			SetPlayMode(PCM_8_STEREO);
			MassageAudio(&snd);
			do
			{
				DigPlay2(&snd);
			} while ( getkey() != 27 );

			printf("Press a key to play test sound effect in 8 bit stereo mode.\n");
			printf("JUST out of the RIGHT speaker.\n");
			printf("Press ESC to go to next step in the test suite.\n");
			Right8(sound,workspace,siz);
			snd.sound = workspace;
			snd.frequency = 11000;
			snd.sndlen = siz*2;
			SetPlayMode(PCM_8_STEREO);
			MassageAudio(&snd);
			do
			{
				DigPlay2(&snd);
			} while ( getkey() != 27 );
		}
		if ( cap&PCM16 )
		{
			printf("Press a key to play test sound effect in 16 bit MONO mode.\n");
			printf("Press ESC to go to next step in the test suite.\n");
			Mono16(sound,workspace,siz);
			snd.sound = workspace;
			snd.frequency = 11000;
			snd.sndlen = siz*2;
			SetPlayMode(PCM_16_MONO);
			do
			{
				DigPlay2(&snd);
			} while ( getkey() != 27 );
		}


		if ( cap&PCM16STEREO )
		{
			printf("Press a key to play test sound effect in 16 bit STEREO mode.\n");
			printf("Will play 16 bit sound effect, just out of LEFT channel.\n");
			printf("Press ESC to go to next step in the test suite.\n");
			Left16(sound,workspace,siz);
			snd.sound = workspace;
			snd.frequency = 11000;
			snd.sndlen = siz*4;
			SetPlayMode(PCM_16_STEREO);
			do
			{
				DigPlay2(&snd);
			} while ( getkey() != 27 );

			printf("Press a key to play test sound effect in 16 bit STEREO mode.\n");
			printf("Will play 16 bit sound effect, just out of RIGHT channel.\n");
			printf("Press ESC to go to next step in the test suite.\n");
			Right16(sound,workspace,siz);
			snd.sound = workspace;
			snd.frequency = 11000;
			snd.sndlen = siz*4;
			SetPlayMode(PCM_16_STEREO);
			do
			{
				DigPlay2(&snd);
			} while ( getkey() != 27 );
		}
		SetPlayMode(PCM_8_MONO);
	}
	else
		printf("Unable to load test sound effect 'TEST.SND'.\n");

	UnLoadDigPak();
}
