/// <summary> /// Get the next segment of Ogg data decoded into PCM format /// </summary> /// <param name="SegmentLength"> /// A <see cref="System.Int32"/> indicating the number of bytes of data to request. /// Defaults to 4096 if set to 0. /// Use the ReturnValue property of the result to discover how many bytes are actually returned /// </param> /// <returns> /// Am <see cref="OggBufferSegment"/> containing the returned data /// </returns> public OggBufferSegment GetBufferSegment(int SegmentLength) { if (SegmentLength <= 0) { SegmentLength = _SEGMENTLENGTH; } // If segment length is invalid, use default segment length OggBufferSegment retVal; // Declare the buffer segment structure retVal.BufferLength = SegmentLength; retVal.Buffer = new Byte[retVal.BufferLength]; // Init buffer retVal.ReturnValue = m_CSVorbisFileInstance.read(retVal.Buffer, retVal.BufferLength, _BIGENDIANREADMODE, _WORDREADMODE, _SGNEDREADMODE, null); retVal.RateHz = m_TagLibFile.Properties.AudioSampleRate; //m_Info[0].rate; return(retVal); }
/// <summary> /// Begins playing the given clip. /// </summary> /// <param name="file">The clip to play.</param> public void Play(VorbisFileInstance clip) { DequeuUsedBuffers(); CurrentFormat = DetermineFormat(clip); CurrentRate = DetermineRate(clip); CurrentClip = clip; eof = false; // Buffer initial audio int usedBuffers = 0; for (int i = 0; i < BufferCount; i++) { int bytesRead = clip.read(SegmentBuffer, SegmentBuffer.Length, _BIGENDIANREADMODE, _WORDREADMODE, _SGNEDREADMODE, null); if (bytesRead > 0) { // Buffer the segment AL.BufferData(Buffers[i], CurrentFormat, SegmentBuffer, bytesRead, CurrentRate); usedBuffers++; } else if (bytesRead == 0) { // Clip is too small to fill the initial buffer, so stop // buffering. break; } else { // TODO: There was an error reading the file throw new System.IO.IOException("Error reading or processing OGG file"); } } // Start playing the clip AL.SourceQueueBuffers(Source, usedBuffers, Buffers); AL.SourcePlay(Source); }
/// <summary> /// Caches the given number of bytes by reading them in and discarding /// them. This is useful so that when the sound if first played, /// there's not a delay. /// </summary> /// <param name="bytes">Then number of PCM bytes to read.</param> protected void Cache(int bytes) { VorbisFileInstance instance = rawClip.makeInstance(); int totalBytes = 0; byte[] buffer = new byte[4096]; while (totalBytes < bytes) { int bytesRead = instance.read(buffer, buffer.Length, 0, 2, 1, null); if (bytesRead <= 0) { break; } totalBytes += bytesRead; } }