5

I'm trying to use annyang to convert speech into text, but I've ran into some problems. It works, but there's a few things that doesn't yet. First, I would like to know how would I be able to pass whatever the user said, into the alert function. Next, I would like to know how to end the annyang function when the user finished speaking. And finally, I'd like to know how to keep the allow and disallow microphone prompt from appearing again and again once it appeared once.

<script>
if (annyang) {

  var commands = {
    'Hello': function() {
      alert("Success");
    }
  };

  annyang.addCommands(commands);

}
</script>

<input type = 'submit' value = 'listen' onclick = "annyang.start();">
frosty
  • 2,559
  • 8
  • 37
  • 73

3 Answers3

4

Intead of use annyang for convert to text , you could test yourself with the original google speechrecognition demo

Original Demo

See the source code of above and you will easily do what you want with SpeechRecognition

I recommend this, because annyang is more a Voice Control Plugin. By the other side you can use Artyom.js in case that you want to use a library for this.

Artyom offers an easy "dictation" object to convert speech to text quickly:

var settings = {
    continuous:true, // Don't stop never because i have https connection
    onResult:function(text){
        console.log(text);
    },
    onStart:function(){
        console.log("Dictation started by the user");
    },
    onEnd:function(){
        alert("Dictation stopped by the user");
    }
};

var UserDictation = artyom.newDictation(settings);

// Start listening
UserDictation.start();

// To stop
//UserDictation.stop();

The language needs to be providen in the initialize method.

Carlos Delgado
  • 2,930
  • 4
  • 23
  • 49
3
var anything = function(anything) {
    alert(anything);
};
var commands = {
    '*anything': anything
};

this works, also it would not allert defined commands

1

I would like to know how would I be able to pass whatever the user said, into the alert function.

You can do something like

<script>
if (annyang) {

  var commands = {
    'Hello :variable': function(variable) {
      alert(variable);
    }
  };

  annyang.addCommands(commands);

}
</script>

variable is the string, that the webspeech-api has recognized.

Next, I would like to know how to end the annyang function when the user finished speaking.

Set continuousto false. Annyang will automatically stop the recognition, when the user has finished talking.

annyang.start({ autoRestart: false, continuous: false });

You can also add a callback function, that annyang will call, when the speech recognition has finished:

annyang.addCallback('end', function () { // your code here});

And finally, I'd like to know how to keep the allow and disallow microphone prompt from appearing again and again once it appeared once.

The only way to prevent this, is to deliver the site via https not http There is no other way to achieve it. Also it will improve the speed of the recognition.

Michael B
  • 1,660
  • 3
  • 28
  • 59
  • Is there a way to pass whatever the user said into the alert function without a preemptive word before it? Basically, without having to say hello. – frosty Aug 19 '15 at 07:51
  • Unfortunately I have never tried that before... Have you tried just adding the variable without the "hello"? `':variable': function(variable) {` – Michael B Aug 19 '15 at 07:56
  • No, I haven't. But I'll try it. – frosty Aug 19 '15 at 07:57
  • Also when using variable it only recognizes one word at a time. If you speak more than one word, it doesn't output anything. – frosty Aug 19 '15 at 08:08
  • Ok, basically Annyang is for recognizing **Voice Commands**, not for dictation. _A tiny javascript SpeechRecognition library that lets your users control your site with voice commands._ But I think what you want might possible nonetheless. Just add the annyang callback `result`. This is fired as soon as some speech was identified. Here is the documentation: https://github.com/TalAter/annyang/blob/master/docs/README.md#addcallbacktype-callback-context – Michael B Aug 19 '15 at 08:44
  • @frosty that's because you should write `*variable` instead of `:variable`, the former catches all words spoken after "hello"! – luke_mclachlan Feb 02 '18 at 22:18