How to activate on a magic word only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kingabhi
    New Member
    • May 2010
    • 5

    How to activate on a magic word only

    Hello Guys,
    I am working on a home automation project which is voice based.Everythin g is working fine.But what i wanna do is that the recogniton becomes activated only when a magic word is spoken like "alpha gamma" and the system says"welcome master gimme ur commands" and then when i speak"switch on" then only it performs the action.If i say "switch on" before saying "alpha gamma" then it simply ignores it.
    Partial code is as follows:-
    Code:
     private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
            {
                txtReco.Text = Result.PhraseInfo.GetText(0, -1, true);
                
                //..........................Device 1------------------------------------
                    if (txtReco.Text == "Device One ON")
                    {
    
    
                        //SerialPortDataReader.SendDataToPort("1");
                        controller.Speak("SWITCHED ON");
                        //System.Diagnostics.Process.Start(@"C:/1.wma");
                        //System.Diagnostics.Process.Start("msconfig.exe");
                        //------------------My Alter
                        System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                        myPlayer.SoundLocation = @"c:\on.wav";
                        myPlayer.Play();
    
                        //------------------My Alter
                        //TODO: Insert Image
                        Bitmap bmp = new Bitmap(@"C:/1.jpg");
                        pbImage.Image = bmp;
    
                       
    
    
                    }
                    else if (txtReco.Text == "Device One OFF")
                    {
    
                        controller.Speak("Ha ha ha ha ah...");
                        //------------------My Alter
                        System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                        myPlayer.SoundLocation = @"c:\off.wav";
                        myPlayer.Play();
    
                        //------------------My Alter
                        SerialPortDataReader.SendDataToPort("5");
    
                    }
                    //............................Device 2....................................
                    else if (txtReco.Text == "Device Two ON")
                    {
                        SerialPortDataReader.SendDataToPort("2");
    
    
                    }
                    else if (txtReco.Text == "Device Two OFF")
                    {
                        SerialPortDataReader.SendDataToPort("6");
    
                    }
                    //.....................Device 3-----------------------------------
                    else if (txtReco.Text == "Device Three ON")
                    {
                        SerialPortDataReader.SendDataToPort("3");
    
                    }
                    else if (txtReco.Text == "Device Three OFF")
                    {
                        SerialPortDataReader.SendDataToPort("7");
    
                    }
    Thanks in advance
    -King
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    May I make a suggestion about the architecture of your project?

    You don't want this system of
    If "device one on",
    "Device two on"
    etc. if you can avoid it.

    When you have 1000 devices it makes for a real mess. First your people can't remember which is device 800 and which is device 949. Second your code is mile long.

    If it is possible, take the spoken phrase and study it, take it apart and then act on it with a more generic method.

    You are trying to control objects in the real world. The is exactly what Object Oriented Programming was designed around. I might suggest something more along this direction.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Winston
    {
        public partial class Form1 : Form
        {
            private List<RemoteDevice> myRemoteDeviceList = new List<RemoteDevice>();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Load all of my SavedRemote devices
    
                // Activate the 
            }
    
            private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
            {
                string UserSentence = Result.PhraseInfo.GetText(0, -1, true);
    
                // If our format for commands is  "Device {name} {command}" let's break that down
    
                string[] Sentence = UserSentence.Split(' ');
                if (Sentence.Length == 3)
                {
                    // Right sentence length. This could be a vaild instruction
                    if (Sentence[0] == "Device")
                    {
                        // So far so good
                        foreach (RemoteDevice Rosey in myRemoteDeviceList)
                        {
                            if (Sentence[1] == Rosey.Name)
                            {
                                // We found a vaild device
                                string CommandResult = Rosey.DoCommand(Sentence[2]);
                            }
                        }
                    }
                }
    
            }
        }
    
        class RemoteDevice
        {
            public string Name { get; set; }
    
            public RemoteDevice(string NewName)
            {
                Name = NewName;
            }
    
            public virtual string DoCommand(string CommandInput)
            {
                switch (CommandInput.ToLower())
                {
                    case "on":
                        return On();
                        break;
                    case "off":
                        return Off();
                        break;
                }
                return "Device not found";
            }
    
            public virtual string On()
            {
                string Results = string.Empty;
                // since each remote device has its own needs
                // each instance will override this to handle its 
                // own process for turning on.
                return Results;
            }
    
            public virtual string Off()
            {
                string Results = string.Empty;
                // since each remote device has its own needs
                // each instance will override this to handle its 
                // own process for turning off.
                return Results;
            }
    
        }
    }
    This code will process commands for 1 or 1000 RemoteDevices without growing in length.

    Now you just add an instance of RemoteDevice for each device you want to control. Give each device it's own personallizatio n. In other words, the program doesn't know much about each device. The device is responseible for itself.

    Code:
            class WashingMachine : RemoteDevice
            {
    
                public override string  On()
                {
     	            //SerialPortDataReader.SendDataToPort("1");
                    controller.Speak("SWITCHED ON");
    
                    //System.Diagnostics.Process.Start(@"C:/1.wma");
                    //System.Diagnostics.Process.Start("msconfig.exe");
                    //------------------My Alter
                    System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                    myPlayer.SoundLocation = @"c:\on.wav";
                    myPlayer.Play();
                    return "Switched On";
                }
            }

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      As for the command password, you check it in the Reco_Event.
      If the password is given, set a flag to true.
      Have a timer running. If no commands are given for 30 seconds then set the flag back to false.
      Then when you have a valid command sentence, first check if the flag is true. If its not, don't proceed.

      Comment

      • kingabhi
        New Member
        • May 2010
        • 5

        #4
        WoW.....awesome ..m really very thankful to you.

        Comment

        • kingabhi
          New Member
          • May 2010
          • 5

          #5
          Hi tlhintoq,this is what I am implimenting
          Code:
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using SpeechLib;
          using System.Drawing;
          using System.Text;
          using System.Windows.Forms;
          using System.Threading;
          
          namespace Winston
          {
              public partial class Form1 : Form
              {
                 private List<RemoteDevice> myRemoteDeviceList = new List<RemoteDevice>();
          
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void Form1_Load(object sender, EventArgs e)
                  {
                      // Load all of my SavedRemote devices
          
                      // Activate them 
                  }
          
                  private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
                  {
                      string UserSentence = Result.PhraseInfo.GetText(0, -1, true);
          
                      // If our format for commands is  "Device {name} {command}" let's break that down
          
                      string[] Sentence = UserSentence.Split(' ');
                      if (Sentence.Length == 3)
                      {
                          // Right sentence length. This could be a vaild instruction
                          if (Sentence[0] == "Device")
                          {
                              // So far so good
                              foreach (RemoteDevice Rosey in myRemoteDeviceList)
                              {
                                  if (Sentence[1] == Rosey.Name)
                                  {
                                      // We found a vaild device
                                      string CommandResult = Rosey.DoCommand(Sentence[2]);
                                  }
                              }
                          }
                      }
          
                  }
              }
              
              class RemoteDevice
              {
                  public string Name 
                  { get; 
                    set; 
                  }
          
                  public RemoteDevice(string NewName)
                  {
                      Name = NewName;
                  }
          
                  public virtual string DoCommand(string CommandInput)
                  {
                      switch (CommandInput.ToLower())
                      {
                          case "on":
                              return On();
                              break;
                          case "off":
                              return Off();
                              break;
                      }
                      return "Device not found";
                  }
          
                  public virtual string On()
                  {
                      string Results = string.Empty;
                      // since each remote device has its own needs
                      // each instance will override this to handle its 
                      // own process for turning on.
                      return Results;
                  }
          
                  public virtual string Off()
                  {
                      string Results = string.Empty;
                      // since each remote device has its own needs
                      // each instance will override this to handle its 
                      // own process for turning off.
                      return Results;
                  }
          
              }
          }
          
          class WashingMachine : RemoteDevice
          {
          
              public override string On()
              {
                  //SerialPortDataReader.SendDataToPort("1");
                  controller.Speak("SWITCHED ON");
          
                  //System.Diagnostics.Process.Start(@"C:/1.wma");
                  //System.Diagnostics.Process.Start("msconfig.exe");
                  //------------------My Alter
                  System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                  myPlayer.SoundLocation = @"c:\on.wav";
                  myPlayer.Play();
                  return "Switched On";
              }
          }
          But i am getting an error
          Error 1 The type or namespace name 'RemoteDevice' could not be found (are you missing a using directive or an assembly reference?)
          Please Help.

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Move the WashingMachine class inside the namespace of Winston.
            As you have it, it is totally outside the namespace, Thus it has no idea of class RemoteDevice.

            Comment

            • kingabhi
              New Member
              • May 2010
              • 5

              #7
              This time i am getting
              Code:
              Error	1	'SpeechDiff.RemoteDevice.Name.get' must declare a body because it is not marked abstract or extern
              Error	2	'SpeechDiff.RemoteDevice.Name.set' must declare a body because it is not marked abstract or extern
              Please help..i am a newbie.

              Comment

              Working...