public virtual Dictionary <string, string> GetAudioHeaders(string token, AudioOutputFormatOptions outputFormat)
 {
     return(new Dictionary <string, string>()
     {
         { "Content-Type", "application/ssml+xml" },
         { "X-Microsoft-OutputFormat", outputFormat.MemberAttrValue() },
         { "Authorization", $"Bearer {token}" },
         { "User-Agent", "TTSClient" }
     });
 }
Example #2
0
 public virtual Dictionary <string, string> GetAudioHeaders(string token, AudioOutputFormatOptions outputFormat)
 {
     return(new Dictionary <string, string>()
     {
         { "Content-Type", "application/ssml+xml" },
         { "X-Microsoft-OutputFormat", JsonConvert.SerializeObject(outputFormat) },
         { "Authorization", $"Bearer {token}" },
         { "X-Search-AppId", "07D3234E49CE426DAA29772419F436CA" },
         { "X-Search-ClientID", "1ECFAE91408841A480F00935DC390960" },
         { "User-Agent", "TTSClient" }
     });
 }
Example #3
0
 public virtual Task <Stream> TextToSpeechAsync(string text, SpeechLocaleOptions locale, VoiceName voiceName,
                                                GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
 {
     return(PolicyService.ExecuteRetryAndCapture400Errors(
                "SpeechService.TextToSpeechAsync",
                ApiKeys.SpeechRetryInSeconds,
                () =>
     {
         var result = SpeechRepository.TextToSpeechAsync(text, locale, voiceName, voiceType, outputFormat);
         return result;
     },
                null));
 }
        public virtual Stream TextToSpeech(string text, BingSpeechLocaleOptions locale, string voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            try {
                var result = SpeechRepository.TextToSpeech(text, locale, voiceName, voiceType, outputFormat);

                return(result);
            } catch (Exception ex) {
                Logger.Error("SpeechService.TextToSpeech failed", this, ex);
            }

            return(null);
        }
Example #5
0
        public virtual async Task <Stream> GetAudioStreamAsync(string url, string text, BingSpeechLocaleOptions locale, string voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat, string token)
        {
            HttpClientHandler handler = new HttpClientHandler()
            {
                CookieContainer = new CookieContainer(), UseProxy = false
            };
            HttpClient client = new HttpClient(handler);

            client.DefaultRequestHeaders.Clear();

            var Headers = GetAudioHeaders(token, outputFormat);

            foreach (var header in Headers)
            {
                client.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
            }

            var request = new HttpRequestMessage(HttpMethod.Post, url)
            {
                Content = GetAudioContent(text, locale, voiceName, voiceType)
            };

            var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None);

            return(await response.Content.ReadAsStreamAsync());
        }
        public virtual Stream GetAudioStream(string url, string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat, string token)
        {
            // Construct HTTP request to get the logo
            HttpWebRequest httpRequest = (HttpWebRequest)
                                         WebRequest.Create(url);

            httpRequest.Method = WebRequestMethods.Http.Post;

            var headers = GetAudioHeaders(token, outputFormat);

            httpRequest.Headers.Add("X-Microsoft-OutputFormat", outputFormat.MemberAttrValue());
            httpRequest.Headers.Add("Authorization", $"Bearer {token}");
            httpRequest.ContentType = "application/ssml+xml";
            httpRequest.UserAgent   = "TTSClient";

            byte[] reqData = Encoding.UTF8.GetBytes(GetAudioContent(text, locale, voiceName, voiceType));
            httpRequest.ContentLength = (long)reqData.Length;
            Stream requestStream = httpRequest.GetRequestStream();

            requestStream.Write(reqData, 0, reqData.Length);
            requestStream.Close();

            // Get back the HTTP response for web server
            HttpWebResponse httpResponse       = (HttpWebResponse)httpRequest.GetResponse();
            Stream          httpResponseStream = httpResponse.GetResponseStream();

            var newStream = new MemoryStream();

            httpResponseStream.CopyTo(newStream);
            httpResponse.Close();
            newStream.Position = 0;

            return(newStream);
        }
Example #7
0
        public virtual bool TextToFile(string text, string filePath, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            if (File.Exists(filePath))
            {
                return(true);
            }

            //can only send 1000 characters at a time
            //speech xml request = 178 characters
            var textLimit = 822;

            var textParts = (text.Length > textLimit)
                ? SplitToLength(text, textLimit)
                : new List <string> {
                text
            };

            using (var fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Write))
            {
                foreach (var t in textParts)
                {
                    using (var dataStream = TextToSpeech(t, locale, voiceName, voiceType, outputFormat))
                    {
                        if (dataStream != null)
                        {
                            dataStream.CopyTo(fileStream);
                        }
                    }
                }
                fileStream.Close();
            }

            return(true);
        }
Example #8
0
        public virtual async Task <Stream> TextToSpeechAsync(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            var response = await RepositoryClient.GetAudioStreamAsync(
                ApiKeys.SpeechEndpoint,
                text,
                locale,
                voiceName,
                voiceType,
                outputFormat,
                GetSpeechToken());

            return(response);
        }
        public virtual async Task <Stream> TextToSpeechAsync(string text, BingSpeechLocaleOptions locale, string voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
        {
            var response = await RepositoryClient.GetAudioStreamAsync(
                $"{ApiKeys.BingSpeechEndpoint}synthesize",
                text,
                locale,
                voiceName,
                voiceType,
                outputFormat,
                RepositoryClient.SendBingSpeechTokenRequest(ApiKeys.BingSpeech));

            return(response);
        }