#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "dsp_read_write.h"
#include "myfft.h"

int data_ffted[2210];//figure is after fft
int hist_ffted[2210 / 2 ];//it's histogram
int sampling=0;
int draw_data[2210];
int dsp_in;
int sequence[3][2];
#define WINDOW_SIZE     400

void set_sequence(int sqe[3][2] );

int check_sequence( int code, int sqe[3][2] )
{
int i;

for( i = 0; i < 3 ; i++ )
 {
   if( sqe[i][1] == 0 )
     {
      printf("sqe[%d][1] == 0 ", i);
     break;
     }
 }

if( sqe[i][0] > code - 100 &&  sqe[i][0] < ( code + 100) )
  sqe[i][1] = 1;

  if( sqe[2][1] == 1 )
    {
    set_sequence( sqe );
    return 1;
    }
  else
    {
    return 0;
    }
 

}


void set_sequence(int sqe[3][2] )
{
  int i;
  for(i =0; i < 3 ; i++)
  {  sqe[i][0]=0; sqe[i][1]=0;  }
  
  sqe[0][0] = 1200;
  sqe[1][0] = 1200;
  sqe[2][0] = 1200;

}

int biggest_element( )
{
int most;
int most_biggest_element_number;
int i;

most = data_ffted[1];
most_biggest_element_number = 1 ;

for( i = 1; i < 1105 ; i ++)
  {
    if( data_ffted[i] > most )
      {
	most = data_ffted[i];
	most_biggest_element_number = i ;
      }
  }
return most_biggest_element_number;
}

int draw_wave()
{
int   window_size = WINDOW_SIZE;
int n,p;
   
   
       glClear (GL_COLOR_BUFFER_BIT);
       glLineWidth (1.0);
       glBegin (GL_LINES);
      
	 for (n = 0; n < 2200; n += 1) {
	   //wave
	 glVertex2f ( n, draw_data[n]);
	 glVertex2f ( n+1, draw_data[n+1]);
	 }

        
	 //power_spectral
	     for( n = 0 ; n < 400 ; n ++ )
	     {
	       	 glVertex2f ( n, 0);
		 glVertex2f ( n ,  data_ffted[n+100]/30);
	      }
	       
       glEnd ();
       glutSwapBuffers();
       glFlush ();
       
}

void get_data()
{
  int i=0;


  unsigned int out_put;
  int zyo=0;
  int count = 0;
  int tmp = 0;

  double datar[2210],datai[2210];
  unsigned  char s[50];
   
  
 //
  for( zyo=0 ; zyo < 2210 ; zyo++)
    {
      hist_ffted[zyo] = 0;
      data_ffted[zyo] = 0;
      datar[zyo] = 0;
       datai[zyo] = 0;
       draw_data[zyo]=0;
    }
   
  zyo=0;
  count = 0;
      for( count=0 ; count < 221 ; count ++  )
      {
	memset( s, '\0', 50);
	dsp_getn(10, dsp_in, s);
	
	for( zyo = 0 ; zyo < 10  ;zyo++)
	  { 

	out_put =   (unsigned int)(s[zyo]);
	datar[zyo+count*10]= (double)(int)(out_put -124); //8bit data
	draw_data[zyo+count*10] =(int)(out_put*2); //8bit data    
	datai[zyo+count*10]= (double)(0);
	  }
      }

      myfft(11,datar,datai,-1);

      for(i=0;i< 1105 ; i++ ){
        data_ffted[i] = (double)(int)( sqrt ( (datar[i]*datar[i])+(datai[i]*datai[i]) ) );
         }

      tmp  = (int)(3.9825*biggest_element() );
      /*      printf("%d\n", tmp);
        if( check_sequence( tmp ,  sequence ) == 1 )
              {system("kterm &");}*/

      draw_wave();

}

void idle(void)
{
get_data();
}


void display (void) {
    glClear (GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
    glFlush ();
}

void reshape (int w, int h) {
  glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity ();
  gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

void keyfunc (unsigned char key, int x, int y) {
  if (key == 'q' || key == 'Q') exit (0);


}

void mousefunc (int button, int status, int xi, int yi) {
  if (status == GLUT_UP) {
    return; /* ボタンが離された場合 */
  } else {

  }
}


int main(int argc, char **argv)
{


 dsp_in  = dsp_iopen();
  dsp_mono_set(dsp_in);
  dsp_n_bit_set(dsp_in , 8);  
  dsp_n_sampling_rate(dsp_in , 8000);

  glutInit (&argc, argv);
  set_sequence( sequence );
  /* ウィンドウを開く */
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* モード設定 */
  glutInitWindowSize (400, 400);                /* ウィンドウの大きさ */
  glutInitWindowPosition (100, 100);            /* ウィンドウの位置 */
  glutCreateWindow (argv[0]);                   /* ウィンドウの生成 */
  
  glClearColor (0.0, 0.0, 0.0, 0.0);
  glShadeModel (GL_FLAT);

  glColor3f (1.0, 1.0, 1.0);

  glutDisplayFunc  (display);
  glutReshapeFunc  (reshape);
  glutKeyboardFunc (keyfunc);
  glutMouseFunc   (mousefunc);
  glutIdleFunc  (idle);

  glutMainLoop ();
 
  return 0;
}



