Response time of one or more actions C#

Asked

Viewed 4 times

0

I’m developing an app in windowsforms in order to create a personal assistant and I’m having a hard time limiting her action. The problem is that when I start the voice identification process: recognizeasync(Recognizemode.Multiple); And for some reason he starts recording a conversation or a song, he stops responding, then he restarts the code. However when this part is restarted for the second time and for some reason the previous problem of locking in the recording does not occur, because the environment is silent or something like that, it does the action 2x or more depending on how many times it restarted. Then I tried to create a limiter that is activated as soon as the first answer is given to then block the second, but apparently it did not work because the two are answered at the same time. So I tried to finally try to isolate the problem in another Forms but the fact of opening and closing the other Forms all the time made it hinder the use of the other applications on the pc. Do you have any idea how to make a kind of timer that cancels the action when a time limit is reached?

Code where the problem occurs:

    public void Gramática()
    {
        try
        {
            reconhecedor = new SpeechRecognitionEngine(ci);
        }
        catch (Exception ex)
        {
            txb_depurador.Text = "Erro ao integrar linguagem escolhida: " + ex.Message;
            TravaFala = false;

        }
        var gramatica = new Choices();
        gramatica.Add(ListaComandos);
        var gb = new GrammarBuilder();
        gb.Append(gramatica);
        try
        {

            var g = new Grammar(gb);
            try
            {
                Limitador = false;
                //O erro ocorre aqui
                reconhecedor.RequestRecognizerUpdate();
                reconhecedor.LoadGrammarAsync(g);
                reconhecedor.SpeechRecognized += Sre_Reconhecimento;
                reconhecedor.SetInputToDefaultAudioDevice();
                resposta.SetOutputToDefaultAudioDevice();
                reconhecedor.RecognizeAsync(RecognizeMode.Multiple);
                //Até aqui
            }
            catch (Exception ex)
            {
                txb_depurador.Text = "Erro ao criar reconhecedor: " + ex.Message;
                TravaFala = false;
            }
        }
        catch (Exception ex)
        {
            txb_depurador.Text = "Erro ao criar gramática: " + ex.Message;
            TravaFala = false;
        }
    }

Complete code:

    private void timer_1s_Tick(object sender, EventArgs e)
    {
        Init();
        Limite++;
    }

    void Sre_Reconhecimento(object sender, SpeechRecognizedEventArgs e)
    {
        string frase = e.Result.Text;
        txb_depurador.Text = "Comando usado: " + frase;
        try
        {
            if (Limite < 10 || Limitador == false)
            {
                Limitador = true;
                switch (frase)
                {
                    //Respostas Console
                    case "oi": { resposta.Speak("Olá, como posso te ajudar?"); break; }
                    case "quem é você": { resposta.SpeakAsync("sou papaguaio, seu assistente"); break; }
                    case "que horas são": { resposta.Speak("Agora são " + DateTime.Now.ToString("h e m")); break; }
                    //Resposta Alterar Volume                    
                    case "volume 10": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 100; break; }
                    case "volume 9": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 90; break; }
                    case "volume 8": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 80; break; }
                    case "volume 7": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 70; break; }
                    case "volume 6": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 60; break; }
                    case "volume 5": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 50; break; }
                    case "volume 4": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 40; break; }
                    case "volume 3": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 30; break; }
                    case "volume 2": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 20; break; }
                    case "volume 1": { resposta.SpeakAsync("Ok"); CoreAudioDevice defaultPlaybackDevice = new CoreAudioController().DefaultPlaybackDevice; defaultPlaybackDevice.Volume = 10; break; }
                }
            }
        }
        finally { TravaFala = false; Limite = 0;}
    }
    public void Gramática()
    {
        try
        {
            reconhecedor = new SpeechRecognitionEngine(ci);
        }
        catch (Exception ex)
        {
            txb_depurador.Text = "Erro ao integrar linguagem escolhida: " + ex.Message;
            TravaFala = false;

        }
        var gramatica = new Choices();
        gramatica.Add(ListaComandos);
        var gb = new GrammarBuilder();
        gb.Append(gramatica);
        try
        {

            var g = new Grammar(gb);
            try
            {
                Limitador = false;

                reconhecedor.RequestRecognizerUpdate();
                reconhecedor.LoadGrammarAsync(g);
                reconhecedor.SpeechRecognized += Sre_Reconhecimento;
                reconhecedor.SetInputToDefaultAudioDevice();
                resposta.SetOutputToDefaultAudioDevice();
                reconhecedor.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch (Exception ex)
            {
                txb_depurador.Text = "Erro ao criar reconhecedor: " + ex.Message;
                TravaFala = false;
            }
        }
        catch (Exception ex)
        {
            txb_depurador.Text = "Erro ao criar gramática: " + ex.Message;
            TravaFala = false;
        }
    }
    public void Init()
    {
        if (Limite == 20)
        {
            txb_depurador.Text = "Limite de tempo para resposta atingido";
            TravaFala = true;
            resposta.Volume = 100;
            resposta.Rate = 3;
            Gramática();
        }
        if (TravaFala == false)
        {
            TravaFala = true;
            resposta.Volume = 100;
            resposta.Rate = 3;
            Gramática();
        }
    }
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.