예제 #1
0
        public static void Call(string subscriptionKey, string issueTokenUrl, string endpointUrl, string voiceName, string locale, string script, string outputFile, bool isSSML)
        {
            const string SsmlPattern = @"<speak version=""1.0"" xmlns=""http://www.w3.org/2001/10/synthesis"" xmlns:mstts=""http://www.w3.org/2001/mstts"" xml:lang=""{0}"">" +
                                       @"<voice name = ""{1}"">{2}</voice>" +
                                       @"</speak>";
            string ssml  = "";
            string token = APIHelper.GetToken(issueTokenUrl, subscriptionKey);

            HttpWebRequest webRequest     = (HttpWebRequest)WebRequest.Create(endpointUrl);
            string         ImpressionGUID = Guid.NewGuid().ToString();

            webRequest.ContentType = "application/ssml+xml";
            webRequest.Headers.Add("X-MICROSOFT-OutputFormat", "riff-16khz-16bit-mono-pcm");
            webRequest.Headers["Authorization"] = "Bearer " + token;
            webRequest.Headers.Add("X-FD-ClientID", ImpressionGUID);
            webRequest.Headers.Add("X-FD-ImpressionGUID", ImpressionGUID);
            webRequest.UserAgent = "TTSClient";
            webRequest.Method    = "POST";

            if (isSSML)
            {
                ssml = script;
            }
            else
            {
                ssml = string.Format(CultureInfo.InvariantCulture, SsmlPattern, locale, voiceName, SecurityElement.Escape(script));
            }
            byte[] btBodyS = Encoding.UTF8.GetBytes(ssml);
            webRequest.ContentLength = btBodyS.Length;
            webRequest.GetRequestStream().Write(btBodyS, 0, btBodyS.Length);
            webRequest.Timeout = 6000000;

            using (var response = webRequest.GetResponse() as HttpWebResponse)
            {
                var sstream = response.GetResponseStream();
                using (FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write))
                {
                    sstream.CopyTo(fs);
                }
            }
        }