コード例 #1
0
        static void Main(string[] args)
        {
            string text      = null;
            string output    = null;
            string userId    = null;
            string token     = null;
            string lang      = null;
            string inputType = null;
            string ttsId     = null;

            try
            {
                // Parsing commandline arguments
                Parser.Default.ParseArguments <Options>(args)
                .WithParsed <Options>(opts => {
                    text      = opts.TextFile;
                    output    = opts.Output;
                    userId    = opts.UserId;
                    token     = opts.ApiToken;
                    lang      = opts.Language;
                    inputType = opts.InputType;
                    ttsId     = opts.TTSId;
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception thrown while parsing arguments: " + ex.Message);
                return;
            }
            Synthesizer   synthesizer = new Synthesizer(userId, token, lang);
            Task <string> voicesTask  = synthesizer.GetVoices();

            if (voicesTask != null)
            {
                if (!(voicesTask.IsCanceled || voicesTask.IsFaulted))
                {
                    if (!voicesTask.IsCompleted)
                    {
                        voicesTask.Wait();
                    }
                    Console.WriteLine(voicesTask.Result);
                }
            }
            Task <byte[]> resultTask = null;

            if (inputType == "text")
            {
                resultTask = synthesizer.SynthesizeText(text, ttsId);
            }
            else
            {
                resultTask = synthesizer.Synthesize(text, ttsId);
            }
            if (resultTask == null)
            {
                Console.WriteLine("Text-to-speech process failed.");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }
            if (resultTask.IsCanceled || resultTask.IsFaulted)
            {
                Console.WriteLine("Text-to-speech process failed.");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }
            if (!resultTask.IsCompleted)
            {
                resultTask.Wait();
            }
            byte[] result = resultTask.Result;
            if (result == null)
            {
                Console.WriteLine("Text-to-speech process failed.");
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
                return;
            }
            FileStream writer = new FileStream(output, FileMode.OpenOrCreate);

            writer.Write(result, 0, result.Length);
            writer.Close();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }