package talkhouse;
import java.util.LinkedList;

public class SpeechTrimmer {
  int leader = 5;
  int trailer = 5;
  boolean speechStarted = false;
  BentCentMarker centMarker = new BentCentMarker(); 
  int falseCount=0;
  int trueCount=0;
  LinkedList<double[]> queue = new LinkedList<double[]>();
  boolean eosReached = false;
  int scs = 20;
  int ecs = 50;
  Segmenter seg;

  public SpeechTrimmer(int frequency) {
    seg = new Segmenter(frequency/100, frequency/100);
  }

  public double[][] apply(double[] pcm) {
    if (eos())
      return null;

    double[][] segments = seg.apply(pcm);
    double[][] speechSegments= new double[segments.length][];
    int speechCount=0;
    for (int i=0;i<segments.length;++i) {
      enqueue(segments[i]);
      double[] centispeech = dequeue();
      while (centispeech != null) {
        speechSegments[speechCount++] = centispeech;
        centispeech = dequeue();
      }
      if (eos())
        break;
    }

    if (eos() && speechCount == 0)
      return null;

    double[][] result = new double[speechCount][];
    for (int i=0;i<speechCount;++i) {
      result[i] = speechSegments[i];
    }
    return result;
  }

  public void enqueue(double[] pcm) {
    if (eosReached)
      return;

    queue.offer(pcm);
    if (centMarker.apply(pcm)) {
      falseCount = 0;
      trueCount += 1;
    } else {
      falseCount += 1;
      trueCount = 0;
    }
    if (speechStarted) {
        if (falseCount == ecs) {
            eosReached = true;
            int newSize = queue.size() - ecs + trailer;
            queue = new LinkedList<double[]>(queue.subList(0, newSize));
        }
    } else if (trueCount > scs) {
      if (leader + scs < queue.size()) {
        int start = queue.size() - leader - scs - 1;
        int remainder = queue.size() - start;
        queue = new LinkedList<double[]>(queue.subList(
                                          start, start + remainder));
      }
      speechStarted = true;
    }
  }

  public double[] dequeue() {
    if (eosReached  && queue.size() > 0 || speechStarted && queue.size() > ecs) {
      return queue.remove();
    } else
      return null;
  }
  public boolean eos() {
    return eosReached;
  }
}

@@ -40,16 +40,67 @@
 kedList;
+%0Aimport java.util.List;%0Aimport java.util.ArrayList;
 %0A%0Apublic
@@ -650,46 +650,103 @@
     
-double%5B%5D%5B%5D speechSegments= new 
+if (segments == null)%0A      return null;%0A%0A    List%3Cdouble%5B%5D%3E resultQ = new ArrayList%3C
 double%5B
+%5D%3E(
 segm
@@ -760,34 +760,9 @@
 ngth
-%5D%5B%5D;%0A    int speechCount=0
+)
 ;%0A  
@@ -917,40 +917,20 @@
     
-speechSegments%5BspeechCount++%5D = 
+resultQ.add(
 cent
@@ -936,16 +936,17 @@
 tispeech
+)
 ;%0A      
@@ -1036,27 +1036,30 @@
 os() && 
-speechCount
+resultQ.size()
  == 0)%0A 
@@ -1085,153 +1085,60 @@
     
-double%5B%5D%5B%5D result = new double%5BspeechCount%5D%5B%5D;%0A    for (int i=0;i%3CspeechCount;++i) %7B%0A      result%5Bi%5D = speechSegments%5Bi%5D;%0A    %7D%0A    return result
+return resultQ.toArray(new double%5BresultQ.size()%5D%5B%5D)
 ;%0A  

Fix potential buffer overwrite in java implementation of speech trimmer.