/// <summary> /// This function invokes api SpeechToText to convert the given wav amr file and displays the result. /// </summary> private void ConvertToSpeech(string parEndPoint, string parAccessToken, string parXspeechContext, string parXArgs, string parSpeechFilePath, bool parChunked) { Stream postStream = null; FileStream audioFileStream = null; try { audioFileStream = new FileStream(parSpeechFilePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(audioFileStream); byte[] binaryData = reader.ReadBytes((int)audioFileStream.Length); reader.Close(); audioFileStream.Close(); if (null != binaryData) { HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(string.Empty + parEndPoint ); httpRequest.Headers.Add("Authorization", "Bearer " + parAccessToken); httpRequest.Headers.Add("X-SpeechContext", parXspeechContext); if (!string.IsNullOrEmpty(parXArgs)) { httpRequest.Headers.Add("X-Arg", parXArgs); } string contentType = this.MapContentTypeFromExtension(Path.GetExtension(parSpeechFilePath)); httpRequest.ContentLength = binaryData.Length; httpRequest.ContentType = contentType; httpRequest.Accept = "application/json"; httpRequest.Method = "POST"; httpRequest.KeepAlive = true; httpRequest.SendChunked = parChunked; postStream = httpRequest.GetRequestStream(); postStream.Write(binaryData, 0, binaryData.Length); postStream.Close(); HttpWebResponse speechResponse = (HttpWebResponse)httpRequest.GetResponse(); using (StreamReader streamReader = new StreamReader(speechResponse.GetResponseStream())) { string speechRequestResponse = streamReader.ReadToEnd(); /*if (string.Compare(SpeechContext.SelectedValue, "TV") == 0) { speechErrorMessage = speechRequestResponse; streamReader.Close(); return; }*/ if (!string.IsNullOrEmpty(speechRequestResponse)) { JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer(); SpeechResponse deserializedJsonObj = (SpeechResponse)deserializeJsonObject.Deserialize(speechRequestResponse, typeof(SpeechResponse)); if (null != deserializedJsonObj) { speechResponseData = new SpeechResponse(); speechResponseData = deserializedJsonObj; speechSuccessMessage = "true"; //speechErrorMessage = speechRequestResponse; } else { speechErrorMessage = "Empty speech to text response"; } } else { speechErrorMessage = "Empty speech to text response"; } streamReader.Close(); } } else { speechErrorMessage = "Empty speech to text response"; } } catch (WebException we) { string errorResponse = string.Empty; try { using (StreamReader sr2 = new StreamReader(we.Response.GetResponseStream())) { errorResponse = sr2.ReadToEnd(); sr2.Close(); } } catch { errorResponse = "Unable to get response"; } speechErrorMessage = errorResponse; } catch (Exception ex) { speechErrorMessage = ex.ToString(); } finally { if (null != postStream) { postStream.Close(); } } }
/// <summary> /// This function invokes api SpeechToText to convert the given wav amr file and displays the result. /// </summary> private void ConvertToSpeech(string parEndPoint, string parAccessToken, string parXspeechContext, string parXArgs, string parSpeechFilePath) { Stream postStream = null; FileStream audioFileStream = null; audioFileStream = new FileStream(parSpeechFilePath, FileMode.Open, FileAccess.Read); BinaryReader reader = new BinaryReader(audioFileStream); try { byte[] binaryData = reader.ReadBytes((int)audioFileStream.Length); if (null != binaryData) { string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(string.Empty + parEndPoint); httpRequest.Headers.Add("Authorization", "Bearer " + parAccessToken); httpRequest.Headers.Add("X-SpeechContext", parXspeechContext); httpRequest.Headers.Add("Content-Language", "en-us"); httpRequest.ContentType = "multipart/x-srgs-audio; " + "boundary=" + boundary; if (!string.IsNullOrEmpty(parXArgs)) { httpRequest.Headers.Add("X-Arg", parXArgs); } string filenameArgument = "filename"; if (!string.IsNullOrEmpty(SpeechContext.SelectedValue)) { if (string.Compare("GenericHints", SpeechContext.SelectedValue) == 0) { filenameArgument = nameParam.SelectedValue.ToString(); } } string contentType = this.MapContentTypeFromExtension(Path.GetExtension(parSpeechFilePath)); string data = string.Empty; data += "--" +boundary + "\r\n" + "Content-Disposition: form-data; name=\"x-dictionary\"; " + filenameArgument + "=\"speech_alpha.pls\"\r\nContent-Type: application/pls+xml\r\n"; data += "\r\n" + xdictionaryContent + "\r\n\r\n\r\n"; data += "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"x-grammar\""; //data += "filename=\"prefix.srgs\" "; data += "\r\nContent-Type: application/srgs+xml \r\n" + "\r\n" + xgrammerContent + "\r\n\r\n\r\n" + "--" + boundary + "\r\n"; data += "Content-Disposition: form-data; name=\"x-voice\"; " + filenameArgument + "=\"" + audio_file.SelectedValue + "\""; data += "\r\nContent-Type: " + contentType + "\r\n\r\n"; UTF8Encoding encoding = new UTF8Encoding(); byte[] firstPart = encoding.GetBytes(data); int newSize = firstPart.Length + binaryData.Length; var memoryStream = new MemoryStream(new byte[newSize], 0, newSize, true, true); memoryStream.Write(firstPart, 0, firstPart.Length); memoryStream.Write(binaryData, 0, binaryData.Length); byte[] postBytes = memoryStream.GetBuffer(); byte[] byteLastBoundary = encoding.GetBytes("\r\n\r\n" + "--" + boundary + "--"); int totalSize = postBytes.Length + byteLastBoundary.Length; var totalMS = new MemoryStream(new byte[totalSize], 0, totalSize, true, true); totalMS.Write(postBytes, 0, postBytes.Length); totalMS.Write(byteLastBoundary, 0, byteLastBoundary.Length); byte[] finalpostBytes = totalMS.GetBuffer(); httpRequest.ContentLength = totalMS.Length; //httpRequest.ContentType = contentType; httpRequest.Accept = "application/json"; httpRequest.Method = "POST"; httpRequest.KeepAlive = true; postStream = httpRequest.GetRequestStream(); postStream.Write(finalpostBytes, 0, finalpostBytes.Length); postStream.Close(); HttpWebResponse speechResponse = (HttpWebResponse)httpRequest.GetResponse(); using (StreamReader streamReader = new StreamReader(speechResponse.GetResponseStream())) { string speechRequestResponse = streamReader.ReadToEnd(); if (!string.IsNullOrEmpty(speechRequestResponse)) { JavaScriptSerializer deserializeJsonObject = new JavaScriptSerializer(); SpeechResponse deserializedJsonObj = (SpeechResponse)deserializeJsonObject.Deserialize(speechRequestResponse, typeof(SpeechResponse)); if (null != deserializedJsonObj) { speechResponseData = new SpeechResponse(); speechResponseData = deserializedJsonObj; speechSuccessMessage = "true"; //speechErrorMessage = speechRequestResponse; } else { speechErrorMessage = "Empty speech to text response"; } } else { speechErrorMessage = "Empty speech to text response"; } streamReader.Close(); } } else { speechErrorMessage = "Empty speech to text response"; } } catch (WebException we) { string errorResponse = string.Empty; try { using (StreamReader sr2 = new StreamReader(we.Response.GetResponseStream())) { errorResponse = sr2.ReadToEnd(); sr2.Close(); } } catch { errorResponse = "Unable to get response"; } speechErrorMessage = errorResponse; } catch (Exception ex) { speechErrorMessage = ex.ToString(); } finally { reader.Close(); audioFileStream.Close(); if (null != postStream) { postStream.Close(); } } }
/// <summary> /// Displays the result onto the page /// </summary> /// <param name="speechResponse">SpeechResponse received from api</param> private void DisplayResult(SpeechResponse speechResponse) { statusPanel.Visible = true; lblStatus.Text = speechResponse.Recognition.Status; lblResponseId.Text = speechResponse.Recognition.Responseid; foreach (NBest nbest in speechResponse.Recognition.NBest) { lblHypothesis.Text = nbest.Hypothesis; lblLanguageId.Text = nbest.LanguageId; lblResultText.Text = nbest.ResultText; lblGrade.Text = nbest.Grade; lblConfidence.Text = nbest.Confidence.ToString(); string words = "[ "; if (null != nbest.Words) { foreach (string word in nbest.Words) { words += "\"" + word + "\", "; } words = words.Substring(0, words.LastIndexOf(",")); words = words + " ]"; } lblWords.Text = nbest.Words != null ? words : string.Empty; if (null != nbest.WordScores) { lblWordScores.Text = "[ " + string.Join(", ", nbest.WordScores.ToArray()) + " ]"; } } if (ddlSpeechContext.SelectedValue != "TV") { tvContextPanel.Visible = false; tvContextProgramPanel.Visible = false; tvContextShowtimePanel.Visible = false; } if (ddlSpeechContext.SelectedValue == "TV") { tvContextPanel.Visible = true; if (null != speechResponse.Recognition.Info) { lblInfoActionType.Text = speechResponse.Recognition.Info.ActionType; this.lblRecognized.Text = speechResponse.Recognition.Info.Recognized; } if (null != speechResponse.Recognition.Info.Interpretation) { lblInterpretation_genre_id.Text = speechResponse.Recognition.Info.Interpretation.Genre_id; lblInterpretation_genre_words.Text = speechResponse.Recognition.Info.Interpretation.Genre_words; } if (null != speechResponse.Recognition.Info.Metrics) { lblMetrics_audioBytes.Text = speechResponse.Recognition.Info.Metrics.AudioBytes.ToString(); this.lblMetrics_audioTime.Text = speechResponse.Recognition.Info.Metrics.AudioTime.ToString(); } List<Program> programs = null; if (null != speechResponse.Recognition.Info.Search && null != speechResponse.Recognition.Info.Search.Meta) { this.lblDescription.Text = speechResponse.Recognition.Info.Search.Meta.Description; if (null != speechResponse.Recognition.Info.Search.Meta.GuideDateStart) this.lblGuideDateStart.Text = speechResponse.Recognition.Info.Search.Meta.GuideDateStart.ToString(); if (null != speechResponse.Recognition.Info.Search.Meta.GuideDateEnd) this.lblGuideDateEnd.Text = speechResponse.Recognition.Info.Search.Meta.GuideDateEnd.ToString(); this.lblLineup.Text = speechResponse.Recognition.Info.Search.Meta.Lineup; this.lblMarket.Text = speechResponse.Recognition.Info.Search.Meta.Market; this.lblResultCount.Text = speechResponse.Recognition.Info.Search.Meta.ResultCount.ToString(); programs = speechResponse.Recognition.Info.Search.Programs; if (null != programs) { this.DisplayProgramDetails(programs); } } List<Showtime> showtimes = null; if (null != speechResponse.Recognition.Info.Search) { showtimes = speechResponse.Recognition.Info.Search.Showtimes; if (null != showtimes) { this.DisplayShowTimeDetails(showtimes); } } } }
/// <summary> /// Displays the result onto the page /// </summary> /// <param name="speechResponse">SpeechResponse received from api</param> private void DisplayResult(SpeechResponse speechResponse) { lblResponseId.Text = speechResponse.Recognition.ResponseId; foreach (NBest nbest in speechResponse.Recognition.NBest) { lblHypothesis.Text = nbest.Hypothesis; lblLanguageId.Text = nbest.LanguageId; lblResultText.Text = nbest.ResultText; lblGrade.Text = nbest.Grade; lblConfidence.Text = nbest.Confidence.ToString(); string strText = "["; foreach (string word in nbest.Words) { strText += "\"" + word + "\", "; } strText = strText.Substring(0, strText.LastIndexOf(",")); strText = strText + "]"; lblWords.Text = nbest.Words != null ? strText : string.Empty; lblWordScores.Text = "[" + string.Join(", ", nbest.WordScores.ToArray()) + "]"; } }
/// <summary> /// Displays the result onto the page /// </summary> /// <param name="speechResponse">SpeechResponse received from api</param> private void DisplayResult(SpeechResponse speechResponse) { lblResponseId.Text = speechResponse.Recognition.ResponseId; foreach (NBest nbest in speechResponse.Recognition.NBest) { lblHypothesis.Text = nbest.Hypothesis; lblLanguageId.Text = nbest.LanguageId; lblResultText.Text = nbest.ResultText; lblGrade.Text = nbest.Grade; lblConfidence.Text = nbest.Confidence.ToString(); lblWords.Text = nbest.Words != null ? string.Join(", ", nbest.Words.ToArray()) : string.Empty; lblWordScores.Text = string.Join(", ", nbest.WordScores.ToArray()); } }
/// <summary> /// Displays the result onto the page /// </summary> /// <param name="speechResponse">SpeechResponse received from api</param> private void DisplayResult(SpeechResponse speechResponse) { lblResponseId.Text = speechResponse.Recognition.ResponseId; lblStatus.Text = speechResponse.Recognition.Status; if ((speechResponse.Recognition.NBest != null) && (speechResponse.Recognition.NBest.Count > 0)) { foreach (NBest nbest in speechResponse.Recognition.NBest) { lblHypothesis.Text = nbest.Hypothesis; lblLanguageId.Text = nbest.LanguageId; lblResultText.Text = nbest.ResultText; lblGrade.Text = nbest.Grade; lblConfidence.Text = nbest.Confidence.ToString(); string strText = "["; foreach (string word in nbest.Words) { strText += "\"" + word + "\", "; } strText = strText.Substring(0, strText.LastIndexOf(",")); strText = strText + "]"; lblWords.Text = nbest.Words != null ? strText : string.Empty; lblWordScores.Text = "[" + string.Join(", ", nbest.WordScores.ToArray()) + "]"; } } else { hypoRow.Visible = false; langRow.Visible = false; confRow.Visible = false; gradeRow.Visible = false; resultRow.Visible = false; wordsRow.Visible = false; wordScoresRow.Visible = false; } }
/// <summary> /// Displays the result onto the page /// </summary> /// <param name="speechResponse">SpeechResponse received from api</param> private void DisplayResult(SpeechResponse speechResponse) { statusPanel.Visible = true; lblStatus.Text = speechResponse.Recognition.Status; lblResponseId.Text = speechResponse.Recognition.Responseid; foreach (NBest nbest in speechResponse.Recognition.NBest) { lblHypothesis.Text = nbest.Hypothesis; lblLanguageId.Text = nbest.LanguageId; lblResultText.Text = nbest.ResultText; lblGrade.Text = nbest.Grade; lblConfidence.Text = nbest.Confidence.ToString(); string words = "[ "; if (null != nbest.Words) { foreach (string word in nbest.Words) { words += "\"" + word + "\", "; } words = words.Substring(0, words.LastIndexOf(",")); words = words + " ]"; } lblWords.Text = nbest.Words != null ? words : string.Empty; if (null != nbest.WordScores) { lblWordScores.Text = "[ " + string.Join(", ", nbest.WordScores.ToArray()) + " ]"; } } }