Skip to content

Commit

Permalink
Improving Performance
Browse files Browse the repository at this point in the history
Much faster than BitConverter.ToSingle
  • Loading branch information
RachamimYaakobov authored Aug 5, 2020
1 parent 1f2cbc7 commit 523e0f4
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions NAudio/Wave/SampleProviders/WaveToSampleProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ public override int Read(float[] buffer, int offset, int count)
int bytesRead = source.Read(sourceBuffer, 0, bytesNeeded);
int samplesRead = bytesRead / 4;
int outputIndex = offset;
for (int n = 0; n < bytesRead; n+=4)
unsafe
{
buffer[outputIndex++] = BitConverter.ToSingle(sourceBuffer, n);
fixed(byte* pBytes = &sourceBuffer[0])
{
float* pFloat = (float*)pBytes;
for (int n = 0, i = 0; n < bytesRead; n += 4, i++)
{
buffer[outputIndex++] = *(pFloat + i);
}
}
}
return samplesRead;
}
Expand Down

0 comments on commit 523e0f4

Please sign in to comment.