#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>

#include "AudioStreamFromWave.h"
#include "MultiFFT.h"

#if 0
/* 今回は8chのデータを対象とする */

int main(int argc, char *argv[])
{
    int stat;
    int channels;
    int count;
    int index;
    int frame_index;
    
    int ch;
    int length;
    int advance;
    
    float *data[16];
    
    struct timeval tv1;
    struct timeval tv2;
    
    FILE *fp;

    MULTI_FFT_INFO_t *info;
    COMPLEX_FLOAT *f_data[16];
    COMPLEX_FLOAT f_data1[512];
    COMPLEX_FLOAT f_data2[512];
    COMPLEX_FLOAT f_data3[512];
    COMPLEX_FLOAT f_data4[512];
    COMPLEX_FLOAT f_data5[512];
    COMPLEX_FLOAT f_data6[512];
    COMPLEX_FLOAT f_data7[512];
    COMPLEX_FLOAT f_data8[512];
    
    if(argc != 2)
    {
        printf("Usage : %s <wav file>\n", argv[0]);
        return -1;
    }
    fp = fopen("Result_tablet.dat", "wt");
    if(fp == NULL)
    {
        printf("File Open Error.\n");
        return -1;
    }
    
    length  = 512;
    advance = 160;
    
    stat = OpenWaveFile(argv[1]);
    if(stat < 0)
    {
        return -1;
    }
    
    channels = GetWaveDataChannels();
    printf("channels = %d\n", channels);
    
    f_data[0] = f_data1;
    f_data[1] = f_data2;
    f_data[2] = f_data3;
    f_data[3] = f_data4;
    f_data[4] = f_data5;
    f_data[5] = f_data6;
    f_data[6] = f_data7;
    f_data[7] = f_data8;

    /* FFTの初期化 */    
    SetFFTWindowGain(1.0);
    info = InitMultiFFT("HANNING", 512);
    if(info == NULL)
    {
        printf("InitMultiFFT Error.\n");
        goto MultiFFTEnd;
    }
    
    {
        channels = GetWaveDataChannels();
        
        /* バッファ確保 */
        for(ch=0; ch<channels; ch++)
        {
            data[ch] = (float *)malloc(sizeof(float) * length);
            if(data[ch] == NULL)
            {
                do
                {
                    free(data[ch]);
                    data[ch] = NULL;
                    ch--;
                }while(ch>=0);
                
                ReleaseMultiFFT(info);
                fclose(fp);
                return -1;
            }
        }
    }
    
    {   
        gettimeofday(&tv1, NULL);
        
        /* 波形を読み出しながらFFT処理 */
        frame_index = 0;
        while(1)
        {
            /* データを読み出す */
            stat = ReadWaveData(data, length, advance, 0);
            //printf("Read stat = %d\n", stat);
            if(stat < 0)
            {
                break;
            }
            
            /* FFT演算 */
            MultiFFT(info, f_data, data, channels);

            if ((frame_index % 1000) == 0) {
              fprintf(fp, "\n [%d]\n\n", frame_index);
              for(index=0; index<512/2+1; index++)
              {
                fprintf(fp, "%f , %f\n", f_data1[index].real, f_data1[index].imag);
                fprintf(fp, "%f\n", sqrt(f_data1[index].real * f_data1[index].real + f_data1[index].imag * f_data1[index].imag));
              }
	    }            
            frame_index++;
        }
        
        gettimeofday(&tv2, NULL);
        
        ReleaseMultiFFT(info);
        
        /* バッファ開放 */
        for(ch=0; ch<channels; ch++)
        {
            free(data[ch]);
        }
    }
    
    {
        time_t      tv_sec;
        suseconds_t tv_usec;
        
        if(tv1.tv_usec > tv2.tv_usec)
        {
            tv_usec = 1000 * 1000 + tv2.tv_usec - tv1.tv_usec;
            tv2.tv_sec = tv2.tv_sec - 1;
        }
        tv_sec = tv2.tv_sec - tv1.tv_sec;
        
        printf("Exec Time : %d sec, %d usec\n", (int)tv_sec, (int)tv_usec);
    }
    
MultiFFTEnd:
    CloseWaveFile();
    fclose(fp);
    
    printf("Frame Index = %d\n", frame_index);
    
    return 0;

}
#endif

/* EOF */

