SoFunction
Updated on 2025-03-01

How to implement C# voice function


 private void button1_Click(object sender, EventArgs e)
        {
            Analyse(this.);
        }

 

public void Analyse(string strSpeak)
        {
            int iCbeg = 0;
            int iEbeg = 0;
            bool IsChina = true;
            for (int i = 0; i < ; i++)
            {
                char chr = strSpeak[i];
                if (IsChina)
                {
                    if (Convert.ToInt32(chr) <= 122 && Convert.ToInt32(chr) >= 65)
                    {
                        int iLen = i - iCbeg;
                        string strValue =
             (iCbeg, iLen);
                        SpeakChina(strValue);
                        iEbeg = i;
                        IsChina = false;
                    }
                }
                else
                {
                    if (Convert.ToInt32(chr) > 122 || Convert.ToInt32(chr) < 65)
                    {
                        int iLen = i - iEbeg;
                        string strValue =
(iEbeg, iLen);
                        (strValue);
                        iCbeg = i;
                        IsChina = true;
                    }
                }
            }

             if (IsChina)    
             {       int iLen = - iCbeg;
                 string strValue = (iCbeg, iLen);
                 SpeakChina(strValue);
             }     
             else
             {
                 int iLen = - iEbeg;
                 string strValue = (iEbeg, iLen);
                 SpeakEnglishi(strValue);
             }



        }

 

//Chinese
        private void SpeakChina(string speak)
        {
            voice = new SpVoice();
= (, ).Item(3);//3 of which are Chinese and 024 are English
            (speak, );

        }
//English
        private void SpeakEnglishi(string speak)
        {
            voice = new SpVoice();
= (, ).Item(0);//3 of which are Chinese and 024 are English
            (speak, );

        }

 

//Save voice
        private void button2_Click(object sender, EventArgs e)
        {
                 try    
                 {
                     SpeechVoiceSpeakFlags SpFlags = ;
                     SpVoice Voice = new SpVoice();      
                     SaveFileDialog sfd = new SaveFileDialog();     
                     = "All files (*.*)|*.*|wav files (*.wav)|*.wav";      
                     = "Save to a wave file";      
                     = 2;      
                     = true;      
                     if (() == )      
                     {        
                         SpeechStreamFileMode SpFileMode = ;
                         SpFileStream SpFileStream = new SpFileStream();
                         (, SpFileMode, false);
                         = SpFileStream;
                         (this., SpFlags);
                         (100);
                         ();
                     }    
                 }  
                 catch (Exception er)
                 {
                     ("An Error Occured!", "SpeechApp", , );
                 }


        }