Jump to content
  • Advertisement
Sign in to follow this  
munawar

[.net] Getting a PCM memory stream from a stream that contains ogg data

This topic is 3741 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I found a .NET implementation of an ogg player for .NET here: http://www.codeproject.com/KB/audio-video/OggPlayer.aspx However, I have a question about it, and am wondering if someone can help me. I wish to modify this code in the following manner: 1. Remove the need to instantiate the OggPlayManager class. 2. Modify the --DecodeThreadProc method to return a SecondaryBuffer with wave data decoded from the given ogg data. I have modified the code so that it now looks like this: using System; using System.IO; using System.Diagnostics; using System.Text; using System.Collections.Generic; using System.Windows.Forms; using System.Threading; using Microsoft.DirectX.DirectSound; namespace TG.Sound { public class Decoder { private static int ErrorHoleCount=0; private static int ErrorBadLinkCount=0; public static SecondaryBuffer getSBuffer(Byte[] oggStream, Device directSoundDevice) { // Call external C++ functions to decode the Ogg Vorbis data unsafe { // Declare members that must be disposed later void *vf = null; SecondaryBuffer SecBuf = null; BufferDescription MyDescription = null; MemoryStream PcmStream = null; int ErrorCode = 0; int bitsPerSample=16; bool firstTime=true; ErrorCode = NativeMethods.memory_stream_for_ogg_decode(oggStream, oggStream.Length, &vf); int PcmBytes=0, ChannelsCount = 0, SamplingRate = 0, AverageBytesPerSecond = 0, BlockAlign = 0; // Vorbisfile API documentation recommends // a PCM buffer of 4096 bytes. byte[] PcmBuffer = new byte[4096]; PcmStream = new MemoryStream(); WaveFormat MyWaveFormat = new WaveFormat(); // Decode the Ogg Vorbis data into its PCM data while (true) { // Get the next chunk of PCM data, pin these so the GC can't // relocate them. fixed(byte* buf = &PcmBuffer[0]) { fixed (int* HoleCount = &ErrorHoleCount) { fixed(int* BadLinkCount = &ErrorBadLinkCount) { // The sample size of the returned PCM data -- either 8-bit // or 16-bit samples -- is set by BitsPerSample. PcmBytes = NativeMethods.ogg_decode_one_vorbis_packet( vf, buf, PcmBuffer.Length, bitsPerSample, &ChannelsCount, &SamplingRate, HoleCount, BadLinkCount); } } } if (firstTime) { MyDescription=new BufferDescription(); BlockAlign = ChannelsCount * (bitsPerSample / 8); AverageBytesPerSecond = SamplingRate * BlockAlign; MyWaveFormat.AverageBytesPerSecond = AverageBytesPerSecond; MyWaveFormat.BitsPerSample = (short)bitsPerSample; MyWaveFormat.BlockAlign = (short)BlockAlign; MyWaveFormat.Channels = (short)ChannelsCount; MyWaveFormat.SamplesPerSecond = SamplingRate; MyWaveFormat.FormatTag = WaveFormatTag.Pcm; MyDescription.ControlVolume = true; MyDescription.ControlPan = true; MyDescription.GlobalFocus = false; firstTime=false; } //We've reached the end of the ogg stream, //so break and assemble the secondaryBuffer if (PcmBytes == 0) { break; } // Append the new Pcm data to the existing stream. long pLength=PcmStream.Length; PcmStream.SetLength(PcmStream.Length + PcmBytes); PcmStream.Position=pLength; PcmStream.Write(PcmBuffer, 0, PcmBytes); } //while PcmStream.Position=0; SecBuf=new SecondaryBuffer(PcmStream, 4096, directSoundDevice); return SecBuf; } //unsafe } //getSBuffer } //class } However, When the code creates the SecondaryBuffer, I get an InvalidArgumentException: Argument does not fall within the expected range error. Can someone please direct me on where I've gone wrong? How can I modify the project I referenced above to create a SecondaryBuffer with the entire Pcm version--actually playable--from the ogg byte stream, rather than having that method play it itself like the author does in the original code? Thanks for any assistance you can give me.

Share this post


Link to post
Share on other sites
Advertisement
Hi,
Sorry, made an error in the last post: here is the real exception I am getting.
Error base exception: System.ArgumentException: Value does not fall within the expected range.
If someone can point me to a resource to solve this problem, I'd much appreciate it. I'm reading in the pcm data exactly as it is given to me by the ogg decoder, so don't understand what's going on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!