DotNetSpeech ---- text-to-speech wave file

wav operation 
introduced dll
need to select (DotNetSpeech.dll), after the introduction of dll, right-click the project introduced, select properties, to "embed interoperable type" is set to False. Or will not be prompted Embed Interop Types "SpeechLib.SpVoiceClass". Please use a suitable interface.

DotNetSpeech.SpeechAudioFormatType.SAFTCCITT_uLaw_11kHzMono
represent audio encoding formatG711U
           var text = " I have to test AB consecutive letters spaces! " ;
             var path = Path.Combine ( @ " C: \ ZZ_VoiceFile " , $ " 1.wav " ); 
            text = AddKongGeToPlateNo (text) .trim (); 
            SpVoiceUtil SpVoiceUtil = new new SpVoiceUtil (); 
            SpVoiceUtil.setRate ( 0 ); 
            SpVoiceUtil.setVolume ( 100 ); 
            SpVoiceUtil.WreiteToWAV (path, text, DotNetSpeech.SpeechAudioFormatType.SAFTCCITT_uLaw_11kHzMono); // SAFT11kHz16BitMono generating wav file 



       // 连续字母中加空格
        private static string AddKongGeToPlateNo(string s)
        {
            int length = s.Length;
            string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            for (int i = 0; i < length - 1; i++)
            {
                string str = s.Substring(i, 1);
                string str1 = s.Substring(i + 1, 1);
                if (letters.Contains(str) && letters.Contains(str1))
                {
                    s = s.Substring(0, i + 1) + " " + s.Substring(i + 1);
                    length = length + 1;
                }
            }
            return s;
        }
        

Help class

 class SpVoiceUtil 
    { 
        SpVoice Voice = new new DotNetSpeech.SpVoiceClass (); 

        public  the delegate  void the CallBack ( BOOL B, int InputWordPosition, int InputWordLength); 

        ///  <Summary> 
        /// read text
         ///  </ Summary> 
        ///  < param name = "str"> should be spoken text </ param> 
        ///  <param name = "the callBack"> callback address </ param> 
        ///  <returns> returns BOOL </ returns> 
        public  BOOL Speak ( String str, CallBack CallBack)
        {
            int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            Thread thread = new Thread(new ParameterizedThreadStart(Call));
            thread.IsBackground = true;
            thread.Start((Object)CallBack);
            return !(n != 1);
        }


        /// <summary>
        /// 回调函数线程子程序
        /// </summary>
        /// <param name="callBack"></param>
        private void Call(Object callBack)
        {
            int InputWordLength = 0;    //局部_朗读长度
            int InputWordPosition = 0; //局部_朗读位置

            CallBack CallBack = (CallBack)callBack;

            while ((int)voice.Status.RunningState != 1)
            {
                if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)
                {
                    InputWordPosition = voice.Status.InputWordPosition;
                    InputWordLength = voice.Status.InputWordLength;

                    //回调                  
                    CallBack(false, InputWordPosition, InputWordLength);
                }
            }
            CallBack(true, InputWordPosition, InputWordLength);
        }

        /// <summary>
        /// 获取语音库
        /// </summary>
        /// <returns>List<string></returns>
        public List<string> getDescription()
        {
            List<string> list = new List<string>();
            Obj DotNetSpeech.ISpeechObjectTokens = voice.GetVoices ();
             int COUNT = obj.Count; // Get the total number of speech database 
            for ( int I = 0 ; I <COUNT; I ++ ) 
            { 
                String desc = obj.Item (I) .GetDescription ( ); // iterate speech database 
                list.add (desc); 
            } 
            return List; 
        } 

        ///  <Summary> 
        /// set the currently used speech database
         ///  </ Summary> 
        ///  <Returns> BOOL </ Returns > 
        public  BOOL setDescription (String name) 
        { 
            List < String > List = new new List < String > (); 
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices ();
             int COUNT = obj.Count; // get the total number of voice library 
            BOOL Result = to false ;
             for ( int = I 0 ; I <COUNT; I ++ ) 
            { 
                String desc = obj.Item (I) .GetDescription (); // iterate speech database 
                IF (desc.Equals (name)) 
                {
                    voice.Voice = obj.Item (I); 
                    Result = to true ; 
                } 
            } 
            return Result; 
        } 

        ///  <Summary> 
        /// Set Speed
         ///  </ Summary> 
        ///  <param name = "n-" > </ param> 
        public  void setRate ( int n-) 
        { 
            voice.Rate = n-; 
        } 

        ///  <Summary> 
        /// set the sound volume
         ///  </ Summary> 
        ///  <param name = "n-"> </param>
        public void setVolume(int n)
        {
            voice.Volume = n;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            voice.Pause();
        }

        /// <summary>
        /// 继续
        /// </summary>
        public void Resume()
        {
            voice.Resume();
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }


        /// <summary>
        /// 输出WAV
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="str">要转换的文本内容</param>
        /// <returns></returns>
        public bool WreiteToWAV(string path, string str, SpeechAudioFormatType SpAudioType)
        {
            SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            SpFileStream SpFileStream = new SpFileStream();
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat();
            SpAudio.Type = SpAudioType;
            SpFileStream.Format = SpAudio;
            SpFileStream.Open(path, SpFileMode, false);
            voice.AudioOutputStream = SpFileStream;
            voice.Speak(str, SpFlags);//阅读函数
            voice.WaitUntilDone(Timeout.Infinite);
            SpFileStream.Close();
            return File.Exists(path);
        }
    }

 

Guess you like

Origin www.cnblogs.com/macT/p/11771177.html