static void Main(string[] args) { Boolean isUTF8 = false; Boolean wav = false; string speakText = null; string inputFileLocation = null; string outputFileLocation = null; string replacementFileLocation = null; string deleteSpecificationLocation = null; int rate = 0; Tuple <string, string> voiceAndCulture = null; //A lot of arguement checking if (args.Length > 0) { if ((voiceAndCulture = ValidateVoiceSelection(args[0])) != null) { for (int i = 1; i < args.Length; i++) { if (args[i].Equals("-utf8")) { isUTF8 = true; } else if (args[i].Equals("-wav")) { if (i + 1 < args.Length) { if (!SetAudioFormat(args[i + 1])) { return; } i++; wav = true; } else { Console.WriteLine("Missing wav format"); return; } } else if (args[i].Equals("-replace")) { if (i + 1 < args.Length) { replacementFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing replacement file name"); PrintHelp(); return; } } else if (args[i].Equals("-delete")) { if (i + 1 < args.Length) { deleteSpecificationLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing DeleteSpecification file name"); PrintHelp(); return; } } else if (args[i].Equals("-rate")) { if (i + 1 < args.Length) { string s = args[i + 1]; i++; if (!Int32.TryParse(s, out rate) || rate < -10 || rate > 10) { Console.WriteLine("Rate must be an integer between -10 and 10"); PrintHelp(); return; } } else { Console.WriteLine("Missing replacement file name"); PrintHelp(); return; } } else if (args[i].Equals("-i")) { if (speakText != null) { Console.WriteLine("Cannot use both file and command line for text input"); return; } else if (i + 1 < args.Length) { inputFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing input file name"); return; } } else if (args[i].Equals("-o")) { if (i + 1 < args.Length) { outputFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing output file name"); return; } } else { if (i == 1) { speakText = args[1]; } else { Console.WriteLine("Unknown command: " + args[i]); PrintHelp(); return; } } } } else if (args[0].Equals("-l")) { ShowInstalledLanguage(); return; } else { Console.WriteLine("Invalid speaker name/ID: " + args[0]); Console.WriteLine("Type \"TTS.exe -l\" to show list of installed language/voice"); return; } } else { PrintHelp(); return; } if (voiceAndCulture == null) { Console.WriteLine("Invalid voice ID"); PrintHelp(); return; } if (speakText == null && inputFileLocation == null) { Console.WriteLine("No text input"); PrintHelp(); return; } string voiceName = voiceAndCulture.Item1; string culture = voiceAndCulture.Item2; //Read the replacement text Dictionary <string, string> replacementDictionary = new Dictionary <string, string>(); //============================== For developement only ======================================= /* * String voiceName = "VW Hui"; * String culture = "zh-CN"; * replacementFileLocation = "replacement.txt"; * inputFileLocation = "1.txt"; * //outputFileLocation = "out.mp3"; * isUTF8 = true; * rate = 3; */ //============================================================================================ if (replacementFileLocation != null) { try { //using (StreamReader sr = new StreamReader(inputFileLocation, Encoding.GetEncoding(pt.TextInfo.ANSICodePage), true)) using (StreamReader sr = new StreamReader(replacementFileLocation)) { string line; while ((line = sr.ReadLine()) != null) { string[] parts = Regex.Split(line, ","); if (parts.Length == 2) { replacementDictionary[parts[0]] = parts[1]; } } } } catch (Exception e) { Console.WriteLine("Invalid replacement file location"); PrintHelp(); return; } } //Read text file for input if (inputFileLocation != null) { try { if (isUTF8) { using (StreamReader sr = new StreamReader(inputFileLocation)) { speakText = sr.ReadToEnd(); } } else { CultureInfo pt = CultureInfo.GetCultureInfo(culture); using (StreamReader sr = new StreamReader(inputFileLocation, Encoding.GetEncoding(pt.TextInfo.ANSICodePage), true)) { speakText = sr.ReadToEnd(); } } fileName = Path.GetFileName(inputFileLocation); } catch (Exception e) { Console.WriteLine("Invalid input file location."); PrintHelp(); return; } } speakText = TextReplace(speakText, replacementDictionary); textLength = speakText.Length; MandarinPromptSpeaker mps = new MandarinPromptSpeaker(fileName, voiceName, rate, outputFileLocation, deleteSpecificationLocation); mps.ParseText(speakText); //mps.Speak(voiceName, rate, outputFileLocation); /* * if (synthesizerChoice == 1) //Use System Speech Synthesizer * { * using (System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) * { * synth.SelectVoice(voiceName); * synth.Rate = rate; * synth.Volume = 100; * * if (outputFileLocation != null) * { * synth.SpeakProgress += new EventHandler<System.Speech.Synthesis.SpeakProgressEventArgs>(SynthProgressUpdate); * * if (wav) * { * synth.SetOutputToWaveFile(outputFileLocation, sysAF); * synth.Speak(speakText); * Console.WriteLine("\r " + fileName + " Progress: 100% "); * } * else * { * MemoryStream ms = new MemoryStream(); * synth.SetOutputToWaveStream(ms); * synth.Speak(speakText); * Console.WriteLine("\r " + fileName + " Progress: 100% "); * ms.Seek(0, SeekOrigin.Begin); * * //Convert from wav to mp3 to save space * using (var rdr = new WaveFileReader(ms)) * using (var wtr = new LameMP3FileWriter(outputFileLocation, rdr.WaveFormat, LAMEPreset.VBR_90)) * { * rdr.CopyTo(wtr); * } * } * } * else * { * synth.SetOutputToDefaultAudioDevice(); * synth.Speak(speakText); * } * } * } * else if (synthesizerChoice == 2) //Use Microsoft Speech Sythnesizer * { * using (Microsoft.Speech.Synthesis.SpeechSynthesizer synth = new Microsoft.Speech.Synthesis.SpeechSynthesizer()) * { * synth.SelectVoice(voiceName); * synth.Rate = rate; * synth.Volume = 100; * * if (outputFileLocation != null) * { * synth.SpeakProgress += new EventHandler<Microsoft.Speech.Synthesis.SpeakProgressEventArgs>(SynthProgressUpdate); * if (wav) * { * synth.SetOutputToWaveFile(outputFileLocation, msftAF); * synth.Speak(speakText); * Console.WriteLine("\r " + fileName + " Progress: 100% "); * } * else * { * MemoryStream ms = new MemoryStream(); * synth.SetOutputToWaveStream(ms); * synth.Speak(speakText); * Console.WriteLine("\r " + fileName + " Progress: 100% "); * ms.Seek(0, SeekOrigin.Begin); * * //Convert from wav to mp3 to save space * using (var rdr = new WaveFileReader(ms)) * using (var wtr = new LameMP3FileWriter(outputFileLocation, rdr.WaveFormat, LAMEPreset.VBR_90)) * { * rdr.CopyTo(wtr); * } * } * } * else * { * synth.SetOutputToDefaultAudioDevice(); * synth.Speak(speakText); * } * } * } * * //Console.WriteLine("Press any key to exit..."); * //Console.ReadKey(true); */ }
static void Main(string[] args) { Boolean isUTF8 = false; Boolean wav = false; string speakText = null; string inputFileLocation = null; string outputFileLocation = null; string replacementFileLocation = null; string deleteSpecificationLocation = null; int rate = 0; Tuple<string, string> voiceAndCulture = null; //A lot of arguement checking if (args.Length > 0) { if((voiceAndCulture = ValidateVoiceSelection(args[0])) != null) { for (int i = 1; i < args.Length; i++) { if (args[i].Equals("-utf8")) isUTF8 = true; else if (args[i].Equals("-wav")) { if (i + 1 < args.Length) { if (!SetAudioFormat(args[i + 1])) return; i++; wav = true; } else { Console.WriteLine("Missing wav format"); return; } } else if (args[i].Equals("-replace")) { if (i + 1 < args.Length) { replacementFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing replacement file name"); PrintHelp(); return; } } else if (args[i].Equals("-delete")) { if (i + 1 < args.Length) { deleteSpecificationLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing DeleteSpecification file name"); PrintHelp(); return; } } else if (args[i].Equals("-rate")) { if (i + 1 < args.Length) { string s = args[i + 1]; i++; if (!Int32.TryParse(s, out rate) || rate < -10 || rate > 10) { Console.WriteLine("Rate must be an integer between -10 and 10"); PrintHelp(); return; } } else { Console.WriteLine("Missing replacement file name"); PrintHelp(); return; } } else if (args[i].Equals("-i")) { if (speakText != null) { Console.WriteLine("Cannot use both file and command line for text input"); return; } else if (i + 1 < args.Length) { inputFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing input file name"); return; } } else if (args[i].Equals("-o")) { if (i + 1 < args.Length) { outputFileLocation = args[i + 1]; i++; } else { Console.WriteLine("Missing output file name"); return; } } else { if (i == 1) speakText = args[1]; else { Console.WriteLine("Unknown command: " + args[i]); PrintHelp(); return; } } } } else if (args[0].Equals("-l")) { ShowInstalledLanguage(); return; } else { Console.WriteLine("Invalid speaker name/ID: " + args[0]); Console.WriteLine("Type \"TTS.exe -l\" to show list of installed language/voice"); return; } } else { PrintHelp(); return; } if (voiceAndCulture == null) { Console.WriteLine("Invalid voice ID"); PrintHelp(); return; } if (speakText == null && inputFileLocation == null) { Console.WriteLine("No text input"); PrintHelp(); return; } string voiceName = voiceAndCulture.Item1; string culture = voiceAndCulture.Item2; //Read the replacement text Dictionary<string, string> replacementDictionary = new Dictionary<string, string>(); //============================== For developement only ======================================= /* String voiceName = "VW Hui"; String culture = "zh-CN"; replacementFileLocation = "replacement.txt"; inputFileLocation = "1.txt"; //outputFileLocation = "out.mp3"; isUTF8 = true; rate = 3; */ //============================================================================================ if (replacementFileLocation != null) { try { //using (StreamReader sr = new StreamReader(inputFileLocation, Encoding.GetEncoding(pt.TextInfo.ANSICodePage), true)) using (StreamReader sr = new StreamReader(replacementFileLocation)) { string line; while((line = sr.ReadLine()) != null) { string[] parts = Regex.Split(line, ","); if (parts.Length == 2) { replacementDictionary[parts[0]] = parts[1]; } } } } catch (Exception e) { Console.WriteLine("Invalid replacement file location"); PrintHelp(); return; } } //Read text file for input if (inputFileLocation != null) { try { if (isUTF8) { using (StreamReader sr = new StreamReader(inputFileLocation)) { speakText = sr.ReadToEnd(); } } else { CultureInfo pt = CultureInfo.GetCultureInfo(culture); using (StreamReader sr = new StreamReader(inputFileLocation, Encoding.GetEncoding(pt.TextInfo.ANSICodePage), true)) { speakText = sr.ReadToEnd(); } } fileName = Path.GetFileName(inputFileLocation); } catch (Exception e) { Console.WriteLine("Invalid input file location."); PrintHelp(); return; } } speakText = TextReplace(speakText, replacementDictionary); textLength = speakText.Length; MandarinPromptSpeaker mps = new MandarinPromptSpeaker(fileName, voiceName, rate, outputFileLocation, deleteSpecificationLocation); mps.ParseText(speakText); //mps.Speak(voiceName, rate, outputFileLocation); /* if (synthesizerChoice == 1) //Use System Speech Synthesizer { using (System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) { synth.SelectVoice(voiceName); synth.Rate = rate; synth.Volume = 100; if (outputFileLocation != null) { synth.SpeakProgress += new EventHandler<System.Speech.Synthesis.SpeakProgressEventArgs>(SynthProgressUpdate); if (wav) { synth.SetOutputToWaveFile(outputFileLocation, sysAF); synth.Speak(speakText); Console.WriteLine("\r " + fileName + " Progress: 100% "); } else { MemoryStream ms = new MemoryStream(); synth.SetOutputToWaveStream(ms); synth.Speak(speakText); Console.WriteLine("\r " + fileName + " Progress: 100% "); ms.Seek(0, SeekOrigin.Begin); //Convert from wav to mp3 to save space using (var rdr = new WaveFileReader(ms)) using (var wtr = new LameMP3FileWriter(outputFileLocation, rdr.WaveFormat, LAMEPreset.VBR_90)) { rdr.CopyTo(wtr); } } } else { synth.SetOutputToDefaultAudioDevice(); synth.Speak(speakText); } } } else if (synthesizerChoice == 2) //Use Microsoft Speech Sythnesizer { using (Microsoft.Speech.Synthesis.SpeechSynthesizer synth = new Microsoft.Speech.Synthesis.SpeechSynthesizer()) { synth.SelectVoice(voiceName); synth.Rate = rate; synth.Volume = 100; if (outputFileLocation != null) { synth.SpeakProgress += new EventHandler<Microsoft.Speech.Synthesis.SpeakProgressEventArgs>(SynthProgressUpdate); if (wav) { synth.SetOutputToWaveFile(outputFileLocation, msftAF); synth.Speak(speakText); Console.WriteLine("\r " + fileName + " Progress: 100% "); } else { MemoryStream ms = new MemoryStream(); synth.SetOutputToWaveStream(ms); synth.Speak(speakText); Console.WriteLine("\r " + fileName + " Progress: 100% "); ms.Seek(0, SeekOrigin.Begin); //Convert from wav to mp3 to save space using (var rdr = new WaveFileReader(ms)) using (var wtr = new LameMP3FileWriter(outputFileLocation, rdr.WaveFormat, LAMEPreset.VBR_90)) { rdr.CopyTo(wtr); } } } else { synth.SetOutputToDefaultAudioDevice(); synth.Speak(speakText); } } } //Console.WriteLine("Press any key to exit..."); //Console.ReadKey(true); */ }