0
votes

I am trying to load a *.wav file to a byte array using C# 3.0 and .NET 3.5 like this:

  var fs = File.Open(filedialog.FileName, FileMode.Open,FileAccess.Read);
  long numBytes = new FileInfo(filedialog.FileName).Length;
  BinaryReader br = new BinaryReader(fs);
  byte[] bytes = br.ReadBytes((int)numBytes);

From byte[58] and to the end (~50k bytes) all values are 127 or 128 (I guess the first ~58 bytes are header stuff?).

The wave file is playing fine in Windows media player and other players, and I am sure it is nothing wrong with it (it's recorded with the sound recorder in WinXP).

Wave file info:

BitRate: 176kbps
Audio sample size: 8bit
Audio sample rate: 22kHz
Audio format: PCM

When I try to play the byte stream using the .NET SoundPlayer it sounds terrible :-) Any idèas?

[SOLVED]
This was not the problem after all, so I'll have to continue my search for the real bug.

1
What does the file look like in a hex editor?Jon Skeet
It looks fine, no bunches of 7f and 80's :)Ezombort
The question may sound stupid, but as the file looks fine in the hex editor and not from .NET, are you definitely certain that you are opening the same file?Dirk Vollmar
Well, other than overusing the var keyword and not closing the file after reading it, I don't see anything wrong with the code. How did you determine that there are 127s and 128s in the array, and what is your code for playing the sound?Guffa
This was not the problem after all, so I'll have to continue my search for the real bug. Thanks for helping and sorry.Ezombort

1 Answers

2
votes

The code looks all right, as far as I can see.

You could try the simpler code:

byte[] bytes = File.ReadAllBytes(filedialog.FileName);