Play PCM-file

Feb 18, 2010 at 5:08 PM
Edited Feb 18, 2010 at 5:08 PM

Hello all!

 

I'm stuck with a rather simple (as I thought at first) question... I'm trying to play PCM, raw wave file, but no luck...  I can play *.mp3, *.wav, but no raw waves... There is a method "CreatePcmStream", which only argument is WaveStream, but I don't know how to create that WaveStream. Actually, I don't know, if I really have to create a PCM stream, because I already have one :), in my file. I can read it into MemoryStream, or FileStream, but what's next? Or maybe I have to make my own class PCMStream, inherited from WaveStream?..

Could you please give me a hand with it?

 

Thanks a lot in advance!

Editor
Feb 18, 2010 at 8:31 PM

What do you mean by RAW Wave Stream? Do you actually mean a wave file without a header specifying the contents of the file?

If that's what you are referring to, then what you are missing is that you haven't specified the format for NAudio. NAudio will handle most WAVE files but requires a valid WAVE header that will specify the information such as channels, bits per sample etc. If your not providing that information in the file then it's up to you to specify that information in the code by specifying it yourself before opening the stream.

~DSebJ

Feb 19, 2010 at 2:46 PM

Thanks for quick answer!

 

> What do you mean by RAW Wave Stream? Do you actually mean a wave file without a header specifying the contents of the file?

 

Yes, exactly, wave file with no header.

 

> If that's what you are referring to, then what you are missing is that you haven't specified the format for NAudio

 

OK , I get it... I know that info about my file: sample rate is 8000,  mono channel, resolution is 8 bit, and compression format is 8-bit mu-Law.

 

> ...it's up to you to specify that information in the code by specifying it yourself before opening the stream.

 

Please, can you tell me how can I specify it in the code? I can create a WaveFormat object, such as:

WaveFormat wf = new WaveFormat(8000, 8, 1);

But how will I use that object? How can I create a WaveStream object, using specified WaveFormat and my raw audiostream (byte array)?

 

P.S. Just in case - the PCM-file is playable, I can open it with CoolEdit (using parameters from above) and play it...

Coordinator
Feb 22, 2010 at 12:58 PM

have a look at WaveFormatConversionStream.CreatePcmStream

 

Feb 22, 2010 at 2:15 PM

>have a look at WaveFormatConversionStream.CreatePcmStream

Yeah, I already looked at that method, but it requires a WaveStream object as its only argument, and the problem is creaing some WaveStream object. I have MemoryStream of my file (or FileStream), I have a WaveFormat object (new WaveFormat(8000, 8, 1)), but I don't know how to create a PCM Stream from all that stuff... WaveStream is an abstract class, so what kind of inherited class I have to create? And how can I create such object? Could you please give me an example of the code or just the names of the suitable classes and methods?

Coordinator
Feb 22, 2010 at 3:40 PM

you have to create your own derived class from WaveStream. In the Read method, return the requested amount of data from your memory stream.

 

Feb 22, 2010 at 5:40 PM

It works!!! :) Thanks for help!