//Write to text file... 形式で出力したPraatのフォルマント解析結果を csv 形式に変換する
// 2010/04/20 宮澤 幸希
// 2012/02/03 指定した秒ごとに書き出すことで高速化を検討（フレーム長0.002sなら20、0.001sなら10がおすすめ）

import java.io.*;
import java.text.DecimalFormat;

public class ConvertPraatFormantTxt {
	public static void main(String args[]){
		String writeToTextFile = args[0];
		String outputFile = args[1];
		String outputTime = args[2];

		try {
			//書き出すファイルの準備
	    	writeFile(outputFile, "", true);	//初期化
	    	writeFile(outputFile, "time(s),forment1(Hz),formant2(Hz)\n", false);

	    	String output = "";

	    	//ファイル読み込み
		    BufferedReader brk = new BufferedReader( new InputStreamReader( new FileInputStream( writeToTextFile ) , "SJIS") );
		    String line = "";
		    int outputtimeCount = 1;
		    double timeStep = 0;
		    double thisTime = 0;
		    boolean f1Switch = false;
		    boolean f2Switch = false;
		    double f1 = 0;
		    double f2 = 0;
		    while((line = brk.readLine()) != null) {
		    	//Time Step（シフト長）を取得
		    	if (line.indexOf("dx = ") != -1) {
		    		String[] spl = line.split("= ");
		    		timeStep = Double.parseDouble(spl[1]);
		    		//System.out.println(timeStep);
		    	}
		    	//第一フレームの開始(?)時間を取得
		    	if (line.indexOf("x1 = ") != -1) {
		    		String[] spl = line.split("= ");
		    		thisTime = Double.parseDouble(spl[1]);
		    		//System.out.println(thisTime);
		    	}
		    	//各フレームの値を取得
		    	if(f1Switch == true){
		    		//System.out.println(line);
		    		String[] spl = line.split("= ");
		    		f1 = Double.parseDouble(spl[1]);
		    		f1Switch = false;
		    	}
		    	if(f2Switch == true){
		    		//System.out.println(line);
		    		String[] spl = line.split("= ");
		    		f2 = Double.parseDouble(spl[1]);
		    		f2Switch = false;

		    		//桁数をそろえて表示
		    		DecimalFormat df = new DecimalFormat();
		    		df.applyLocalizedPattern("#####.###");
		    		//System.out.println(df.format(thisTime) + "," + df.format(f1) + "," + df.format(f2));

		    		String outputLine = df.format(thisTime) + "," + df.format(f1) + "," + df.format(f2);
		    		output = output + outputLine + "\n";

		    		thisTime = thisTime + timeStep;

		    		//System.out.println(thisTime);

				    //outputTime秒ごとに書き出す
				    if ( (thisTime > (Integer.parseInt(outputTime) * outputtimeCount)) && (thisTime < (Integer.parseInt(outputTime) * outputtimeCount + timeStep)) ) {
				    	writeFile(outputFile, output, false);
				    	output = "";
				    	outputtimeCount++;
				    }
		    	}
		    	if (line.indexOf("formant [1]:") != -1) {
		    		f1Switch = true;
		    	}
		    	if (line.indexOf("formant [2]:") != -1) {
		    		f2Switch = true;
		    	}
		    }
			writeFile(outputFile, output, false);
		} catch (Exception e) {
			System.out.println(e);
		}
	}
	//ファイル出力
	public static void writeFile(String filePath, String data, boolean initCheck) {
		try {
		    if (initCheck == false) {
		    	//追加
		    	//System.out.println(filePath + "\t" + data);
			    OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(filePath, true) , "UTF-8");
			    out.write(data);
			    out.close();
		    } else {
		    	//新規
		    	//System.out.println(filePath + "\t" + data);
			    OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(filePath) , "UTF-8");
			    out.write("");
			    out.close();
		    }

		} catch (Exception e) {
			System.err.println(e);
		}
	}
}
