import java.util.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.OutputStream;
import java.io.FileInputStream;

public class WavAnalysis
{
	
	private static TargetDataLine line = null;

	private static void initDataLine() throws Exception
	{
		final AudioFormat format = new AudioFormat(
             44100.0F, 16, 1, true, false);
             
		DataLine.Info info = new DataLine.Info(
			TargetDataLine.class, format);
		line = (TargetDataLine) AudioSystem.getLine(info);
		line.open(format);
		line.start();

	}
        
    public static void main(String[] args)
    {
        //String fileLocation = "/home/jcd/a440.wav";
        //File file;
        //FileInputStream fis = null;
        

        try
        {			

            Long readBytes = 0L;
            Long currentByteCount;
            byte[] readBuff = new byte[44100];
            int buffCount;
            byte firstByte;
            byte secondByte;
            long currentCompleteSample;
            long lastCompleteSample = -1;
            boolean started = false;
            boolean pos = false;
            int freqCount;
            long max = 0L;
            long min = 0L;
            long mid = 65535/2;
            boolean go = false;

			initDataLine();
			
            while((buffCount = line.read(readBuff, 0, readBuff.length)) != -1)

            {
                readBytes += buffCount;
                freqCount = 0;
                for(int i = 0; i < buffCount; i++)
                {
                    // firstByte = readBuff[i];
                    // secondByte = readBuff[++i];

                    // currentCompleteSample = (short) ((firstByte << 8)
                       // | (secondByte & 0xFF));

                    currentCompleteSample = ((readBuff[i] & 0xffL)
                        | ((readBuff[++i] & 0xffL) <<  8));
                    if(currentCompleteSample > max)
                    {
                        max = currentCompleteSample;
                    }

                    if(currentCompleteSample < min)
                    {
                        min = currentCompleteSample;
                    }

                    // currentCompleteSample = Integer.parseInt(("" +
                    //   firstByte) + secondByte);

                    if(started)
                    {
                        if(pos)
                        {
                            if(currentCompleteSample < mid)//0)
                            {
                                pos = false;
                                freqCount ++;
                            }
                        }
                        else
                        {
                            if(currentCompleteSample >= mid) //0)
                            {
                                pos = true;
                                freqCount ++;
                            }
                        }
                    }
                    else
                    {
                        started = true;
                        if(currentCompleteSample >= mid)//0)
                        {
                            pos = true;
                        }
                    }
                }
                
                System.out.println("Freq: " + (freqCount));
                if((freqCount > 420) && (freqCount < 500))
                {
                    System.out.println("changing go");
                    go = true;
                }

                System.out.println("max [" + max + "] min [" + min + "]");
                
                if(go)
                {
                    line.stop();
                    line.flush();
                    go = false;
                    freqCount = 0;
                    String s;
                    Process p;
                    try
                    {
                        System.out.println("TRIGGERING");
                        p = Runtime.getRuntime().exec("python /home/jcd/Desktop/Desktop/down.py");
                        java.io.BufferedReader br = new java.io.BufferedReader(
                            new java.io.InputStreamReader(p.getInputStream()));
                        while ((s = br.readLine()) != null)
                            System.out.println("line: " + s);
                        p.waitFor();
                        System.out.println ("exit: " + p.exitValue());
                        p.destroy();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    
                    line.start();
                }
            }


            System.out.println("Finished.");

            line.close();
            line = null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(line != null)
            {
                try
                {
                    line.close();
                }
                catch(Exception e)
				{
					e.printStackTrace();
				}
            }
        }

    }
}
