import ketai.sensors.*;

import java.lang.reflect.Method;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;

import android.media.AudioManager;
import android.media.AudioTrack;

//------------------------------------------

import info.kramann.filesystem.Data;
import info.kramann.mathematik.Statistik;
import info.kramann.wav.Wav;

public Statistik stat;
public Data dat;
public Wav wav;
byte[] testton;


private int PUFFERGROESSE = 8192;
private int SAMPLERATE = 44100;
private short[] data;
private double gross = 0.5*(255.0*256.0 + 255.0);
byte[] zwei = new byte[2];

//Bytepuffer in passender Größe anlegen:
byte[] puffer = new byte[PUFFERGROESSE*4];
public Mikrofon mikrofon;
public AudioTrack audioTrack;


String[] texte = {"Bitte Brille abnehmen", "Das","ist","ein","Test"};
String[] textefile = {"bitte", "das","ist","ein","test"};
byte[][] textewav;

double MAX_PHI = 90.0+50.0;
double MIN_PHI = 90.0-50.0;

double DELTA_PHI = (MAX_PHI-MIN_PHI)/texte.length;
double TOLERANZ_PHI = DELTA_PHI/4.0;
int AUSWAHL = -1;
int AUSWAHL_ALT = -1;


    private void double2Byte(byte[] zwei,double wert)
    {
                int gesamt = (int)(wert*gross + gross);    
               
                int hi = gesamt/256;
                int lo = gesamt - hi*256;
            
                int zh = hi-128;
                int zl = 0;
                if(lo<=127)
                    zl = lo;
                else        
                    zl = lo-256;
                    
                zwei[0]=(byte)zl;    
                zwei[1]=(byte)zh;    
      
    }


KetaiSensor sensor;

float accelerometerX, accelerometerY, accelerometerZ;

//   z in Tablet hinein
//       |-------|
//  Kabel| TABLET|----> y
//       |_______|
//           |
//           v x

void setup()
{
//String[] texte = {"Bitte Brille abnehmen", "Das","ist","ein","Test"};
//String[] textefile = {"bitte", "das","ist","ein","test"};
//byte[][] textewav = new byte[texte.length][];
    mikrofon = new Mikrofon(this);

          //create an audiotrack object
    audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLERATE, 
                                  AudioFormat.CHANNEL_OUT_STEREO, 
                                  AudioFormat.ENCODING_PCM_16BIT, 
                                  puffer.length, 
                                  AudioTrack.MODE_STREAM);
  
  
  sensor = new KetaiSensor(this);
  sensor.start();
  
    stat = new Statistik();
    dat = new Data(this);
    wav = new Wav(this,dat,stat);
  
  testton = dat.ladeBinaer("bitte.wav");
  testton = null;

    textewav = new byte[texte.length][];  
    for(int i=0;i<texte.length;i++)
    {
        textewav[i] = dat.ladeBinaer(textefile[i]+".wav"); 
    }
//    textewav[0] = dat.ladeBinaer("bitte.wav"); 
//    textewav[1] = dat.ladeBinaer("das.wav"); 
//    textewav[2] = dat.ladeBinaer("ist.wav"); 
//    textewav[3] = dat.ladeBinaer("ein.wav"); 
//    textewav[4] = dat.ladeBinaer("test.wav"); 
  
  
  orientation(LANDSCAPE);
  textAlign(CENTER, CENTER);
  textSize(64);
}

private int COUNTDOWN = 10;
void draw()
{
      if(COUNTDOWN>0)
      {
            COUNTDOWN--;
      }
      else if(COUNTDOWN==0)
      {
            audioTrack.play();
            mikrofon.start();
            COUNTDOWN--;
      }
      else
      {
      }
  
  
  background(78, 93, 75);
    
    
    // cos phi = az/g
    // g = sqrt(ax^2 + ay^2 + az^2)
    double g = Math.sqrt(accelerometerX*accelerometerX+accelerometerY*accelerometerY+
                         accelerometerZ*accelerometerZ);
    double phi = (180.0/Math.PI)*Math.acos(accelerometerZ/g);
    /*
  text("Accelerometer: \n" + 
    "x: " + nfp(accelerometerX, 1, 3) + "\n" +
    "y: " + nfp(accelerometerY, 1, 3) + "\n" +
    "z: " + nfp(accelerometerZ, 1, 3) + "\n" +
    "phi = "+phi, 0, 0, width, height);
    */
  int AUS = -1;  
  for(int i=0;i<texte.length;i++)
  {
        double y_betrachter = ((double)(height*texte.length)*(phi - MIN_PHI))/(MAX_PHI-MIN_PHI);
        double phi_betrachter = MIN_PHI + (double)i*((MAX_PHI-MIN_PHI)/(double)texte.length);
        double y =  ((double)(height*texte.length)*(phi_betrachter - MIN_PHI))/(MAX_PHI-MIN_PHI);
    
        if(Math.abs(phi_betrachter-phi)<TOLERANZ_PHI)
        {
             AUS = i;
             fill(100,255,100);
        }
        else
        {
             fill(100,100,100);
        }
//        float br = size(texte[i]);
        text(texte[i],-width/4, (float)(y-y_betrachter), width, height);
        text(texte[i],+width/4, (float)(y-y_betrachter), width, height);
        
        
  }
  AUSWAHL     = AUS;
}

double t = 0.0;
double dt = 1.0/44100.0;

int iii=44;
public void onAudioEvent(short[] _data)
{
      data= _data;
      for(int i=0;i<PUFFERGROESSE;i++)
      {
            double wert = 0.8*Math.sin(2.0*Math.PI*440.0*t);
           double2Byte(zwei,wert);
           
           
           //puffer[i*4+0] = zwei[0];
           //puffer[i*4+1] = zwei[1];
           //puffer[i*4+2] = zwei[0];
           //puffer[i*4+3] = zwei[1];
           if(testton!=null)
           {
              puffer[i*4+0] = testton[iii+0];
              puffer[i*4+1] = testton[iii+1];
              puffer[i*4+2] = testton[iii+0];
              puffer[i*4+3] = testton[iii+1];
           }
           else
           {
              double2Byte(zwei,0.0);
              puffer[i*4+0] = zwei[0];
              puffer[i*4+1] = zwei[1];
              puffer[i*4+2] = zwei[0];
              puffer[i*4+3] = zwei[1];
           }
           t+=dt;
           
           if(i%2==0)
           {
              iii+=2;
              if(testton!=null)
                  iii%=testton.length;
              else
                  iii=0;
              if(iii==0)
              {
                  iii=44;
                  
                  if(AUSWAHL != AUSWAHL_ALT)
                  {
                        if(AUSWAHL>=0)
                            testton = textewav[AUSWAHL];
                        else
                            testton = null;
                        
                        AUSWAHL_ALT = AUSWAHL;
                  }
              }    
           }   
      }
      int REST  = puffer.length;       
      int START = 0;
      int geschrieben = audioTrack.write(puffer, START,REST);
      
}

void onAccelerometerEvent(float x, float y, float z)
{
  accelerometerX = x;
  accelerometerY = y;
  accelerometerZ = z;  
}

