Ejemplo n.º 1
0
        /// <summary>
        /// SendStorageFile method
        /// </summary>
        /// <param name="wavFile">StorageFile associated with the audio file which
        /// will be sent to the SpeechToText Services.
        /// </param>
        /// <param name="locale">language associated with the current buffer/recording.
        /// for instance en-US, fr-FR, pt-BR, ...
        /// </param>
        /// <return>The result of the SpeechToText REST API.
        /// </return>
        public async System.Threading.Tasks.Task <SpeechToTextResponse> SendStorageFile(Windows.Storage.StorageFile wavFile, string locale)
        {
            SpeechToTextResponse r = null;
            int loop = 1;

            while (loop-- > 0)
            {
                try
                {
                    string os        = "Windows" + SpeechToText.SystemInformation.SystemVersion;
                    string deviceid  = "b2c95ede-97eb-4c88-81e4-80f32d6aee54";
                    string speechUrl = SpeechUrl + "?scenarios=ulm&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&version=3.0&device.os=" + os + "&locale=" + locale + "&format=json&requestid=" + Guid.NewGuid().ToString() + "&instanceid=" + deviceid + "&result.profanitymarkup=1&maxnbest=3";
                    Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient();

                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", Token);
                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("ContentType", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
                    Windows.Web.Http.HttpResponseMessage hrm = null;

                    Windows.Storage.StorageFile file = wavFile;
                    if (file != null)
                    {
                        using (var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                        {
                            if (STTStream != null)
                            {
                                STTStream.AudioLevel -= STTStream_AudioLevel;
                                STTStream.Dispose();
                                STTStream = null;
                            }
                            STTStream = SpeechToTextMainStream.Create();
                            if (STTStream != null)
                            {
                                byte[] byteArray = new byte[fileStream.Size];
                                fileStream.ReadAsync(byteArray.AsBuffer(), (uint)fileStream.Size, Windows.Storage.Streams.InputStreamOptions.Partial).AsTask().Wait();
                                STTStream.WriteAsync(byteArray.AsBuffer()).AsTask().Wait();

                                Windows.Web.Http.HttpStreamContent content = new Windows.Web.Http.HttpStreamContent(STTStream.AsStream().AsInputStream());
                                content.Headers.ContentLength = STTStream.GetLength();
                                System.Diagnostics.Debug.WriteLine("REST API Post Content Length: " + content.Headers.ContentLength.ToString() + " bytes");
                                System.Threading.CancellationTokenSource  cts      = new System.Threading.CancellationTokenSource();
                                IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler);
                                hrm = await hc.PostAsync(new Uri(speechUrl), content).AsTask(cts.Token, progress);
                            }
                        }
                    }
                    if (hrm != null)
                    {
                        switch (hrm.StatusCode)
                        {
                        case Windows.Web.Http.HttpStatusCode.Ok:
                            var b = await hrm.Content.ReadAsBufferAsync();

                            string result = System.Text.UTF8Encoding.UTF8.GetString(b.ToArray());
                            if (!string.IsNullOrEmpty(result))
                            {
                                r = new SpeechToTextResponse(result);
                            }
                            break;

                        case Windows.Web.Http.HttpStatusCode.Forbidden:
                            string token = await RenewToken();

                            if (string.IsNullOrEmpty(token))
                            {
                                loop++;
                            }
                            break;

                        default:
                            int    code      = (int)hrm.StatusCode;
                            string HttpError = "Http Response Error: " + code.ToString() + " reason: " + hrm.ReasonPhrase.ToString();
                            System.Diagnostics.Debug.WriteLine(HttpError);
                            r = new SpeechToTextResponse(string.Empty, HttpError);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception while sending the audio file:" + ex.Message);
                }
            }
            return(r);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// SendAudioStream method
        /// This method sends a SpeechToTextAudioStream towards Cognitive Services REST API.
        /// Usually, the method GetAudioStream returns the SpeechToTextAudioStream (if available) then the method SendAudioStream
        /// sends a SpeechToTextAudioStream towards Cognitive Services REST API.
        /// </summary>
        /// <param name="locale">language associated with the current buffer/recording.
        /// for instance en-US, fr-FR, pt-BR, ...
        /// </param>
        /// <param name="stream">AudioStream which will be forwarded to REST API.
        /// </param>
        /// <return>The result of the SpeechToText REST API.
        /// </return>
        public async System.Threading.Tasks.Task <SpeechToTextResponse> SendAudioStream(string locale, SpeechToTextAudioStream stream)
        {
            SpeechToTextResponse r = null;
            int loop = 1;

            while (loop-- > 0)
            {
                try
                {
                    string os        = "Windows" + SpeechToText.SystemInformation.SystemVersion;
                    string deviceid  = "b2c95ede-97eb-4c88-81e4-80f32d6aee54";
                    string speechUrl = SpeechUrl + "?scenarios=ulm&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&version=3.0&device.os=" + os + "&locale=" + locale + "&format=json&requestid=" + Guid.NewGuid().ToString() + "&instanceid=" + deviceid + "&result.profanitymarkup=1&maxnbest=3";
                    Windows.Web.Http.HttpClient hc = new Windows.Web.Http.HttpClient();
                    System.Threading.CancellationTokenSource cts = new System.Threading.CancellationTokenSource();
                    hc.DefaultRequestHeaders.TryAppendWithoutValidation("Authorization", Token);
                    Windows.Web.Http.HttpResponseMessage hrm     = null;
                    Windows.Web.Http.HttpStreamContent   content = null;
                    content = new Windows.Web.Http.HttpStreamContent(stream.GetInputStreamAt(0));
                    content.Headers.ContentLength = (ulong)stream.Size;
                    if ((content != null) && (content.Headers.ContentLength > 0))
                    {
                        System.Diagnostics.Debug.WriteLine("REST API Post Content Length: " + content.Headers.ContentLength.ToString());
                        content.Headers.TryAppendWithoutValidation("ContentType", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
                        IProgress <Windows.Web.Http.HttpProgress> progress = new Progress <Windows.Web.Http.HttpProgress>(ProgressHandler);
                        hrm = await hc.PostAsync(new Uri(speechUrl), content).AsTask(cts.Token, progress);
                    }
                    if (hrm != null)
                    {
                        switch (hrm.StatusCode)
                        {
                        case Windows.Web.Http.HttpStatusCode.Ok:
                            var b = await hrm.Content.ReadAsBufferAsync();

                            string result = System.Text.UTF8Encoding.UTF8.GetString(b.ToArray());
                            if (!string.IsNullOrEmpty(result))
                            {
                                r = new SpeechToTextResponse(result);
                            }
                            break;

                        case Windows.Web.Http.HttpStatusCode.Forbidden:
                            string token = await RenewToken();

                            if (string.IsNullOrEmpty(token))
                            {
                                loop++;
                            }
                            break;

                        default:
                            int    code      = (int)hrm.StatusCode;
                            string HttpError = "Http Response Error: " + code.ToString() + " reason: " + hrm.ReasonPhrase.ToString();
                            System.Diagnostics.Debug.WriteLine(HttpError);
                            r = new SpeechToTextResponse(string.Empty, HttpError);
                            break;
                        }
                    }
                }
                catch (System.Threading.Tasks.TaskCanceledException)
                {
                    System.Diagnostics.Debug.WriteLine("http POST canceled");
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("http POST exception: " + ex.Message);
                }
                finally
                {
                    System.Diagnostics.Debug.WriteLine("http POST done");
                }
            }
            return(r);
        }