/*
 *    Example program for the Allegro library, by Shawn Hargreaves.
 *
 *    This program demonstrates how to play samples.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "allegro.h"
#include "asm.h"
#include "errors.h"
#include "commnd.h"
#include "parse.h"
#include "image.h"
#include "imagwind.h"
#include "plotutil.h"

extern VENSTER *vp[MAXSIG];
extern int current_signal;  /* The actual signal drawn to screen */
extern BOOL don;

int ReadWavCi(char *p)
{
	int i,im, c, k;
	int filetype;
	unsigned char *real_data_char;
	unsigned int alloc_size;
	char *file_name;
	char *signalname;
	char buffer1[128],buffer2[128];
	FILE *fp;
	ASM_SAMPLE *real_data;
	ASM_SAMPLE *imag_data;

    SAMPLE *the_sample;
    int pan = 128;
    int pitch = 1000;

	file_name  = strarg(&p,NULL,"Filename","a.wav",buffer1);
	signalname = strarg(&p,NULL,"Signal",file_name,buffer2);


   /* read in the WAV file */
   the_sample = load_sample(file_name);
   if (!the_sample) {
      asm_wprintf("Error reading WAV file '%s'\n", file_name);
      return(0);
   }

   asm_wprintf("bits          : %d\n", the_sample->bits);
   asm_wprintf("sample freg   : %d\n", the_sample->freq);
   asm_wprintf("sample length : %d\n", the_sample->len);

	/* Neem een voorlopige alloc_size, er moet nog gerealloceerd worden
    omdat de grootte nu nog niet bekend is.                  */

	alloc_size = 128; /* willekeurig */

	im = Find_Signal_Venster(signalname);   /* Vind het juiste venster */
	if (im == -1)                           /* Signaal bestond nog niet */
	{
		im = Create_Venster(signalname,alloc_size);
		if (im == -1) return ERR_WINDOWS;      /* Te veel displaywindows */
	}
	else Realloc_Venster(vp[im],alloc_size);

	strncpy(Window_File(vp[im]),"",sizeof(Window_File(vp[im])));


	DataPixelFormat(vp[im])=DATA_PIXEL_FORMAT;
	DataLength(vp[im])= the_sample->len;
	DataRecords(vp[im])= (unsigned short) 1;
	DataFileSeq(vp[im])=DATA_FILE_SEQ;
	DataBits_p_s(vp[im])= (short)the_sample->bits;
	DataChannels(vp[im])= 1;
	DataSampleRate(vp[im])= (the_sample->freq)/10;
	DataDomain(vp[im])= TIME;
	DataType(vp[im])= REAL;
	strncpy(DataIdString(vp[im]),ASM_ID_STRING,sizeof(DataIdString(vp[im])));
	signalname[sizeof(DataSignalName(vp[im]))-1] = '\0';
	strncpy(DataSignalName(vp[im]),signalname,sizeof(DataSignalName(vp[im])));
	strncpy(DataUserText(vp[im]),"DataUserText",sizeof(DataUserText(vp[im])));
	strncpy(DataDescription(vp[im]),"WAV",sizeof(DataDescription(vp[im])));
	Window_HScale(vp[im]) = (double)SCREEN_W/(double)((the_sample->len)+2*SCREEN_W);
	Window_Record(vp[im]) = 0;
	Window_Channel(vp[im]) = 0;

    Realloc_Venster(vp[im],(int)the_sample->len);

	real_data = DataReal(vp[im]);
	imag_data = DataImag(vp[im]);

	/* Allegro supports only 8 bits mono WAV files
	   =>  Signal is real, data 8 bit
	 */
		   real_data_char = the_sample->data;

			for (i=0;i < the_sample->len; i++)
			{
				real_data[i]=(ASM_SAMPLE)real_data_char[i];
				imag_data[i]=(ASM_SAMPLE)0;
			}


   destroy_sample(the_sample);

   	if (don)
	{
		make_plot(vp[im]);
		current_signal = im;
		Window_InUse(vp[im])= TRUE;
	}
	else
		Window_InUse(vp[im]) = FALSE;
    return(0);
}
