using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Speech.AudioFormat; using System.Speech.Synthesis; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web; using CallCenter.Utility.Common; using SpeechLib; namespace CallCenter.Utility { public class TtsHelper { public static string GetAudioPath(string text, string name = "") { if (!string.IsNullOrEmpty(text)) { name = string.IsNullOrEmpty(name) ? CommonHelper.MD5(text) : name + "-" + CommonHelper.MD5(text); string filename = name + ".wav"; string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); string pathfile = path + filename; if (!File.Exists(pathfile)) { File.Create(pathfile).Dispose(); Task.Factory.StartNew(() => { SpeechSynthesizer synth = new SpeechSynthesizer(); SpeechAudioFormatInfo wavFromat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, 1024 * 11, 16, 1, 16000, 2, null); synth.SetOutputToWaveFile(pathfile, wavFromat); synth.Speak(text); synth.SetOutputToNull(); }); } return pathfile; } else { return ""; } } public static string GetAudioPath1(string text) { if (!string.IsNullOrEmpty(text)) { string filename = CommonHelper.MD5(text) + ".wav"; string path = HttpContext.Current.Server.MapPath("..") + "\\Audio\\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); string pathfile = path + filename; if (!File.Exists(pathfile)) { SpFileStream stream = new SpFileStream(); stream.Open(pathfile, SpeechStreamFileMode.SSFMCreateForWrite, false); SpVoice sv = new SpVoice(); sv.AudioOutputStream = stream; sv.Speak(text); sv.WaitUntilDone(Timeout.Infinite); stream.Close(); } return pathfile; } else { return ""; } } } }