/// <summary> /// Extract the bufferdata from an ogg-file. /// </summary> /// <param name="file">The file to load the data from.</param> /// <returns>A SoundBufferData object containing the data from the specified file.</returns> public static SoundBufferData FromOgg(Stream file) { var buffers = new List <short[]>(); ALFormat format; int sampleRate; using (var vorbis = new VorbisReader(file, true)) { // Save format and samplerate for playback format = vorbis.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16; sampleRate = vorbis.SampleRate; var buffer = new float[16384]; int count; while ((count = vorbis.ReadSamples(buffer, 0, buffer.Length)) > 0) { // Sample value range is -0.99999994f to 0.99999994f // Samples are interleaved (chan0, chan1, chan0, chan1, etc.) // Use the OggStreamer method to cast to the right format var castBuffer = new short[count]; SoundBufferData.CastBuffer(buffer, castBuffer, count); buffers.Add(castBuffer); } } return(new SoundBufferData(buffers, format, sampleRate)); }
/// <summary> /// Fills the buffers of the specified stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="bufferId">The id of the buffer to be filled.</param> /// <returns></returns> public bool FillBuffer(OggStream stream, int bufferId) { int readSamples; lock (this.readMutex) { readSamples = stream.Reader.ReadSamples(this.readSampleBuffer, 0, this.BufferSize); SoundBufferData.CastBuffer(this.readSampleBuffer, this.castBuffer, readSamples); } if (readSamples > 0) { AL.BufferData(bufferId, stream.Reader.Channels == 1 ? ALFormat.Mono16 : ALFormat.Stereo16, this.castBuffer, readSamples * sizeof(short), stream.Reader.SampleRate); ALHelper.Check(); } return(readSamples != this.BufferSize); }
public static void CastBuffer(float[] inBuffer, short[] outBuffer, int length) { SoundBufferData.CastBuffer(inBuffer, outBuffer, length); }