#include <stdio.h>
#include <stdlib.h>

#include "../Lib/Define_config.h"
#include "../Lib/SamsungSolomonVoiceW.h"

#define DIAMONDVOICE_FRAME_TX			320 //	SAMSUNGDIAMONDVOICEWIDEBAND_TX, SAMSUNGDIAMONDVOICENARROWBAND_TX

int FrmCnt_main;

FILE *fp_debug_ns_para, *fp_debug_ns_vec1, *fp_debug_ns_vec2, *fp_debug_aecoutpwr, *fp_debug_ns_vec3, *fp_debug_ns_vec4, *fp_debug_ns_vec5, *fp_debug_res_regression;
FILE *fp_debug_Pwr_RX_AECIN_dB_C, *fp_debug_ADF_C;
FILE *fp_debug_NS;
FILE *fp_debug_dump;
FILE *fp_WnlmsFilterCoeff;
FILE *fp_npvss_nlms;
FILE *fp_Wfnlms_new;
FILE *fp_inoutVAD;
FILE *fp_f0contour;
FILE *fp_dbg_Mixing;
FILE *fp_fft_out_inner;
FILE *fp_fft_out_outer;
FILE *fp_dbg_MixingState;

FILE *fp_debug_Flag_BWE_IN_dB_C;
FILE *fp_debug_Flag_Outer_BWE_IN_dB_C;
FILE *fp_debug_BWE_pwr_lowf_out_dB_C;
FILE *fp_debug_BWE_pwr_highf_out_dB_C;
FILE *fp_debug_lpc_gain_c;

// solomonvoice_sub_module_point_tmp
FILE *fp_fx_SolomonVoice_ringbacktone_detection;

FILE *fp_f0_based_preNS_para;

// 1mic ECNS-------------------------------
SamsungSolomonVoiceParam DiamondVoice_DV_TX_Config = { 0, };
SamsungSolomonVoiceParam DiamondVoice_DV_TX_Config_Outer = { 0, };
SamsungSolomonVoiceEngine_Handle_t SamsungDiamondVoiceEngine_vars_Tx;

short Speech_buf_TX[SAMSUNGDIAMONDVOICEWIDEBAND_TX];//   = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);
short Speech_buf_RX[SAMSUNGDIAMONDVOICEWIDEBAND_TX];//   = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);
short Output_buf_TX[SAMSUNGDIAMONDVOICEWIDEBAND_TX];//   = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);
short Speech_buf_Outer[SAMSUNGDIAMONDVOICEWIDEBAND_TX];// = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);			//20180703 kyuhan 2mic in-ear-solution
short Output_buf_Outer[SAMSUNGDIAMONDVOICEWIDEBAND_TX];// = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);

short Output_buf_Outer_BWE[SAMSUNGDIAMONDVOICEWIDEBAND_TX];// = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);
short Output_buf_Outer_Outer[SAMSUNGDIAMONDVOICEWIDEBAND_TX];// = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);
short Output_buf_Outer_Inner[SAMSUNGDIAMONDVOICEWIDEBAND_TX];// = (short *)malloc(sizeof(short)* SAMSUNGDIAMONDVOICEWIDEBAND_TX);

int main(int argc, char *argv[])
{

	FILE *ifp1, *ifp2, *ifp3, *ofp2;
	FILE *ofp1, *ofp3, *ofp4, *ofp5;

	// solomonvoice_sub_module_point_tmp
	fp_fx_SolomonVoice_ringbacktone_detection = fopen(".\\debug\\fp_fx_SolomonVoice_ringbacktone_detection.txt", "w");

	
	// 1mic ECNS-------------------------------
	fp_Wfnlms_new = fopen(".\\debug\\Wfnlms_new.txt", "w");

	fp_debug_ns_para = fopen(".\\debug\\debug_ns_para.txt", "w");
	fp_debug_ns_vec1 = fopen(".\\debug\\debug_ns_vec1.txt", "w");
	fp_debug_ns_vec2 = fopen(".\\debug\\debug_ns_vec2.txt", "w");
	fp_debug_aecoutpwr = fopen(".\\debug\\debug_ns_vec2.txt", "w");
	fp_debug_ns_vec3 = fopen(".\\debug\\debug_ns_vec3.txt", "w");
	fp_debug_ns_vec4 = fopen(".\\debug\\debug_ns_vec4.txt", "w");
	fp_debug_ns_vec5 = fopen(".\\debug\\debug_ns_vec5.txt", "w");
	fp_WnlmsFilterCoeff = fopen(".\\debug\\WnlmsFilterCoeff.txt", "w");
	fp_npvss_nlms = fopen(".\\debug\\npvss_nlms.txt", "w");

	fp_debug_res_regression = fopen(".\\debug\\debug_res_regression.txt", "w");

	fp_debug_Pwr_RX_AECIN_dB_C = fopen(".\\debug\\Pwr_RX_AECIN_dB_C.txt", "w");
	fp_debug_ADF_C = fopen(".\\debug\\ADF_C.txt", "w");	
	fp_debug_NS = fopen(".\\debug\\NS.txt", "w");
	fp_debug_dump = fopen(".\\debug\\dump.txt", "w");

	fp_f0contour = fopen(".\\debug\\f0contour.txt", "w");
	fp_inoutVAD = fopen(".\\debug\\inoutVAD.txt", "w");
	fp_f0_based_preNS_para = fopen(".\\debug\\fp_f0_based_preNS_para.txt", "w");

	// BWE
	fp_debug_Flag_BWE_IN_dB_C = fopen(".\\debug\\Flag_BWE_IN_dB_C.txt", "w");
	fp_debug_Flag_Outer_BWE_IN_dB_C = fopen(".\\debug\\Flag_Outer_BWE_IN_dB_C.txt", "w");
	fp_debug_BWE_pwr_lowf_out_dB_C = fopen(".\\debug\\pwr_BWE_lowf_out_dB_C.txt", "w");
	fp_debug_BWE_pwr_highf_out_dB_C = fopen(".\\debug\\pwr_BWE_highf_out_dB_C.txt", "w");
	fp_debug_lpc_gain_c = fopen(".\\debug\\pwr_lpc_gain_C.txt", "w");
	

	//Mixing
	fp_dbg_MixingState = fopen(".\\debug\\_MixingState.pcm", "wb");
	fp_fft_out_inner = fopen(".\\debug\\_pf_fft_1.bin", "wb");
	fp_fft_out_outer = fopen(".\\debug\\_pf_fft_2.bin", "wb");


	if ((ifp1 = fopen(argv[1], "rb")) == (FILE *)NULL)
		exit(fprintf(stderr, "1 TX input file error..\n"));

	if ((ifp2 = fopen(argv[3], "rb")) == (FILE *)NULL)
		exit(fprintf(stderr, "3 RX input file error..\n"));

	if ((ifp3 = fopen(argv[2], "rb")) == (FILE *)NULL)
		exit(fprintf(stderr, "2 Outer input file error..\n"));

	if ((ofp1 = fopen(argv[4], "wb")) == (FILE *)NULL)
		exit(fprintf(stderr, "4 output file error..\n"));

	if ((ofp2 = fopen(argv[5], "wb")) == (FILE *)NULL)
		exit(fprintf(stderr, "5 Outer output file error..\n"));

	if ((ofp3 = fopen(argv[6], "wb")) == (FILE *)NULL)
		exit(fprintf(stderr, "6 Outer output file error..\n"));

	if ((ofp4 = fopen(argv[7], "wb")) == (FILE *)NULL)
		exit(fprintf(stderr, "7 Outer output file error..\n"));

	if ((ofp5 = fopen(argv[8], "wb")) == (FILE *)NULL)
		exit(fprintf(stderr, "8 Outer output file error..\n"));

	//Mixing dbg
	if ((fp_dbg_Mixing = fopen(argv[9], "w")) == (FILE *)NULL)
		exit(fprintf(stderr, "Outer output file error..\n"));


	SamsungSolomonVoiceWEngineInit(&SamsungDiamondVoiceEngine_vars_Tx);

	SamsungSolomonVoiceWEngineConfig(&SamsungDiamondVoiceEngine_vars_Tx, &DiamondVoice_DV_TX_Config, &DiamondVoice_DV_TX_Config_Outer, Speech_buf_TX, Speech_buf_RX, Output_buf_TX, Speech_buf_Outer, Output_buf_Outer, Output_buf_Outer_BWE, Output_buf_Outer_Outer, Output_buf_Outer_Inner);

	FrmCnt_main = 0;
	while ((fread(Speech_buf_TX, sizeof(short), DIAMONDVOICE_FRAME_TX, ifp1) == (unsigned)DIAMONDVOICE_FRAME_TX)
		&& (fread(Speech_buf_RX, sizeof(short), DIAMONDVOICE_FRAME_TX, ifp2) == (unsigned)DIAMONDVOICE_FRAME_TX)
		&& (fread(Speech_buf_Outer, sizeof(short), DIAMONDVOICE_FRAME_TX, ifp3) == (unsigned)DIAMONDVOICE_FRAME_TX))
	{
		FrmCnt_main++;
		if (FrmCnt_main == 1611)
			FrmCnt_main = FrmCnt_main;

		SamsungSolomonVoiceWEngineExe(&SamsungDiamondVoiceEngine_vars_Tx, &DiamondVoice_DV_TX_Config, Speech_buf_TX, Speech_buf_RX, Output_buf_TX, Speech_buf_Outer, Output_buf_Outer, Output_buf_Outer_BWE, Output_buf_Outer_Outer, Output_buf_Outer_Inner);	//20180703 kyuhan 2mic in-ear-solution

		fwrite(Output_buf_TX, sizeof(short), 160, ofp1);
		fwrite(Output_buf_Outer_BWE, sizeof(short), 320, ofp3);
		fwrite(Output_buf_Outer_Outer, sizeof(short), 320, ofp4);
		fwrite(Output_buf_Outer_Inner, sizeof(short), 320, ofp5);
		fwrite(Output_buf_Outer, sizeof(short), DIAMONDVOICE_FRAME_TX, ofp2);

		printf("%ld ", FrmCnt_main);

	}
	printf("\n\n\n");

	fclose(ifp1);
	fclose(ifp2);
	fclose(ifp3);
	fclose(ofp1);
	fclose(ofp3);
	fclose(ofp4);
	fclose(ofp5);
	fclose(ofp2);

	fclose(fp_Wfnlms_new);
	fclose(fp_debug_ns_para);
	fclose(fp_debug_ns_vec1);
	fclose(fp_debug_ns_vec2);
	fclose(fp_debug_aecoutpwr);
	fclose(fp_debug_ns_vec3);
	fclose(fp_debug_ns_vec4);
	fclose(fp_debug_ns_vec5);
	fclose(fp_WnlmsFilterCoeff);
	fclose(fp_npvss_nlms);

	fclose(fp_debug_res_regression);

	fclose(fp_debug_Pwr_RX_AECIN_dB_C);
	fclose(fp_debug_ADF_C);
	fclose(fp_debug_NS);
	fclose(fp_debug_dump);

	fclose(fp_f0contour);
	fclose(fp_inoutVAD);

	fclose(fp_f0_based_preNS_para);

	 //solomonvoice_sub_module_point_tmp
	fclose(fp_fx_SolomonVoice_ringbacktone_detection);

	fclose(fp_debug_Flag_BWE_IN_dB_C);
	fclose(fp_debug_Flag_Outer_BWE_IN_dB_C);
	fclose(fp_debug_BWE_pwr_lowf_out_dB_C);
	fclose(fp_debug_BWE_pwr_highf_out_dB_C);
	fclose(fp_debug_lpc_gain_c);

	fclose(fp_dbg_Mixing);
	fclose(fp_dbg_MixingState);
	fclose(fp_fft_out_inner);
	fclose(fp_fft_out_outer);

	return 0;
}

