Problems with reading wav files

G

Guest

I am trying to access the data with in a wav file. I am testing with very
small files in order to keep the code simple to start with.

Basically, im writing the entire wav file to a byte[] using a fileStream.

The problem is that when reading back the data word by word and printing the
results on a general form, once the data chunk header has been read, i.e,
"data" and "chunk size", all the rest of the bytes in the array have the
value "0". Is there some thing strange about wav files that no one is telling
me. The code that reads the file into a byt array looks like this:

NOTE THE FILE IS SMALLER THAN 1MB AND IS NOT SILENT!

private int myBufferSize = 1024;
private string myFilePath "C:\\File.wav";

private byte[] myChunk = new byte[myBufferSize];

FileStream fileStream = new FileStream(myFilePath, FileMode.Open,
FileAccess.Read, FileShare.Read, myBufferSize, true);

fileStream.Read(myChunk, 0, myBufferSize);

Any help would be REALLY apprciated.

Thanks.
 
J

Jon Skeet [C# MVP]

Erpman said:
I am trying to access the data with in a wav file. I am testing with very
small files in order to keep the code simple to start with.

Basically, im writing the entire wav file to a byte[] using a fileStream.

The problem is that when reading back the data word by word and printing the
results on a general form, once the data chunk header has been read, i.e,
"data" and "chunk size", all the rest of the bytes in the array have the
value "0". Is there some thing strange about wav files that no one is telling
me. The code that reads the file into a byt array looks like this:

NOTE THE FILE IS SMALLER THAN 1MB AND IS NOT SILENT!

private int myBufferSize = 1024;
private string myFilePath "C:\\File.wav";

private byte[] myChunk = new byte[myBufferSize];

FileStream fileStream = new FileStream(myFilePath, FileMode.Open,
FileAccess.Read, FileShare.Read, myBufferSize, true);

fileStream.Read(myChunk, 0, myBufferSize);

Any help would be REALLY apprciated.

Well, you're only reading 1024 *bytes* of data at most - and you're not
guaranteed to read that, either. (See the page referenced in the other
post.)
 
B

Bill Butler

The problem is that when reading back the data word by word and printing the
results on a general form, once the data chunk header has been read, i.e,
"data" and "chunk size", all the rest of the bytes in the array have the
value "0". Is there some thing strange about wav files that no one is telling
me. The code that reads the file into a byt array looks like this:

Wave files can come in some bizarre flavors, but in general, most are not too bad.

You always have a "format" chunk and a "data" chunk.
You may have other chunks as well.
They are not guaranteed to be in any particular order, although it is usually
"format", "data", other.

Each chunk has a small "chunk header" including it's ID and ChunkLength.

So far all of this sounds like it should be familiar to you.

/******* What I think is wrong **********/
Wave files have TONS of data.
A common sampling rate is 44100 samples per second.
If it represents 2 channel/16 bit data that is 176,400 bytes of data for 1 second of sound.
Your byte[1024] buffer would capture ~1/172 sec (assuming 2channel/16bit).

Before chasing this problem any further I suggest that you read a bigger block of data.
Most wave files start off with a bunch of zeros (silence).

---------------
Try this

//------- Psuedocode ------//
FileInfo fileinfo = new FileInfo("C:\\File.wav")
samples = dataChunk.Size/formatChunk.WBlockAlign; // total number of samples
byte[] buf = ReadDataChunk(fileinfo, samples);

// This should read the entire data block into a byte[]
public byte[] ReadDataChunk(FileInfo fileinfo, int samples)
{
byte[] buf;
using(FileStream fs = fileinfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
using(BinaryReader br = new BinaryReader(fs))
{
fs.Position = dataChunk.Offset;
buf = br.ReadBytes(samples*4);
}
return (buf);
}

Hope this helps
Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top