Exemple #1
0
        public static void Main(string[] args)
        {
            var runFunction = args.Length > 0 && args[0].StartsWith("-run:")
                ? args[0].Substring(args[0].IndexOf(':') + 1)
                : null;

            var speakString = args.Length > 0 && args[0].StartsWith("-speak:")
                ? args[0].Substring(args[0].IndexOf(':') + 1)
                : null;

            try
            {
                using (SpeechSynthesizer synthesizer = new SpeechSynthesizer())
                {
                    if (!string.IsNullOrEmpty(runFunction) &&
                        TimedDialogs.RunFunction(synthesizer, (TimeType.Relative, new TimeSpan(), $"^{runFunction}", true), DateTime.Now))
                    {
                        return;
                    }

                    if (!string.IsNullOrEmpty(speakString) &&
                        TimedDialogs.SpeakString(synthesizer, (TimeType.Relative, new TimeSpan(), $"{speakString}", true)))
                    {
                        return;
                    }

                    var filename = args.Length > 0 && args[0].StartsWith("-inputfile:")
                        ? args[0].Substring(args[0].IndexOf(':') + 1)
                        : "TimedDialogs.txt";

                    if (!File.Exists(filename))
                    {
                        return;
                    }

                    var speaker = new TimedDialogs(synthesizer);
                    speaker.Speak(filename);
                }
            }
            catch (Exception ex)
            {
                ShowUsage();
            }
        }
Exemple #2
0
        public void Speak(string filename, VoiceGender genderHint = VoiceGender.Female, VoiceAge ageHint = VoiceAge.Child)
        {
            synthesizer.SelectVoiceByHints(genderHint, ageHint);
            synthesizer.Volume = 80;
            synthesizer.Rate   = 0;

            lines = File.ReadAllLines(filename);

            var startTime = DateTime.Now;

            lines.ToList().ForEach(line =>
            {
                if (line.StartsWith("#"))
                {
                    return;
                }

                try
                {
                    var parsed = Parse(line);
                    var parsedWithVariables = InsertVariable(parsed);

                    WaitForTime(startTime, parsedWithVariables.TimeType, parsedWithVariables.AtTime);
                    var functionRun = RunFunction(synthesizer, parsedWithVariables, DateTime.Now);

                    if (functionRun)
                    {
                        return;
                    }

                    TimedDialogs.SpeakString(synthesizer, parsedWithVariables);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")} Skipping: {line}");
                    Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")} {ex.Message}");
                    Console.WriteLine(ex.StackTrace);
                }
            });

            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff")} Speaking: Bye");
            synthesizer.Speak("Bye");
        }