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


#ifndef _DEBUG
#define ALLEGRO_STATICLINK
#endif


#include <allegro.h>
#include "speechy.h"
#include "voice_mgr.h"

int main(int argc, char *argv[])
{
	SAMPLE *speak;
	unsigned length, length2;
	unsigned int id;
	short codes[512];
	char text[1025], path[256]="allophones/";
	FILE *fp;
	VOICE_MGR *voice_mgr=NULL;
	int quit=FALSE, say=FALSE, save=FALSE;
	int index=0;

	allegro_init();
	install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);
	install_keyboard();
	install_timer();
	set_gfx_mode(GFX_GDI, 640, 480, 0, 0);

	if(argc<2)
	{
		strcat(path, "sam");
	}
	else
	{
		strcat(path, argv[1]);
	}

	strcat(path, ".txt");
	voice_mgr=voice_load(path);

	if(!voice_mgr)
		return -1;
	
	while(!quit)
	{
		say=FALSE;

		while(!say)
		{
			int keycode; 
			int i, x, y;
			
			x=0;
			y=16;

			textout(screen, font, "ENTER to say, TAB to save, and ESC to quit", x, y, 
				makecol(255, 255, 255));

			y+=32;

			for(i=0; i<(int)voice_mgr->phon_count; i++)
			{
				textout(screen, font, voice_mgr->phon_list[i].name, x, 16+y, 
					makecol(255, 255, 255));

				x+=4*8;
				if(x>SCREEN_W-4*8)
				{
					x=0;
					y+=16;
				}
			}					
		
			if(keypressed())
			{
				keycode=readkey();

				switch(keycode>>8)
				{
					case KEY_ESC:
						quit=TRUE;
						say=TRUE;
						break;

					case KEY_TAB:
						save=TRUE;

					case KEY_ENTER:
						text[index]=0;
						say=TRUE;
						break;

					case KEY_BACKSPACE:
						index--;
						if(index<0)
							index=0;
						text[index]=0;
						break;

					case KEY_DEL:
						index=0;
						text[index]=0;
						rectfill(screen, 0, SCREEN_H/2, SCREEN_W-1, SCREEN_H-32, 
							makecol(0, 0, 0));
						break;

					default:
						text[index]=keycode&0xff;
						index++;
						if(index>=1024)
							index=1023;
				}

				if(!say)
				{
					int i=0, row=0, col=0;
					char str[81];

					for(i=0; i<1024; i++)
					{
						if(i<index)
							str[col]=text[i];
						else
							break;

						col++;

						if(col>75)
						{
							str[col]=0;
							textprintf(screen, font, 16, SCREEN_H/2+row*8, makecol(255, 255, 255), "%s ", str);
							col=0;
							row++;
						}
					}
					
					str[col]=0;

					textprintf(screen, font, 16, SCREEN_H/2+row*8, makecol(255, 255, 255), "%s ", str);
//					textprintf(screen, font, 0, 0, makecol(255, 255, 255), "%d, %d, %d ", col, row, index);
				}
			}

			rest(10);

			textout(screen, font, "> ", 0, SCREEN_H/2, makecol(255, 255, 255));

			yield_timeslice();
		}

		text[index]=0;

		if(quit)
			break;

		voice_parse(voice_mgr, text, codes, 512);

		length=speechy_sequence(voice_mgr->voice, codes);

		speak=create_sample(8, FALSE, voice_mgr->freq, length);
		length2=speechy_read(voice_mgr->voice, speak->data, length);

		id=play_sample(speak, 255, 128, 1000, 0);

		while(voice_check(id))
			yield_timeslice();

		if(save)
		{
			fp=fopen("out.raw", "wb");
			fwrite(speak->data, length2, 1, fp);
			fclose(fp);	
			save=FALSE;
		}

		destroy_sample(speak);
	}

	voice_unload(voice_mgr);

	return 0;
}
END_OF_MAIN()