VBA中的C#类DLL

时间:2019-06-23 05:03:30

标签: c# vba

我正在从事机器学习,对Windows中的dll和com不太熟悉。
我在com中成功注册了我的C#dll,并且可以在VBA中使用 但是,即使无法工作 我已经看到了一些答案,但听不懂。 我应该怎么做?

例如,我的“文本转语音”功能在VBA中可以正常工作


    namespace VoiceAssistance
    {
      public class VoiceAssistance
       {
         public class SpeechRecedEventArg
         {
            public string TextResult;
            public float Confidence;
            public RecognizedAudio ResultAudio;
        }

        public class SpeeschNotRecedEventArg
        {
            public string TextResult;
            public RecognizedAudio ResultAudio;
        }

        SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();

        public delegate void SpeechRecognizedHandler(object sender, SpeechRecedEventArg e);
        public event SpeechRecognizedHandler OnSpeechRecognized;

        public delegate void SpeechRejectedHandler(object sender, SpeeschNotRecedEventArg e);
        public event SpeechRejectedHandler OnSpeechRejected;

        public string KeyWords;
        public int SilenceTimeOut = 1000;

        public VoiceAssistance()
        {

        }

        public void StartSpeechRecognitionEngine()
        {
            Choices keys = new Choices();
            keys.Add(KeyWords.Split(','));
            GrammarBuilder gb = new GrammarBuilder();
            gb.Append(keys);
            Grammar g = new Grammar(gb);
            recEngine.LoadGrammarAsync(g);

            recEngine.SetInputToDefaultAudioDevice();

            recEngine.SpeechRecognized += recEngine_SpeechRecognized;
            recEngine.SpeechRecognitionRejected += recEngine_SpeechRecognitionRejected;


            TimeSpan ts = new TimeSpan(0, 0, 0, 0, SilenceTimeOut);
            recEngine.EndSilenceTimeout = ts;
            recEngine.RecognizeAsync(RecognizeMode.Multiple);
        }

        public void StopSpeechRecognitionEngine()
        {
            recEngine.RecognizeAsyncStop();
        }

        private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            SpeechRecedEventArg SREA = new SpeechRecedEventArg();
            SREA.TextResult = e.Result.Text;
            SREA.Confidence = e.Result.Confidence;
            SREA.ResultAudio = e.Result.Audio;

            SpeechRecognizedHandler handler = OnSpeechRecognized;
            handler?.Invoke(this, SREA);           
        }

        private void recEngine_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            SpeeschNotRecedEventArg SNREA = new SpeeschNotRecedEventArg();
            SNREA.TextResult = e.Result.Text;
            SNREA.ResultAudio = e.Result.Audio;

            SpeechRejectedHandler handler = OnSpeechRejected;
            handler?.Invoke(this, SNREA);
        }

        // *********************************************************************************************************************

        SpeechSynthesizer synthesizer = new SpeechSynthesizer();

        public void Text2Speech(string Text, int Volume=100, int Rate = -1)
        { 
            synthesizer.Volume = Volume;  // 0...100
            synthesizer.Rate   = Rate;      // -10...10

            synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Adult);
            synthesizer.SpeakAsync(Text); 
        }
    }
}

0 个答案:

没有答案