public byte[] SpeakContentStream(string content) { byte[] buffer = CacheHelper.Get(content) as byte[]; if (buffer != null) { return(buffer); } else { using (System.Speech.Synthesis.SpeechSynthesizer speechSyn = new System.Speech.Synthesis.SpeechSynthesizer()) { speechSyn.Volume = 100; speechSyn.Rate = 0; using (var ms = new System.IO.MemoryStream()) { speechSyn.SetOutputToWaveStream(ms); speechSyn.Speak(content); speechSyn.SetOutputToNull(); buffer = ms.ToArray(); //设置平滑过期 CacheHelper.Set(content, buffer, TimeSpan.FromSeconds(30)); return(buffer); } } } }
private static string generateSpeech(string userid, string condition, string pathResponseFile, string path) { // ********* generate wav file voicing the response ***************** // Using Microsoft voices // initiate new instance of speech synthesizer // needs own separate dedicated thread... string response = readResponse(pathResponseFile); string audioFilePath = ""; SQLLog.InsertLog(DateTime.Now, "Beginning Thread", "Beginning Thread", "ResponseGeneration.moveSpeak", 1); Thread t = new Thread(() => { try { System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer(); string gender = SQLConditionGenderInfo.GetGender(userid); bool makecopy = false; if (synth != null) { if (gender.Contains("female")) { synth.SelectVoice("Microsoft Zira Desktop"); } else { synth.SelectVoice("Microsoft David Desktop"); } if (condition == "nonsocial" || condition == "social") { synth.SetOutputToWaveFile(path + "data\\agentAudio\\transformed.wav"); audioFilePath = "transformed.wav"; makecopy = true; } else { synth.SetOutputToWaveFile(path + "data\\agentAudio\\response.wav"); audioFilePath = "response.wav"; } synth.Speak(response); if (makecopy) { string formatFileName = string.Format("data\\agentAudio\\transformed_{0:yyyy-MM-dd_hh-mm-ss-tt}.wav", DateTime.Now); audioFilePath = string.Format("transformed_{0:yyyy-MM-dd_hh-mm-ss-tt}.wav", DateTime.Now); synth.SetOutputToWaveFile(path + formatFileName); synth.Speak(response); } synth.SetOutputToNull(); } } catch (Exception e) { SQLLog.InsertLog(DateTime.Now, "Something went wrong in generating speech", "", "ResponseGeneration.generateSpeech", 1); } }); t.Start(); t.Join(); Thread.Sleep(1000); return(audioFilePath); }