Exemplo n.º 1
0
        private static object GetProperty(ISpRecognizer sapiRecognizer, string name, bool integer)
        {
            object         obj = null;
            SAPIErrorCodes sAPIErrorCodes;

            if (integer)
            {
                int plValue;
                sAPIErrorCodes = (SAPIErrorCodes)sapiRecognizer.GetPropertyNum(name, out plValue);
                obj            = plValue;
            }
            else
            {
                string ppCoMemValue;
                sAPIErrorCodes = (SAPIErrorCodes)sapiRecognizer.GetPropertyString(name, out ppCoMemValue);
                obj            = ppCoMemValue;
            }
            if (sAPIErrorCodes == SAPIErrorCodes.S_FALSE)
            {
                throw new KeyNotFoundException(SR.Get(SRID.RecognizerSettingNotSupported));
            }
            if (sAPIErrorCodes < SAPIErrorCodes.S_OK)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(new COMException(SR.Get(SRID.RecognizerSettingUpdateError), (int)sAPIErrorCodes));
            }
            return(obj);
        }
 private void Initialize(RecognizerInfo recognizerInfo)
 {
     try
     {
         _sapiRecognizer = new SapiRecognizer(SapiRecognizer.RecognizerType.InProc);
     }
     catch (COMException e)
     {
         throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(e);
     }
     if (recognizerInfo != null)
     {
         ObjectToken objectToken = recognizerInfo.GetObjectToken();
         if (objectToken == null)
         {
             throw new ArgumentException(SR.Get(SRID.NullParamIllegal), "recognizerInfo");
         }
         try
         {
             _sapiRecognizer.SetRecognizer(objectToken.SAPIToken);
         }
         catch (COMException e2)
         {
             throw new ArgumentException(SR.Get(SRID.RecognizerNotFound), RecognizerBase.ExceptionFromSapiCreateRecognizerError(e2));
         }
     }
     _sapiRecognizer.SetRecoState(SPRECOSTATE.SPRST_INACTIVE);
 }
        private void Initialize(RecognizerInfo recognizerInfo)
        {
            try
            {
                _sapiRecognizer = new SapiRecognizer(SapiRecognizer.RecognizerType.InProc);
            }
            catch (COMException e)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(e);
            }

            if (recognizerInfo != null)
            {
                ObjectToken token = recognizerInfo.GetObjectToken();
                if (token == null)
                {
                    throw new ArgumentException(SR.Get(SRID.NullParamIllegal), nameof(recognizerInfo));
                }
                try
                {
                    _sapiRecognizer.SetRecognizer(token.SAPIToken);
                }
                catch (COMException e)
                {
                    throw new ArgumentException(SR.Get(SRID.RecognizerNotFound), RecognizerBase.ExceptionFromSapiCreateRecognizerError(e));
                }
            }

            // For the SpeechRecognitionEngine we don't want recognition to start until the Recognize() or RecognizeAsync() methods are called.
            _sapiRecognizer.SetRecoState(SPRECOSTATE.SPRST_INACTIVE);
        }
Exemplo n.º 4
0
        private static object GetProperty(ISpRecognizer sapiRecognizer, string name, bool integer)
        {
            SAPIErrorCodes errorCode;
            object         result = null;

            if (integer)
            {
                int value;
                errorCode = (SAPIErrorCodes)sapiRecognizer.GetPropertyNum(name, out value);
                result    = value;
            }
            else
            {
                string value;
                errorCode = (SAPIErrorCodes)sapiRecognizer.GetPropertyString(name, out value);
                result    = value;
            }

            if (errorCode == SAPIErrorCodes.S_FALSE)
            {
                throw new KeyNotFoundException(SR.Get(SRID.RecognizerSettingNotSupported));
            }
            else if (errorCode < SAPIErrorCodes.S_OK)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(new COMException(SR.Get(SRID.RecognizerSettingUpdateError), (int)errorCode));
            }
            return(result);
        }
Exemplo n.º 5
0
        internal SapiRecognizer(RecognizerType type)
        {
            ISpRecognizer recognizer;

            try
            {
                if (type == RecognizerType.InProc)
                {
                    recognizer = (ISpRecognizer) new SpInprocRecognizer();
                }
                else
                {
                    recognizer = (ISpRecognizer) new SpSharedRecognizer();
                }
                _isSap53 = recognizer is ISpRecognizer2;
            }
            catch (COMException e)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(e);
            }

            // Back out if the recognizer we have SAPI 5.1
            if (!IsSapi53 && System.Threading.Thread.CurrentThread.GetApartmentState() == System.Threading.ApartmentState.STA)
            {
                // must be recreated on a different thread
                Marshal.ReleaseComObject(recognizer);
                _proxy = new SapiProxy.MTAThread(type);
            }
            else
            {
                _proxy = new SapiProxy.PassThrough(recognizer);
            }
        }
Exemplo n.º 6
0
        private static void SetProperty(ISpRecognizer sapiRecognizer, string name, object value)
        {
            SAPIErrorCodes sAPIErrorCodes = (SAPIErrorCodes)((!(value is int)) ? sapiRecognizer.SetPropertyString(name, (string)value) : sapiRecognizer.SetPropertyNum(name, (int)value));

            if (sAPIErrorCodes == SAPIErrorCodes.S_FALSE)
            {
                throw new KeyNotFoundException(SR.Get(SRID.RecognizerSettingNotSupported));
            }
            if (sAPIErrorCodes < SAPIErrorCodes.S_OK)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(new COMException(SR.Get(SRID.RecognizerSettingUpdateError), (int)sAPIErrorCodes));
            }
        }
Exemplo n.º 7
0
        internal SapiRecognizer(RecognizerType type)
        {
            ISpRecognizer spRecognizer;

            try
            {
                spRecognizer = ((type != 0) ? ((ISpRecognizer) new SpSharedRecognizer()) : ((ISpRecognizer) new SpInprocRecognizer()));
                _isSap53     = (spRecognizer is ISpRecognizer2);
            }
            catch (COMException e)
            {
                throw RecognizerBase.ExceptionFromSapiCreateRecognizerError(e);
            }
            if (!IsSapi53 && Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
            {
                Marshal.ReleaseComObject(spRecognizer);
                _proxy = new SapiProxy.MTAThread(type);
            }
            else
            {
                _proxy = new SapiProxy.PassThrough(spRecognizer);
            }
        }