#include <v_lib.h>

#define	DEC_SIZE	4096
u8_t	decomp[DEC_SIZE];

int main(int argc,char *argv[])
{
	u32_t nc,freq,bps,len,q_len,arg;
	vhandle_t in,out;
//	char *inf="../../media/compressed.wav";
//	char *inf="//m/Media/Music/Games/wolf2/x_action.wav";
	char *inf="//m/Media/Music/Games/quake3/loss.wav";
//	char *inf="//m/Media/Music/Games/quake2/Track10.mp3";
//	char *inf = argv[1];
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	in = mp3Open(inf);
	if(!in)
		return -1;
	/* Open wave output device */
    arg = 1;
    fileControl(in,pctl_wave_loop,&arg);
    fileControl(in,pctl_wave_get_num_channels,&nc);
	fileControl(in,pctl_wave_get_sampling_frequency,&freq);
	fileControl(in,pctl_wave_get_bits_per_sample,&bps);
	out = wavePlaybackOpen(nc,freq,bps);
	if(!out)
	{
		fileClose(in);
		return -1;
	}
	for(;;)
	{
		len = fileRead(in,decomp,DEC_SIZE);
		if(!len)
			break;
		fileWrite(out,decomp,len);
		q_len=0;
		fileControl(out,pctl_waveout_get_pending_samples,&q_len);
		while(q_len > 256 * 4096)
		{
			timeDelay(20);
			fileControl(out,pctl_waveout_get_pending_samples,&q_len);
		}
	}
	fileClose(in);
	fileControl(out,pctl_waveout_wait,NULL);
	fileControl(out,pctl_waveout_reset,NULL);
	fileClose(out);
	
	memPrintStats();
	return 0;
}
