cmusphinx/pocketsphinx-android

Timeout not working correct

Opened this issue · 0 comments

I have found out that timeout in continuous mode is not stopping utt. Is this a bug?
Made some changes in SpeechRecognizer.java to get it stopped (see "//UN" comments):

    @Override
    public void run() {

        recorder.startRecording();
        if (recorder.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
            recorder.stop();
            IOException ioe = new IOException(
                    "Failed to start recording. Microphone might be already in use.");
            mainHandler.post(new OnErrorEvent(ioe));
            return;
        }

        Log.d(TAG, "Starting decoding");

        decoder.startUtt();
        short[] buffer = new short[bufferSize];
        boolean inSpeech = decoder.getInSpeech();

        // Skip the first buffer, usually zeroes
        recorder.read(buffer, 0, buffer.length);
//UN moved from while loop
   if (inSpeech)
   		remainingSamples = timeoutSamples;
//UN end
        while (!interrupted()
                && ((timeoutSamples == NO_TIMEOUT) || (remainingSamples > 0))) {
            int nread = recorder.read(buffer, 0, buffer.length);

            if (-1 == nread) {
                throw new RuntimeException("error reading audio buffer");
            } else if (nread > 0) {
                decoder.processRaw(buffer, nread, false, false);

                // int max = 0;
                // for (int i = 0; i < nread; i++) {
                //     max = Math.max(max, Math.abs(buffer[i]));
                // }
                // Log.e("!!!!!!!!", "Level: " + max);
                
                if (decoder.getInSpeech() != inSpeech) {
                    inSpeech = decoder.getInSpeech();
                    mainHandler.post(new InSpeechChangeEvent(inSpeech));
                }
              //UN moved this out of while loop
              //UN if (inSpeech)
              //UN      remainingSamples = timeoutSamples;

                final Hypothesis hypothesis = decoder.hyp();
                mainHandler.post(new ResultEvent(hypothesis, false));
            }

            if (timeoutSamples != NO_TIMEOUT) {
                remainingSamples = remainingSamples - nread;
            }
        }

        recorder.stop();
        decoder.endUtt();

        // Remove all pending notifications.
        mainHandler.removeCallbacksAndMessages(null);

        // If we met timeout signal that speech ended
        if (timeoutSamples != NO_TIMEOUT && remainingSamples <= 0) {
            mainHandler.post(new TimeoutEvent());
        }
    }
}