Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When setting new language and restart in continous mode it gives error #3

Closed
ashram78 opened this issue Jun 10, 2016 · 3 comments
Closed

Comments

@ashram78
Copy link

ashram78 commented Jun 10, 2016

Hello, when i'm trying to set the language, i stop artyom and the i reinitiliaze it with the new language but it gives me an error. here is my code.

function start(language){
if(language){ language = language; } else { language = artyom.getLanguage(); }
artyom.initialize({
lang: language,// A lot of languages are supported. Read the docs !
continuous:true,// Artyom will listen forever
listen:true, // Start recognizing
debug:true, // Show everything in the console
speed:1, // talk normally
executionKeyword: "now"
});
if(artyom.isRecognizing()){
console.log("Artyom is listening to your commands");
} else {
console.log("Artyom is not listening to you.");
}
}

{
indexes:["switch to italian","switch to german","switch to french","switch to spanish","switch to portuguese","switch to dutch","switch to polish","switch to indonesian","switch to english"],
action: function(i){
switch(i){
case 0: stop(); start("it"); break;
case 1: start("de"); break;
case 2: start("fr"); break;
case 3: start("es"); break;
case 4: start("pt"); break;
case 5: start("nl"); break;
case 6: start("pl"); break;
case 7: start("id"); break;
case 8: start("en-GB"); break;
}
}
}

@sdkcarlos
Copy link
Owner

Hi @ashram78 , if you add an error listener to your code, you'll find that the problem will be identified with "recognition_overlap" (read more about artyom.when here). This error happens when you try to start artyom but it's still active.

To solve it, as you want immediately change of language, you can use a setTimeout to wait 300 ms to be started again.

The speechRecognition cannot be started till the last speechRecognition is finished. However the original API doesn't stop immediately the speechRecognition, therefore you need to use a setTimeout to prevent errors.

/**
 * Note that this commands will work only in english mode .. in other language they will be not so good 
 * recognized.
 */ 
artyom.addCommands({
    // note that if you're in spanish for example
    // the command should be instead : "Iniciar en ingles, Iniciar en alemán" etc...
    indexes:["switch to italian","switch to german",
             "switch to french","switch to spanish",
             "switch to portuguese","switch to dutch",
             "switch to polish","switch to indonesian",
             "switch to english"],
    action: function(i){
        switch(i){
            case 0: stop(); start("it-IT"); break;
            case 1: start("de-IT"); break;
            case 2: start("fr-FR"); break;
            case 3: start("es-ES"); break;
            case 4: start("pt-PT"); break;
            case 5: start("nl-NL"); break;
            case 6: start("pl-PL"); break;
            case 7: start("id-ID"); break;
            case 8: start("en-GB"); break;
        }
    }
});

// on command recognition end
artyom.when("COMMAND_RECOGNITION_END",function(status){
    if(status.code == "continuous_mode_enabled"){
        console.info("Command reconition finalized, restarting because the continuous mode is enabled");
    }
});

function start(language){
    if(!language){
        language = artyom.getLanguage(); 
    }

    var _startArtyom = function(){
        artyom.initialize({
            lang: language,// Start artyom with provided language
            continuous:true,// Continuous mode enabled
            listen:true, // Start recognizing
            debug:true, // Show everything in the console
            speed:1, // talk normally
            executionKeyword: "now" // say "now" at the end of a command to execute it immediately
        });

        console.log("Artyom is listening to your commands");
    };

    // stop artyom if stills active
    artyom.fatality();
    setTimeout(_startArtyom,250);
}

// Catch errors
artyom.when("ERROR",function(data){
    console.error(data);
});

start("en-GB");
// or 
// start("en-US");

@ashram78
Copy link
Author

Thank you Carlos for your great jpb and the quick response. I found the solution with this function:
pausecomp(3000);

function pausecomp(ms) {
ms += new Date().getTime();
while (new Date() < ms){}
}

it's a little hacky, but it works, :-)

@sdkcarlos
Copy link
Owner

@ashram78 , that works however that will block the UI. I recommend to use setTimeout function instead. Thanks for use artyom !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants