/***************************************************************************
  Set your editor's TAB width to 3

  A C example for using the Audio.asm routines from a C application. Note
  that you must assemble and link with the file, Audio.asm

****************************************************************************/

#include <functions.h>
#include <exec/types.h>
#include <exec/memory.h>

/* Here are the Audio.asm routines */
extern void		SetupAudio();
extern void		FreeAudio();
extern ULONG	TransposePeriod(); /* amigaPer = TransposePeriod(nanoPer); */
extern ULONG	RateToPeriod();	/* nanoPer = RateToPeriod(rate); */
extern ULONG	PeriodToRate(); /* Is usually a value < 28000 for the amiga. rate = PeriodToRate(nanoPer); */
extern void		PlayNote();		/* PlayNote(chanNum,oneShotSize,oneShotPtr,loopSize,loopPtr,vol,period); */
extern void		StopChan();		/* StopChan(chanNum); */

BYTE Wave1shot[] =	/* 512 sample points (bytes) */
  {-128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124};

BYTE Wave1loop[] =	/* 256 data points */
  {-128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124,
  -128,-124,-120,-116,-112,-108,-104,-100,-96,-92,-88,-84,-80,-76,-72,
  -68,-64,-60,-56,-52,-48,-44,-40,-36,-32,-28,-24,-20,-16,-12,
  -8,-4,0,4,8,12,16,20,24,28,32,36,40,44,48,
  52,56,60,64,68,72,76,80,84,88,92,96,100,104,108,
  112,116,120,124};


/************************ MAIN ROUTINE *****************************/

VOID main(argc, argv)
LONG argc;
char **argv;
{
	ULONG		nanoPeriod;
	ULONG	amigaPeriod;
	BYTE	* dataptr;
	USHORT i;

	/* Allocate CHIP mem for the wave data. Can you believe that Manx
	   still doesn't support declaring data in CHIP?
	*/
	dataptr = (BYTE *)AllocMem(512L+256L,MEMF_CHIP);

	if (dataptr)
	{
		/* Copy our wave to CHIP */
		for (i = 0; i < 512; ++i)
		{
			dataptr[i] = Wave1shot[i];
		}
		for (i = 0; i < 256; ++i)
		{
			dataptr[i+512] = Wave1loop[i];
		}

		/* Setup the audio hardware */
		SetupAudio();

		/* Let's turn our sample rate into a period in nanoseconds so that
  		   we can use TransposePeriod to play this wave at various musical pitches.
		   NOTE: Our sampling rate for this wave is 18Khz. The sampling rate is
		   found in the VHDR chunk of any loaded 8SVX sample.
		*/
		nanoPeriod = RateToPeriod( 18000L );

		/* Play the 8SVX sample with the oneShot and repeat portions at orig pitch */
		amigaPeriod = TransposePeriod( 0L, nanoPeriod ); /* steps = 0 means original pitch */
		if (amigaPeriod)
		{
			PlayNote(0L, 512L, dataptr, 256L, dataptr+512, 64L, amigaPeriod);
		}

		/* Delay for a while so that the loop can continue. Then, we must eventually
			stop the note by either starting another note on this channel or stopping
			the channel. Do the later.
		*/
		Delay(127L);
		StopChan(0L);
		Delay(50L);

		/* Play only the oneShot with no looping portion. Note that because
			there is no looping portion, our "block done" interrupt automatically
			stops the channel at the end of the oneShot. Therefore, we don't
			have to stop the channel ourselves like we did when we played that
			note with the looping portion.
			Note: This oneShot portion is a very small sample (only 512 bytes)
			so we're not going to hear much sound; just a quick beep and it's over */
		*/
		PlayNote(0L, 512L, dataptr, 0L, 0L, 64L, amigaPeriod);
		Delay(50L);	/* Give this oneShot note time to play and stop itself */

		/* Now, let's play a major triad. This consists of the original pitch, plus
			a pitch that is 4 half steps above orig pitch, plus a pitch that is 3
			half steps above that. We'll call PlayNote 3 times, starting a note on
			each of 3 amiga chans
		*/
		PlayNote(0L, 512L, dataptr, 256L, dataptr+512, 64L, amigaPeriod);
		amigaPeriod = TransposePeriod( 4L, nanoPeriod );	/* steps = 3 */
		if (amigaPeriod)
		{
			PlayNote(1L, 512L, dataptr, 256L, dataptr+512, 64L, amigaPeriod);
		}
		amigaPeriod = TransposePeriod( 7L, nanoPeriod );		/* steps = 4+3 */
		if (amigaPeriod)
		{
			PlayNote(2L, 512L, dataptr, 256L, dataptr+512, 64L, amigaPeriod);
		}

		/* Delay for a while so that the chord can sound. Then, we must eventually
			stop the notes.
		*/
		Delay(127L);
		StopChan(0L);
		StopChan(1L);
		StopChan(2L);

		/* Free the audio hardware */
		FreeAudio();

		/* Free the chip mem */
		FreeMem(dataptr,512L+256L);

		}

	/* That's it! */
	exit( 0L );
}
