/// <summary> /// Load configuration from App.config /// </summary> /// <returns></returns> public static T2WParam LoadConfig(string inputFile) { var param = new T2WParam(); if (string.IsNullOrEmpty(inputFile)) { param.PathToText = ConfigurationManager.AppSettings["PathToText"].ToString(); } else { param.PathToText = inputFile; } param.VoiceName = ConfigurationManager.AppSettings["VoiceName"].ToString(); param.WordsPerSub = Int32.Parse(ConfigurationManager.AppSettings["WordsPerSub"].ToString()); param.VoiceRate = Int32.Parse(ConfigurationManager.AppSettings["VoiceRate"].ToString()); param.AskParams = ConfigurationManager.AppSettings["AskParams"].ToString(); param.FactorOrReplace = ConfigurationManager.AppSettings["FactorOrReplace"].ToString(); param.ResizeFactor = decimal.Parse(ConfigurationManager.AppSettings["ResizeFactor"].ToString()); param.ReplaceFinalTime = decimal.Parse(ConfigurationManager.AppSettings["ReplaceFinalTime"].ToString()); param.OutputFormat = ConfigurationManager.AppSettings["OutputFormat"].ToString(); param.SampleRate = Int32.Parse(ConfigurationManager.AppSettings["SampleRate"]); param.BitsPerSample = Int32.Parse(ConfigurationManager.AppSettings["BitsPerSample"]); param.ChannelCount = Int32.Parse(ConfigurationManager.AppSettings["ChannelCount"]); param.AvgBytesPerSecond = Int32.Parse(ConfigurationManager.AppSettings["AvgBytesPerSecond"]); param.BlockAlign = Int32.Parse(ConfigurationManager.AppSettings["BlockAlign"]); return(param); }
/// <summary> /// Writes out the SRT File /// </summary> /// <param name="param"></param> /// <param name="texts"></param> private void ListStr2SRTFile(T2WParam param, List <string> texts) { string srtPath = Path.Combine( Path.GetDirectoryName(param.PathToText), Path.GetFileNameWithoutExtension(param.PathToText) + ".srt"); using (FileStream fs = File.Create(srtPath)) { foreach (var sub in texts) { Byte[] info = new UTF8Encoding(true).GetBytes(sub); //Byte[] info = System.Text.Encoding.Unicode.GetBytes(sub); fs.Write(info, 0, info.Length); } } }
/// <summary> /// Generates SRT file after the wav file has been created /// </summary> /// <param name="progress"></param> /// <param name="param"></param> /// <returns></returns> private List <string> GenSubtitles(List <SpeakProgress> progress, T2WParam param) { //MergeWords(ref progress, text); var p = progress[progress.Count - 1]; progress.Add(EstimateEndTime(p.AudioPosition.Ticks, p.CharacterPosition, p.CharacterCount)); //added to avoid exception on final loop iteration. //Get original text (text with punctuation) //text = Regex.Replace(text, @"[\s\r\n]+", " "); //replace new line with blank space. //var originalWords = text.Split(' '); var finalSubs = new List <string>(); TimeSpan t1 = new TimeSpan(); TimeSpan t2 = new TimeSpan(); int wordCounter = 0; int subNo = 1; string sub = string.Empty; t1 = progress[0].AudioPosition; for (int i = 0; i < progress.Count - 1; i++) { wordCounter++; t2 = progress[i + 1].AudioPosition; //sub = sub + " " + originalWords[j]; sub = sub + progress[i].Text; if (wordCounter >= param.WordsPerSub || i >= progress.Count - 2) { finalSubs.Add( string.Format(_outputSub, (subNo).ToString("D5"), t1.Hours, t1.Minutes, t1.Seconds, t1.Milliseconds, t2.Hours, t2.Minutes, t2.Seconds, t2.Milliseconds, sub.Trim()) ); subNo++; sub = string.Empty; t1 = t2; wordCounter = 0; } } return(finalSubs); }
/// <summary> /// Create a wav file from text /// </summary> /// <param name="text"></param> /// <param name="param"></param> /// <param name="wavePath"></param> /// <returns></returns> private List <SpeakProgress> CreateWav(string text, T2WParam param, out string wavePath) { wavePath = Path.Combine( Path.GetDirectoryName(param.PathToText), Path.GetFileNameWithoutExtension(param.PathToText) + ".wav"); using (var synthesizer = new SpeechSynthesizer()) { var synthFormat = new SpeechAudioFormatInfo(EncodingFormat.Pcm, param.SampleRate, param.BitsPerSample, param.ChannelCount, param.AvgBytesPerSecond, param.BlockAlign, null); //synthesizer.SetOutputToAudioStream(streamAudio, synthFormat); synthesizer.SpeakProgress += new EventHandler <SpeakProgressEventArgs>(synthesizer_SpeakProgress); //synthesizer.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synthesizer_SpeakCompleted); //synthesizer.SetOutputToDefaultAudioDevice(); synthesizer.SetOutputToWaveFile(wavePath, synthFormat); synthesizer.SelectVoice(param.VoiceName); synthesizer.Rate = param.VoiceRate; PromptBuilder builder = new PromptBuilder(); builder.StartVoice(param.VoiceName); builder.StartStyle(new PromptStyle(PromptEmphasis.Moderate)); //builder.AppendText(text, (PromptRate)param.VoiceRate); //if (param.VoiceName == "Ines22k_HQ") //{ // var dirtyText = text.Replace("...", ",,"); // dirtyText = Regex.Replace(dirtyText, @"[\s]*[\r\n]+", " "); // dirtyText = dirtyText.Replace(".", ";"); // builder.AppendText(dirtyText, PromptVolume.Loud); //} //else //{ // builder.AppendText(text, PromptVolume.Loud); //} builder.AppendText(text, PromptVolume.Loud); builder.EndStyle(); builder.EndVoice(); synthesizer.Speak(builder); } return(_progress); }
/// <summary> /// Instantiate with default parameters but use specific input file /// </summary> /// <param name="inputFile"></param> public TTSLogic(string inputFile) { _config = TTSLogic.LoadConfig(inputFile); //_config.PathToText = inputFile; }
/// <summary> /// Constructor with specific parameters /// </summary> /// <param name="conf"></param> public TTSLogic(T2WParam conf) { _config = conf; }
/// <summary> /// Default constructor /// </summary> public TTSLogic() { _config = TTSLogic.LoadConfig(""); }