/* SigLib Echo / Reverb Generation Example */

#include <stdio.h>
#include <siglib.h>
#include "GraphFunctions.h"

#define	SAMPLE_LENGTH		256L
#define	TIME_DELAY		((SLData_t)0.1)
#define	ECHO_DECAY		SIGLIB_HALF
#define ECHO_BUF_SIZE	((SLArrayIndex_t)64)

SLData_t	pSrc[SAMPLE_LENGTH], pDst[SAMPLE_LENGTH];
SLData_t	EchoArray[ECHO_BUF_SIZE];

SLData_t	ImpulsePhase;
SLArrayIndex_t	EchoArrayOffset;

void main(void)
{
	GraphObject *h2DGraph;							/* Declare graph object */
	SLFixData_t	i;

	h2DGraph =										/* Initialize graph */
		Create2DGraph ("Echo / Reverb Generation",	/* Graph title */
					   "Time",						/* X-Axis label */
					   "Magnitude",					/* Y-Axis label */
					   SV_AUTO_SCALE,				/* Scaling mode */
					   SV_SIGNED,					/* Sign mode */
					   SV_GRAPH_LINE,				/* Graph type */
					   "localhost");				/* Graph server */
	if (h2DGraph == NULL)							/* Graph creation failed - e.g is server running ? */
	{
		printf ("\nGraph creation failure. Please check that the server is running\n");
		exit (1);
	}

	EchoArrayOffset = 0;
	ImpulsePhase = SIGLIB_ZERO;
	SDA_SignalGenerate (pSrc,						/* Pointer to destination array */
						SIGLIB_IMPULSE_STREAM,		/* Signal type - Impulse stream */
						((SLData_t)0.9),			/* Signal peak level */
						SIGLIB_FILL,				/* Fill (overwrite) or add to existing array contents */
						((SLData_t)0.019),			/* Signal frequency */
						SIGLIB_ZERO,				/* D.C. Offset */
						SIGLIB_ZERO,				/* Unused */
						SIGLIB_ZERO,				/* Signal end value - Unused */
						&ImpulsePhase,				/* Signal phase - maintained across array boundaries */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SAMPLE_LENGTH);				/* Output array length */

	for (i = 0; i < SAMPLE_LENGTH; i++)
	{
		pDst[i] =
			SDS_EchoGenerate (pSrc[i],				/* Sample */
							  EchoArray,			/* Pointer to echo state array */
							  &EchoArrayOffset,		/* Echo array data input location */
							  TIME_DELAY,			/* Echo delay */
							  ECHO_DECAY,			/* Echo decay */
							  SIGLIB_ECHO,			/* Echo type */
							  ECHO_BUF_SIZE);		/* Echo array size */
	}


	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Source Signal",				/* Title of the dataset */
				    pSrc,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("\nSource Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Echo'd Signal",				/* Title of the dataset */
				    pDst,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_RED,							/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_ADD);					/* New graph */
	printf ("\nEcho'd Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();


	ImpulsePhase = SIGLIB_ZERO;
	SDA_SignalGenerate (pSrc,						/* Pointer to destination array */
						SIGLIB_IMPULSE_STREAM,		/* Signal type - Impulse stream */
						((SLData_t)0.9),			/* Signal peak level */
						SIGLIB_FILL,				/* Fill (overwrite) or add to existing array contents */
						((SLData_t)0.019),			/* Signal frequency */
						SIGLIB_ZERO,				/* D.C. Offset */
						SIGLIB_ZERO,				/* Unused */
						SIGLIB_ZERO,				/* Signal end value - Unused */
						&ImpulsePhase,				/* Signal phase - maintained across array boundaries */
						SIGLIB_NULL_DATA_PTR,		/* Unused */
						SAMPLE_LENGTH);				/* Output array length */

	for (i = 0; i < SAMPLE_LENGTH; i++)
	{
		pDst[i] =
			SDS_EchoGenerate (pSrc[i],				/* Sample */
							  EchoArray,			/* Pointer to echo state array */
							  &EchoArrayOffset,		/* Echo array data input location */
							  TIME_DELAY,			/* Echo delay */
							  ECHO_DECAY,			/* Echo decay */
							  SIGLIB_REVERB,		/* Echo type */
							  ECHO_BUF_SIZE);		/* Echo array size */
		
	}

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Source Signal",				/* Title of the dataset */
				    pSrc,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_BLUE,						/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_NEW);					/* New graph */
	printf ("\nSource Signal\nPlease hit <Carriage Return> to continue . . ."); getchar ();

	Display2DGraph (h2DGraph,						/* Graph handle */
				    "Reverberated Signal",			/* Title of the dataset */
				    pDst,							/* Array of Double dataset */
				    SAMPLE_LENGTH,					/* Number of data points */
					SV_GRAPH_LINE,					/* Graph type */
				    SV_RED,							/* Colour */
					SV_HIDE_MARKERS,				/* Marker enable / disable */
					SV_GRAPH_ADD);					/* New graph */
	printf ("\nReverberated Signal\n");
}


