1

I'm trying to transcribe text using webkitSpeechRecognition. I found this example:

https://developers.google.com/web/updates/2013/01/Voice-Driven-Web-Apps-Introduction-to-the-Web-Speech-API?hl=en

and have adopted it into my own site. This works great under certain conditions. However, I essentially just want to leave the transcription 'on' while people have a discussion.

I've simplified the code for my purposes as follows:

//Speech Transcription
var recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;

// initiated text recognition
recognition.onstart = function() {
    recognizing = true;
    ignore_onend = false;

}

//
recognition.onresult = function(event) {
    var interim_transcript = '';


    for (var i = event.resultIndex; i < event.results.length; ++i) {
        if (event.results[i].isFinal) {
            final_transcript += event.results[i][0].transcript;
        } else {
            interim_transcript += event.results[i][0].transcript;
        }
    }

    final_transcript = capitalize(final_transcript);

    // update final transcript
    enter.innerHTML = linebreak(final_transcript);

    // update temp transcript
    interim_enter.innerHTML = linebreak(interim_transcript);


}

recognition.onerror = function(event) { }

recognition.onend = function() {
    recognizing = false;
    if (ignore_onend) {
        return;
    }
}


// formatting the text
var two_line = /\n\n/g;
var one_line = /\n/g;
function linebreak(s) {
  return s.replace(two_line, '<p></p>').replace(one_line, '<br>');
}

var first_char = /\S/;
function capitalize(s) {
  return s.replace(first_char, function(m) { return m.toUpperCase(); });
}

// trigger the transcription
function startButton(event) {
    final_transcript = '';
    recognition.start();    
}

it works well for anywhere from 2 seconds to 5 minutes, but inevitably, randomly seems to stop. I saw the comments on the question here:

WebkitSpeechRecognition stops recording randomly

which imply a solution Obj3ctiv3_C_88 figured out. However i couldn't figure out how to implement what was described.

Any help is appreciated. Thanks!

Community
  • 1
  • 1
christopher
  • 137
  • 8

2 Answers2

1

I know this is a bit late but I also ran into this issue where voice input would randomly stop working. Hopefully you got your problem solved but this may help someone else in the future.

I am using this for voice input form filling. The solution you linked as provided by Obj3ctiv3_C_88 handles it - you can just basically cut and paste his code into your existing js. The setInterval calls the resetVoiceRecog function every 10000. That function forces a recognition stop. Then in your .onend function you can call .start. In my application I call the start unless the user has clicked a pause button:

if ($("#pauseClicked").html() !=1) {
  recognition.start();
}

I was originally waiting for an onend event and then doing a .start but even though it "said" it restarted, it would sporadically not accept voice input. Who knows that is going on on the backside of the onend/start but setting the interval timer to do a forced stop/start seems to have fixed the issue for me.

Jules Dupont
  • 7,259
  • 7
  • 39
  • 39
sjg123
  • 11
  • 1
0

I've noticed the behavior noted above as well. It seems like some sort of kink in the speech recognition engine, maybe to reduce traffic?

However, my solution, which may be a little more elegant way of handling this nuisance; was to set a variable called noReason to true in the recognition start method, and in the error and result methods I set it to false, as there is a reason (Error, or Results). Then in the onEnd method, I just check if(noReason), then if so, call recognition.start();

Gryu
  • 2,102
  • 2
  • 16
  • 29