Speach.cs
上传用户:cookies0
上传日期:2022-07-28
资源大小:284k
文件大小:4k
源码类别:

语音合成与识别

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using SpeechLib;
  5. namespace tryreco
  6. {
  7.     class Speach
  8.     {
  9.         private static Speach _Instance = null;
  10.         private SpeechLib.SpVoiceClass voice = null;
  11.         public Speach()
  12.         {
  13.             BuildSpeach();
  14.         }
  15.         public static Speach instance()
  16.         {
  17.             if (_Instance == null)
  18.                 _Instance = new Speach();
  19.             return _Instance;
  20.         }
  21.         private void SetChinaVoice()
  22.         {
  23.             voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
  24.         }
  25.         private void SetEnglishVoice()
  26.         {
  27.             voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(1);
  28.         }
  29.         private void SpeakChina(string strSpeak)
  30.         {
  31.             SetChinaVoice();
  32.             Speak(strSpeak);
  33.         }
  34.         private void SpeakEnglishi(string strSpeak)
  35.         {
  36.             SetEnglishVoice();
  37.             Speak(strSpeak);
  38.         }
  39.         public void AnalyseSpeak(string strSpeak)
  40.         {
  41.             int iCbeg = 0;
  42.             int iEbeg = 0;
  43.             bool IsChina = true;
  44.             for (int i = 0; i < strSpeak.Length; i++)
  45.             {
  46.                 char chr = strSpeak[i];
  47.                 if (IsChina)
  48.                 {
  49.                     if (chr <= 122 && chr >= 65)
  50.                     {
  51.                         int iLen = i - iCbeg;
  52.                         string strValue = strSpeak.Substring(iCbeg, iLen);
  53.                         SpeakChina(strValue);
  54.                         iEbeg = i;
  55.                         IsChina = false;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     if (chr > 122 || chr < 65)
  61.                     {
  62.                         int iLen = i - iEbeg;
  63.                         string strValue = strSpeak.Substring(iEbeg, iLen);
  64.                         this.SpeakEnglishi(strValue);
  65.                         iCbeg = i;
  66.                         IsChina = true;
  67.                     }
  68.                 }
  69.             }
  70.             if (IsChina)
  71.             {
  72.                 int iLen = strSpeak.Length - iCbeg;
  73.                 string strValue = strSpeak.Substring(iCbeg, iLen);
  74.                 SpeakChina(strValue);
  75.             }
  76.             else
  77.             {
  78.                 int iLen = strSpeak.Length - iEbeg;
  79.                 string strValue = strSpeak.Substring(iEbeg, iLen);
  80.                 SpeakEnglishi(strValue);
  81.             }
  82.         }
  83.         private void BuildSpeach()
  84.         {
  85.             if (voice == null)
  86.                 voice = new SpVoiceClass();
  87.         }
  88.         public int Volume
  89.         {
  90.             get
  91.             {
  92.                 return voice.Volume;
  93.             }
  94.             set
  95.             {
  96.                 voice.SetVolume((ushort)(value));
  97.             }
  98.         }
  99.         public int Rate
  100.         {
  101.             get
  102.             {
  103.                 return voice.Rate;
  104.             }
  105.             set
  106.             {
  107.                 voice.SetRate(value);
  108.             }
  109.         }
  110.         private void Speak(string strSpeack)
  111.         {
  112.             try
  113.             {
  114.                 voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
  115.             }
  116.             catch (Exception err)
  117.             {
  118.                 throw (new Exception("发生一个错误:" + err.Message));
  119.             }
  120.         }
  121.         public void Stop()
  122.         {
  123.             voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
  124.         }
  125.         public void Pause()
  126.         {
  127.             voice.Pause();
  128.         }
  129.         public void Continue()
  130.         {
  131.             voice.Resume();
  132.         }
  133.     }
  134. }