0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech.Recognition;

namespace ConsoleApp2
{
  class Program
  {
     static SpeechRecognitionEngine recEngine = new 
     SpeechRecognitionEngine();
     bool keyHold = false;

     static void Main(string[] args)
     {
        Choices commands = new Choices();
        commands.Add(new string[] { "Current dollar value", "Current euro value" });
        GrammarBuilder gBuilder = new GrammarBuilder();
        gBuilder.Append(commands);
        Grammar grammar = new Grammar(gBuilder);

        recEngine.LoadGrammarAsync(grammar);
        recEngine.SetInputToDefaultAudioDevice();
        recEngine.RecognizeAsync(RecognizeMode.Multiple);
     }

     void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
     {
         switch (e.Result.Text)
         {
            case "Current dollar value":
                Console.WriteLine("10kr");
                break;

            case "Current euro value":
                Console.WriteLine();
                break;
        }
     }
  }
}

The program is just closing on startup, and does not record my voice, I tried with putting a console.readkey(); under this line in the code recEngine.RecognizeAsync(RecognizeMode.Multiple);. It holds the program up and did not close automatically ofc but still the program does not record my incoming voice command.
Why is this?

Jimi
  • 29,621
  • 8
  • 43
  • 61
wille480
  • 45
  • 8
  • you need some type of application loop where events are responded to. – Ctznkane525 Jan 08 '18 at 23:06
  • Where did you subscribe that event handler? `recEngine.SpeechRecognized += new EventHandler(recEngine_SpeechRecognized);` – Jimi Jan 08 '18 at 23:25
  • Mind that you should comment under the answer if you need clarifications, not make a new answer. I'm not receiving a notification for it. To the point: in the answer I just inserted the relevant code, starting with `static void Main()`. Insert that code in your original `namespace ConsoleApp2 { class Program { **Here goes the code** } }`. – Jimi Jan 09 '18 at 13:39
  • Did you manage to insert the code in the right place? Note that you can comment on anything inside your own question. Also note that if your system is Windows 7, `System.Speech.Recognition` will not work. You need [Microsoft Speech Platform](https://msdn.microsoft.com/en-us/library/office/hh361572(v=office.14).aspx) - SDK, Runtime and languages. – Jimi Jan 09 '18 at 15:09
  • Hey Jimi ! Yees i managed it, really appriciate the help you came with :) working currently on making the program open notepad on the recognize "notepad" command ^^ – wille480 Jan 10 '18 at 08:05
  • Need some help? BTW, I saw this by pure chance. If you want to ping someone, put an at mark "@" prefix to the nick if you're not commenting under this person's question/answer.. – Jimi Jan 11 '18 at 00:11

1 Answers1

1

Speech Recognition is event-driven.
You need to define the necessary event handlers.
SpeechRecognitionEngine.SpeechRecognized Event must be subscribed to receive the results of the recognition.
See also SpeechRecognitionEngine.AudioStateChanged Event

Also, the Engine must be updated with the new settings before starting the Recognition:
See SpeechRecognitionEngine.RequestRecognizerUpdate Method

Note:
I used only "dollar" and "euro" as Grammar items as a hint: try to use the less key words possible in you grammars, otherwise they become very difficult to handle. You can pronounce the whole "Current dollar value" phrase, the recognized key word, however, is "dollar".


using System.Speech.Recognition;

  static void Main(string[] args)
  {
    using (SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine())
    {
       recEngine.SpeechRecognized += 
             new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
       recEngine.AudioStateChanged += 
             new EventHandler<AudioStateChangedEventArgs>(recEngine_AudioStateChange);

       Choices commands = new Choices();
       commands.Add(new string[] { "dollar", "euro" });
       GrammarBuilder gBuilder = new GrammarBuilder();
       gBuilder.Append(commands);
       Grammar grammar = new Grammar(gBuilder);

       recEngine.SetInputToDefaultAudioDevice();
       recEngine.LoadGrammarAsync(grammar);
       recEngine.RequestRecognizerUpdate();

       recEngine.RecognizeAsync(RecognizeMode.Multiple);

       Console.ReadKey();
       recEngine.SpeechRecognized -= recEngine_SpeechRecognized;
       recEngine.AudioStateChanged -= recEngine_AudioStateChange;
    }
  }

  internal static void recEngine_AudioStateChange(object sender, AudioStateChangedEventArgs e)
  {
     Console.WriteLine("Current AudioLevel: {0}", e.AudioState);
  }

  internal static void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
     switch (e.Result.Text)
     {
        case "dollar":
           Console.WriteLine("10kr");
           break;
        case "euro":
           Console.WriteLine("A lot more");
           break;
     }
  }

Note2:
On Windows 7, System.Speech.Recognition is not working.
Speech Recognition and TTS (Text To Speech) can be enabled using the free development tools of Microsoft Speech Platform. Download and install the SDK, Runtime and desired languages. This is the server speech recognition implementation, developed to support speech recognition through telephone lines. So it features a very good native filter for noises and a good tolerance in regard to pronunciation quirks.

Jimi
  • 29,621
  • 8
  • 43
  • 61