Speech Input Source

While our code is already set up for keywords to work, we still need to wire up the keywords to our app. In this section, we will be adding two new components to the LeftRect and RightRect objects: the Speech Input Source and the SetGlobal Listener. 

  1. Select the LeftRect object.
  2. Click on the Add Component button.
  3. Scroll down to Scripts and click on HoloToolkit.Unity.InputModule.
  4. Scroll down and select Speech Input Source:

You will see the No keywords have been assigned! warning. Let's fix that. 

  1. Click on the Add keyword plus sign on the right-hand side of the component:
  1. A line will appear; under Keyword, type change color.
  2. Click on the Add keyword plus sign again; in the second line under Keyword, type Reset:

At this moment, were you to go Build Compile and Deploy, you this would function, but let's talk about the code that is handling this from the ChangeRectColor.cs file before we do that:

public void VoiceColorChange()
    {
           rend.material.color = Color.blue;

    }
    public void ResetColor()
    {
          rend.material.color = Color.white;
          selected = false;
    }
    public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData)
    {
        var voiceCommand = eventData.RecognizedText.ToLower();

        switch (voiceCommand)
        {
           case "change color":
           {
              VoiceColorChange();
              break;
           }
          case "reset":
          {
              ResetColor();
              break;
          }
        default:
              break;
       }
   }

I know that we have not covered scripting yet and this may mean little at the moment, but it is important enough to get this functioning; so, we can go through it real quick and you can come back and revisit this once we have covered scripting and switches as well.

The last three methods of this class, that is, VoiceColorChange(), Reset(), and OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData) handle the work once Speech Input Source recognizes one of our words. 

OnSpeechKeywordRecognized receives the eventData from SpeechInputSource and the first thing it does is to create a variable named voiceCommand, and converts the RecognizedText in the eventData to lowercase using the ToLower() method. 

The next block of code is a switch statement that will take the information in the voiceCommand variable and compare that data with the cases offered in the switch statement, based on which voice command the change color or reset case is run, and then the VoiceColorChange() or ResetColor() methods are called. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.12.172