#include <v_lib.h>
#include <stdio.h>

#define	RATE	44100
#define	BITS	16
#define	CH		2
#define	TIME	4

V_LOCAL	u32_t	lps = RATE * CH * (BITS >> 3);
V_LOCAL	u8_t	buffer[RATE * CH * (BITS >> 3)];

int main()
{
	u32_t i, len;
	vhandle_t out;

	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);
	
	if(!waveCaptureInit(CH,RATE,BITS,NULL,NULL))
	{
		conPrintf("waveCaptureInit() failed\n");
		return -1;
	}

	if(!waveCaptureBlockMode(true))
	{
		conPrintf("Can't set block mode.\n");
		waveCaptureShutdown();
		return -1;
	}

	out=wavOpenWrite("out.wav",CH,RATE,BITS);

	if(!waveCaptureResume())
	{
		conPrintf("waveCaptureResume() failed.\n");
		waveCaptureShutdown();
		return -1;
	}
	
	for(i=0;i<TIME;i++)
	{
		timeDelay(1000);
		len = waveCaptureGet(buffer,lps);
		fileWrite(out,buffer,len);
	}
	
	fileClose(out);
	waveCaptureShutdown();

	memPrintMemStats();
	
	return 0;
}
