Example #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));
        }
Example #2
0
        private static async Task <byte[]> EncodeBinaryContent(IEnumerable <Windows.Storage.StorageFile> attachments, string textContent, string timeStampStr)
        {
            var encoding = new UTF8Encoding();

            byte[] buffer          = null;
            bool   isFirstFile     = true;
            string currentFileName = String.Empty;

            try
            {
                foreach (var fileName in attachments)
                {
                    currentFileName = fileName.Name;
                    byte[] attachmentPrefixBytes = encoding.GetBytes(CreateAttachmentPrefix(isFirstFile, textContent, fileName.Path, timeStampStr));
                    byte[] fileBytes             = await BinaryFileUtils.ReadAllBytes(fileName);

                    byte[] attachBuffer = BinaryFileUtils.CombineByteArrays(attachmentPrefixBytes, fileBytes);

                    if (isFirstFile)
                    {
                        buffer = attachBuffer;
                    }
                    else
                    {
                        buffer = BinaryFileUtils.CombineByteArrays(buffer, attachBuffer);
                    }

                    isFirstFile = false;
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new FileNotFoundException(string.Format("File '{0}' not found", currentFileName), ex);
            }

            return(BinaryFileUtils.CombineByteArrays(buffer, encoding.GetBytes("\r\n--" + timeStampStr + "--\r\n")));
        }