#include <stdio.h>
#include <iostream.h>
#include <MidiShare.h>
#include "udpServerClass.h"
#include <time.h>
#include "AudioCollector.h"

udpServerClass* server;

char fftChars[16];
float fftVals[16];
float fftHistory[16][40];
int verbose = 0;

int historyIndex; 

void serverRecv(){
  
  int numBars = NUM_BARS;  
  int value = 0;
  
  value = server->recvString(fftChars);
  
  if(value > 0){
           
      for (int i = 0; i < NUM_BARS; i++){
        fftVals[i] = (float)(((int) fftChars[i])-1)/125.0;
      }
      
      for (int i = 0; i < NUM_BARS; i++){
	fftHistory[i][historyIndex] = fftVals[i];
      }      
      historyIndex++;
      
      if (verbose){
	for (int j = 0; j < NUM_BARS; j++){
	  printf("%02x ", fftChars[j]);  
	}
	cout<< endl;
	
	for (int j = 0; j < NUM_BARS; j++){
	  printf("%f ", fftVals[j]);  
	}
	cout<< endl;
	cout << endl;
      }

  }
}


int main( int argc, char *argv[])
{
  char c;
  
  printf("Argc = %d \n", argc);

  // verify number of command line parameters

  if (argc < 2) {
    printf("Usage is msReceive <port> <silent/verbose s/v>");
    return 0;
  }

  // read command line parameters
  
  int port_number = atol(argv[1]);
  if (argv[3][0] == 's'){
    verbose = 0;
    printf ("Silent..\n\n");
  } else if (argv[3][0] == 'v'){
    verbose = 1;
    printf("Verbose..\n\n");
  }



  server = new udpServerClass(port_number);
  server->setBlocking(TRUE); 
  
  while(1){
    usleep(100);
    serverRecv();
  }       
  
  server->closeSocket();
  delete(server);
  
  return 0;

}

