/// <summary> /// Loads the .ts file with the given URI and extracts the audio. /// </summary> /// <param name="tsFileUri">The .ts file URI.</param> /// <returns>Loaded and processed media segment content.</returns> public async Task <MediaSegmentContent> LoadTsFileAndExtractAudioAsync(Uri tsFileUri) { if (string.IsNullOrWhiteSpace(tsFileUri.ToString())) { throw new ArgumentNullException("TS file URI missing"); } byte[] contentAsByteArray = null; using (HttpClient httpClient = new HttpClient()) { using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(tsFileUri)) { using (HttpContent httpContent = httpResponseMessage.Content) { contentAsByteArray = await httpContent.ReadAsByteArrayAsync(); } } } MediaSegmentContent mediaSegmentContent = new MediaSegmentContent() { FullContent = contentAsByteArray }; MemoryStream contentAsMemoryStream = new MemoryStream(contentAsByteArray); using (WaveStream pcmStream = WaveFormatConversionStream.CreatePcmStream( new StreamMediaFoundationReader(contentAsMemoryStream))) { // TODO: Read in chunks if the length is long WaveStream blockAlignReductionStream = new BlockAlignReductionStream(pcmStream); mediaSegmentContent.Audio = new AudioSample(blockAlignReductionStream); } System.Diagnostics.Debug.WriteLine($"Extracted: {mediaSegmentContent.Audio}"); return(mediaSegmentContent); }
/// <summary> /// Processes the stream with the given URI: /// Extracts the audio and does the audio transcription. /// Once the processing is complete, MediaSegmentProcessed event, /// including the processed content, is fired. /// </summary> /// <param name="streamUri">The stream URI.</param> public async Task ProcessStreamAsync(Uri streamUri) { MediaSegmentContent mediaSegmentContent = null; try { mediaSegmentContent = await LoadTsFileAndExtractAudioAsync(streamUri); } catch (Exception e) { System.Diagnostics.Debug.WriteLine($"Failed to extract audio: {e.Message}"); } if (!string.IsNullOrWhiteSpace(_bingSpeechToTextApiSubscriptionKey) && mediaSegmentContent.Audio != null) { string transcription = string.Empty; DetailedSpeechRecognition detailedSpeechRecognition = null; try { transcription = new Transcriber(_bingSpeechToTextApiSubscriptionKey).Transcribe(mediaSegmentContent.Audio); detailedSpeechRecognition = JsonConvert.DeserializeObject <DetailedSpeechRecognition>(transcription); } catch (Exception e) { System.Diagnostics.Debug.WriteLine($"Failed to transcribe audio/deserialize transcription: {e.Message}"); } mediaSegmentContent.TranscriptionResult = transcription; mediaSegmentContent.Transcription = detailedSpeechRecognition; } // Notify MediaSegmentProcessed?.Invoke(this, mediaSegmentContent); }