#include <math.h>
#include <stdio.h>
#include <sys/soundcard.h>
#include "Osc.h"
#include "miditabl.h"
#include "MIDIInpt.h"
#include "RTWvOut.h"

main(int argc, char** argv)
{
  int i, DELTA=4, j;
  int N = 1, over = 1;
  double freq=400, temp, temp2, out;
  RTWvOut output;
  Osc **o;
  MIDIInpt controller;
  
  if (argc>1)
    N = atoi(argv[1]);

  if (argc>2)
    freq = atoi(argv[2]);

  o = new (Osc*)[N];
  
  for (i=0; i<N; i++)
    o[i] = new Osc(i*freq, exp(-i));
  

  while(1)
  {
    if (controller.nextMessage() > 0) {
      //      printf("Got a message!\n");
      if (controller.getType() == MIDI_CTL_CHANGE)
      {
	temp2 = controller.getByteThree();
	temp = controller.getByteTwo();	
	j = (int) temp;
	printf("ControlChange   0.0 1 %i %f\n",j,temp2);

	if (j<N*2) 
	  if (j%2)
	  {
	    printf("Changed amp of o[%d] to %f\n", j/2, temp2/128);
	    o[j/2]->setAmp(temp2/128);
	  }
	  else
	  {
	    o[j/2]->setFreq(temp2*10);
	    printf("Changed freq of o[%d] to %f\n", j/2, temp2*10);
	  }    
      }
    }
    fflush(stdout);

    for (i=0;i<DELTA;i++)
    {
      out=0;
      for (i=0; i<N; i++)
	out += o[i]->tick();
      
      output.tick(out/sqrt(N));
    }
  }
}













