Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Starting Speech Recognition on Device Events

7 Jan 2013 1  
Starting Speech Recognition on device events.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

 

I just started at Plantronics and started learning the Spokes SDK not too long ago.

Just to show how simple it is to use it and to give some idea on how many cool things can be done, I challenged myself to build this simple plugin that will start the Windows Speech Recognition tool when the headset is placed on (device event Don), and stop the application when the headset is removed (device event Doff).

Here's my IPlugin implementation (pretty much based on the sample code and cleaned up to use only what's needed):

using System;
using System.Windows.Forms;
using System.Threading;
using Plantronics.Device.Common;
using Plantronics.UC.Common;
using System.Diagnostics;
namespace Plantronics.Plugin.Sample.SpeechReco
{
 public class SpeechRecoPlugin : IPlugin
 {
        ISession m_session = null;
        IDevice m_device = null;

       
        public string Name
        {
            get { return "Plantronics SDK Speech Recognition Plugin"; }
        }
        
        public bool Init()
        {
            m_session = SessionManager.Instance.Register(Name);
            if (m_session != null)

            {
                m_device = m_session.ActiveDevice;
                if (m_device != null)
                {
                    // looking for Don and Doff only
                    // Don - start windows speech recognition app
                    // Doff - exits the app
                    m_device.DeviceListener.HeadsetStateChanged += new DeviceListenerEventHandler(
                        DeviceListener_HeadsetStateChanged);
                }

            }
            return true;
        }
         // Exit code
        public void Exit()
        {   
            if (m_device != null)
            {

                m_device.DeviceListener.HeadsetStateChanged -= DeviceListener_HeadsetStateChanged;
            }
            SessionManager.Instance.UnRegister(m_session);
        }
        #region DeviceListener events
        void DeviceListener_HeadsetStateChanged(object sender, DeviceListenerEventArgs e)
        {
            if (e.HeadsetStateChange == HeadsetStateChange.Don)
            {

                // check if speech reco tool is already running
                Boolean isSapiRunning = false;
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains("sapisvr"))
                    {
                        isSapiRunning = true;
                        break;
                    }

                }
                // start msft speech reco tool
                if (!isSapiRunning)
                {
                    Process p = Process.Start(@"C:\Windows\Speech\Common\sapisvr.exe",
                        @"-SpeechUX");
                }
            }
            else if (e.HeadsetStateChange == HeadsetStateChange.Doff)
            {

                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains("sapisvr"))
                    {
                        clsProcess.CloseMainWindow();
                        break;
                    }
                }
            }

        }
 
        #endregion DeviceListener events
 }
}

The same code can be used to start and stop any other application, and other events could be used to automate other tasks on your PC as well.

Here's are some pointers from Microsoft on speech recognition:

http://windows.microsoft.com/en-US/windows7/What-can-I-do-with-Speech-Recognition

And I highly recommend (this is really a must do) setting up your headset as the microphone to be used for speech recognition.

http://windows.microsoft.com/en-US/windows7/Set-up-your-microphone-for-Speech-Recognition

I hope you enjoy, and keep us posted on all sort of other cool things you are doing with the SDK.

This article was written by Ricardo de Andrade. Ricardo is a Systems Architect and Evangelist at Plantronics helping the developer community, clients and partners with the Spokes SDK and building solutions around current and future offerings. Ricardo has an extensive background in software and cloud distributed architectures, and in specific telecommunications. Ricardo previously worked for Microsoft where he helped clients and partners to develop cloud based speech recognition applications and integrate their web services into the Microsoft Tellme services.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here