This project has moved and is read-only. For the latest updates, please go here.

Playing raw pcm bytes

Oct 25, 2011 at 4:11 PM


I try to play file that contain raw pcm but with no success.

the file has no header but i know his format.

I tried to read the file into RawSourceWaveStream with the right format and then do CreatePcmStream and CreateWaveFile but when I play the file with win media player i ear just noise i tried also other options to save or play the file with NAudio but always i getting just noise.

Actually i get the raw pcm  from the net but first i want play a local raw pcm.

Thanks in advance

Oct 26, 2011 at 9:31 AM

I must say that cooledit can play the file when i gave him the right format so no problem with file or his format.

Do i need to build the header in myself?


Oct 26, 2011 at 9:33 AM

If you get the WaveFormat right the WAV file should play no problem. Can you post a code snippet?

Oct 27, 2011 at 9:39 AM

The following example work fine with a regular wave file (file with header) but make noise with raw bytes.

void On_Receive(byte[] stream)
    if (activeFormat == null)
        activeFormat = new WaveFormat(8000,16,1);

    if (bufferedSampleStream == null)
        bufferedSampleStream = new BufferedSampleStream(activeFormat);
        inputStream = new WaveChannel32(bufferedSampleStream);

    bufferedSampleStream.AddSamples(stream, 0, stream.Length);

void playTimer_Tick(object sender, EventArgs e)
    if (waveOutDevice == null)
        waveOutDevice = new WaveOut{DesiredLatency = 200};

    inputStream.Sample += inputStream_Sample;

    if (bufferedSampleStream != null)
        playTime += PlayTimeInterval/60;
        var bufferedSeconds = bufferedSampleStream.TotalTime.TotalSeconds;

        if (bufferedSeconds < MinimumTimeToPlay && playbackState == StreamingPlaybackState.Playing)
            // some stuff
        else if (bufferedSeconds >=MinimumTimeToPlay && playbackState == StreamingPlaybackState.Buffering)
           playbackState == StreamingPlaybackState.Playing

I tried also to take bytes and create a new wave file but the file created also have just noise.
Oct 27, 2011 at 9:59 AM
Edited Oct 27, 2011 at 10:10 AM

If I builed the header in myself I can play the file but I must build the header for each stream arrived. i think its strange...

I build it like that:

in On_Receive method that i wrote in prev. post i add this:

var temp = AddHeader();
var newStream = new byte[stream.length + temp.Length];
temp.CopyTo(newStream, 0);
stream.CopyTo(newStream, 46);
stream = null;
stream = newStream;

and this is the AddHeader method:

private byte[] AddHeader()
    var outStream = new MemoryStream(46) // header size
    var binWriter = new BinaryWriter(outStream, System.Text.Encoding.ASCII);
    binWriter.Write(System.Text.Encoding.ASCII.GetBytes("fmt "));
    return outStream.ToArray();
so I can play it but i must did it every time new stream arrived 
and also i here a little noise every short time (i think its happen in beggin of every stream)
p.s. dont look for syntax errors, i write the code manually because i couldnt copy/paste it.
I tried also to add the data length for the SubChenk2Size and the all stream - 8 for the ChunkSize but still i ear a little noise sometimes
Oct 27, 2011 at 10:01 AM

You don't need to do this yourself, just use WaveFileWriter which does this all for you

Oct 27, 2011 at 10:21 AM

but i want to add the stream to BufferedSampleStream and the to WaveChannel32 and play it when it arrive, i dont want to write it.

anyway i tried to add the stream (raw bytes pcm) to WaveFileWriter and give him the format and the file was created but contain just noise.

i'm lost :-(

please help me :-)

Oct 27, 2011 at 10:24 AM

you only need to add the WAV file header if you are writing to a file. All streams in NAudio have a WaveFormat property, and as long as this is set correctly, the right thing will happen.

if your WAV file contains just noise, then it is very likely that you are using the wrong WaveFormat

Oct 27, 2011 at 10:32 AM

I understand and its make sense

but if the format is wrong how i can play it when i add the header with this format?

and also i can play it in cooledit with this format

maybe there are more settings except the encoding, rate, channels, and bit depth?

or maybe the problem is when i try to add the bufferedSampleStream to the WaveChannel32 stream?

Oct 27, 2011 at 10:42 AM
Edited Oct 27, 2011 at 10:44 AM

something else came up in my head, there is a difference in your lib between bif/little endian?

the file is Motorola (MSB, LSB).

this is a problem?

Oct 27, 2011 at 10:43 AM

that is likely your problem. WAV files are always in Intel byte order, use AIFF files for the other way round

Oct 27, 2011 at 10:45 AM

Thanks! :-)

I'll try it and let you know.

Oct 27, 2011 at 10:59 AM

can you give direction how to do that?

There is an AiffFileReader class but its expect to aiff header and my stream contains only raw data.

I can build the header and add it to the begin of the stream but i guess its not the right way.

There is a methods of endian utils that i can use?

Thanks again

Oct 27, 2011 at 11:00 AM

I'd just swap every two bytes in your byte array

Oct 27, 2011 at 1:28 PM

i did a long lunch break...

anyway i did your suggestion and it WORK!

thank you very much! :-)

Oct 27, 2011 at 1:29 PM

great! glad you got it sorted