/// <summary> /// Releases all resources used by the <see cref="OggReader"/>. /// </summary> public override void Dispose() { _reader.Dispose(); }
public ISoundData Decode(Stream stream) { VorbisReader reader = null; try { reader = new VorbisReader (stream, false); int channels = reader.Channels; const int BITS_PER_SAMPLE = 16; int sampleRate = reader.SampleRate; //Code from: https://github.com/AdamsLair/duality/blob/a911c46b6bc830c05d3bb1202f8e7060543eaefa/Duality/Audio/OggVorbis.cs List<float[]> allBuffers = new List<float[]>(); bool eof = false; int totalSamplesRead = 0; int dataRead = 0; while (!eof) { float[] buffer = new float[DEFAULT_BUFFER_SIZE]; int bufferSamplesRead = 0; while(bufferSamplesRead < buffer.Length) { int samplesRead; lock (_readMutex) { samplesRead = reader.ReadSamples(buffer, dataRead, buffer.Length - dataRead); } if (samplesRead > 0) { bufferSamplesRead += samplesRead; } else { eof = true; break; } } allBuffers.Add(buffer); totalSamplesRead += bufferSamplesRead; } if (totalSamplesRead > 0) { short[] data = new short[totalSamplesRead]; int offset = 0; for (int i = 0; i < allBuffers.Count; i++) { int len = Math.Min(totalSamplesRead - offset, allBuffers[i].Length); castBuffer(allBuffers[i], data, offset, len); offset += len; } return new SoundData(channels, BITS_PER_SAMPLE, sampleRate, data, Marshal.SizeOf(typeof(short)) * data.Length); } else { Debug.WriteLine("OggDecoder: No samples found in ogg file"); return null; } } catch (InvalidDataException e) { Debug.WriteLine("OggDecoder: Failed to read ogg. " + e.ToString()); return null; } finally { if (reader != null) reader.Dispose(); } }