package zouxe.streamclient;

import PocketSphinxIce.*;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.util.Log;

class AudioRecorder {
	private static final int REC_SR = 16000;
	private static final int REC_CHAN = AudioFormat.CHANNEL_IN_MONO;
	private static final int REC_ENC = AudioFormat.ENCODING_PCM_16BIT;
	private static int bufferSize;
	private AudioRecord recorder = null;
	private Ice.Communicator communicator = null;
	private IPocketSphinxServerPrx server = null;
	private short[][] audioData;
	private int current;

	public AudioRecorder() {
		if (communicator == null)
			initIce();
		initServer();
	}

	public void record() {
		bufferSize = AudioRecord.getMinBufferSize(REC_SR, REC_CHAN, REC_ENC);
		recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
				REC_SR, REC_CHAN,
				REC_ENC, bufferSize);
		recorder.startRecording();
		audioData = new short[4096][];
		current = 0;
		new Thread(new Runnable() {
			public void run() {
				while (current < 4096 && recorder.getRecordingState() == AudioRecord.RECORDSTATE_RECORDING) {
					audioData[current] = new short[bufferSize];
					recorder.read(audioData[current++], 0, bufferSize);
				}
			}
		}).start();
	}

	private void initIce() {
		try {
			communicator = Ice.Util.initialize();
		} catch (Exception e) {
			Log.e("Ice", e.toString());
		}
	}

	private void initServer() {
		if (communicator == null)
			return;
		try {
			Ice.ObjectPrx base = communicator.stringToProxy("PocketSphinxServer:tcp -h 188.226.241.233 -p 20000");
			server = PocketSphinxIce.IPocketSphinxServerPrxHelper.checkedCast(base);
		} catch (Exception e) {
			Log.e("PocketSphinxServer", e.toString());
		}
	}

	public void stopRecord() {
		recorder.stop();
		new Thread(new Runnable() {
			public void run() {
				int i = 0;
				for (int j = 0; j < current; ++j) {
					i += audioData[j].length;
				}
				short[] data = new short[i];
				i = 0;
				for (int j = 0; j < current; ++j) {
					for (short s : audioData[j]) {
						data[i++] = s;
					}
				}
				try {
					Log.v("Output", server.decode(data));
				} catch (PocketSphinxIce.Error e) {
					Log.e("PocketSphinx", e.toString());
				}
			}
		}).start();
	}
}

@@ -178,16 +178,42 @@
 l.Log;%0A%0A
+import java.util.Arrays;%0A%0A
 class Au
@@ -582,18 +582,16 @@
  short%5B%5D
-%5B%5D
  audioDa
@@ -966,14 +966,22 @@
 ort%5B
-4096%5D%5B
+bufferSize*100
 %5D;%0A%09
@@ -1050,17 +1050,16 @@
 () %7B%0A%09%09%09
-%09
 while (c
@@ -1071,12 +1071,22 @@
 t %3C 
-4096
+bufferSize*100
  && 
@@ -1162,58 +1162,18 @@
 %09%09%09%09
-%09audioData%5Bcurrent%5D = new short%5BbufferSize%5D;%0A%09%09%09%09%09
+int tmp = 
 reco
@@ -1195,22 +1195,17 @@
 Data
-%5B
+, 
 current
-++%5D, 0
 , bu
@@ -1212,24 +1212,43 @@
 fferSize);%0A%09
+%09%09%09current += tmp;%0A
 %09%09%09%7D%0A%09%09%09%7D%0A%09%09
@@ -1845,254 +1845,8 @@
 ) %7B%0A
-%09%09%09%09int i = 0;%0A%09%09%09%09for (int j = 0; j %3C current; ++j) %7B%0A%09%09%09%09%09i += audioData%5Bj%5D.length;%0A%09%09%09%09%7D%0A%09%09%09%09short%5B%5D data = new short%5Bi%5D;%0A%09%09%09%09i = 0;%0A%09%09%09%09for (int j = 0; j %3C current; ++j) %7B%0A%09%09%09%09%09for (short s : audioData%5Bj%5D) %7B%0A%09%09%09%09%09%09data%5Bi++%5D = s;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1890,12 +1890,41 @@
 ode(
-data
+Arrays.copyOf(audioData, current)
 ));%0A

increase buffer size to fix pocketsphinx transcription