
/**
 * @file app.c
 * MP3 decoder console application (example and test application on using 
 * mp3_decode_frame())
 *
 * - Copyright (C) 2003-2006 Vanguard Software Solutions, Inc.
 * - All rights are reserved.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

//#include <conio.h>	//	for getch()
//#include <ctype.h>

#include "vsofts_mp3.h"

#define BUF_SIZE 16536

void Usage(char*);
void EndStream(void);
void DecErr(int e, int addr);

FILE *f,*g;
t_ui8 BUF[ BUF_SIZE ];
sample_t Out[1152*2];

char srcFile [] = { "test.mp3" };
char dstFile [] = { "test.raw" };


#define N_TEST_BLOCK	100000000

char txt[256];

int main(int argc, char *argv[])
{
	int nbytes = 0;
	int addr = 0;
	int n;int i;
	char srcFileName[512];
	char dstFileName[512];
	unsigned long OutSize, OutBlockSize;
	int nFs;


	printf("MPEG1,2 decoder fixed-point arithmetics, ver1.3, 12.11.03\n");


	if(	argc==1 )
	{
		strcpy(srcFileName,srcFile);
		strcpy(dstFileName,dstFile);
	}
	else
	if( argc==2 )
	{
		strcpy(srcFileName,argv[1]);
		strcpy(dstFileName,argv[1]);
		n = strlen(dstFileName);
		if( n > 4 )
		{
			if( dstFileName[n-4]=='.' )
					dstFileName[n-4] = 0;
		}
			
		strcat(dstFileName,".raw");
	}
	else
	{
		strcpy(srcFileName,argv[1]);
		strcpy(dstFileName,argv[2]);
	}

	f = fopen(srcFileName, "rb");
	if (f==NULL) { perror(srcFileName); exit(1); }

	g = fopen(dstFileName,"wb");
	if (g==NULL) { perror(dstFileName); exit(1); }

	printf("Input file  %s\n",srcFileName);
	printf("Output file %s\n",dstFileName);
	printf("please, wait...\n");
	OutSize = 0;

	mp3_init();

	mp3_reset(); 

	for (i=0; i<N_TEST_BLOCK; i++)
	{

		printf("Block %d\n",i);

		n = fread(BUF+nbytes, 1, BUF_SIZE-nbytes, f);

		assert (n>=0);

		nbytes += n;

		n = mp3_locate_header(BUF,nbytes);
		if (n==0)
		{
			int s;
			int freqs[16];

			if (nbytes <= 0) { EndStream(); break; }

			n = mp3_decode_frame(BUF,Out,freqs);

			//for (s=0; s<16; s++) printf("%d, ", freqs[s]);
			//printf("\n");

			if (n <=0) DecErr(n, addr);

			OutBlockSize = mp3_get_output_pcm_data_size(0);
			
			if (mp3_get_output_pcm_data_size(1)!=1152) 
				printf("\n%d",mp3_get_output_pcm_data_size(1));

			/*fwrite(Out,1,OutBlockSize,g);*/
/*
			for (s=0; s<2*1152; s++)
			{
				short sample16;
                
				sample16 = (short)Out[s];
				putc((unsigned short)sample16&255,g);
				putc((unsigned short)sample16>>8,g);
			}
*/


			OutSize += OutBlockSize;
		}

		nbytes -=n;
		if (nbytes > 0)
			memmove(BUF,BUF+n,nbytes);
		else if (nbytes<0) { EndStream(); break; }
	}

//	FINALLY:
	mp3_shutdown();

	fclose(f);
	fclose(g);

	//	Duration of output signal
	nFs = mp3_get_FrequencySamplingHz();
	printf("Fs = %d Hz\n",nFs);
#ifdef _WIN32_
	{
		double Fs,seconds,ms,mks,factor;

		Fs = (double)nFs;
		Fs = Fs ? Fs : 1;	// check case Fs = 0
		factor = 2*Fs*sizeof(sample_t);
		seconds = ((double)OutSize)/factor;
		ms		= (1000.0*OutSize)/factor;
		mks		= (1000.0*1000.0*OutSize)/factor;
		printf("Duration of output sound file is %2.1f s, %5.1f ms, %8.1f mks \n",seconds,ms,mks);
	}
#else	//	#ifdef _WIN32_
	{
		//	To compile without float point arithmetic
		unsigned long Fs,seconds,ms,mks,factor;

		Fs = nFs;
		Fs = Fs ? Fs : 1;	// check case Fs = 0
		factor = 2*Fs*sizeof(sample_t);
		seconds = OutSize/factor;
		ms		= (1000*OutSize)/factor;	// (1000*(OutSize/4))/Fs;
		mks		= 1000*ms;					// (1000*1000*(OutSize/4))/Fs;
		printf("Duration of output sound file is %u s, %u ms, %u mks \n",seconds,ms,mks);
	}
#endif	//	#ifdef _WIN32_

	return 0;
}

void Usage(char *name)
{
	printf("Usage: %s <mp3-file> <pcm-file>\n",name);
	exit(1);
}

void EndStream()
{
	printf("Input stream decoded completely\n");

}

void DecErr(int e, int addr)
{
	printf("Decoder returned error code: %d @ %08x\n", e, addr);
	exit(1);
}
