-
Notifications
You must be signed in to change notification settings - Fork 254
Improve performance for wav read sample #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,4 @@ UpgradeLog* | |
| *.mpq | ||
|
|
||
| .idea | ||
| /.vs | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,10 +59,10 @@ private void ReadCallback(float[] data) | |
| var chunk = _chunks[_chunkIndex]; | ||
| int samplesAvailable = chunk.sampleCount + chunk.sampleOffset - _sampleIndex; | ||
| int samplesToRead = Math.Min(data.Length - i, samplesAvailable); | ||
| for (int j = 0; j < samplesToRead; ++j, ++i) | ||
| { | ||
| data[i] = ReadSample(_reader); | ||
| } | ||
| // When smaplesToRead reaches 35000 or above, reading a float a time will consumes about 3 seconds, which stalls the whole game. | ||
| // Use bunch read to spped up. | ||
|
mofr marked this conversation as resolved.
|
||
| ReadSample(_reader, data, i, samplesToRead); | ||
| i += samplesToRead; | ||
| _sampleIndex += samplesToRead; | ||
| if (i < data.Length) | ||
| ++_chunkIndex; | ||
|
|
@@ -130,6 +130,19 @@ private static float ReadSample(BinaryReader reader) | |
| return BytesToFloat(byte1, byte2); | ||
| } | ||
|
|
||
| private static void ReadSample(BinaryReader reader, float[] sampleBuffer, int startIndex, int sampleCount) | ||
| { | ||
| byte[] buffer = new byte[sampleCount * 2]; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we reuse the buffer array somehow to avoid frequent allocations and excessive garbage collector work?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about create a reuseable fixed length byte[] buffer when
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good |
||
| reader.BaseStream.Read(buffer, 0, buffer.Length); | ||
| for (int i = 0; i < buffer.Length; i += 2) | ||
| { | ||
| byte byte1 = buffer[i]; | ||
| byte byte2 = buffer[i + 1]; | ||
| short s = (short)((byte2 << 8) | byte1); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's reuse
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we shall test the performances of the following two solutions:
After performance test, choose the fastest one.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you expect significant function call overhead? In this case, I like your idea to test both of them. If the inlined version works faster I think it's a reasonable cost to duplicate the code. |
||
| sampleBuffer[startIndex + i / 2] = s / 32768.0F; | ||
| } | ||
| } | ||
|
|
||
| private static float BytesToFloat(byte byte1, byte byte2) | ||
| { | ||
| // convert two bytes to one short (little endian) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.