#include <v_lib.h>

int	main(void)
{
	u32_t ql;
	u32_t len;
	file_t out;
	file_t in;
	u8_t buf[4096];
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	in=mp3Open("../../samples/media/test-mp3.wav");
	/* in=mp3Open("/mnt/media/music/JeanMichel/topic15/track08.wav"); */
	if(!in)
		return -1;
	out=wavePlaybackOpen(1,22050,16);
	/* out=wavePlaybackOpen(2,44100,16); */
	/* out=fileOpenWrite("out.raw"); */
	for(;;)
	{
		len=fileRead(in,buf,4096);
		if(!len)
			break;
		fileWrite(out,buf,len);
		ql=0;
		fileControl(out,pctl_waveout_get_pending_samples,&ql);
		printf("sound output queue len: %u\n",(unsigned int)ql);
		while(ql > 44100 * 40)
		{
			puts("waiting /dev/dsp ...");
			timeDelay(50);
			fileControl(out,pctl_waveout_get_pending_samples,&ql);
		}
	}
	fileClose(in);
	fileControl(out,pctl_waveout_wait,NULL);
	fileClose(out);

	memPrintStats();
	return 0;
}
