/// <summary>
        /// Connect to the server before sending audio
        /// It will get the authentication credentials and add it to the header
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task Connect(string from, string to, string voice, OnSpeechTranslateResult onSpeechTranslateResult, OnTextToSpeechData onTextToSpeechData)
        {
            this.webSocket               = new MessageWebSocket();
            this.onTextToSpeechData      = onTextToSpeechData;
            this.onSpeechTranslateResult = onSpeechTranslateResult;

            // Get Azure authentication token
            var tokenProvider = new AzureAuthToken(this.clientSecret);
            var bearerToken   = await tokenProvider.GetAccessTokenAsync();

            this.webSocket.SetRequestHeader("Authorization", bearerToken);

            var url = String.Format(SpeechTranslateUrl, from, to, voice == null ? String.Empty : "&features=texttospeech&voice=" + voice);

            this.webSocket.MessageReceived += WebSocket_MessageReceived;

            // setup the data writer
            this.dataWriter           = new DataWriter(this.webSocket.OutputStream);
            this.dataWriter.ByteOrder = ByteOrder.LittleEndian;
            this.dataWriter.WriteBytes(GetWaveHeader());

            // connect to the service
            await this.webSocket.ConnectAsync(new Uri(url));

            // flush the dataWriter periodically
            this.timer = new Timer(async(s) =>
            {
                if (this.dataWriter.UnstoredBufferLength > 0)
                {
                    try
                    {
                        await this.dataWriter.StoreAsync();
                    }
                    catch (Exception e)
                    {
                        this.onSpeechTranslateResult(new Result()
                        {
                            Status = "DataWriter Failed: " + e.Message
                        });
                    }
                }

                // reset the timer
                this.timer.Change(TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
            },
                                   null, TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
        }
示例#2
0
        public async Task Connect(string from, string to, string voice, OnSpeechTranslateResult onSpeechTranslateResult, OnTextToSpeechData onTextToSpeechData)
        {
            this.webSocket               = new MessageWebSocket();
            this.onTextToSpeechData      = onTextToSpeechData;
            this.onSpeechTranslateResult = onSpeechTranslateResult;

            this.webSocket.SetRequestHeader("Ocp-Apim-Subscription-Key", $"{Common.CoreConstants.TranslatorSpeechSubscriptionKey}");

            var url = String.Format(SpeechTranslateUrl, from, to, voice == null ? "" : "&features=texttospeech&voice=" + voice);

            this.webSocket.MessageReceived += OnMessageReceived;

            this.dataWriter           = new DataWriter(this.webSocket.OutputStream);
            this.dataWriter.ByteOrder = ByteOrder.LittleEndian;
            this.dataWriter.WriteBytes(GetWaveHeader());

            await this.webSocket.ConnectAsync(new Uri(url));

            this.timer = new Timer(async(s) =>
            {
                if (this.dataWriter.UnstoredBufferLength > 0)
                {
                    try
                    {
                        await this.dataWriter.StoreAsync();
                    }
                    catch (Exception e)
                    {
                        this.onSpeechTranslateResult(new SpeechTranslationResult()
                        {
                            Status = "DataWriter Failed: " + e.Message
                        });
                    }
                }

                this.timer.Change(TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
            },
                                   null, TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
        }
        /// <summary>
        /// Connect to the server before sending audio
        /// It will get the ADM credentials and add it to the header
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task Connect(string from, string to, string voice, OnSpeechTranslateResult onSpeechTranslateResult, OnTextToSpeechData onTextToSpeechData)
        {
            this.webSocket               = new MessageWebSocket();
            this.onTextToSpeechData      = onTextToSpeechData;
            this.onSpeechTranslateResult = onSpeechTranslateResult;

            // get Azure Data Market token
            using (var httpClient = new HttpClient())
            {
                var response = await httpClient.PostAsync(AzureMarketPlaceUrl, new FormUrlEncodedContent(
                                                              new KeyValuePair <string, string>[] {
                    new KeyValuePair <string, string>("grant_type", "client_credentials"),
                    new KeyValuePair <string, string>("client_id", clientId),
                    new KeyValuePair <string, string>("client_secret", clientSecret),
                    new KeyValuePair <string, string>("scope", AzureScope),
                }));

                response.EnsureSuccessStatusCode();

                var json = await response.Content.ReadAsStringAsync();

                dynamic admAccessToken = JObject.Parse(json);

                var admToken = "Bearer " + admAccessToken.access_token;

                this.webSocket.SetRequestHeader("Authorization", admToken);
            }

            var url = String.Format(SpeechTranslateUrl, from, to, voice == null ? String.Empty : "&features=texttospeech&voice=" + voice);

            this.webSocket.MessageReceived += WebSocket_MessageReceived;

            // setup the data writer
            this.dataWriter           = new DataWriter(this.webSocket.OutputStream);
            this.dataWriter.ByteOrder = ByteOrder.LittleEndian;
            this.dataWriter.WriteBytes(GetWaveHeader());

            // connect to the service
            await this.webSocket.ConnectAsync(new Uri(url));

            // flush the dataWriter periodically
            this.timer = new Timer(async(s) =>
            {
                if (this.dataWriter.UnstoredBufferLength > 0)
                {
                    try
                    {
                        await this.dataWriter.StoreAsync();
                    }
                    catch (Exception e)
                    {
                        this.onSpeechTranslateResult(new Result()
                        {
                            Status = "DataWriter Failed: " + e.Message
                        });
                    }
                }

                // reset the timer
                this.timer.Change(TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
            },
                                   null, TimeSpan.FromMilliseconds(250), Timeout.InfiniteTimeSpan);
        }