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

void do_decompress(char *in_name,char *out_name)
{
	u32_t freq, len;
	u8_t buf[1024 * 16];
	vhandle_t in, out;
	in = mp3Open(in_name);
	assert(in);
	fileControl(in,pctl_wave_get_sampling_frequency,&freq);
	out = wavOpenWrite(out_name,2,freq,16);
	for(;;)
	{
		len = fileRead(in,buf,1024 * 16);
		if(!len)
			break;
		fileWrite(out,buf,len);
	}
	fileClose(in);
	fileClose(out);
}

#ifdef	__linux

#define	INN1	"/mnt/backup/Music/Accept/36-96-RunThroughTheNight.mp3"
#define	INN2	"/mnt/backup/Music/games/starwars/mus_a_650.mp3"

#else

#define	INN1	"//k/Music/Accept/36-96-RunThroughTheNight.mp3"
#define	INN2	"//k/Music/games/starwars/mus_a_650.mp3"

#endif

void V_CALL p1(void *arg)
{
	do_decompress(INN1,"test1.wav");
	puts("thread1: done");
}

void V_CALL p2(void *arg)
{
	do_decompress(INN2,"test2.wav");
	puts("thread2: done");
}

/*
static int dstart;

static int dend;

#include "../../../include/v_lib.h"

void dumpdata(char *fn)
{
	u32_t len = &dend - &dstart;
	vhandle_t of = fileOpenWrite(fn);
	fileWrite(of,&dstart,len);
	fileClose(of);
}
*/

int main(void)
{
	vhandle_t t1, t2;
	logStartConsoleLog();
	logUpperLevel(LOG_TRACE);

	t1 = threadCreate(p1,NULL);
	t2 = threadCreate(p2,NULL);
	threadJoin(t1,NULL);
	threadJoin(t2,NULL);

	memPrintStats();
	return 0;
}
