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);
        }
Beispiel #2
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);
        }
        public virtual string GetAudioContent(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType)
        {
            var langValue     = locale.MemberAttrValue();
            var namespaceAttr = $"xmlns=\"http://www.w3.org/2001/10/synthesis\"";
            var versionAttr   = $"version=\"1.0\"";
            var langAttr      = $"xml:lang=\"{langValue}\"";
            var genderAttr    = $"xmlns:gender=\"{voiceType}\"";
            var nameAttr      = $"name=\"Microsoft Server Speech Text to Speech Voice {voiceName.MemberAttrValue()}\"";
            var speakTag      = $"<speak {versionAttr} {namespaceAttr} {langAttr}><voice {nameAttr}>{text}</voice></speak>";

            return(speakTag);
        }
Beispiel #4
0
 public virtual Stream TextToSpeech(string text, SpeechLocaleOptions locale, VoiceName voiceName, GenderOptions voiceType, AudioOutputFormatOptions outputFormat)
 {
     return(PolicyService.ExecuteRetryAndCapture400Errors(
                "SpeechService.TextToSpeech",
                ApiKeys.SpeechRetryInSeconds,
                () =>
     {
         var result = SpeechRepository.TextToSpeech(text, locale, voiceName, voiceType, outputFormat);
         return result;
     },
                null));
 }
Beispiel #5
0
 public virtual Task <SpeechToTextResponse> SpeechToTextAsync(Stream audioStream, ScenarioOptions scenario,
                                                              SpeechLocaleOptions locale, SpeechOsOptions os, Guid fromDeviceId, int maxnbest = 1, int profanitycheck = 1)
 {
     return(PolicyService.ExecuteRetryAndCapture400Errors(
                "SpeechService.SpeechToTextAsync",
                ApiKeys.SpeechRetryInSeconds,
                () =>
     {
         var result = SpeechRepository.SpeechToTextAsync(audioStream, scenario, locale, os, fromDeviceId, maxnbest, profanitycheck);
         return result;
     },
                null));
 }
Beispiel #6
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> GetAudioStreamAsync(string url, string text, SpeechLocaleOptions locale, VoiceName 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 = new StringContent(GetAudioContent(text, locale, voiceName, voiceType))
            };

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

            if (!response.IsSuccessStatusCode)
            {
                throw new Exception($"({response.StatusCode}) {response.ReasonPhrase}");
            }

            return(await response.Content.ReadAsStreamAsync());
        }
Beispiel #8
0
        /// <summary>
        /// This transcribes voice queries
        /// </summary>
        /// <param name="audioStream">Audio stream</param>
        /// <param name="scenario">The context for performing a recognition.</param>
        /// <param name="locale">Language code of the audio content in IETF RFC 5646. Case does not matter. </param>
        /// <param name="os">Operating system the client is running on. This is an open field but we encourage clients to use it consistently across devices and applications.</param>
        /// <param name="fromDeviceId">A globally unique device identifier of the device making the request.</param>
        /// <param name="maxnbest">Maximum number of results the voice application API should return. The default is 1. The maximum is 5. Example: maxnbest=3</param>
        /// <param name="profanitycheck">Scan the result text for words included in an offensive word list. If found, the word will be delimited by bad word tag. Example: result.profanity=1 (0 means off, 1 means on, default is 1.)</param>
        public virtual async Task <SpeechToTextResponse> SpeechToTextAsync(Stream audioStream, ScenarioOptions scenario, SpeechLocaleOptions locale, SpeechOsOptions os, Guid fromDeviceId, int maxnbest = 1, int profanitycheck = 1)
        {
            string url   = GetSpeechToTextUrl(scenario, locale, os, fromDeviceId, maxnbest, profanitycheck);
            string token = GetSpeechToken();

            byte[] data = RepositoryClient.GetByteArray(audioStream);

            var response = await RepositoryClient.SendAsync(ApiKeys.Speech, url, data, contentType, "POST", token, true, "speech.platform.bing.com");

            return(JsonConvert.DeserializeObject <SpeechToTextResponse>(response));
        }
Beispiel #9
0
 protected virtual string GetSpeechToTextUrl(ScenarioOptions scenario, SpeechLocaleOptions locale, SpeechOsOptions os, Guid fromDeviceId, int maxnbest, int profanitycheck)
 {
     return($"{ApiKeys.SpeechEndpoint}recognize?version=3.0&scenarios={scenario}&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&requestid={Guid.NewGuid()}&format=json&locale={locale}&device.os={os}&instanceid={fromDeviceId}&maxnbest={maxnbest}&result.profanitymarkup={profanitycheck}");
 }