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

TryReadFloat problem with Version 1.4

May 2, 2011 at 4:34 PM

Hey guys,

I'm having a problem with TryReadFloat that was previously working under version 1.3.  When I run this code block:

while (reader.HasData(array.Length))
reader.TryReadFloat(out nextSample);
if (reader.WaveFormat.Channels > 1)
    reader.TryReadFloat(out nextSample);

I get a System.ApplicationException with a message "Must read complete blocks".   The audio I'm running is a wav file that has 2 channels of audio.  I've search around on the site for source code for the samples associated with version 1.4 but I couldn't find anything.  If someone can direct me to the location of the source of the samples, I'm sure I could find out the issue.  Thanks for your help!

May 2, 2011 at 5:08 PM

OK that is probably a bug you have found. That part of the code is not one I tend make use of myself (I prefer using something like Wave16ToFloatProvider). I am trying to increase unit test coverage for future NAudio so things like that don't get inadvertently broken.  I had been contemplating making TryReadFloat an extension method anyway, so you could do the same and make a TryReadFloat with a stereo overload (two out parameters) for use with stereo files.


May 2, 2011 at 7:27 PM

Hey Mark!  No worries.  Can you give me an example of using the Wave16ToFloatProvider to gather samples in the same way I'm trying to do above?

May 2, 2011 at 9:26 PM

might be easiest for you just to do it yourself. here's the snippet for a stereo TryReadFloat extension method

public static bool TryReadFloat(this WaveFileReader waveFileReader, out float sampleValueLeft, out float sampleValueRight)
// 16 bit PCM data
    if (waveFileReader.waveFormat.BitsPerSample == 16)     {      byte[] value = new byte[4];         int read = waveFileReader.Read(value, 0, value.Length);         if (read < value.Length)          return false;         sampleValueLeft = (float)BitConverter.ToInt16(value, 0) / 32768f;
sampleValueRight = (float)BitConverter.ToInt16(value, 2) / 32768f;
return true;     }
// etc if you need to support other input bit depths