Quantcast
Channel: NAudio
Viewing all articles
Browse latest Browse all 5831

Commented Issue: Bug in the NotifyingSampleProvider class [16355]

$
0
0
The NotifyingSampleProvider class notifies also on non read samples because it uses the "sampleCount" variable which is the size of the incoming buffer. However the amount of bytes read may be smaller than the destination buffer! So the samplesRead variable should be used.

Wrong code:

public int Read(float[] buffer, int offset, int sampleCount)
{
int samplesRead = source.Read(buffer, offset, sampleCount);
if (Sample != null)
{
for (int n = 0; n < sampleCount; n += channels)
{
sampleArgs.Left = buffer[offset + n];
sampleArgs.Right = channels > 1 ? buffer[offset + n + 1] : sampleArgs.Left;
Sample(this, sampleArgs);
}
}
return samplesRead;
}

Right code:
public int Read(float[] buffer, int offset, int sampleCount)
{
int samplesRead = source.Read(buffer, offset, sampleCount);
if (Sample != null)
{
for (int n = 0; n < samplesRead ; n += channels)
{
sampleArgs.Left = buffer[offset + n];
sampleArgs.Right = channels > 1 ? buffer[offset + n + 1] : sampleArgs.Left;
Sample(this, sampleArgs);
}
}
return samplesRead;
}
Comments: good spot

Viewing all articles
Browse latest Browse all 5831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>