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

Rounding error in TrimWaveStream (EndPosition / StartPosition)

Dec 25, 2009 at 9:48 AM

Hi Marc,

the  setters of   public TimeSpan EndPosition   / StartPosition   inside the TrimWaveStream  class   does not always  deliver  correct results for startBytePosition/endBytePosition:

 set
            {
                endPosition = value;
                endBytePosition = (int)(WaveFormat.AverageBytesPerSecond *endPosition.TotaSeconds is double) endPosition.TotalSeconds);
                endBytePosition = endBytePosition - (endBytePosition % WaveFormat.BlockAlign);
            }

e.g. the file length is 2800 milliseconds with 32 Bit, 2 Channels and samplerate of 44100 . This will calculate 987832  for endBytePosition. The correct value would be:

987840. This is a rounding issue (endPosition.TotalSeconds is double). So just use:

 set
            {
                endPosition = value;
                endBytePosition = (int) Math.Round(WaveFormat.AverageBytesPerSecond * endPosition.TotalSeconds);
                endBytePosition = endBytePosition - (endBytePosition % WaveFormat.BlockAlign);
            }

regards, henrik


Jan 8, 2010 at 3:58 PM

TrimWaveStream isn't in NAudio yet, its in VoiceRecorder, but thanks for reporting, I'll get it fixed.