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;
  int ecs;

  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;
            queue = new LinkedList<double[]>(queue.subList(0, trailer-1));
        }
    } 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 || speechStarted && queue.size() > ecs)
      return queue.remove();
    else
      return null;
  }
  public boolean eos() {
    return eosReached;
  }
}

@@ -331,16 +331,21 @@
  int scs
+ = 20
 ;%0A  int 
@@ -347,16 +347,21 @@
  int ecs
+ = 50
 ;%0A%0A  pub
@@ -675,24 +675,82 @@
 hed = true;%0A
+            int newSize = queue.size() - (ecs - trailer);%0A
             
@@ -803,17 +803,15 @@
 (0, 
-trailer-1
+newSize
 ));%0A
@@ -1216,16 +1216,37 @@
 Reached 
+ && queue.size() %3E 0 
 %7C%7C speec
@@ -1276,16 +1276,18 @@
 ) %3E ecs)
+ %7B
 %0A      r
@@ -1308,19 +1308,21 @@
 move();%0A
-
    
+ %7D
  else%0A  

Fix java speech trimmer.