private void ExtractDictationAlternates(ISpRecoResult recoResult, int maxAlternates)
 {
     if (recoResult != null && base.Grammar is DictationGrammar)
     {
         _alternates = new Collection <RecognizedPhrase>();
         IntPtr[] array = new IntPtr[maxAlternates];
         try
         {
             recoResult.GetAlternates(0, -1, maxAlternates, array, out maxAlternates);
         }
         catch (COMException)
         {
             maxAlternates = 0;
         }
         for (uint num = 0u; num < maxAlternates; num++)
         {
             ISpPhraseAlt spPhraseAlt = (ISpPhraseAlt)Marshal.GetObjectForIUnknown(array[num]);
             try
             {
                 IntPtr ppCoMemPhrase;
                 spPhraseAlt.GetSerializedPhrase(out ppCoMemPhrase);
                 try
                 {
                     RecognizedPhrase   recognizedPhrase = new RecognizedPhrase();
                     SPSERIALIZEDPHRASE phraseHeader     = RecognizedPhrase.GetPhraseHeader(ppCoMemPhrase, uint.MaxValue, _isSapi53Header);
                     bool hasIPAPronunciation            = (_header.fAlphabet & 1) != 0;
                     recognizedPhrase.InitializeFromSerializedBuffer(this, phraseHeader, ppCoMemPhrase, (int)phraseHeader.ulSerializedSize, _isSapi53Header, hasIPAPronunciation);
                     _alternates.Add(recognizedPhrase);
                 }
                 finally
                 {
                     Marshal.FreeCoTaskMem(ppCoMemPhrase);
                 }
             }
             finally
             {
                 Marshal.Release(array[num]);
             }
         }
     }
 }
Example #2
0
        private void ExtractDictationAlternates(ISpRecoResult recoResult, int maxAlternates)
        {
            // Get the alternates for dictation
            // alternates for dictation are not part of the recognition results and must be pulled out
            // from the recognition result bits.

            if (recoResult != null) // recoResult is null if we are in the case of our unit test.
            {
                if (Grammar is DictationGrammar)
                {
                    _alternates = new Collection <RecognizedPhrase>();
                    IntPtr[] sapiAlternates = new IntPtr[maxAlternates];
                    try
                    {
                        recoResult.GetAlternates(0, -1, maxAlternates, sapiAlternates, out maxAlternates);
                    }
                    catch (COMException)
                    {
                        // In some cases such as when the dictation grammar has been unloaded, the engine may not be able
                        // to provide the alternates. We set the alternate list to empty.
                        maxAlternates = 0;
                    }

                    //InnerList.Capacity = (int)numSapiAlternates;
                    for (uint i = 0; i < maxAlternates; i++)
                    {
                        ISpPhraseAlt phraseAlt = (ISpPhraseAlt)Marshal.GetObjectForIUnknown(sapiAlternates[i]);
                        try
                        {
                            IntPtr coMemSerializedPhrase;
                            phraseAlt.GetSerializedPhrase(out coMemSerializedPhrase);
                            try
                            {
                                // Build a recognition phrase result
                                RecognizedPhrase phrase = new();

                                // we cannot use a constructor parameter because RecognitionResult also derives from RecognizedPhrase
                                SPSERIALIZEDPHRASE serializedPhrase = RecognizedPhrase.GetPhraseHeader(coMemSerializedPhrase, uint.MaxValue, _isSapi53Header);

                                //
                                // If we are getting the alternates from SAPI, the alphabet should have already been converted
                                // to the alphabet we (applications) want.
                                //
                                bool hasIPAPronunciation = (_header.fAlphabet & (uint)SPRESULTALPHABET.SPRA_APP_UPS) != 0;

                                phrase.InitializeFromSerializedBuffer(this, serializedPhrase, coMemSerializedPhrase, (int)serializedPhrase.ulSerializedSize, _isSapi53Header, hasIPAPronunciation);
                                _alternates.Add(phrase);
                            }
                            finally
                            {
                                Marshal.FreeCoTaskMem(coMemSerializedPhrase);
                            }
                        }
                        finally
                        {
                            Marshal.Release(sapiAlternates[i]);
                        }
                    }
                }
            }
        }