public static bool StreamChunk(VorbisStreamHandle handle, out PcmData pcm, uint bufferSize = DefaultBufferSize) { pcm.dataLength = 0; pcm.channelCount = handle.VorbisInstance.Channels; pcm.sampleRate = handle.VorbisInstance.SampleRate; bool eof = false; float[] buffer = new float[bufferSize]; while (pcm.dataLength < buffer.Length) { int samplesRead; lock (readMutex) { samplesRead = handle.VorbisInstance.ReadSamples(buffer, pcm.dataLength, buffer.Length - pcm.dataLength); } if (samplesRead > 0) { pcm.dataLength += samplesRead; } else { eof = true; break; } } pcm.data = new short[pcm.dataLength]; CastBuffer(buffer, pcm.data, 0, pcm.dataLength); return(pcm.dataLength > 0 && !eof); }
/// <summary> /// Sets up a new OpenAL buffer for this AudioData. This will result in decompressing /// the Ogg Vorbis data and uploading it to OpenAL, unless the AudioData is streamed. /// </summary> public void SetupAlBuffer() { // No AudioData available if (this.data.Length == 0 || this.data == null) { this.DisposeAlBuffer(); return; } // Streamed Audio if (this.IsStreamed) { this.DisposeAlBuffer(); this.alBuffer = AlBuffer_StreamMe; } // Non-Streamed Audio else { if (this.alBuffer <= AlBuffer_NotAvailable && DualityApp.Sound.IsAvailable) { this.alBuffer = AL.GenBuffer(); PcmData pcm = OggVorbis.LoadFromMemory(this.data); AL.BufferData( this.alBuffer, pcm.channelCount == 1 ? ALFormat.Mono16 : ALFormat.Stereo16, pcm.data.ToArray(), pcm.dataLength * PcmData.SizeOfDataElement, pcm.sampleRate); } else { // Buffer already there? Do nothing. } } }
public static bool ReadAll(VorbisStreamHandle handle, out PcmData pcm) { pcm.channelCount = handle.VorbisInstance.Channels; pcm.sampleRate = handle.VorbisInstance.SampleRate; pcm.dataLength = 0; List <float[]> allBuffers = new List <float[]>(); bool eof = false; int totalSamplesRead = 0; while (!eof) { float[] buffer = new float[DefaultBufferSize]; int bufferSamplesRead = 0; while (bufferSamplesRead < buffer.Length) { int samplesRead; lock (readMutex) { samplesRead = handle.VorbisInstance.ReadSamples(buffer, pcm.dataLength, buffer.Length - pcm.dataLength); } if (samplesRead > 0) { bufferSamplesRead += samplesRead; } else { eof = true; break; } } allBuffers.Add(buffer); totalSamplesRead += bufferSamplesRead; } if (totalSamplesRead > 0) { pcm.dataLength = totalSamplesRead; pcm.data = new short[totalSamplesRead]; int offset = 0; for (int i = 0; i < allBuffers.Count; i++) { int len = Math.Min(pcm.data.Length - offset, allBuffers[i].Length); CastBuffer(allBuffers[i], pcm.data, offset, len); offset += len; } return(!eof); } else { pcm.data = new short[0]; return(false); } }
/// <summary> /// Sets up a new native buffer for this AudioData. This will result in decompressing /// the Ogg Vorbis data and uploading it to OpenAL, unless the AudioData is streamed. /// </summary> private void SetupNativeBuffer() { // No AudioData available if (this.data == null || this.data.Length == 0) { this.DisposeNativeBuffer(); return; } // Streamed Audio if (this.IsStreamed) { this.DisposeNativeBuffer(); this.native = null; } // Non-Streamed Audio else { if (this.native == null) { this.native = DualityApp.AudioBackend.CreateBuffer(); PcmData pcm = OggVorbis.LoadFromMemory(this.data); this.native.LoadData( pcm.SampleRate, pcm.Data, pcm.DataLength, pcm.ChannelCount == 1 ? AudioDataLayout.Mono : AudioDataLayout.LeftRight, AudioDataElementType.Short); } else { // Buffer already there? Do nothing. } } }
public static bool ReadAll(VorbisStreamHandle handle, out PcmData pcm) { pcm.channelCount = handle.VorbisInstance.Channels; pcm.sampleRate = handle.VorbisInstance.SampleRate; pcm.dataLength = 0; List<float[]> allBuffers = new List<float[]>(); bool eof = false; int totalSamplesRead = 0; while (!eof) { float[] buffer = new float[DefaultBufferSize]; int bufferSamplesRead = 0; while(bufferSamplesRead < buffer.Length) { int samplesRead; lock (readMutex) { samplesRead = handle.VorbisInstance.ReadSamples(buffer, pcm.dataLength, buffer.Length - pcm.dataLength); } if (samplesRead > 0) { bufferSamplesRead += samplesRead; } else { eof = true; break; } } allBuffers.Add(buffer); totalSamplesRead += bufferSamplesRead; } if (totalSamplesRead > 0) { pcm.dataLength = totalSamplesRead; pcm.data = new short[totalSamplesRead]; int offset = 0; for (int i = 0; i < allBuffers.Count; i++) { int len = Math.Min(pcm.data.Length - offset, allBuffers[i].Length); CastBuffer(allBuffers[i], pcm.data, offset, len); offset += len; } return !eof; } else { pcm.data = new short[0]; return false; } }
public static bool StreamChunk(VorbisStreamHandle handle, out PcmData pcm, uint bufferSize = DefaultBufferSize) { pcm.dataLength = 0; pcm.channelCount = handle.VorbisInstance.Channels; pcm.sampleRate = handle.VorbisInstance.SampleRate; bool eof = false; float[] buffer = new float[bufferSize]; while (pcm.dataLength < buffer.Length) { int samplesRead; lock (readMutex) { samplesRead = handle.VorbisInstance.ReadSamples(buffer, pcm.dataLength, buffer.Length - pcm.dataLength); } if (samplesRead > 0) { pcm.dataLength += samplesRead; } else { eof = true; break; } } pcm.data = new short[pcm.dataLength]; CastBuffer(buffer, pcm.data, 0, pcm.dataLength); return pcm.dataLength > 0 && !eof; }