private void initialize() { language = "DE"; tokenCat = new SpObjectTokenCategory(); tokenCat.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryVoices, false); tokens = tokenCat.EnumerateTokens(null, null); int n = 0; int languageIndex = 0; foreach (SpObjectToken item in tokens) { if (language == "DE") { if (item.GetDescription(0).Contains("Hedda")) { languageIndex = n; GermanLanguageIndex = n; } } if (language == "ENG") { if (item.GetDescription(0).Contains("Zira")) { languageIndex = n; EnglishLanguageIndex = n; } } n++; } voice = new SpVoice(); voice.Voice = tokens.Item(languageIndex); speechFileMode = SpeechStreamFileMode.SSFMCreateForWrite; }
private void buttonX2_Click(object sender, EventArgs e) { try { SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFDefault; SpVoice Voice = new SpVoice(); SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav"; sfd.Title = "Save to a wave file"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(sfd.FileName, SpFileMode, false); Voice.AudioOutputStream = SpFileStream; Voice.Speak(textspeechText.Text, SpFlags); Voice.WaitUntilDone(1000); SpFileStream.Close(); } } catch (Exception er) { //MessageBox.Show("An Error Occured!", "SpeechApp", MessageBoxButtons.OK, MessageBoxIcon.Error); System.Console.WriteLine(er.ToString()); } }
public TTSHelper(int volume, int rate) { //param spFileStream = new SpFileStream(); //declaring and Initializing fileStream obj spFileMode = SpeechStreamFileMode.SSFMCreateForWrite; //declaring fileStreamMode as to Create or Write // SpeechVoiceSpeakFlags my_Spflag = SpeechVoiceSpeakFlags.SVSFlagsAsync; // declaring and initializing Speech Voice Flags my_Voice = new SpVoice(); //declaring and initializing SpVoice Class sp_Rate = rate; sp_Volume = volume; my_Spflag = SpeechVoiceSpeakFlags.SVSFlagsAsync; }
private void btnSpeak_Click(object sender, System.EventArgs e) { if ((Convert.ToInt32(btnSpeak.Tag) == 0) && (sender != null)) { btnSpeak.Tag = 1; btnSpeak.Text = "STOP"; try { if (cbVoices.Text.Length > 0) { m_voice.Voice = m_voice.GetVoices(string.Format("Name={0}", cbVoices.Text), "Language=409").Item(0); } if (cbSaveToWave.Checked) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav"; sfd.Title = "Save to a wave file"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(sfd.FileName, SpFileMode, false); m_voice.AudioOutputStream = SpFileStream; m_voice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync); m_voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); } } else { m_voice.Speak(textBox1.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync); } } catch { MessageBox.Show("Speak error"); } } else { btnSpeak.Tag = 0; btnSpeak.Text = "SPEAK"; m_voice.Speak(null, (SpeechVoiceSpeakFlags)2); m_voice.Resume(); } }
public void txtToFile(string value, string fileName) { GC.Collect(); SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpeechLib.SpVoice voice = new SpVoiceClass(); SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(fileName, SpFileMode, false); voice.AudioOutputStream = SpFileStream; voice.Speak(value, SpFlags); voice.WaitUntilDone(System.Threading.Timeout.Infinite); SpFileStream.Close(); }
public void suaraToFile(string kata, string fname) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } voice = ConfVoice(); SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream spFs = new SpFileStream(); spFs.Open(path + "/" + fname + ".wav", spFileMode, false); voice.AudioOutputStream = spFs; voice.Speak(kata, SpeechVoiceSpeakFlags.SVSFlagsAsync); voice.WaitUntilDone(Timeout.Infinite); spFs.Close(); }
static bool say(string filename, string sent, SpVoice voice) { SpFileStream fileStream = null; bool success = true; try { SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFIsXML; //SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFIsNotXML; //SpVoice voice = new SpVoice(); SpeechStreamFileMode fileMode = SpeechStreamFileMode.SSFMCreateForWrite; fileStream = new SpFileStream(); fileStream.Open(filename, fileMode, false); // audio output /* * voice.Speak(sent, flags); * voice.WaitUntilDone(Timeout.Infinite); */ // file output voice.AudioOutputStream = fileStream; voice.Speak(sent, flags); voice.WaitUntilDone(Timeout.Infinite); } catch (Exception error) { success = false; Console.Error.WriteLine("Error speaking sentence: " + error); Console.Error.WriteLine("error.Data: " + error.Data); Console.Error.WriteLine("error.HelpLink: " + error.HelpLink); } finally { if (fileStream != null) { fileStream.Close(); } } return(success); }
private void btnSpeak_Click(object sender, System.EventArgs e) { //Create a TTS voice and speak. try { SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); if (chkSaveToWavFile.Checked) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav"; sfd.Title = "Save to a wave file"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(sfd.FileName, SpFileMode, false); Voice.AudioOutputStream = SpFileStream; Voice.Speak(txtSpeakText.Text, SpFlags); Voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); } } else { Voice.Speak(txtSpeakText.Text, SpFlags); } } catch (Exception error) { MessageBox.Show("Speak error", error.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); throw; } }
private void btnToWave_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav"; sfd.Title = "Save to a wave file"; sfd.FilterIndex = 2; sfd.RestoreDirectory = true; if (sfd.ShowDialog() == DialogResult.OK) { SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream spFileStream = new SpFileStream(); spFileStream.Open(sfd.FileName, spFileMode, false); sp.AudioOutputStream = spFileStream; sp.Volume = trackBar1.Value; sp.Rate = trackBar2.Value; sp.Speak(tbTextInput.Text, spFlags); sp.WaitUntilDone(System.Threading.Timeout.Infinite); spFileStream.Close(); } }
public int Open([NativeTypeName("BSTR")] ushort *FileName, SpeechStreamFileMode FileMode = SSFMOpenForRead, [NativeTypeName("VARIANT_BOOL")] short DoEvents = 0) { return(((delegate * unmanaged <ISpeechFileStream *, ushort *, SpeechStreamFileMode, short, int>)(lpVtbl[12]))((ISpeechFileStream *)Unsafe.AsPointer(ref this), FileName, FileMode, DoEvents)); }
private void SaveFile(string FileName, string Content) { try { DelFile(FileName); if (SingletonInfo.GetInstance().IsNationFlag) { string[] FileNamesp = FileName.Split('.'); FileName = FileNamesp[0] + ".wav"; } SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpVoice Voice = new SpVoice(); Voice.Rate = SingletonInfo.GetInstance()._rate; string filename = FileName; string filenametmp = FileName.Split('.')[0] + "tmp.wav"; string texttxt = Content; SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpFileStream SpFileStream = new SpFileStream(); SpFileStream.Open(filenametmp, SpFileMode, false); Voice.AudioOutputStream = SpFileStream; Voice.Speak(texttxt, SpFlags); Voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); int nFrontPackCnt = SingletonInfo.GetInstance()._nFrontPackCnt; int nTailPackCnt = SingletonInfo.GetInstance()._nTailPackCnt; int de = NativeMethods.InsertBlankAudio(filename, filenametmp, nFrontPackCnt, nTailPackCnt); DelFile(filenametmp); FileInfo MyFileInfo = new FileInfo(filename); float dirTime = (float)MyFileInfo.Length / 32000; string[] filepathname = FileName.Split('\\'); string filenamesignal = ""; if (SingletonInfo.GetInstance().IsNationFlag) { filenamesignal = filepathname[filepathname.Length - 1].Split('.')[0] + ".mp3"; } else { filenamesignal = filepathname[filepathname.Length - 1]; } string senddata = "PACKETTYPE~TTS|FILE~" + filenamesignal + "|TIME~" + ((uint)dirTime).ToString(); LogMessage(senddata); #region 如果是需要mp3文件 则需要调用ffmepg if (SingletonInfo.GetInstance().IsNationFlag) { //转换MP3 string wavfilename = filenamesignal.Replace(".mp3", ".wav"); string fromMusic = SingletonInfo.GetInstance()._path + "\\" + wavfilename; //转换音乐路径 string toMusic = SingletonInfo.GetInstance()._path + "\\" + filenamesignal; //转换后音乐路径 int bitrate = 128 * 1000; //恒定码率 string Hz = "44100"; //采样频率 ExcuteProcess("ffmpeg.exe", "-y -ab " + bitrate + " -ar " + Hz + " -i \"" + fromMusic + "\" \"" + toMusic + "\""); // Thread.Sleep(100); File.Delete(fromMusic); //转换完成 } #endregion #region ftp文件传输 20190107新增 if (SingletonInfo.GetInstance().FTPEnable) { string ftppath = filenamesignal; string path = SingletonInfo.GetInstance()._path + "\\" + filenamesignal; SingletonInfo.GetInstance().ftphelper.UploadFile(path, ftppath);//阻塞式非线程模式 } #endregion SendMQMessage(senddata); } catch (Exception ex) { LogHelper.WriteLog(typeof(MainForm), "SaveFile处理异常:" + ex.ToString() + "----------" + ex.InnerException + "-------" + ex.StackTrace + "-------" + ex.Message, "2"); } }
public virtual extern void Open([In, MarshalAs(UnmanagedType.BStr)] string FileName, [In, Optional, DefaultParameterValue(SpeechStreamFileMode.SSFMOpenForRead)] SpeechStreamFileMode FileMode, [In, Optional, DefaultParameterValue(false)] bool DoEvents);
//button5生成读音 private void button5_Click(object sender, RoutedEventArgs e) { //数据库全部读出 WordLogic[] wordList = WordLogic.FindAll(); SpFileStream SpFileStream = new SpFileStream(); String fileName; SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync; SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite; SpVoice voice = new SpVoice(); // prepare voice SpObjectTokenCategory aotc = new SpObjectTokenCategory(); aotc.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryVoices); foreach (ISpeechObjectToken token in aotc.EnumerateTokens()) { if (token.GetDescription() == "VW Julie") { englishToken = (SpObjectToken)token; } else if (token.GetDescription() == "VW Hui") { chineseToken = (SpObjectToken)token; } } voice.Voice = englishToken; voice.Rate = -4; String outFolderPath = Directory.GetParent("../../TestCases/") + @"\单词音频\"; if (!Directory.Exists(outFolderPath)) { Directory.CreateDirectory(outFolderPath); } for (int i = 0; i < wordList.Length; i++) { String word = wordList[i].word; if (word != null) { word = word.Trim(); } if (String.IsNullOrEmpty(word)) { // 遇到无效内容,退出 continue; } word = convert(word); fileName = outFolderPath + word + ".wav"; SpFileStream.Open(fileName, SpFileMode, false); voice.AudioOutputStream = SpFileStream; voice.Speak(word, SpFlags); voice.WaitUntilDone(Timeout.Infinite); SpFileStream.Close(); } MessageBox.Show("音频生成完毕!"); }