Skip to content

Commit aa481cb

Browse files
committed
Update performance
1 parent 3d81d8c commit aa481cb

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

EXILED/Exiled.API/Features/Audio/WavStreamSource.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77

88
namespace Exiled.API.Features.Audio
99
{
10+
using System;
1011
using System.IO;
12+
using System.Runtime.InteropServices;
1113

1214
using Exiled.API.Interfaces;
1315

16+
using VoiceChat;
17+
1418
/// <summary>
1519
/// Provides a PCM audio source from a WAV file stream.
1620
/// </summary>
@@ -19,6 +23,7 @@ public sealed class WavStreamSource : IPcmSource
1923
private readonly long endPosition;
2024
private readonly long startPosition;
2125
private readonly BinaryReader reader;
26+
private readonly byte[] readBuffer = new byte[VoiceChatSettings.PacketSizePerChannel * 2];
2227

2328
/// <summary>
2429
/// Initializes a new instance of the <see cref="WavStreamSource"/> class.
@@ -52,15 +57,26 @@ public bool Ended
5257
/// <returns>The number of samples read.</returns>
5358
public int Read(float[] buffer, int offset, int count)
5459
{
55-
int i = 0;
60+
int bytesNeeded = count * 2;
5661

57-
while (i < count && reader.BaseStream.Position < endPosition)
58-
{
59-
buffer[offset + i] = reader.ReadInt16() / 32768f;
60-
i++;
61-
}
62+
if (bytesNeeded > readBuffer.Length)
63+
bytesNeeded = readBuffer.Length;
64+
65+
int bytesRead = reader.Read(readBuffer, 0, bytesNeeded);
66+
if (bytesRead == 0)
67+
return 0;
68+
69+
if (bytesRead % 2 != 0)
70+
bytesRead--;
71+
72+
Span<byte> byteSpan = readBuffer.AsSpan(0, bytesRead);
73+
Span<short> shortSpan = MemoryMarshal.Cast<byte, short>(byteSpan);
74+
75+
int samplesRead = shortSpan.Length;
76+
for (int i = 0; i < samplesRead; i++)
77+
buffer[offset + i] = shortSpan[i] / 32768f;
6278

63-
return i;
79+
return samplesRead;
6480
}
6581

6682
/// <summary>

EXILED/Exiled.API/Features/Audio/WavUtility.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace Exiled.API.Features.Audio
99
{
10+
using System;
1011
using System.IO;
12+
using System.Runtime.InteropServices;
1113

1214
using VoiceChat;
1315

@@ -23,16 +25,22 @@ public static class WavUtility
2325
/// <returns>An array of floats representing the PCM data.</returns>
2426
public static float[] WavToPcm(string path)
2527
{
26-
using FileStream fs = File.OpenRead(path);
27-
using BinaryReader br = new(fs);
28+
byte[] fileBytes = File.ReadAllBytes(path);
29+
30+
using MemoryStream ms = new(fileBytes);
31+
using BinaryReader br = new(ms);
2832

2933
SkipHeader(br);
3034

31-
int samples = (int)((fs.Length - fs.Position) / 2);
32-
float[] pcm = new float[samples];
35+
int headerOffset = (int)ms.Position;
36+
int dataLength = fileBytes.Length - headerOffset;
37+
38+
Span<byte> audioDataSpan = fileBytes.AsSpan(headerOffset, dataLength);
39+
Span<short> samples = MemoryMarshal.Cast<byte, short>(audioDataSpan);
3340

34-
for (int i = 0; i < samples; i++)
35-
pcm[i] = br.ReadInt16() / 32768f;
41+
float[] pcm = new float[samples.Length];
42+
for (int i = 0; i < samples.Length; i++)
43+
pcm[i] = samples[i] / 32768f;
3644

3745
return pcm;
3846
}

EXILED/Exiled.API/Features/Toys/Speaker.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ private IEnumerator<float> PlayBackCoroutine()
309309
if (Loop)
310310
{
311311
source.Reset();
312-
timeAccumulator = 0f;
313-
break;
312+
continue;
314313
}
315314

316315
if (DestroyAfter)

0 commit comments

Comments
 (0)