Exemplo n.º 1
0
        /// <summary>
        /// The main conversion API. Calling this function sends an audio file to AT&amp;T service for translation to text.
        /// </summary>
        /// <param name="attachment">Audio filename as it is stored on disk.</param>
        /// <param name="speechContext">Speech content.</param>
        /// <returns>Instance of <see cref="SpeechResponse"/> with sent speech response information.</returns>
        /// <exception cref="System.ArgumentNullException">Throws an exception when attachment is null.</exception>
        public async Task <SpeechResponse> SpeechToText(StorageFile attachment, XSpeechContext speechContext = XSpeechContext.Generic)
        {
            Argument.ExpectNotNull(() => attachment);

            byte[] audioFileBytes = await BinaryFileUtils.ReadAllBytes(attachment);

            var restEndPoint = new Uri(Settings.EndPoint, SendRelativeUrl);

            var    content     = new ByteArrayContent(audioFileBytes);
            string contentType = ContentTypeMapping.MapContentTypeFromExtension(attachment.FileType);

            content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
            content.Headers.Add("X-SpeechContext", Enum.GetName(typeof(XSpeechContext), speechContext));

            string strResponse = await SendContentRequest(HttpMethod.Post, restEndPoint.ToString(), content);

            return(SpeechResponse.Parse(strResponse));
        }
Exemplo n.º 2
0
    /// <summary>
    /// Method that calls SpeechToText method of RequestFactory to transcribe to text
    /// </summary>
    /// <param name="FileName">Wave file to transcribe</param>
    private ATT_MSSDK.Speechv3.SpeechResponse SpeechToTextService(String FileName, String SpeechContext, String AudioContentType)
    {
        ATT_MSSDK.Speechv3.SpeechResponse response = null;

        try
        {
            if (string.IsNullOrEmpty(FileName))
            {
                Debug.Log("No sound file specified");
                return(null);
            }

            XSpeechContext speechContext   = XSpeechContext.Generic;
            string         contentLanguage = string.Empty;
            string         xArgData        = "ClientApp=SpeechApp";
            switch (SpeechContext)
            {
            case "Generic": speechContext = XSpeechContext.Generic; contentLanguage = "en-US"; break;

            case "BusinessSearch": speechContext = XSpeechContext.BusinessSearch; break;

            case "TV": speechContext = XSpeechContext.TV; xArgData = "Search=True,Lineup=91983"; break;

            case "Gaming": speechContext = XSpeechContext.Gaming; break;

            case "SocialMedia": speechContext = XSpeechContext.SocialMedia; xArgData = "ClientApp=SpeechApps"; break;

            case "WebSearch": speechContext = XSpeechContext.WebSearch; break;

            case "SMS": speechContext = XSpeechContext.SMS; break;

            case "VoiceMail": speechContext = XSpeechContext.VoiceMail; break;

            case "QuestionAndAnswer": speechContext = XSpeechContext.QuestionAndAnswer; break;
            }

            string subContext = string.Empty;

            response = this.requestFactory.SpeechToText(FileName, speechContext, xArgData, contentLanguage, subContext, AudioContentType);

            if (null != response)
            {
                return(response);
            }
        }
        catch (InvalidScopeException invalidscope)
        {
            Debug.Log(invalidscope.Message);
        }
        catch (ArgumentException argex)
        {
            Debug.Log(argex.Message);
        }
        catch (InvalidResponseException ie)
        {
            Debug.Log(ie.Body);
        }
        catch (Exception ex)
        {
            Debug.Log(ex.Message);
        }
        finally
        {
            Debug.Log("SpeechToTextService completed.");
        }

        return(response);
    }
Exemplo n.º 3
0
    /// <summary>
    /// Method that calls SpeechToText method of RequestFactory when user clicked on submit button
    /// </summary>
    /// <param name="sender">sender that invoked this event</param>
    /// <param name="e">eventargs of the button</param>
    protected void SpeechToTextButton_Click(object sender, EventArgs e)
    {
        try
        {
            resultsPanel.Visible = false;
            this.Initialize();
            if (string.IsNullOrEmpty(fileUpload1.FileName))
            {
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["DefaultFile"]))
                {
                    this.fileToConvert = Request.MapPath(ConfigurationManager.AppSettings["DefaultFile"]);
                }
                else
                {
                    this.DrawPanelForFailure(statusPanel, "No file selected, and default file is not defined in web.config");
                    return;
                }
            }
            else
            {
                string fileName = fileUpload1.FileName;
                if (fileName.CompareTo("default.wav") == 0)
                {
                    fileName = "1" + fileUpload1.FileName;
                }
                fileUpload1.PostedFile.SaveAs(Request.MapPath("") + "/" + fileName);
                this.fileToConvert = Request.MapPath("").ToString() + "/" + fileName;
                this.deleteFile    = true;
            }

            XSpeechContext speechContext   = XSpeechContext.Generic;
            string         contentLanguage = string.Empty;
            this.xArgData = this.commonXArg;
            switch (ddlSpeechContext.SelectedValue)
            {
            case "Generic": speechContext = XSpeechContext.Generic; contentLanguage = ddlContentLang.SelectedValue; break;

            case "BusinessSearch": speechContext = XSpeechContext.BusinessSearch; break;

            case "TV": speechContext = XSpeechContext.TV; this.xArgData = this.xArgTVContext; break;

            case "Gaming": speechContext = XSpeechContext.Gaming; break;

            case "SocialMedia": speechContext = XSpeechContext.SocialMedia; this.xArgData = this.xArgSocialMediaContext; break;

            case "WebSearch": speechContext = XSpeechContext.WebSearch; break;

            case "SMS": speechContext = XSpeechContext.SMS; break;

            case "VoiceMail": speechContext = XSpeechContext.VoiceMail; break;

            case "QuestionAndAnswer": speechContext = XSpeechContext.QuestionAndAnswer; break;
            }

            string subContext = txtSubContext.Text;
            if (subContext.ToLower().Contains("example"))
            {
                subContext = string.Empty;
            }

            SpeechResponse response = this.requestFactory.SpeechToText(fileToConvert, speechContext, this.xArgData, contentLanguage, subContext, ddlAudioContentType.SelectedValue);

            if (null != response)
            {
                resultsPanel.Visible = true;
                this.DrawPanelForSuccess(statusPanel, "Response Parameters listed below");
                this.DisplayResult(response);
            }
        }
        catch (InvalidScopeException invalidscope)
        {
            this.DrawPanelForFailure(statusPanel, invalidscope.Message);
        }
        catch (ArgumentException argex)
        {
            this.DrawPanelForFailure(statusPanel, argex.Message);
        }
        catch (InvalidResponseException ie)
        {
            this.DrawPanelForFailure(statusPanel, ie.Body);
        }
        catch (Exception ex)
        {
            this.DrawPanelForFailure(statusPanel, ex.Message);
        }
        finally
        {
            if ((this.deleteFile == true) && (File.Exists(this.fileToConvert)))
            {
                File.Delete(this.fileToConvert);
                this.deleteFile = false;
            }
        }
    }