By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,407 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,407 IT Pros & Developers. It's quick & easy.

Problems with reading wav files

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
This page might help...

http://www.yoda.arachsys.com/csharp/readbinary.html

Nov 17 '05 #2

P: n/a
Erpman <Er****@discussions.microsoft.com> wrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

P: n/a
"Erpman" <Er****@discussions.microsoft.com> wrote in message
news:CB**********************************@microsof t.com...
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
Nov 17 '05 #4

P: n/a
Hi,

You can get details of the wav file format and others at this site:

http://www.wotsit.org/

Hope this helps,
Phil

"Erpman" wrote:
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.

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.