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

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

int main()
{
	SAMPLE *speak;
	unsigned length, length2;
	unsigned int id, i, j;
	short codes[512];
//	char text[1024];
	FILE *fp;
	VOICE_MGR *voice_mgr;

	// SAM phones (sam, sam2)

/*
	char *str="h eh l ow pa3 p iy p ul pa3 ax v pa3 ax l eh g r ow pa3 d aa t pa3 s iy pa3 s iy pa5 pa5 "
		"dh ih s pa3 ih z pa3 dh ax pa3 v oy s pa3 ax v pa3 y oh r pa3 n uw pa3 m ae s t er pa5 pa5 "
		"b aw pa3 d aw n pa3 b iy f oh r pa3 m iy pa5 ae n d pa3 ay pa3 w ih l pa3 s p eh r pa3 "
		"y oh r pa3 l ay v z pa5 pa5 d iy f ay pa3 m iy pa5 ae n d pa3 m ay pa3 r ow b aa t pa3 "
		"ah r m iy z pa3 w ih l pa3 d iy s t r oy pa3 y uw pa5 pa5 "
		"h ah h ah h eh h eh h eh h iy h ae h aa h aa aa h ah";


	char *str="w iy pa3 k ax n t r ow l pa4 dh ax "
		"y uw n ih v er s";
*/
	char *str="h ae p iy b er th d ey pa3 m ae t pa3 s m ih th pa5 pa5 dh ih s "
		"h oh oh l pa5 b er th d ey pa5 th ih nx pa5 "
		"d ah z n t pa3 m iy n dh ae t m ay r ow b aa t ah r m iy z pa3 "
		"w oh n t pa3 d iy s t r oy y uw pa5 dh ow pa5 bleeeeep";


/*	
	// SPO256 phones (spo256)
	char *str="ww iy pa3 kk3 ax nn tt rr2 ow ll pa4 dh1 ax "
		"yy2 uw1 nn ih vv er1 ss ss";

	char *str="ww iy pa3 kk1 ey mm pa3 ih ih nn pa3 pp iy ss ss pa4 ff or pa3 "
		"ao ll ll pa3 mm ae nn pa3 kk1 ay nn dd1";

	char *str="ay pa3 ww ax zz pa3 pp rr2 ow pa1 gg2 rr1 ae mm pa1 dd1 pa3 tt2 uw2 pa3 "
		"ih ih nn1 vv eh eh ss pa2 tt2 ih pa1 gg1 ey pa2 tt2 pa5 ay pa3 mm ax ss tt1 pa3 "
		"rr1 iy pa3 iy vv ae ll pa1 yy2 uw1 ey tt";


	char *str="hh1 eh ll ow pa3 pp iy pp el pa3 ax vv pa3 ax ll eh pa2 gg2 rr2 ow pa3 dd2 aa pa2 tt2 pa3 ss iy pa3 ss iy pa5 pa5 "
		"dh1 ih ss pa3 ih zz pa3 dh1 ax pa3 vv oy ss pa3 ax vv pa3 yy2 or pa3 nn1 uw2 pa3 mm ae ss pa2 tt2 er2 pa5 pa5 "
		"bb2 aw pa3 dd2 aw nn1 pa3 bb2 iy ff or pa3 mm iy pa5 ae nn1 dd1 ay ww ih ll ss pp xr pa3 "
		"yy2 or pa3 ll ay vv zz pa5 pa5 dd2 iy ff ay pa3 mm iy pa5 ae nn1 dd1 pa3 mm ay pa3 rr1 ow bb2 aa pa2 tt2 pa3 "
		"ae rr2 mm iy zz pa3 ww ih ll pa3 dd2 iy ss pa2 tt2 rr2 oy pa3 yy2 uw2 pa5 pa5 "
		"hh1 ao hh1 ao hh1 eh hh1 eh hh1 eh hh1 iy hh1 ae hh1 aa hh1 aa aa hh1 ao";
*/
	allegro_init();
	install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL);

	voice_mgr=voice_load("allophones/sam3.txt");
	if(!voice_mgr)
		return -1;

	voice_parse(voice_mgr, str, codes, 256);

/*
	// spit out all phones
	j=0;
	for(i=0; i<voice_mgr->phon_count; i++)
	{
		codes[j]=i;
		codes[j+1]=2;
		j+=2;
	}

	codes[j]=END_SEQ;
*/
	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));

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

	destroy_sample(speak);

	voice_unload(voice_mgr);

	return 0;
}
END_OF_MAIN()