// Waits for accumulates all audio associated with a given PullAudioOutputStream and then plays it to the // MediaElement. Long spoken audio will create extra latency and a streaming playback solution (that plays // audio while it continues to be received) should be used -- see the samples for examples of this. private void SynchronouslyPlayActivityAudio(PullAudioOutputStream activityAudio) { var playbackStreamWithHeader = new MemoryStream(); playbackStreamWithHeader.Write(Encoding.ASCII.GetBytes("RIFF"), 0, 4); // ChunkID playbackStreamWithHeader.Write(BitConverter.GetBytes(UInt32.MaxValue), 0, 4); // ChunkSize: max playbackStreamWithHeader.Write(Encoding.ASCII.GetBytes("WAVE"), 0, 4); // Format playbackStreamWithHeader.Write(Encoding.ASCII.GetBytes("fmt "), 0, 4); // Subchunk1ID playbackStreamWithHeader.Write(BitConverter.GetBytes(16), 0, 4); // Subchunk1Size: PCM playbackStreamWithHeader.Write(BitConverter.GetBytes(1), 0, 2); // AudioFormat: PCM playbackStreamWithHeader.Write(BitConverter.GetBytes(1), 0, 2); // NumChannels: mono playbackStreamWithHeader.Write(BitConverter.GetBytes(16000), 0, 4); // SampleRate: 16kHz playbackStreamWithHeader.Write(BitConverter.GetBytes(32000), 0, 4); // ByteRate playbackStreamWithHeader.Write(BitConverter.GetBytes(2), 0, 2); // BlockAlign playbackStreamWithHeader.Write(BitConverter.GetBytes(16), 0, 2); // BitsPerSample: 16-bit playbackStreamWithHeader.Write(Encoding.ASCII.GetBytes("data"), 0, 4); // Subchunk2ID playbackStreamWithHeader.Write(BitConverter.GetBytes(UInt32.MaxValue), 0, 4); // Subchunk2Size byte[] pullBuffer = new byte[2056]; uint lastRead = 0; do { lastRead = activityAudio.Read(pullBuffer); playbackStreamWithHeader.Write(pullBuffer, 0, (int)lastRead); }while (lastRead == pullBuffer.Length); var task = Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { mediaElement.SetSource(playbackStreamWithHeader.AsRandomAccessStream(), "audio/wav"); mediaElement.Play(); }); }
private unsafe AudioFrame ReadAudioData(uint samples) { // Buffer size is (number of samples) * (size of each sample) uint bufferSize = samples * sizeof(byte) * 2; AudioFrame frame = new Windows.Media.AudioFrame(bufferSize); using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write)) using (IMemoryBufferReference reference = buffer.CreateReference()) { byte *dataInBytes; uint capacityInBytes; // Get the buffer from the AudioFrame ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes); // Read audio data from the stream and copy it to the AudioFrame buffer var readBytes = new byte[capacityInBytes]; uint bytesRead = audioStream.Read(readBytes); if (bytesRead == 0) { frameInputNode.Stop(); } for (int i = 0; i < bytesRead; i++) { dataInBytes[i] = readBytes[i]; } } return(frame); }
public int Read(byte[] buffer, int offset, int count) { if (_stream == null) { return(0); } //Fast case for when the count and offset are exactly as the read method expects if (offset == 0 && count == buffer.Length) { return((int)_stream.Read(buffer)); } //We'll have to allocate an array to read into which is the right size and alignment :( var temp = new byte[count]; var read = (int)_stream.Read(temp); Buffer.BlockCopy(temp, 0, buffer, offset, read); return(read); }
/// <summary> /// Write TTS Audio to WAV file. /// </summary> /// <param name="audio"> TTS Audio.</param> /// <param name="baseFileName"> File name where this test is specified. </param> /// <param name="dialogID">The value of the DialogID in the input test file.</param> /// <param name="turnID">The value of the TurnID in the input test file.</param> /// <param name="indexActivityWithAudio">Index value of the current TTS response.</param> private int WriteAudioToWAVfile(PullAudioOutputStream audio, string baseFileName, string dialogID, int turnID, int indexActivityWithAudio) { FileStream fs = null; string testFileOutputFolder = Path.Combine(this.appsettings.OutputFolder, baseFileName + "Output"); string wAVFolderPath = Path.Combine(testFileOutputFolder, ProgramConstants.WAVFileFolderName); int durationInMS = 0; if (indexActivityWithAudio == 0) { // First TTS WAV file to be written, create the WAV File Folder Directory.CreateDirectory(wAVFolderPath); } this.outputWAV = Path.Combine(wAVFolderPath, baseFileName + "-BotResponse-" + dialogID + "-" + turnID + "-" + indexActivityWithAudio + ".WAV"); byte[] buff = new byte[MaxSizeOfTtsAudioInBytes]; uint bytesReadtofile; try { fs = File.Create(this.outputWAV); fs.Write(new byte[WavHeaderSizeInBytes]); while ((bytesReadtofile = audio.Read(buff)) > 0) { fs.Write(buff, 0, (int)bytesReadtofile); } WriteWavHeader(fs); } catch (Exception e) { Trace.TraceError(e.ToString()); } finally { fs.Close(); } WaveFileReader waveFileReader = new WaveFileReader(this.outputWAV); durationInMS = (int)waveFileReader.TotalTime.TotalMilliseconds; waveFileReader.Dispose(); return(durationInMS); }