Playing raw pcm bytes

Oct 25, 2011 at 3:11 PM

Hi,

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 8: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?

Thanks!

Coordinator
Oct 26, 2011 at 8: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 8: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};
        waveOutDevice.Init(inputStream);
    }

    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)
       {
           waveOutDevice.Play();
           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 8:59 AM
Edited Oct 27, 2011 at 9: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("RIFF"));
    binWriter.Write((int)0);
    binWriter.Write(System.Text.Encoding.ASCII.GetBytes("WAVE"));
    binWriter.Write(System.Text.Encoding.ASCII.GetBytes("fmt "));
    binWriter.Write((int)18);
    binWriter.Write((short)activeFormat.Encoding);
    binWriter.Write((short)activeFormat.Channels);
    binWriter.Write((int)activeFormat.SampleRate);
    binWriter.Write((int)activeFormat.AverageBytesPerSecond);
    binWriter.Write((short)activeFormat.BlockAlign);
    binWriter.Write((short)activeFormat.BitPerSample);
    binWriter.Write((short)0);
    binWriter.Write(System.Text.Encoding.ASCII.GetBytes("data"));
    binWriter.Write((int)0);
    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.
Thanks!
Edit
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
Coordinator
Oct 27, 2011 at 9:01 AM

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

Oct 27, 2011 at 9: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 :-)

Coordinator
Oct 27, 2011 at 9: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 9: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 9:42 AM
Edited Oct 27, 2011 at 9: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?

Coordinator
Oct 27, 2011 at 9: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 9:45 AM

Thanks! :-)

I'll try it and let you know.

Oct 27, 2011 at 9: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

Coordinator
Oct 27, 2011 at 10:00 AM

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

Oct 27, 2011 at 12:28 PM

i did a long lunch break...

anyway i did your suggestion and it WORK!

thank you very much! :-)

Coordinator
Oct 27, 2011 at 12:29 PM

great! glad you got it sorted